Possible future bugs found by the creation of newSVpvn_flags().
[p5sagit/p5-mst-13.2.git] / regcomp.c
1 /*    regcomp.c
2  */
3
4 /*
5  * "A fair jaw-cracker dwarf-language must be."  --Samwise Gamgee
6  */
7
8 /* This file contains functions for compiling a regular expression.  See
9  * also regexec.c which funnily enough, contains functions for executing
10  * a regular expression.
11  *
12  * This file is also copied at build time to ext/re/re_comp.c, where
13  * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT.
14  * This causes the main functions to be compiled under new names and with
15  * debugging support added, which makes "use re 'debug'" work.
16  */
17
18 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
19  * confused with the original package (see point 3 below).  Thanks, Henry!
20  */
21
22 /* Additional note: this code is very heavily munged from Henry's version
23  * in places.  In some spots I've traded clarity for efficiency, so don't
24  * blame Henry for some of the lack of readability.
25  */
26
27 /* The names of the functions have been changed from regcomp and
28  * regexec to  pregcomp and pregexec in order to avoid conflicts
29  * with the POSIX routines of the same names.
30 */
31
32 #ifdef PERL_EXT_RE_BUILD
33 #include "re_top.h"
34 #endif
35
36 /*
37  * pregcomp and pregexec -- regsub and regerror are not used in perl
38  *
39  *      Copyright (c) 1986 by University of Toronto.
40  *      Written by Henry Spencer.  Not derived from licensed software.
41  *
42  *      Permission is granted to anyone to use this software for any
43  *      purpose on any computer system, and to redistribute it freely,
44  *      subject to the following restrictions:
45  *
46  *      1. The author is not responsible for the consequences of use of
47  *              this software, no matter how awful, even if they arise
48  *              from defects in it.
49  *
50  *      2. The origin of this software must not be misrepresented, either
51  *              by explicit claim or by omission.
52  *
53  *      3. Altered versions must be plainly marked as such, and must not
54  *              be misrepresented as being the original software.
55  *
56  *
57  ****    Alterations to Henry's code are...
58  ****
59  ****    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
60  ****    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 by Larry Wall and others
61  ****
62  ****    You may distribute under the terms of either the GNU General Public
63  ****    License or the Artistic License, as specified in the README file.
64
65  *
66  * Beware that some of this code is subtly aware of the way operator
67  * precedence is structured in regular expressions.  Serious changes in
68  * regular-expression syntax might require a total rethink.
69  */
70 #include "EXTERN.h"
71 #define PERL_IN_REGCOMP_C
72 #include "perl.h"
73
74 #ifndef PERL_IN_XSUB_RE
75 #  include "INTERN.h"
76 #endif
77
78 #define REG_COMP_C
79 #ifdef PERL_IN_XSUB_RE
80 #  include "re_comp.h"
81 #else
82 #  include "regcomp.h"
83 #endif
84
85 #ifdef op
86 #undef op
87 #endif /* op */
88
89 #ifdef MSDOS
90 #  if defined(BUGGY_MSC6)
91  /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
92 #    pragma optimize("a",off)
93  /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
94 #    pragma optimize("w",on )
95 #  endif /* BUGGY_MSC6 */
96 #endif /* MSDOS */
97
98 #ifndef STATIC
99 #define STATIC  static
100 #endif
101
102 typedef struct RExC_state_t {
103     U32         flags;                  /* are we folding, multilining? */
104     char        *precomp;               /* uncompiled string. */
105     REGEXP      *rx_sv;                 /* The SV that is the regexp. */
106     regexp      *rx;                    /* perl core regexp structure */
107     regexp_internal     *rxi;           /* internal data for regexp object pprivate field */        
108     char        *start;                 /* Start of input for compile */
109     char        *end;                   /* End of input for compile */
110     char        *parse;                 /* Input-scan pointer. */
111     I32         whilem_seen;            /* number of WHILEM in this expr */
112     regnode     *emit_start;            /* Start of emitted-code area */
113     regnode     *emit_bound;            /* First regnode outside of the allocated space */
114     regnode     *emit;                  /* Code-emit pointer; &regdummy = don't = compiling */
115     I32         naughty;                /* How bad is this pattern? */
116     I32         sawback;                /* Did we see \1, ...? */
117     U32         seen;
118     I32         size;                   /* Code size. */
119     I32         npar;                   /* Capture buffer count, (OPEN). */
120     I32         cpar;                   /* Capture buffer count, (CLOSE). */
121     I32         nestroot;               /* root parens we are in - used by accept */
122     I32         extralen;
123     I32         seen_zerolen;
124     I32         seen_evals;
125     regnode     **open_parens;          /* pointers to open parens */
126     regnode     **close_parens;         /* pointers to close parens */
127     regnode     *opend;                 /* END node in program */
128     I32         utf8;           /* whether the pattern is utf8 or not */
129     I32         orig_utf8;      /* whether the pattern was originally in utf8 */
130                                 /* XXX use this for future optimisation of case
131                                  * where pattern must be upgraded to utf8. */
132     HV          *charnames;             /* cache of named sequences */
133     HV          *paren_names;           /* Paren names */
134     
135     regnode     **recurse;              /* Recurse regops */
136     I32         recurse_count;          /* Number of recurse regops */
137 #if ADD_TO_REGEXEC
138     char        *starttry;              /* -Dr: where regtry was called. */
139 #define RExC_starttry   (pRExC_state->starttry)
140 #endif
141 #ifdef DEBUGGING
142     const char  *lastparse;
143     I32         lastnum;
144     AV          *paren_name_list;       /* idx -> name */
145 #define RExC_lastparse  (pRExC_state->lastparse)
146 #define RExC_lastnum    (pRExC_state->lastnum)
147 #define RExC_paren_name_list    (pRExC_state->paren_name_list)
148 #endif
149 } RExC_state_t;
150
151 #define RExC_flags      (pRExC_state->flags)
152 #define RExC_precomp    (pRExC_state->precomp)
153 #define RExC_rx_sv      (pRExC_state->rx_sv)
154 #define RExC_rx         (pRExC_state->rx)
155 #define RExC_rxi        (pRExC_state->rxi)
156 #define RExC_start      (pRExC_state->start)
157 #define RExC_end        (pRExC_state->end)
158 #define RExC_parse      (pRExC_state->parse)
159 #define RExC_whilem_seen        (pRExC_state->whilem_seen)
160 #ifdef RE_TRACK_PATTERN_OFFSETS
161 #define RExC_offsets    (pRExC_state->rxi->u.offsets) /* I am not like the others */
162 #endif
163 #define RExC_emit       (pRExC_state->emit)
164 #define RExC_emit_start (pRExC_state->emit_start)
165 #define RExC_emit_bound (pRExC_state->emit_bound)
166 #define RExC_naughty    (pRExC_state->naughty)
167 #define RExC_sawback    (pRExC_state->sawback)
168 #define RExC_seen       (pRExC_state->seen)
169 #define RExC_size       (pRExC_state->size)
170 #define RExC_npar       (pRExC_state->npar)
171 #define RExC_nestroot   (pRExC_state->nestroot)
172 #define RExC_extralen   (pRExC_state->extralen)
173 #define RExC_seen_zerolen       (pRExC_state->seen_zerolen)
174 #define RExC_seen_evals (pRExC_state->seen_evals)
175 #define RExC_utf8       (pRExC_state->utf8)
176 #define RExC_orig_utf8  (pRExC_state->orig_utf8)
177 #define RExC_charnames  (pRExC_state->charnames)
178 #define RExC_open_parens        (pRExC_state->open_parens)
179 #define RExC_close_parens       (pRExC_state->close_parens)
180 #define RExC_opend      (pRExC_state->opend)
181 #define RExC_paren_names        (pRExC_state->paren_names)
182 #define RExC_recurse    (pRExC_state->recurse)
183 #define RExC_recurse_count      (pRExC_state->recurse_count)
184
185
186 #define ISMULT1(c)      ((c) == '*' || (c) == '+' || (c) == '?')
187 #define ISMULT2(s)      ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
188         ((*s) == '{' && regcurly(s)))
189
190 #ifdef SPSTART
191 #undef SPSTART          /* dratted cpp namespace... */
192 #endif
193 /*
194  * Flags to be passed up and down.
195  */
196 #define WORST           0       /* Worst case. */
197 #define HASWIDTH        0x01    /* Known to match non-null strings. */
198 #define SIMPLE          0x02    /* Simple enough to be STAR/PLUS operand. */
199 #define SPSTART         0x04    /* Starts with * or +. */
200 #define TRYAGAIN        0x08    /* Weeded out a declaration. */
201 #define POSTPONED       0x10    /* (?1),(?&name), (??{...}) or similar */
202
203 #define REG_NODE_NUM(x) ((x) ? (int)((x)-RExC_emit_start) : -1)
204
205 /* whether trie related optimizations are enabled */
206 #if PERL_ENABLE_EXTENDED_TRIE_OPTIMISATION
207 #define TRIE_STUDY_OPT
208 #define FULL_TRIE_STUDY
209 #define TRIE_STCLASS
210 #endif
211
212
213
214 #define PBYTE(u8str,paren) ((U8*)(u8str))[(paren) >> 3]
215 #define PBITVAL(paren) (1 << ((paren) & 7))
216 #define PAREN_TEST(u8str,paren) ( PBYTE(u8str,paren) & PBITVAL(paren))
217 #define PAREN_SET(u8str,paren) PBYTE(u8str,paren) |= PBITVAL(paren)
218 #define PAREN_UNSET(u8str,paren) PBYTE(u8str,paren) &= (~PBITVAL(paren))
219
220
221 /* About scan_data_t.
222
223   During optimisation we recurse through the regexp program performing
224   various inplace (keyhole style) optimisations. In addition study_chunk
225   and scan_commit populate this data structure with information about
226   what strings MUST appear in the pattern. We look for the longest 
227   string that must appear for at a fixed location, and we look for the
228   longest string that may appear at a floating location. So for instance
229   in the pattern:
230   
231     /FOO[xX]A.*B[xX]BAR/
232     
233   Both 'FOO' and 'A' are fixed strings. Both 'B' and 'BAR' are floating
234   strings (because they follow a .* construct). study_chunk will identify
235   both FOO and BAR as being the longest fixed and floating strings respectively.
236   
237   The strings can be composites, for instance
238   
239      /(f)(o)(o)/
240      
241   will result in a composite fixed substring 'foo'.
242   
243   For each string some basic information is maintained:
244   
245   - offset or min_offset
246     This is the position the string must appear at, or not before.
247     It also implicitly (when combined with minlenp) tells us how many
248     character must match before the string we are searching.
249     Likewise when combined with minlenp and the length of the string
250     tells us how many characters must appear after the string we have 
251     found.
252   
253   - max_offset
254     Only used for floating strings. This is the rightmost point that
255     the string can appear at. Ifset to I32 max it indicates that the
256     string can occur infinitely far to the right.
257   
258   - minlenp
259     A pointer to the minimum length of the pattern that the string 
260     was found inside. This is important as in the case of positive 
261     lookahead or positive lookbehind we can have multiple patterns 
262     involved. Consider
263     
264     /(?=FOO).*F/
265     
266     The minimum length of the pattern overall is 3, the minimum length
267     of the lookahead part is 3, but the minimum length of the part that
268     will actually match is 1. So 'FOO's minimum length is 3, but the 
269     minimum length for the F is 1. This is important as the minimum length
270     is used to determine offsets in front of and behind the string being 
271     looked for.  Since strings can be composites this is the length of the
272     pattern at the time it was commited with a scan_commit. Note that
273     the length is calculated by study_chunk, so that the minimum lengths
274     are not known until the full pattern has been compiled, thus the 
275     pointer to the value.
276   
277   - lookbehind
278   
279     In the case of lookbehind the string being searched for can be
280     offset past the start point of the final matching string. 
281     If this value was just blithely removed from the min_offset it would
282     invalidate some of the calculations for how many chars must match
283     before or after (as they are derived from min_offset and minlen and
284     the length of the string being searched for). 
285     When the final pattern is compiled and the data is moved from the
286     scan_data_t structure into the regexp structure the information
287     about lookbehind is factored in, with the information that would 
288     have been lost precalculated in the end_shift field for the 
289     associated string.
290
291   The fields pos_min and pos_delta are used to store the minimum offset
292   and the delta to the maximum offset at the current point in the pattern.    
293
294 */
295
296 typedef struct scan_data_t {
297     /*I32 len_min;      unused */
298     /*I32 len_delta;    unused */
299     I32 pos_min;
300     I32 pos_delta;
301     SV *last_found;
302     I32 last_end;           /* min value, <0 unless valid. */
303     I32 last_start_min;
304     I32 last_start_max;
305     SV **longest;           /* Either &l_fixed, or &l_float. */
306     SV *longest_fixed;      /* longest fixed string found in pattern */
307     I32 offset_fixed;       /* offset where it starts */
308     I32 *minlen_fixed;      /* pointer to the minlen relevent to the string */
309     I32 lookbehind_fixed;   /* is the position of the string modfied by LB */
310     SV *longest_float;      /* longest floating string found in pattern */
311     I32 offset_float_min;   /* earliest point in string it can appear */
312     I32 offset_float_max;   /* latest point in string it can appear */
313     I32 *minlen_float;      /* pointer to the minlen relevent to the string */
314     I32 lookbehind_float;   /* is the position of the string modified by LB */
315     I32 flags;
316     I32 whilem_c;
317     I32 *last_closep;
318     struct regnode_charclass_class *start_class;
319 } scan_data_t;
320
321 /*
322  * Forward declarations for pregcomp()'s friends.
323  */
324
325 static const scan_data_t zero_scan_data =
326   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0};
327
328 #define SF_BEFORE_EOL           (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
329 #define SF_BEFORE_SEOL          0x0001
330 #define SF_BEFORE_MEOL          0x0002
331 #define SF_FIX_BEFORE_EOL       (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
332 #define SF_FL_BEFORE_EOL        (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
333
334 #ifdef NO_UNARY_PLUS
335 #  define SF_FIX_SHIFT_EOL      (0+2)
336 #  define SF_FL_SHIFT_EOL               (0+4)
337 #else
338 #  define SF_FIX_SHIFT_EOL      (+2)
339 #  define SF_FL_SHIFT_EOL               (+4)
340 #endif
341
342 #define SF_FIX_BEFORE_SEOL      (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
343 #define SF_FIX_BEFORE_MEOL      (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
344
345 #define SF_FL_BEFORE_SEOL       (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
346 #define SF_FL_BEFORE_MEOL       (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
347 #define SF_IS_INF               0x0040
348 #define SF_HAS_PAR              0x0080
349 #define SF_IN_PAR               0x0100
350 #define SF_HAS_EVAL             0x0200
351 #define SCF_DO_SUBSTR           0x0400
352 #define SCF_DO_STCLASS_AND      0x0800
353 #define SCF_DO_STCLASS_OR       0x1000
354 #define SCF_DO_STCLASS          (SCF_DO_STCLASS_AND|SCF_DO_STCLASS_OR)
355 #define SCF_WHILEM_VISITED_POS  0x2000
356
357 #define SCF_TRIE_RESTUDY        0x4000 /* Do restudy? */
358 #define SCF_SEEN_ACCEPT         0x8000 
359
360 #define UTF (RExC_utf8 != 0)
361 #define LOC ((RExC_flags & RXf_PMf_LOCALE) != 0)
362 #define FOLD ((RExC_flags & RXf_PMf_FOLD) != 0)
363
364 #define OOB_UNICODE             12345678
365 #define OOB_NAMEDCLASS          -1
366
367 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
368 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
369
370
371 /* length of regex to show in messages that don't mark a position within */
372 #define RegexLengthToShowInErrorMessages 127
373
374 /*
375  * If MARKER[12] are adjusted, be sure to adjust the constants at the top
376  * of t/op/regmesg.t, the tests in t/op/re_tests, and those in
377  * op/pragma/warn/regcomp.
378  */
379 #define MARKER1 "<-- HERE"    /* marker as it appears in the description */
380 #define MARKER2 " <-- HERE "  /* marker as it appears within the regex */
381
382 #define REPORT_LOCATION " in regex; marked by " MARKER1 " in m/%.*s" MARKER2 "%s/"
383
384 /*
385  * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
386  * arg. Show regex, up to a maximum length. If it's too long, chop and add
387  * "...".
388  */
389 #define _FAIL(code) STMT_START {                                        \
390     const char *ellipses = "";                                          \
391     IV len = RExC_end - RExC_precomp;                                   \
392                                                                         \
393     if (!SIZE_ONLY)                                                     \
394         SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv);                   \
395     if (len > RegexLengthToShowInErrorMessages) {                       \
396         /* chop 10 shorter than the max, to ensure meaning of "..." */  \
397         len = RegexLengthToShowInErrorMessages - 10;                    \
398         ellipses = "...";                                               \
399     }                                                                   \
400     code;                                                               \
401 } STMT_END
402
403 #define FAIL(msg) _FAIL(                            \
404     Perl_croak(aTHX_ "%s in regex m/%.*s%s/",       \
405             msg, (int)len, RExC_precomp, ellipses))
406
407 #define FAIL2(msg,arg) _FAIL(                       \
408     Perl_croak(aTHX_ msg " in regex m/%.*s%s/",     \
409             arg, (int)len, RExC_precomp, ellipses))
410
411 /*
412  * Simple_vFAIL -- like FAIL, but marks the current location in the scan
413  */
414 #define Simple_vFAIL(m) STMT_START {                                    \
415     const IV offset = RExC_parse - RExC_precomp;                        \
416     Perl_croak(aTHX_ "%s" REPORT_LOCATION,                              \
417             m, (int)offset, RExC_precomp, RExC_precomp + offset);       \
418 } STMT_END
419
420 /*
421  * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL()
422  */
423 #define vFAIL(m) STMT_START {                           \
424     if (!SIZE_ONLY)                                     \
425         SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv);   \
426     Simple_vFAIL(m);                                    \
427 } STMT_END
428
429 /*
430  * Like Simple_vFAIL(), but accepts two arguments.
431  */
432 #define Simple_vFAIL2(m,a1) STMT_START {                        \
433     const IV offset = RExC_parse - RExC_precomp;                        \
434     S_re_croak2(aTHX_ m, REPORT_LOCATION, a1,                   \
435             (int)offset, RExC_precomp, RExC_precomp + offset);  \
436 } STMT_END
437
438 /*
439  * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL2().
440  */
441 #define vFAIL2(m,a1) STMT_START {                       \
442     if (!SIZE_ONLY)                                     \
443         SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv);   \
444     Simple_vFAIL2(m, a1);                               \
445 } STMT_END
446
447
448 /*
449  * Like Simple_vFAIL(), but accepts three arguments.
450  */
451 #define Simple_vFAIL3(m, a1, a2) STMT_START {                   \
452     const IV offset = RExC_parse - RExC_precomp;                \
453     S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2,               \
454             (int)offset, RExC_precomp, RExC_precomp + offset);  \
455 } STMT_END
456
457 /*
458  * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL3().
459  */
460 #define vFAIL3(m,a1,a2) STMT_START {                    \
461     if (!SIZE_ONLY)                                     \
462         SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv);   \
463     Simple_vFAIL3(m, a1, a2);                           \
464 } STMT_END
465
466 /*
467  * Like Simple_vFAIL(), but accepts four arguments.
468  */
469 #define Simple_vFAIL4(m, a1, a2, a3) STMT_START {               \
470     const IV offset = RExC_parse - RExC_precomp;                \
471     S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3,           \
472             (int)offset, RExC_precomp, RExC_precomp + offset);  \
473 } STMT_END
474
475 #define vWARN(loc,m) STMT_START {                                       \
476     const IV offset = loc - RExC_precomp;                               \
477     Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s" REPORT_LOCATION,      \
478             m, (int)offset, RExC_precomp, RExC_precomp + offset);       \
479 } STMT_END
480
481 #define vWARNdep(loc,m) STMT_START {                                    \
482     const IV offset = loc - RExC_precomp;                               \
483     Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_REGEXP),          \
484             "%s" REPORT_LOCATION,                                       \
485             m, (int)offset, RExC_precomp, RExC_precomp + offset);       \
486 } STMT_END
487
488
489 #define vWARN2(loc, m, a1) STMT_START {                                 \
490     const IV offset = loc - RExC_precomp;                               \
491     Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,         \
492             a1, (int)offset, RExC_precomp, RExC_precomp + offset);      \
493 } STMT_END
494
495 #define vWARN3(loc, m, a1, a2) STMT_START {                             \
496     const IV offset = loc - RExC_precomp;                               \
497     Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,         \
498             a1, a2, (int)offset, RExC_precomp, RExC_precomp + offset);  \
499 } STMT_END
500
501 #define vWARN4(loc, m, a1, a2, a3) STMT_START {                         \
502     const IV offset = loc - RExC_precomp;                               \
503     Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,         \
504             a1, a2, a3, (int)offset, RExC_precomp, RExC_precomp + offset); \
505 } STMT_END
506
507 #define vWARN5(loc, m, a1, a2, a3, a4) STMT_START {                     \
508     const IV offset = loc - RExC_precomp;                               \
509     Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,         \
510             a1, a2, a3, a4, (int)offset, RExC_precomp, RExC_precomp + offset); \
511 } STMT_END
512
513
514 /* Allow for side effects in s */
515 #define REGC(c,s) STMT_START {                  \
516     if (!SIZE_ONLY) *(s) = (c); else (void)(s); \
517 } STMT_END
518
519 /* Macros for recording node offsets.   20001227 mjd@plover.com 
520  * Nodes are numbered 1, 2, 3, 4.  Node #n's position is recorded in
521  * element 2*n-1 of the array.  Element #2n holds the byte length node #n.
522  * Element 0 holds the number n.
523  * Position is 1 indexed.
524  */
525 #ifndef RE_TRACK_PATTERN_OFFSETS
526 #define Set_Node_Offset_To_R(node,byte)
527 #define Set_Node_Offset(node,byte)
528 #define Set_Cur_Node_Offset
529 #define Set_Node_Length_To_R(node,len)
530 #define Set_Node_Length(node,len)
531 #define Set_Node_Cur_Length(node)
532 #define Node_Offset(n) 
533 #define Node_Length(n) 
534 #define Set_Node_Offset_Length(node,offset,len)
535 #define ProgLen(ri) ri->u.proglen
536 #define SetProgLen(ri,x) ri->u.proglen = x
537 #else
538 #define ProgLen(ri) ri->u.offsets[0]
539 #define SetProgLen(ri,x) ri->u.offsets[0] = x
540 #define Set_Node_Offset_To_R(node,byte) STMT_START {                    \
541     if (! SIZE_ONLY) {                                                  \
542         MJD_OFFSET_DEBUG(("** (%d) offset of node %d is %d.\n",         \
543                     __LINE__, (int)(node), (int)(byte)));               \
544         if((node) < 0) {                                                \
545             Perl_croak(aTHX_ "value of node is %d in Offset macro", (int)(node)); \
546         } else {                                                        \
547             RExC_offsets[2*(node)-1] = (byte);                          \
548         }                                                               \
549     }                                                                   \
550 } STMT_END
551
552 #define Set_Node_Offset(node,byte) \
553     Set_Node_Offset_To_R((node)-RExC_emit_start, (byte)-RExC_start)
554 #define Set_Cur_Node_Offset Set_Node_Offset(RExC_emit, RExC_parse)
555
556 #define Set_Node_Length_To_R(node,len) STMT_START {                     \
557     if (! SIZE_ONLY) {                                                  \
558         MJD_OFFSET_DEBUG(("** (%d) size of node %d is %d.\n",           \
559                 __LINE__, (int)(node), (int)(len)));                    \
560         if((node) < 0) {                                                \
561             Perl_croak(aTHX_ "value of node is %d in Length macro", (int)(node)); \
562         } else {                                                        \
563             RExC_offsets[2*(node)] = (len);                             \
564         }                                                               \
565     }                                                                   \
566 } STMT_END
567
568 #define Set_Node_Length(node,len) \
569     Set_Node_Length_To_R((node)-RExC_emit_start, len)
570 #define Set_Cur_Node_Length(len) Set_Node_Length(RExC_emit, len)
571 #define Set_Node_Cur_Length(node) \
572     Set_Node_Length(node, RExC_parse - parse_start)
573
574 /* Get offsets and lengths */
575 #define Node_Offset(n) (RExC_offsets[2*((n)-RExC_emit_start)-1])
576 #define Node_Length(n) (RExC_offsets[2*((n)-RExC_emit_start)])
577
578 #define Set_Node_Offset_Length(node,offset,len) STMT_START {    \
579     Set_Node_Offset_To_R((node)-RExC_emit_start, (offset));     \
580     Set_Node_Length_To_R((node)-RExC_emit_start, (len));        \
581 } STMT_END
582 #endif
583
584 #if PERL_ENABLE_EXPERIMENTAL_REGEX_OPTIMISATIONS
585 #define EXPERIMENTAL_INPLACESCAN
586 #endif /*RE_TRACK_PATTERN_OFFSETS*/
587
588 #define DEBUG_STUDYDATA(str,data,depth)                              \
589 DEBUG_OPTIMISE_MORE_r(if(data){                                      \
590     PerlIO_printf(Perl_debug_log,                                    \
591         "%*s" str "Pos:%"IVdf"/%"IVdf                                \
592         " Flags: 0x%"UVXf" Whilem_c: %"IVdf" Lcp: %"IVdf" %s",       \
593         (int)(depth)*2, "",                                          \
594         (IV)((data)->pos_min),                                       \
595         (IV)((data)->pos_delta),                                     \
596         (UV)((data)->flags),                                         \
597         (IV)((data)->whilem_c),                                      \
598         (IV)((data)->last_closep ? *((data)->last_closep) : -1),     \
599         is_inf ? "INF " : ""                                         \
600     );                                                               \
601     if ((data)->last_found)                                          \
602         PerlIO_printf(Perl_debug_log,                                \
603             "Last:'%s' %"IVdf":%"IVdf"/%"IVdf" %sFixed:'%s' @ %"IVdf \
604             " %sFloat: '%s' @ %"IVdf"/%"IVdf"",                      \
605             SvPVX_const((data)->last_found),                         \
606             (IV)((data)->last_end),                                  \
607             (IV)((data)->last_start_min),                            \
608             (IV)((data)->last_start_max),                            \
609             ((data)->longest &&                                      \
610              (data)->longest==&((data)->longest_fixed)) ? "*" : "",  \
611             SvPVX_const((data)->longest_fixed),                      \
612             (IV)((data)->offset_fixed),                              \
613             ((data)->longest &&                                      \
614              (data)->longest==&((data)->longest_float)) ? "*" : "",  \
615             SvPVX_const((data)->longest_float),                      \
616             (IV)((data)->offset_float_min),                          \
617             (IV)((data)->offset_float_max)                           \
618         );                                                           \
619     PerlIO_printf(Perl_debug_log,"\n");                              \
620 });
621
622 static void clear_re(pTHX_ void *r);
623
624 /* Mark that we cannot extend a found fixed substring at this point.
625    Update the longest found anchored substring and the longest found
626    floating substrings if needed. */
627
628 STATIC void
629 S_scan_commit(pTHX_ const RExC_state_t *pRExC_state, scan_data_t *data, I32 *minlenp, int is_inf)
630 {
631     const STRLEN l = CHR_SVLEN(data->last_found);
632     const STRLEN old_l = CHR_SVLEN(*data->longest);
633     GET_RE_DEBUG_FLAGS_DECL;
634
635     if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
636         SvSetMagicSV(*data->longest, data->last_found);
637         if (*data->longest == data->longest_fixed) {
638             data->offset_fixed = l ? data->last_start_min : data->pos_min;
639             if (data->flags & SF_BEFORE_EOL)
640                 data->flags
641                     |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
642             else
643                 data->flags &= ~SF_FIX_BEFORE_EOL;
644             data->minlen_fixed=minlenp; 
645             data->lookbehind_fixed=0;
646         }
647         else { /* *data->longest == data->longest_float */
648             data->offset_float_min = l ? data->last_start_min : data->pos_min;
649             data->offset_float_max = (l
650                                       ? data->last_start_max
651                                       : data->pos_min + data->pos_delta);
652             if (is_inf || (U32)data->offset_float_max > (U32)I32_MAX)
653                 data->offset_float_max = I32_MAX;
654             if (data->flags & SF_BEFORE_EOL)
655                 data->flags
656                     |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
657             else
658                 data->flags &= ~SF_FL_BEFORE_EOL;
659             data->minlen_float=minlenp;
660             data->lookbehind_float=0;
661         }
662     }
663     SvCUR_set(data->last_found, 0);
664     {
665         SV * const sv = data->last_found;
666         if (SvUTF8(sv) && SvMAGICAL(sv)) {
667             MAGIC * const mg = mg_find(sv, PERL_MAGIC_utf8);
668             if (mg)
669                 mg->mg_len = 0;
670         }
671     }
672     data->last_end = -1;
673     data->flags &= ~SF_BEFORE_EOL;
674     DEBUG_STUDYDATA("commit: ",data,0);
675 }
676
677 /* Can match anything (initialization) */
678 STATIC void
679 S_cl_anything(const RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
680 {
681     ANYOF_CLASS_ZERO(cl);
682     ANYOF_BITMAP_SETALL(cl);
683     cl->flags = ANYOF_EOS|ANYOF_UNICODE_ALL;
684     if (LOC)
685         cl->flags |= ANYOF_LOCALE;
686 }
687
688 /* Can match anything (initialization) */
689 STATIC int
690 S_cl_is_anything(const struct regnode_charclass_class *cl)
691 {
692     int value;
693
694     for (value = 0; value <= ANYOF_MAX; value += 2)
695         if (ANYOF_CLASS_TEST(cl, value) && ANYOF_CLASS_TEST(cl, value + 1))
696             return 1;
697     if (!(cl->flags & ANYOF_UNICODE_ALL))
698         return 0;
699     if (!ANYOF_BITMAP_TESTALLSET((const void*)cl))
700         return 0;
701     return 1;
702 }
703
704 /* Can match anything (initialization) */
705 STATIC void
706 S_cl_init(const RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
707 {
708     Zero(cl, 1, struct regnode_charclass_class);
709     cl->type = ANYOF;
710     cl_anything(pRExC_state, cl);
711 }
712
713 STATIC void
714 S_cl_init_zero(const RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
715 {
716     Zero(cl, 1, struct regnode_charclass_class);
717     cl->type = ANYOF;
718     cl_anything(pRExC_state, cl);
719     if (LOC)
720         cl->flags |= ANYOF_LOCALE;
721 }
722
723 /* 'And' a given class with another one.  Can create false positives */
724 /* We assume that cl is not inverted */
725 STATIC void
726 S_cl_and(struct regnode_charclass_class *cl,
727         const struct regnode_charclass_class *and_with)
728 {
729
730     assert(and_with->type == ANYOF);
731     if (!(and_with->flags & ANYOF_CLASS)
732         && !(cl->flags & ANYOF_CLASS)
733         && (and_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
734         && !(and_with->flags & ANYOF_FOLD)
735         && !(cl->flags & ANYOF_FOLD)) {
736         int i;
737
738         if (and_with->flags & ANYOF_INVERT)
739             for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
740                 cl->bitmap[i] &= ~and_with->bitmap[i];
741         else
742             for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
743                 cl->bitmap[i] &= and_with->bitmap[i];
744     } /* XXXX: logic is complicated otherwise, leave it along for a moment. */
745     if (!(and_with->flags & ANYOF_EOS))
746         cl->flags &= ~ANYOF_EOS;
747
748     if (cl->flags & ANYOF_UNICODE_ALL && and_with->flags & ANYOF_UNICODE &&
749         !(and_with->flags & ANYOF_INVERT)) {
750         cl->flags &= ~ANYOF_UNICODE_ALL;
751         cl->flags |= ANYOF_UNICODE;
752         ARG_SET(cl, ARG(and_with));
753     }
754     if (!(and_with->flags & ANYOF_UNICODE_ALL) &&
755         !(and_with->flags & ANYOF_INVERT))
756         cl->flags &= ~ANYOF_UNICODE_ALL;
757     if (!(and_with->flags & (ANYOF_UNICODE|ANYOF_UNICODE_ALL)) &&
758         !(and_with->flags & ANYOF_INVERT))
759         cl->flags &= ~ANYOF_UNICODE;
760 }
761
762 /* 'OR' a given class with another one.  Can create false positives */
763 /* We assume that cl is not inverted */
764 STATIC void
765 S_cl_or(const RExC_state_t *pRExC_state, struct regnode_charclass_class *cl, const struct regnode_charclass_class *or_with)
766 {
767     if (or_with->flags & ANYOF_INVERT) {
768         /* We do not use
769          * (B1 | CL1) | (!B2 & !CL2) = (B1 | !B2 & !CL2) | (CL1 | (!B2 & !CL2))
770          *   <= (B1 | !B2) | (CL1 | !CL2)
771          * which is wasteful if CL2 is small, but we ignore CL2:
772          *   (B1 | CL1) | (!B2 & !CL2) <= (B1 | CL1) | !B2 = (B1 | !B2) | CL1
773          * XXXX Can we handle case-fold?  Unclear:
774          *   (OK1(i) | OK1(i')) | !(OK1(i) | OK1(i')) =
775          *   (OK1(i) | OK1(i')) | (!OK1(i) & !OK1(i'))
776          */
777         if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
778              && !(or_with->flags & ANYOF_FOLD)
779              && !(cl->flags & ANYOF_FOLD) ) {
780             int i;
781
782             for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
783                 cl->bitmap[i] |= ~or_with->bitmap[i];
784         } /* XXXX: logic is complicated otherwise */
785         else {
786             cl_anything(pRExC_state, cl);
787         }
788     } else {
789         /* (B1 | CL1) | (B2 | CL2) = (B1 | B2) | (CL1 | CL2)) */
790         if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
791              && (!(or_with->flags & ANYOF_FOLD)
792                  || (cl->flags & ANYOF_FOLD)) ) {
793             int i;
794
795             /* OR char bitmap and class bitmap separately */
796             for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
797                 cl->bitmap[i] |= or_with->bitmap[i];
798             if (or_with->flags & ANYOF_CLASS) {
799                 for (i = 0; i < ANYOF_CLASSBITMAP_SIZE; i++)
800                     cl->classflags[i] |= or_with->classflags[i];
801                 cl->flags |= ANYOF_CLASS;
802             }
803         }
804         else { /* XXXX: logic is complicated, leave it along for a moment. */
805             cl_anything(pRExC_state, cl);
806         }
807     }
808     if (or_with->flags & ANYOF_EOS)
809         cl->flags |= ANYOF_EOS;
810
811     if (cl->flags & ANYOF_UNICODE && or_with->flags & ANYOF_UNICODE &&
812         ARG(cl) != ARG(or_with)) {
813         cl->flags |= ANYOF_UNICODE_ALL;
814         cl->flags &= ~ANYOF_UNICODE;
815     }
816     if (or_with->flags & ANYOF_UNICODE_ALL) {
817         cl->flags |= ANYOF_UNICODE_ALL;
818         cl->flags &= ~ANYOF_UNICODE;
819     }
820 }
821
822 #define TRIE_LIST_ITEM(state,idx) (trie->states[state].trans.list)[ idx ]
823 #define TRIE_LIST_CUR(state)  ( TRIE_LIST_ITEM( state, 0 ).forid )
824 #define TRIE_LIST_LEN(state) ( TRIE_LIST_ITEM( state, 0 ).newstate )
825 #define TRIE_LIST_USED(idx)  ( trie->states[state].trans.list ? (TRIE_LIST_CUR( idx ) - 1) : 0 )
826
827
828 #ifdef DEBUGGING
829 /*
830    dump_trie(trie,widecharmap,revcharmap)
831    dump_trie_interim_list(trie,widecharmap,revcharmap,next_alloc)
832    dump_trie_interim_table(trie,widecharmap,revcharmap,next_alloc)
833
834    These routines dump out a trie in a somewhat readable format.
835    The _interim_ variants are used for debugging the interim
836    tables that are used to generate the final compressed
837    representation which is what dump_trie expects.
838
839    Part of the reason for their existance is to provide a form
840    of documentation as to how the different representations function.
841
842 */
843
844 /*
845   Dumps the final compressed table form of the trie to Perl_debug_log.
846   Used for debugging make_trie().
847 */
848  
849 STATIC void
850 S_dump_trie(pTHX_ const struct _reg_trie_data *trie, HV *widecharmap,
851             AV *revcharmap, U32 depth)
852 {
853     U32 state;
854     SV *sv=sv_newmortal();
855     int colwidth= widecharmap ? 6 : 4;
856     GET_RE_DEBUG_FLAGS_DECL;
857
858
859     PerlIO_printf( Perl_debug_log, "%*sChar : %-6s%-6s%-4s ",
860         (int)depth * 2 + 2,"",
861         "Match","Base","Ofs" );
862
863     for( state = 0 ; state < trie->uniquecharcount ; state++ ) {
864         SV ** const tmp = av_fetch( revcharmap, state, 0);
865         if ( tmp ) {
866             PerlIO_printf( Perl_debug_log, "%*s", 
867                 colwidth,
868                 pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), colwidth, 
869                             PL_colors[0], PL_colors[1],
870                             (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) |
871                             PERL_PV_ESCAPE_FIRSTCHAR 
872                 ) 
873             );
874         }
875     }
876     PerlIO_printf( Perl_debug_log, "\n%*sState|-----------------------",
877         (int)depth * 2 + 2,"");
878
879     for( state = 0 ; state < trie->uniquecharcount ; state++ )
880         PerlIO_printf( Perl_debug_log, "%.*s", colwidth, "--------");
881     PerlIO_printf( Perl_debug_log, "\n");
882
883     for( state = 1 ; state < trie->statecount ; state++ ) {
884         const U32 base = trie->states[ state ].trans.base;
885
886         PerlIO_printf( Perl_debug_log, "%*s#%4"UVXf"|", (int)depth * 2 + 2,"", (UV)state);
887
888         if ( trie->states[ state ].wordnum ) {
889             PerlIO_printf( Perl_debug_log, " W%4X", trie->states[ state ].wordnum );
890         } else {
891             PerlIO_printf( Perl_debug_log, "%6s", "" );
892         }
893
894         PerlIO_printf( Perl_debug_log, " @%4"UVXf" ", (UV)base );
895
896         if ( base ) {
897             U32 ofs = 0;
898
899             while( ( base + ofs  < trie->uniquecharcount ) ||
900                    ( base + ofs - trie->uniquecharcount < trie->lasttrans
901                      && trie->trans[ base + ofs - trie->uniquecharcount ].check != state))
902                     ofs++;
903
904             PerlIO_printf( Perl_debug_log, "+%2"UVXf"[ ", (UV)ofs);
905
906             for ( ofs = 0 ; ofs < trie->uniquecharcount ; ofs++ ) {
907                 if ( ( base + ofs >= trie->uniquecharcount ) &&
908                      ( base + ofs - trie->uniquecharcount < trie->lasttrans ) &&
909                      trie->trans[ base + ofs - trie->uniquecharcount ].check == state )
910                 {
911                    PerlIO_printf( Perl_debug_log, "%*"UVXf,
912                     colwidth,
913                     (UV)trie->trans[ base + ofs - trie->uniquecharcount ].next );
914                 } else {
915                     PerlIO_printf( Perl_debug_log, "%*s",colwidth,"   ." );
916                 }
917             }
918
919             PerlIO_printf( Perl_debug_log, "]");
920
921         }
922         PerlIO_printf( Perl_debug_log, "\n" );
923     }
924 }    
925 /*
926   Dumps a fully constructed but uncompressed trie in list form.
927   List tries normally only are used for construction when the number of 
928   possible chars (trie->uniquecharcount) is very high.
929   Used for debugging make_trie().
930 */
931 STATIC void
932 S_dump_trie_interim_list(pTHX_ const struct _reg_trie_data *trie,
933                          HV *widecharmap, AV *revcharmap, U32 next_alloc,
934                          U32 depth)
935 {
936     U32 state;
937     SV *sv=sv_newmortal();
938     int colwidth= widecharmap ? 6 : 4;
939     GET_RE_DEBUG_FLAGS_DECL;
940     /* print out the table precompression.  */
941     PerlIO_printf( Perl_debug_log, "%*sState :Word | Transition Data\n%*s%s",
942         (int)depth * 2 + 2,"", (int)depth * 2 + 2,"",
943         "------:-----+-----------------\n" );
944     
945     for( state=1 ; state < next_alloc ; state ++ ) {
946         U16 charid;
947     
948         PerlIO_printf( Perl_debug_log, "%*s %4"UVXf" :",
949             (int)depth * 2 + 2,"", (UV)state  );
950         if ( ! trie->states[ state ].wordnum ) {
951             PerlIO_printf( Perl_debug_log, "%5s| ","");
952         } else {
953             PerlIO_printf( Perl_debug_log, "W%4x| ",
954                 trie->states[ state ].wordnum
955             );
956         }
957         for( charid = 1 ; charid <= TRIE_LIST_USED( state ) ; charid++ ) {
958             SV ** const tmp = av_fetch( revcharmap, TRIE_LIST_ITEM(state,charid).forid, 0);
959             if ( tmp ) {
960                 PerlIO_printf( Perl_debug_log, "%*s:%3X=%4"UVXf" | ",
961                     colwidth,
962                     pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), colwidth, 
963                             PL_colors[0], PL_colors[1],
964                             (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) |
965                             PERL_PV_ESCAPE_FIRSTCHAR 
966                     ) ,
967                     TRIE_LIST_ITEM(state,charid).forid,
968                     (UV)TRIE_LIST_ITEM(state,charid).newstate
969                 );
970                 if (!(charid % 10)) 
971                     PerlIO_printf(Perl_debug_log, "\n%*s| ",
972                         (int)((depth * 2) + 14), "");
973             }
974         }
975         PerlIO_printf( Perl_debug_log, "\n");
976     }
977 }    
978
979 /*
980   Dumps a fully constructed but uncompressed trie in table form.
981   This is the normal DFA style state transition table, with a few 
982   twists to facilitate compression later. 
983   Used for debugging make_trie().
984 */
985 STATIC void
986 S_dump_trie_interim_table(pTHX_ const struct _reg_trie_data *trie,
987                           HV *widecharmap, AV *revcharmap, U32 next_alloc,
988                           U32 depth)
989 {
990     U32 state;
991     U16 charid;
992     SV *sv=sv_newmortal();
993     int colwidth= widecharmap ? 6 : 4;
994     GET_RE_DEBUG_FLAGS_DECL;
995     
996     /*
997        print out the table precompression so that we can do a visual check
998        that they are identical.
999      */
1000     
1001     PerlIO_printf( Perl_debug_log, "%*sChar : ",(int)depth * 2 + 2,"" );
1002
1003     for( charid = 0 ; charid < trie->uniquecharcount ; charid++ ) {
1004         SV ** const tmp = av_fetch( revcharmap, charid, 0);
1005         if ( tmp ) {
1006             PerlIO_printf( Perl_debug_log, "%*s", 
1007                 colwidth,
1008                 pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), colwidth, 
1009                             PL_colors[0], PL_colors[1],
1010                             (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) |
1011                             PERL_PV_ESCAPE_FIRSTCHAR 
1012                 ) 
1013             );
1014         }
1015     }
1016
1017     PerlIO_printf( Perl_debug_log, "\n%*sState+-",(int)depth * 2 + 2,"" );
1018
1019     for( charid=0 ; charid < trie->uniquecharcount ; charid++ ) {
1020         PerlIO_printf( Perl_debug_log, "%.*s", colwidth,"--------");
1021     }
1022
1023     PerlIO_printf( Perl_debug_log, "\n" );
1024
1025     for( state=1 ; state < next_alloc ; state += trie->uniquecharcount ) {
1026
1027         PerlIO_printf( Perl_debug_log, "%*s%4"UVXf" : ", 
1028             (int)depth * 2 + 2,"",
1029             (UV)TRIE_NODENUM( state ) );
1030
1031         for( charid = 0 ; charid < trie->uniquecharcount ; charid++ ) {
1032             UV v=(UV)SAFE_TRIE_NODENUM( trie->trans[ state + charid ].next );
1033             if (v)
1034                 PerlIO_printf( Perl_debug_log, "%*"UVXf, colwidth, v );
1035             else
1036                 PerlIO_printf( Perl_debug_log, "%*s", colwidth, "." );
1037         }
1038         if ( ! trie->states[ TRIE_NODENUM( state ) ].wordnum ) {
1039             PerlIO_printf( Perl_debug_log, " (%4"UVXf")\n", (UV)trie->trans[ state ].check );
1040         } else {
1041             PerlIO_printf( Perl_debug_log, " (%4"UVXf") W%4X\n", (UV)trie->trans[ state ].check,
1042             trie->states[ TRIE_NODENUM( state ) ].wordnum );
1043         }
1044     }
1045 }
1046
1047 #endif
1048
1049 /* make_trie(startbranch,first,last,tail,word_count,flags,depth)
1050   startbranch: the first branch in the whole branch sequence
1051   first      : start branch of sequence of branch-exact nodes.
1052                May be the same as startbranch
1053   last       : Thing following the last branch.
1054                May be the same as tail.
1055   tail       : item following the branch sequence
1056   count      : words in the sequence
1057   flags      : currently the OP() type we will be building one of /EXACT(|F|Fl)/
1058   depth      : indent depth
1059
1060 Inplace optimizes a sequence of 2 or more Branch-Exact nodes into a TRIE node.
1061
1062 A trie is an N'ary tree where the branches are determined by digital
1063 decomposition of the key. IE, at the root node you look up the 1st character and
1064 follow that branch repeat until you find the end of the branches. Nodes can be
1065 marked as "accepting" meaning they represent a complete word. Eg:
1066
1067   /he|she|his|hers/
1068
1069 would convert into the following structure. Numbers represent states, letters
1070 following numbers represent valid transitions on the letter from that state, if
1071 the number is in square brackets it represents an accepting state, otherwise it
1072 will be in parenthesis.
1073
1074       +-h->+-e->[3]-+-r->(8)-+-s->[9]
1075       |    |
1076       |   (2)
1077       |    |
1078      (1)   +-i->(6)-+-s->[7]
1079       |
1080       +-s->(3)-+-h->(4)-+-e->[5]
1081
1082       Accept Word Mapping: 3=>1 (he),5=>2 (she), 7=>3 (his), 9=>4 (hers)
1083
1084 This shows that when matching against the string 'hers' we will begin at state 1
1085 read 'h' and move to state 2, read 'e' and move to state 3 which is accepting,
1086 then read 'r' and go to state 8 followed by 's' which takes us to state 9 which
1087 is also accepting. Thus we know that we can match both 'he' and 'hers' with a
1088 single traverse. We store a mapping from accepting to state to which word was
1089 matched, and then when we have multiple possibilities we try to complete the
1090 rest of the regex in the order in which they occured in the alternation.
1091
1092 The only prior NFA like behaviour that would be changed by the TRIE support is
1093 the silent ignoring of duplicate alternations which are of the form:
1094
1095  / (DUPE|DUPE) X? (?{ ... }) Y /x
1096
1097 Thus EVAL blocks follwing a trie may be called a different number of times with
1098 and without the optimisation. With the optimisations dupes will be silently
1099 ignored. This inconsistant behaviour of EVAL type nodes is well established as
1100 the following demonstrates:
1101
1102  'words'=~/(word|word|word)(?{ print $1 })[xyz]/
1103
1104 which prints out 'word' three times, but
1105
1106  'words'=~/(word|word|word)(?{ print $1 })S/
1107
1108 which doesnt print it out at all. This is due to other optimisations kicking in.
1109
1110 Example of what happens on a structural level:
1111
1112 The regexp /(ac|ad|ab)+/ will produce the folowing debug output:
1113
1114    1: CURLYM[1] {1,32767}(18)
1115    5:   BRANCH(8)
1116    6:     EXACT <ac>(16)
1117    8:   BRANCH(11)
1118    9:     EXACT <ad>(16)
1119   11:   BRANCH(14)
1120   12:     EXACT <ab>(16)
1121   16:   SUCCEED(0)
1122   17:   NOTHING(18)
1123   18: END(0)
1124
1125 This would be optimizable with startbranch=5, first=5, last=16, tail=16
1126 and should turn into:
1127
1128    1: CURLYM[1] {1,32767}(18)
1129    5:   TRIE(16)
1130         [Words:3 Chars Stored:6 Unique Chars:4 States:5 NCP:1]
1131           <ac>
1132           <ad>
1133           <ab>
1134   16:   SUCCEED(0)
1135   17:   NOTHING(18)
1136   18: END(0)
1137
1138 Cases where tail != last would be like /(?foo|bar)baz/:
1139
1140    1: BRANCH(4)
1141    2:   EXACT <foo>(8)
1142    4: BRANCH(7)
1143    5:   EXACT <bar>(8)
1144    7: TAIL(8)
1145    8: EXACT <baz>(10)
1146   10: END(0)
1147
1148 which would be optimizable with startbranch=1, first=1, last=7, tail=8
1149 and would end up looking like:
1150
1151     1: TRIE(8)
1152       [Words:2 Chars Stored:6 Unique Chars:5 States:7 NCP:1]
1153         <foo>
1154         <bar>
1155    7: TAIL(8)
1156    8: EXACT <baz>(10)
1157   10: END(0)
1158
1159     d = uvuni_to_utf8_flags(d, uv, 0);
1160
1161 is the recommended Unicode-aware way of saying
1162
1163     *(d++) = uv;
1164 */
1165
1166 #define TRIE_STORE_REVCHAR                                                 \
1167     STMT_START {                                                           \
1168         if (UTF) {                                                         \
1169             SV *zlopp = newSV(2);                                          \
1170             unsigned char *flrbbbbb = (unsigned char *) SvPVX(zlopp);      \
1171             unsigned const char *const kapow = uvuni_to_utf8(flrbbbbb, uvc & 0xFF); \
1172             SvCUR_set(zlopp, kapow - flrbbbbb);                            \
1173             SvPOK_on(zlopp);                                               \
1174             SvUTF8_on(zlopp);                                              \
1175             av_push(revcharmap, zlopp);                                    \
1176         } else {                                                           \
1177             char ooooff = (char)uvc;                                               \
1178             av_push(revcharmap, newSVpvn(&ooooff, 1));                     \
1179         }                                                                  \
1180         } STMT_END
1181
1182 #define TRIE_READ_CHAR STMT_START {                                           \
1183     wordlen++;                                                                \
1184     if ( UTF ) {                                                              \
1185         if ( folder ) {                                                       \
1186             if ( foldlen > 0 ) {                                              \
1187                uvc = utf8n_to_uvuni( scan, UTF8_MAXLEN, &len, uniflags );     \
1188                foldlen -= len;                                                \
1189                scan += len;                                                   \
1190                len = 0;                                                       \
1191             } else {                                                          \
1192                 uvc = utf8n_to_uvuni( (const U8*)uc, UTF8_MAXLEN, &len, uniflags);\
1193                 uvc = to_uni_fold( uvc, foldbuf, &foldlen );                  \
1194                 foldlen -= UNISKIP( uvc );                                    \
1195                 scan = foldbuf + UNISKIP( uvc );                              \
1196             }                                                                 \
1197         } else {                                                              \
1198             uvc = utf8n_to_uvuni( (const U8*)uc, UTF8_MAXLEN, &len, uniflags);\
1199         }                                                                     \
1200     } else {                                                                  \
1201         uvc = (U32)*uc;                                                       \
1202         len = 1;                                                              \
1203     }                                                                         \
1204 } STMT_END
1205
1206
1207
1208 #define TRIE_LIST_PUSH(state,fid,ns) STMT_START {               \
1209     if ( TRIE_LIST_CUR( state ) >=TRIE_LIST_LEN( state ) ) {    \
1210         U32 ging = TRIE_LIST_LEN( state ) *= 2;                 \
1211         Renew( trie->states[ state ].trans.list, ging, reg_trie_trans_le ); \
1212     }                                                           \
1213     TRIE_LIST_ITEM( state, TRIE_LIST_CUR( state ) ).forid = fid;     \
1214     TRIE_LIST_ITEM( state, TRIE_LIST_CUR( state ) ).newstate = ns;   \
1215     TRIE_LIST_CUR( state )++;                                   \
1216 } STMT_END
1217
1218 #define TRIE_LIST_NEW(state) STMT_START {                       \
1219     Newxz( trie->states[ state ].trans.list,               \
1220         4, reg_trie_trans_le );                                 \
1221      TRIE_LIST_CUR( state ) = 1;                                \
1222      TRIE_LIST_LEN( state ) = 4;                                \
1223 } STMT_END
1224
1225 #define TRIE_HANDLE_WORD(state) STMT_START {                    \
1226     U16 dupe= trie->states[ state ].wordnum;                    \
1227     regnode * const noper_next = regnext( noper );              \
1228                                                                 \
1229     if (trie->wordlen)                                          \
1230         trie->wordlen[ curword ] = wordlen;                     \
1231     DEBUG_r({                                                   \
1232         /* store the word for dumping */                        \
1233         SV* tmp;                                                \
1234         if (OP(noper) != NOTHING)                               \
1235             tmp = newSVpvn_utf8(STRING(noper), STR_LEN(noper), UTF);    \
1236         else                                                    \
1237             tmp = newSVpvn_utf8( "", 0, UTF );                  \
1238         av_push( trie_words, tmp );                             \
1239     });                                                         \
1240                                                                 \
1241     curword++;                                                  \
1242                                                                 \
1243     if ( noper_next < tail ) {                                  \
1244         if (!trie->jump)                                        \
1245             trie->jump = (U16 *) PerlMemShared_calloc( word_count + 1, sizeof(U16) ); \
1246         trie->jump[curword] = (U16)(noper_next - convert);      \
1247         if (!jumper)                                            \
1248             jumper = noper_next;                                \
1249         if (!nextbranch)                                        \
1250             nextbranch= regnext(cur);                           \
1251     }                                                           \
1252                                                                 \
1253     if ( dupe ) {                                               \
1254         /* So it's a dupe. This means we need to maintain a   */\
1255         /* linked-list from the first to the next.            */\
1256         /* we only allocate the nextword buffer when there    */\
1257         /* a dupe, so first time we have to do the allocation */\
1258         if (!trie->nextword)                                    \
1259             trie->nextword = (U16 *)                                    \
1260                 PerlMemShared_calloc( word_count + 1, sizeof(U16));     \
1261         while ( trie->nextword[dupe] )                          \
1262             dupe= trie->nextword[dupe];                         \
1263         trie->nextword[dupe]= curword;                          \
1264     } else {                                                    \
1265         /* we haven't inserted this word yet.                */ \
1266         trie->states[ state ].wordnum = curword;                \
1267     }                                                           \
1268 } STMT_END
1269
1270
1271 #define TRIE_TRANS_STATE(state,base,ucharcount,charid,special)          \
1272      ( ( base + charid >=  ucharcount                                   \
1273          && base + charid < ubound                                      \
1274          && state == trie->trans[ base - ucharcount + charid ].check    \
1275          && trie->trans[ base - ucharcount + charid ].next )            \
1276            ? trie->trans[ base - ucharcount + charid ].next             \
1277            : ( state==1 ? special : 0 )                                 \
1278       )
1279
1280 #define MADE_TRIE       1
1281 #define MADE_JUMP_TRIE  2
1282 #define MADE_EXACT_TRIE 4
1283
1284 STATIC I32
1285 S_make_trie(pTHX_ RExC_state_t *pRExC_state, regnode *startbranch, regnode *first, regnode *last, regnode *tail, U32 word_count, U32 flags, U32 depth)
1286 {
1287     dVAR;
1288     /* first pass, loop through and scan words */
1289     reg_trie_data *trie;
1290     HV *widecharmap = NULL;
1291     AV *revcharmap = newAV();
1292     regnode *cur;
1293     const U32 uniflags = UTF8_ALLOW_DEFAULT;
1294     STRLEN len = 0;
1295     UV uvc = 0;
1296     U16 curword = 0;
1297     U32 next_alloc = 0;
1298     regnode *jumper = NULL;
1299     regnode *nextbranch = NULL;
1300     regnode *convert = NULL;
1301     /* we just use folder as a flag in utf8 */
1302     const U8 * const folder = ( flags == EXACTF
1303                        ? PL_fold
1304                        : ( flags == EXACTFL
1305                            ? PL_fold_locale
1306                            : NULL
1307                          )
1308                      );
1309
1310 #ifdef DEBUGGING
1311     const U32 data_slot = add_data( pRExC_state, 4, "tuuu" );
1312     AV *trie_words = NULL;
1313     /* along with revcharmap, this only used during construction but both are
1314      * useful during debugging so we store them in the struct when debugging.
1315      */
1316 #else
1317     const U32 data_slot = add_data( pRExC_state, 2, "tu" );
1318     STRLEN trie_charcount=0;
1319 #endif
1320     SV *re_trie_maxbuff;
1321     GET_RE_DEBUG_FLAGS_DECL;
1322 #ifndef DEBUGGING
1323     PERL_UNUSED_ARG(depth);
1324 #endif
1325
1326     trie = (reg_trie_data *) PerlMemShared_calloc( 1, sizeof(reg_trie_data) );
1327     trie->refcount = 1;
1328     trie->startstate = 1;
1329     trie->wordcount = word_count;
1330     RExC_rxi->data->data[ data_slot ] = (void*)trie;
1331     trie->charmap = (U16 *) PerlMemShared_calloc( 256, sizeof(U16) );
1332     if (!(UTF && folder))
1333         trie->bitmap = (char *) PerlMemShared_calloc( ANYOF_BITMAP_SIZE, 1 );
1334     DEBUG_r({
1335         trie_words = newAV();
1336     });
1337
1338     re_trie_maxbuff = get_sv(RE_TRIE_MAXBUF_NAME, 1);
1339     if (!SvIOK(re_trie_maxbuff)) {
1340         sv_setiv(re_trie_maxbuff, RE_TRIE_MAXBUF_INIT);
1341     }
1342     DEBUG_OPTIMISE_r({
1343                 PerlIO_printf( Perl_debug_log,
1344                   "%*smake_trie start==%d, first==%d, last==%d, tail==%d depth=%d\n",
1345                   (int)depth * 2 + 2, "", 
1346                   REG_NODE_NUM(startbranch),REG_NODE_NUM(first), 
1347                   REG_NODE_NUM(last), REG_NODE_NUM(tail),
1348                   (int)depth);
1349     });
1350    
1351    /* Find the node we are going to overwrite */
1352     if ( first == startbranch && OP( last ) != BRANCH ) {
1353         /* whole branch chain */
1354         convert = first;
1355     } else {
1356         /* branch sub-chain */
1357         convert = NEXTOPER( first );
1358     }
1359         
1360     /*  -- First loop and Setup --
1361
1362        We first traverse the branches and scan each word to determine if it
1363        contains widechars, and how many unique chars there are, this is
1364        important as we have to build a table with at least as many columns as we
1365        have unique chars.
1366
1367        We use an array of integers to represent the character codes 0..255
1368        (trie->charmap) and we use a an HV* to store Unicode characters. We use the
1369        native representation of the character value as the key and IV's for the
1370        coded index.
1371
1372        *TODO* If we keep track of how many times each character is used we can
1373        remap the columns so that the table compression later on is more
1374        efficient in terms of memory by ensuring most common value is in the
1375        middle and the least common are on the outside.  IMO this would be better
1376        than a most to least common mapping as theres a decent chance the most
1377        common letter will share a node with the least common, meaning the node
1378        will not be compressable. With a middle is most common approach the worst
1379        case is when we have the least common nodes twice.
1380
1381      */
1382
1383     for ( cur = first ; cur < last ; cur = regnext( cur ) ) {
1384         regnode * const noper = NEXTOPER( cur );
1385         const U8 *uc = (U8*)STRING( noper );
1386         const U8 * const e  = uc + STR_LEN( noper );
1387         STRLEN foldlen = 0;
1388         U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1389         const U8 *scan = (U8*)NULL;
1390         U32 wordlen      = 0;         /* required init */
1391         STRLEN chars = 0;
1392         bool set_bit = trie->bitmap ? 1 : 0; /*store the first char in the bitmap?*/
1393
1394         if (OP(noper) == NOTHING) {
1395             trie->minlen= 0;
1396             continue;
1397         }
1398         if ( set_bit ) /* bitmap only alloced when !(UTF&&Folding) */
1399             TRIE_BITMAP_SET(trie,*uc); /* store the raw first byte
1400                                           regardless of encoding */
1401
1402         for ( ; uc < e ; uc += len ) {
1403             TRIE_CHARCOUNT(trie)++;
1404             TRIE_READ_CHAR;
1405             chars++;
1406             if ( uvc < 256 ) {
1407                 if ( !trie->charmap[ uvc ] ) {
1408                     trie->charmap[ uvc ]=( ++trie->uniquecharcount );
1409                     if ( folder )
1410                         trie->charmap[ folder[ uvc ] ] = trie->charmap[ uvc ];
1411                     TRIE_STORE_REVCHAR;
1412                 }
1413                 if ( set_bit ) {
1414                     /* store the codepoint in the bitmap, and if its ascii
1415                        also store its folded equivelent. */
1416                     TRIE_BITMAP_SET(trie,uvc);
1417
1418                     /* store the folded codepoint */
1419                     if ( folder ) TRIE_BITMAP_SET(trie,folder[ uvc ]);
1420
1421                     if ( !UTF ) {
1422                         /* store first byte of utf8 representation of
1423                            codepoints in the 127 < uvc < 256 range */
1424                         if (127 < uvc && uvc < 192) {
1425                             TRIE_BITMAP_SET(trie,194);
1426                         } else if (191 < uvc ) {
1427                             TRIE_BITMAP_SET(trie,195);
1428                         /* && uvc < 256 -- we know uvc is < 256 already */
1429                         }
1430                     }
1431                     set_bit = 0; /* We've done our bit :-) */
1432                 }
1433             } else {
1434                 SV** svpp;
1435                 if ( !widecharmap )
1436                     widecharmap = newHV();
1437
1438                 svpp = hv_fetch( widecharmap, (char*)&uvc, sizeof( UV ), 1 );
1439
1440                 if ( !svpp )
1441                     Perl_croak( aTHX_ "error creating/fetching widecharmap entry for 0x%"UVXf, uvc );
1442
1443                 if ( !SvTRUE( *svpp ) ) {
1444                     sv_setiv( *svpp, ++trie->uniquecharcount );
1445                     TRIE_STORE_REVCHAR;
1446                 }
1447             }
1448         }
1449         if( cur == first ) {
1450             trie->minlen=chars;
1451             trie->maxlen=chars;
1452         } else if (chars < trie->minlen) {
1453             trie->minlen=chars;
1454         } else if (chars > trie->maxlen) {
1455             trie->maxlen=chars;
1456         }
1457
1458     } /* end first pass */
1459     DEBUG_TRIE_COMPILE_r(
1460         PerlIO_printf( Perl_debug_log, "%*sTRIE(%s): W:%d C:%d Uq:%d Min:%d Max:%d\n",
1461                 (int)depth * 2 + 2,"",
1462                 ( widecharmap ? "UTF8" : "NATIVE" ), (int)word_count,
1463                 (int)TRIE_CHARCOUNT(trie), trie->uniquecharcount,
1464                 (int)trie->minlen, (int)trie->maxlen )
1465     );
1466     trie->wordlen = (U32 *) PerlMemShared_calloc( word_count, sizeof(U32) );
1467
1468     /*
1469         We now know what we are dealing with in terms of unique chars and
1470         string sizes so we can calculate how much memory a naive
1471         representation using a flat table  will take. If it's over a reasonable
1472         limit (as specified by ${^RE_TRIE_MAXBUF}) we use a more memory
1473         conservative but potentially much slower representation using an array
1474         of lists.
1475
1476         At the end we convert both representations into the same compressed
1477         form that will be used in regexec.c for matching with. The latter
1478         is a form that cannot be used to construct with but has memory
1479         properties similar to the list form and access properties similar
1480         to the table form making it both suitable for fast searches and
1481         small enough that its feasable to store for the duration of a program.
1482
1483         See the comment in the code where the compressed table is produced
1484         inplace from the flat tabe representation for an explanation of how
1485         the compression works.
1486
1487     */
1488
1489
1490     if ( (IV)( ( TRIE_CHARCOUNT(trie) + 1 ) * trie->uniquecharcount + 1) > SvIV(re_trie_maxbuff) ) {
1491         /*
1492             Second Pass -- Array Of Lists Representation
1493
1494             Each state will be represented by a list of charid:state records
1495             (reg_trie_trans_le) the first such element holds the CUR and LEN
1496             points of the allocated array. (See defines above).
1497
1498             We build the initial structure using the lists, and then convert
1499             it into the compressed table form which allows faster lookups
1500             (but cant be modified once converted).
1501         */
1502
1503         STRLEN transcount = 1;
1504
1505         DEBUG_TRIE_COMPILE_MORE_r( PerlIO_printf( Perl_debug_log, 
1506             "%*sCompiling trie using list compiler\n",
1507             (int)depth * 2 + 2, ""));
1508         
1509         trie->states = (reg_trie_state *)
1510             PerlMemShared_calloc( TRIE_CHARCOUNT(trie) + 2,
1511                                   sizeof(reg_trie_state) );
1512         TRIE_LIST_NEW(1);
1513         next_alloc = 2;
1514
1515         for ( cur = first ; cur < last ; cur = regnext( cur ) ) {
1516
1517             regnode * const noper = NEXTOPER( cur );
1518             U8 *uc           = (U8*)STRING( noper );
1519             const U8 * const e = uc + STR_LEN( noper );
1520             U32 state        = 1;         /* required init */
1521             U16 charid       = 0;         /* sanity init */
1522             U8 *scan         = (U8*)NULL; /* sanity init */
1523             STRLEN foldlen   = 0;         /* required init */
1524             U32 wordlen      = 0;         /* required init */
1525             U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1526
1527             if (OP(noper) != NOTHING) {
1528                 for ( ; uc < e ; uc += len ) {
1529
1530                     TRIE_READ_CHAR;
1531
1532                     if ( uvc < 256 ) {
1533                         charid = trie->charmap[ uvc ];
1534                     } else {
1535                         SV** const svpp = hv_fetch( widecharmap, (char*)&uvc, sizeof( UV ), 0);
1536                         if ( !svpp ) {
1537                             charid = 0;
1538                         } else {
1539                             charid=(U16)SvIV( *svpp );
1540                         }
1541                     }
1542                     /* charid is now 0 if we dont know the char read, or nonzero if we do */
1543                     if ( charid ) {
1544
1545                         U16 check;
1546                         U32 newstate = 0;
1547
1548                         charid--;
1549                         if ( !trie->states[ state ].trans.list ) {
1550                             TRIE_LIST_NEW( state );
1551                         }
1552                         for ( check = 1; check <= TRIE_LIST_USED( state ); check++ ) {
1553                             if ( TRIE_LIST_ITEM( state, check ).forid == charid ) {
1554                                 newstate = TRIE_LIST_ITEM( state, check ).newstate;
1555                                 break;
1556                             }
1557                         }
1558                         if ( ! newstate ) {
1559                             newstate = next_alloc++;
1560                             TRIE_LIST_PUSH( state, charid, newstate );
1561                             transcount++;
1562                         }
1563                         state = newstate;
1564                     } else {
1565                         Perl_croak( aTHX_ "panic! In trie construction, no char mapping for %"IVdf, uvc );
1566                     }
1567                 }
1568             }
1569             TRIE_HANDLE_WORD(state);
1570
1571         } /* end second pass */
1572
1573         /* next alloc is the NEXT state to be allocated */
1574         trie->statecount = next_alloc; 
1575         trie->states = (reg_trie_state *)
1576             PerlMemShared_realloc( trie->states,
1577                                    next_alloc
1578                                    * sizeof(reg_trie_state) );
1579
1580         /* and now dump it out before we compress it */
1581         DEBUG_TRIE_COMPILE_MORE_r(dump_trie_interim_list(trie, widecharmap,
1582                                                          revcharmap, next_alloc,
1583                                                          depth+1)
1584         );
1585
1586         trie->trans = (reg_trie_trans *)
1587             PerlMemShared_calloc( transcount, sizeof(reg_trie_trans) );
1588         {
1589             U32 state;
1590             U32 tp = 0;
1591             U32 zp = 0;
1592
1593
1594             for( state=1 ; state < next_alloc ; state ++ ) {
1595                 U32 base=0;
1596
1597                 /*
1598                 DEBUG_TRIE_COMPILE_MORE_r(
1599                     PerlIO_printf( Perl_debug_log, "tp: %d zp: %d ",tp,zp)
1600                 );
1601                 */
1602
1603                 if (trie->states[state].trans.list) {
1604                     U16 minid=TRIE_LIST_ITEM( state, 1).forid;
1605                     U16 maxid=minid;
1606                     U16 idx;
1607
1608                     for( idx = 2 ; idx <= TRIE_LIST_USED( state ) ; idx++ ) {
1609                         const U16 forid = TRIE_LIST_ITEM( state, idx).forid;
1610                         if ( forid < minid ) {
1611                             minid=forid;
1612                         } else if ( forid > maxid ) {
1613                             maxid=forid;
1614                         }
1615                     }
1616                     if ( transcount < tp + maxid - minid + 1) {
1617                         transcount *= 2;
1618                         trie->trans = (reg_trie_trans *)
1619                             PerlMemShared_realloc( trie->trans,
1620                                                      transcount
1621                                                      * sizeof(reg_trie_trans) );
1622                         Zero( trie->trans + (transcount / 2), transcount / 2 , reg_trie_trans );
1623                     }
1624                     base = trie->uniquecharcount + tp - minid;
1625                     if ( maxid == minid ) {
1626                         U32 set = 0;
1627                         for ( ; zp < tp ; zp++ ) {
1628                             if ( ! trie->trans[ zp ].next ) {
1629                                 base = trie->uniquecharcount + zp - minid;
1630                                 trie->trans[ zp ].next = TRIE_LIST_ITEM( state, 1).newstate;
1631                                 trie->trans[ zp ].check = state;
1632                                 set = 1;
1633                                 break;
1634                             }
1635                         }
1636                         if ( !set ) {
1637                             trie->trans[ tp ].next = TRIE_LIST_ITEM( state, 1).newstate;
1638                             trie->trans[ tp ].check = state;
1639                             tp++;
1640                             zp = tp;
1641                         }
1642                     } else {
1643                         for ( idx=1; idx <= TRIE_LIST_USED( state ) ; idx++ ) {
1644                             const U32 tid = base -  trie->uniquecharcount + TRIE_LIST_ITEM( state, idx ).forid;
1645                             trie->trans[ tid ].next = TRIE_LIST_ITEM( state, idx ).newstate;
1646                             trie->trans[ tid ].check = state;
1647                         }
1648                         tp += ( maxid - minid + 1 );
1649                     }
1650                     Safefree(trie->states[ state ].trans.list);
1651                 }
1652                 /*
1653                 DEBUG_TRIE_COMPILE_MORE_r(
1654                     PerlIO_printf( Perl_debug_log, " base: %d\n",base);
1655                 );
1656                 */
1657                 trie->states[ state ].trans.base=base;
1658             }
1659             trie->lasttrans = tp + 1;
1660         }
1661     } else {
1662         /*
1663            Second Pass -- Flat Table Representation.
1664
1665            we dont use the 0 slot of either trans[] or states[] so we add 1 to each.
1666            We know that we will need Charcount+1 trans at most to store the data
1667            (one row per char at worst case) So we preallocate both structures
1668            assuming worst case.
1669
1670            We then construct the trie using only the .next slots of the entry
1671            structs.
1672
1673            We use the .check field of the first entry of the node  temporarily to
1674            make compression both faster and easier by keeping track of how many non
1675            zero fields are in the node.
1676
1677            Since trans are numbered from 1 any 0 pointer in the table is a FAIL
1678            transition.
1679
1680            There are two terms at use here: state as a TRIE_NODEIDX() which is a
1681            number representing the first entry of the node, and state as a
1682            TRIE_NODENUM() which is the trans number. state 1 is TRIE_NODEIDX(1) and
1683            TRIE_NODENUM(1), state 2 is TRIE_NODEIDX(2) and TRIE_NODENUM(3) if there
1684            are 2 entrys per node. eg:
1685
1686              A B       A B
1687           1. 2 4    1. 3 7
1688           2. 0 3    3. 0 5
1689           3. 0 0    5. 0 0
1690           4. 0 0    7. 0 0
1691
1692            The table is internally in the right hand, idx form. However as we also
1693            have to deal with the states array which is indexed by nodenum we have to
1694            use TRIE_NODENUM() to convert.
1695
1696         */
1697         DEBUG_TRIE_COMPILE_MORE_r( PerlIO_printf( Perl_debug_log, 
1698             "%*sCompiling trie using table compiler\n",
1699             (int)depth * 2 + 2, ""));
1700
1701         trie->trans = (reg_trie_trans *)
1702             PerlMemShared_calloc( ( TRIE_CHARCOUNT(trie) + 1 )
1703                                   * trie->uniquecharcount + 1,
1704                                   sizeof(reg_trie_trans) );
1705         trie->states = (reg_trie_state *)
1706             PerlMemShared_calloc( TRIE_CHARCOUNT(trie) + 2,
1707                                   sizeof(reg_trie_state) );
1708         next_alloc = trie->uniquecharcount + 1;
1709
1710
1711         for ( cur = first ; cur < last ; cur = regnext( cur ) ) {
1712
1713             regnode * const noper   = NEXTOPER( cur );
1714             const U8 *uc     = (U8*)STRING( noper );
1715             const U8 * const e = uc + STR_LEN( noper );
1716
1717             U32 state        = 1;         /* required init */
1718
1719             U16 charid       = 0;         /* sanity init */
1720             U32 accept_state = 0;         /* sanity init */
1721             U8 *scan         = (U8*)NULL; /* sanity init */
1722
1723             STRLEN foldlen   = 0;         /* required init */
1724             U32 wordlen      = 0;         /* required init */
1725             U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1726
1727             if ( OP(noper) != NOTHING ) {
1728                 for ( ; uc < e ; uc += len ) {
1729
1730                     TRIE_READ_CHAR;
1731
1732                     if ( uvc < 256 ) {
1733                         charid = trie->charmap[ uvc ];
1734                     } else {
1735                         SV* const * const svpp = hv_fetch( widecharmap, (char*)&uvc, sizeof( UV ), 0);
1736                         charid = svpp ? (U16)SvIV(*svpp) : 0;
1737                     }
1738                     if ( charid ) {
1739                         charid--;
1740                         if ( !trie->trans[ state + charid ].next ) {
1741                             trie->trans[ state + charid ].next = next_alloc;
1742                             trie->trans[ state ].check++;
1743                             next_alloc += trie->uniquecharcount;
1744                         }
1745                         state = trie->trans[ state + charid ].next;
1746                     } else {
1747                         Perl_croak( aTHX_ "panic! In trie construction, no char mapping for %"IVdf, uvc );
1748                     }
1749                     /* charid is now 0 if we dont know the char read, or nonzero if we do */
1750                 }
1751             }
1752             accept_state = TRIE_NODENUM( state );
1753             TRIE_HANDLE_WORD(accept_state);
1754
1755         } /* end second pass */
1756
1757         /* and now dump it out before we compress it */
1758         DEBUG_TRIE_COMPILE_MORE_r(dump_trie_interim_table(trie, widecharmap,
1759                                                           revcharmap,
1760                                                           next_alloc, depth+1));
1761
1762         {
1763         /*
1764            * Inplace compress the table.*
1765
1766            For sparse data sets the table constructed by the trie algorithm will
1767            be mostly 0/FAIL transitions or to put it another way mostly empty.
1768            (Note that leaf nodes will not contain any transitions.)
1769
1770            This algorithm compresses the tables by eliminating most such
1771            transitions, at the cost of a modest bit of extra work during lookup:
1772
1773            - Each states[] entry contains a .base field which indicates the
1774            index in the state[] array wheres its transition data is stored.
1775
1776            - If .base is 0 there are no  valid transitions from that node.
1777
1778            - If .base is nonzero then charid is added to it to find an entry in
1779            the trans array.
1780
1781            -If trans[states[state].base+charid].check!=state then the
1782            transition is taken to be a 0/Fail transition. Thus if there are fail
1783            transitions at the front of the node then the .base offset will point
1784            somewhere inside the previous nodes data (or maybe even into a node
1785            even earlier), but the .check field determines if the transition is
1786            valid.
1787
1788            XXX - wrong maybe?
1789            The following process inplace converts the table to the compressed
1790            table: We first do not compress the root node 1,and mark its all its
1791            .check pointers as 1 and set its .base pointer as 1 as well. This
1792            allows to do a DFA construction from the compressed table later, and
1793            ensures that any .base pointers we calculate later are greater than
1794            0.
1795
1796            - We set 'pos' to indicate the first entry of the second node.
1797
1798            - We then iterate over the columns of the node, finding the first and
1799            last used entry at l and m. We then copy l..m into pos..(pos+m-l),
1800            and set the .check pointers accordingly, and advance pos
1801            appropriately and repreat for the next node. Note that when we copy
1802            the next pointers we have to convert them from the original
1803            NODEIDX form to NODENUM form as the former is not valid post
1804            compression.
1805
1806            - If a node has no transitions used we mark its base as 0 and do not
1807            advance the pos pointer.
1808
1809            - If a node only has one transition we use a second pointer into the
1810            structure to fill in allocated fail transitions from other states.
1811            This pointer is independent of the main pointer and scans forward
1812            looking for null transitions that are allocated to a state. When it
1813            finds one it writes the single transition into the "hole".  If the
1814            pointer doesnt find one the single transition is appended as normal.
1815
1816            - Once compressed we can Renew/realloc the structures to release the
1817            excess space.
1818
1819            See "Table-Compression Methods" in sec 3.9 of the Red Dragon,
1820            specifically Fig 3.47 and the associated pseudocode.
1821
1822            demq
1823         */
1824         const U32 laststate = TRIE_NODENUM( next_alloc );
1825         U32 state, charid;
1826         U32 pos = 0, zp=0;
1827         trie->statecount = laststate;
1828
1829         for ( state = 1 ; state < laststate ; state++ ) {
1830             U8 flag = 0;
1831             const U32 stateidx = TRIE_NODEIDX( state );
1832             const U32 o_used = trie->trans[ stateidx ].check;
1833             U32 used = trie->trans[ stateidx ].check;
1834             trie->trans[ stateidx ].check = 0;
1835
1836             for ( charid = 0 ; used && charid < trie->uniquecharcount ; charid++ ) {
1837                 if ( flag || trie->trans[ stateidx + charid ].next ) {
1838                     if ( trie->trans[ stateidx + charid ].next ) {
1839                         if (o_used == 1) {
1840                             for ( ; zp < pos ; zp++ ) {
1841                                 if ( ! trie->trans[ zp ].next ) {
1842                                     break;
1843                                 }
1844                             }
1845                             trie->states[ state ].trans.base = zp + trie->uniquecharcount - charid ;
1846                             trie->trans[ zp ].next = SAFE_TRIE_NODENUM( trie->trans[ stateidx + charid ].next );
1847                             trie->trans[ zp ].check = state;
1848                             if ( ++zp > pos ) pos = zp;
1849                             break;
1850                         }
1851                         used--;
1852                     }
1853                     if ( !flag ) {
1854                         flag = 1;
1855                         trie->states[ state ].trans.base = pos + trie->uniquecharcount - charid ;
1856                     }
1857                     trie->trans[ pos ].next = SAFE_TRIE_NODENUM( trie->trans[ stateidx + charid ].next );
1858                     trie->trans[ pos ].check = state;
1859                     pos++;
1860                 }
1861             }
1862         }
1863         trie->lasttrans = pos + 1;
1864         trie->states = (reg_trie_state *)
1865             PerlMemShared_realloc( trie->states, laststate
1866                                    * sizeof(reg_trie_state) );
1867         DEBUG_TRIE_COMPILE_MORE_r(
1868                 PerlIO_printf( Perl_debug_log,
1869                     "%*sAlloc: %d Orig: %"IVdf" elements, Final:%"IVdf". Savings of %%%5.2f\n",
1870                     (int)depth * 2 + 2,"",
1871                     (int)( ( TRIE_CHARCOUNT(trie) + 1 ) * trie->uniquecharcount + 1 ),
1872                     (IV)next_alloc,
1873                     (IV)pos,
1874                     ( ( next_alloc - pos ) * 100 ) / (double)next_alloc );
1875             );
1876
1877         } /* end table compress */
1878     }
1879     DEBUG_TRIE_COMPILE_MORE_r(
1880             PerlIO_printf(Perl_debug_log, "%*sStatecount:%"UVxf" Lasttrans:%"UVxf"\n",
1881                 (int)depth * 2 + 2, "",
1882                 (UV)trie->statecount,
1883                 (UV)trie->lasttrans)
1884     );
1885     /* resize the trans array to remove unused space */
1886     trie->trans = (reg_trie_trans *)
1887         PerlMemShared_realloc( trie->trans, trie->lasttrans
1888                                * sizeof(reg_trie_trans) );
1889
1890     /* and now dump out the compressed format */
1891     DEBUG_TRIE_COMPILE_r(dump_trie(trie, widecharmap, revcharmap, depth+1));
1892
1893     {   /* Modify the program and insert the new TRIE node*/ 
1894         U8 nodetype =(U8)(flags & 0xFF);
1895         char *str=NULL;
1896         
1897 #ifdef DEBUGGING
1898         regnode *optimize = NULL;
1899 #ifdef RE_TRACK_PATTERN_OFFSETS
1900
1901         U32 mjd_offset = 0;
1902         U32 mjd_nodelen = 0;
1903 #endif /* RE_TRACK_PATTERN_OFFSETS */
1904 #endif /* DEBUGGING */
1905         /*
1906            This means we convert either the first branch or the first Exact,
1907            depending on whether the thing following (in 'last') is a branch
1908            or not and whther first is the startbranch (ie is it a sub part of
1909            the alternation or is it the whole thing.)
1910            Assuming its a sub part we conver the EXACT otherwise we convert
1911            the whole branch sequence, including the first.
1912          */
1913         /* Find the node we are going to overwrite */
1914         if ( first != startbranch || OP( last ) == BRANCH ) {
1915             /* branch sub-chain */
1916             NEXT_OFF( first ) = (U16)(last - first);
1917 #ifdef RE_TRACK_PATTERN_OFFSETS
1918             DEBUG_r({
1919                 mjd_offset= Node_Offset((convert));
1920                 mjd_nodelen= Node_Length((convert));
1921             });
1922 #endif
1923             /* whole branch chain */
1924         }
1925 #ifdef RE_TRACK_PATTERN_OFFSETS
1926         else {
1927             DEBUG_r({
1928                 const  regnode *nop = NEXTOPER( convert );
1929                 mjd_offset= Node_Offset((nop));
1930                 mjd_nodelen= Node_Length((nop));
1931             });
1932         }
1933         DEBUG_OPTIMISE_r(
1934             PerlIO_printf(Perl_debug_log, "%*sMJD offset:%"UVuf" MJD length:%"UVuf"\n",
1935                 (int)depth * 2 + 2, "",
1936                 (UV)mjd_offset, (UV)mjd_nodelen)
1937         );
1938 #endif
1939         /* But first we check to see if there is a common prefix we can 
1940            split out as an EXACT and put in front of the TRIE node.  */
1941         trie->startstate= 1;
1942         if ( trie->bitmap && !widecharmap && !trie->jump  ) {
1943             U32 state;
1944             for ( state = 1 ; state < trie->statecount-1 ; state++ ) {
1945                 U32 ofs = 0;
1946                 I32 idx = -1;
1947                 U32 count = 0;
1948                 const U32 base = trie->states[ state ].trans.base;
1949
1950                 if ( trie->states[state].wordnum )
1951                         count = 1;
1952
1953                 for ( ofs = 0 ; ofs < trie->uniquecharcount ; ofs++ ) {
1954                     if ( ( base + ofs >= trie->uniquecharcount ) &&
1955                          ( base + ofs - trie->uniquecharcount < trie->lasttrans ) &&
1956                          trie->trans[ base + ofs - trie->uniquecharcount ].check == state )
1957                     {
1958                         if ( ++count > 1 ) {
1959                             SV **tmp = av_fetch( revcharmap, ofs, 0);
1960                             const U8 *ch = (U8*)SvPV_nolen_const( *tmp );
1961                             if ( state == 1 ) break;
1962                             if ( count == 2 ) {
1963                                 Zero(trie->bitmap, ANYOF_BITMAP_SIZE, char);
1964                                 DEBUG_OPTIMISE_r(
1965                                     PerlIO_printf(Perl_debug_log,
1966                                         "%*sNew Start State=%"UVuf" Class: [",
1967                                         (int)depth * 2 + 2, "",
1968                                         (UV)state));
1969                                 if (idx >= 0) {
1970                                     SV ** const tmp = av_fetch( revcharmap, idx, 0);
1971                                     const U8 * const ch = (U8*)SvPV_nolen_const( *tmp );
1972
1973                                     TRIE_BITMAP_SET(trie,*ch);
1974                                     if ( folder )
1975                                         TRIE_BITMAP_SET(trie, folder[ *ch ]);
1976                                     DEBUG_OPTIMISE_r(
1977                                         PerlIO_printf(Perl_debug_log, (char*)ch)
1978                                     );
1979                                 }
1980                             }
1981                             TRIE_BITMAP_SET(trie,*ch);
1982                             if ( folder )
1983                                 TRIE_BITMAP_SET(trie,folder[ *ch ]);
1984                             DEBUG_OPTIMISE_r(PerlIO_printf( Perl_debug_log,"%s", ch));
1985                         }
1986                         idx = ofs;
1987                     }
1988                 }
1989                 if ( count == 1 ) {
1990                     SV **tmp = av_fetch( revcharmap, idx, 0);
1991                     STRLEN len;
1992                     char *ch = SvPV( *tmp, len );
1993                     DEBUG_OPTIMISE_r({
1994                         SV *sv=sv_newmortal();
1995                         PerlIO_printf( Perl_debug_log,
1996                             "%*sPrefix State: %"UVuf" Idx:%"UVuf" Char='%s'\n",
1997                             (int)depth * 2 + 2, "",
1998                             (UV)state, (UV)idx, 
1999                             pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 6, 
2000                                 PL_colors[0], PL_colors[1],
2001                                 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) |
2002                                 PERL_PV_ESCAPE_FIRSTCHAR 
2003                             )
2004                         );
2005                     });
2006                     if ( state==1 ) {
2007                         OP( convert ) = nodetype;
2008                         str=STRING(convert);
2009                         STR_LEN(convert)=0;
2010                     }
2011                     STR_LEN(convert) += len;
2012                     while (len--)
2013                         *str++ = *ch++;
2014                 } else {
2015 #ifdef DEBUGGING            
2016                     if (state>1)
2017                         DEBUG_OPTIMISE_r(PerlIO_printf( Perl_debug_log,"]\n"));
2018 #endif
2019                     break;
2020                 }
2021             }
2022             if (str) {
2023                 regnode *n = convert+NODE_SZ_STR(convert);
2024                 NEXT_OFF(convert) = NODE_SZ_STR(convert);
2025                 trie->startstate = state;
2026                 trie->minlen -= (state - 1);
2027                 trie->maxlen -= (state - 1);
2028 #ifdef DEBUGGING
2029                /* At least the UNICOS C compiler choked on this
2030                 * being argument to DEBUG_r(), so let's just have
2031                 * it right here. */
2032                if (
2033 #ifdef PERL_EXT_RE_BUILD
2034                    1
2035 #else
2036                    DEBUG_r_TEST
2037 #endif
2038                    ) {
2039                    regnode *fix = convert;
2040                    U32 word = trie->wordcount;
2041                    mjd_nodelen++;
2042                    Set_Node_Offset_Length(convert, mjd_offset, state - 1);
2043                    while( ++fix < n ) {
2044                        Set_Node_Offset_Length(fix, 0, 0);
2045                    }
2046                    while (word--) {
2047                        SV ** const tmp = av_fetch( trie_words, word, 0 );
2048                        if (tmp) {
2049                            if ( STR_LEN(convert) <= SvCUR(*tmp) )
2050                                sv_chop(*tmp, SvPV_nolen(*tmp) + STR_LEN(convert));
2051                            else
2052                                sv_chop(*tmp, SvPV_nolen(*tmp) + SvCUR(*tmp));
2053                        }
2054                    }
2055                }
2056 #endif
2057                 if (trie->maxlen) {
2058                     convert = n;
2059                 } else {
2060                     NEXT_OFF(convert) = (U16)(tail - convert);
2061                     DEBUG_r(optimize= n);
2062                 }
2063             }
2064         }
2065         if (!jumper) 
2066             jumper = last; 
2067         if ( trie->maxlen ) {
2068             NEXT_OFF( convert ) = (U16)(tail - convert);
2069             ARG_SET( convert, data_slot );
2070             /* Store the offset to the first unabsorbed branch in 
2071                jump[0], which is otherwise unused by the jump logic. 
2072                We use this when dumping a trie and during optimisation. */
2073             if (trie->jump) 
2074                 trie->jump[0] = (U16)(nextbranch - convert);
2075             
2076             /* XXXX */
2077             if ( !trie->states[trie->startstate].wordnum && trie->bitmap && 
2078                  ( (char *)jumper - (char *)convert) >= (int)sizeof(struct regnode_charclass) )
2079             {
2080                 OP( convert ) = TRIEC;
2081                 Copy(trie->bitmap, ((struct regnode_charclass *)convert)->bitmap, ANYOF_BITMAP_SIZE, char);
2082                 PerlMemShared_free(trie->bitmap);
2083                 trie->bitmap= NULL;
2084             } else 
2085                 OP( convert ) = TRIE;
2086
2087             /* store the type in the flags */
2088             convert->flags = nodetype;
2089             DEBUG_r({
2090             optimize = convert 
2091                       + NODE_STEP_REGNODE 
2092                       + regarglen[ OP( convert ) ];
2093             });
2094             /* XXX We really should free up the resource in trie now, 
2095                    as we won't use them - (which resources?) dmq */
2096         }
2097         /* needed for dumping*/
2098         DEBUG_r(if (optimize) {
2099             regnode *opt = convert;
2100
2101             while ( ++opt < optimize) {
2102                 Set_Node_Offset_Length(opt,0,0);
2103             }
2104             /* 
2105                 Try to clean up some of the debris left after the 
2106                 optimisation.
2107              */
2108             while( optimize < jumper ) {
2109                 mjd_nodelen += Node_Length((optimize));
2110                 OP( optimize ) = OPTIMIZED;
2111                 Set_Node_Offset_Length(optimize,0,0);
2112                 optimize++;
2113             }
2114             Set_Node_Offset_Length(convert,mjd_offset,mjd_nodelen);
2115         });
2116     } /* end node insert */
2117     RExC_rxi->data->data[ data_slot + 1 ] = (void*)widecharmap;
2118 #ifdef DEBUGGING
2119     RExC_rxi->data->data[ data_slot + TRIE_WORDS_OFFSET ] = (void*)trie_words;
2120     RExC_rxi->data->data[ data_slot + 3 ] = (void*)revcharmap;
2121 #else
2122     SvREFCNT_dec(revcharmap);
2123 #endif
2124     return trie->jump 
2125            ? MADE_JUMP_TRIE 
2126            : trie->startstate>1 
2127              ? MADE_EXACT_TRIE 
2128              : MADE_TRIE;
2129 }
2130
2131 STATIC void
2132 S_make_trie_failtable(pTHX_ RExC_state_t *pRExC_state, regnode *source,  regnode *stclass, U32 depth)
2133 {
2134 /* The Trie is constructed and compressed now so we can build a fail array now if its needed
2135
2136    This is basically the Aho-Corasick algorithm. Its from exercise 3.31 and 3.32 in the
2137    "Red Dragon" -- Compilers, principles, techniques, and tools. Aho, Sethi, Ullman 1985/88
2138    ISBN 0-201-10088-6
2139
2140    We find the fail state for each state in the trie, this state is the longest proper
2141    suffix of the current states 'word' that is also a proper prefix of another word in our
2142    trie. State 1 represents the word '' and is the thus the default fail state. This allows
2143    the DFA not to have to restart after its tried and failed a word at a given point, it
2144    simply continues as though it had been matching the other word in the first place.
2145    Consider
2146       'abcdgu'=~/abcdefg|cdgu/
2147    When we get to 'd' we are still matching the first word, we would encounter 'g' which would
2148    fail, which would bring use to the state representing 'd' in the second word where we would
2149    try 'g' and succeed, prodceding to match 'cdgu'.
2150  */
2151  /* add a fail transition */
2152     const U32 trie_offset = ARG(source);
2153     reg_trie_data *trie=(reg_trie_data *)RExC_rxi->data->data[trie_offset];
2154     U32 *q;
2155     const U32 ucharcount = trie->uniquecharcount;
2156     const U32 numstates = trie->statecount;
2157     const U32 ubound = trie->lasttrans + ucharcount;
2158     U32 q_read = 0;
2159     U32 q_write = 0;
2160     U32 charid;
2161     U32 base = trie->states[ 1 ].trans.base;
2162     U32 *fail;
2163     reg_ac_data *aho;
2164     const U32 data_slot = add_data( pRExC_state, 1, "T" );
2165     GET_RE_DEBUG_FLAGS_DECL;
2166 #ifndef DEBUGGING
2167     PERL_UNUSED_ARG(depth);
2168 #endif
2169
2170
2171     ARG_SET( stclass, data_slot );
2172     aho = (reg_ac_data *) PerlMemShared_calloc( 1, sizeof(reg_ac_data) );
2173     RExC_rxi->data->data[ data_slot ] = (void*)aho;
2174     aho->trie=trie_offset;
2175     aho->states=(reg_trie_state *)PerlMemShared_malloc( numstates * sizeof(reg_trie_state) );
2176     Copy( trie->states, aho->states, numstates, reg_trie_state );
2177     Newxz( q, numstates, U32);
2178     aho->fail = (U32 *) PerlMemShared_calloc( numstates, sizeof(U32) );
2179     aho->refcount = 1;
2180     fail = aho->fail;
2181     /* initialize fail[0..1] to be 1 so that we always have
2182        a valid final fail state */
2183     fail[ 0 ] = fail[ 1 ] = 1;
2184
2185     for ( charid = 0; charid < ucharcount ; charid++ ) {
2186         const U32 newstate = TRIE_TRANS_STATE( 1, base, ucharcount, charid, 0 );
2187         if ( newstate ) {
2188             q[ q_write ] = newstate;
2189             /* set to point at the root */
2190             fail[ q[ q_write++ ] ]=1;
2191         }
2192     }
2193     while ( q_read < q_write) {
2194         const U32 cur = q[ q_read++ % numstates ];
2195         base = trie->states[ cur ].trans.base;
2196
2197         for ( charid = 0 ; charid < ucharcount ; charid++ ) {
2198             const U32 ch_state = TRIE_TRANS_STATE( cur, base, ucharcount, charid, 1 );
2199             if (ch_state) {
2200                 U32 fail_state = cur;
2201                 U32 fail_base;
2202                 do {
2203                     fail_state = fail[ fail_state ];
2204                     fail_base = aho->states[ fail_state ].trans.base;
2205                 } while ( !TRIE_TRANS_STATE( fail_state, fail_base, ucharcount, charid, 1 ) );
2206
2207                 fail_state = TRIE_TRANS_STATE( fail_state, fail_base, ucharcount, charid, 1 );
2208                 fail[ ch_state ] = fail_state;
2209                 if ( !aho->states[ ch_state ].wordnum && aho->states[ fail_state ].wordnum )
2210                 {
2211                         aho->states[ ch_state ].wordnum =  aho->states[ fail_state ].wordnum;
2212                 }
2213                 q[ q_write++ % numstates] = ch_state;
2214             }
2215         }
2216     }
2217     /* restore fail[0..1] to 0 so that we "fall out" of the AC loop
2218        when we fail in state 1, this allows us to use the
2219        charclass scan to find a valid start char. This is based on the principle
2220        that theres a good chance the string being searched contains lots of stuff
2221        that cant be a start char.
2222      */
2223     fail[ 0 ] = fail[ 1 ] = 0;
2224     DEBUG_TRIE_COMPILE_r({
2225         PerlIO_printf(Perl_debug_log,
2226                       "%*sStclass Failtable (%"UVuf" states): 0", 
2227                       (int)(depth * 2), "", (UV)numstates
2228         );
2229         for( q_read=1; q_read<numstates; q_read++ ) {
2230             PerlIO_printf(Perl_debug_log, ", %"UVuf, (UV)fail[q_read]);
2231         }
2232         PerlIO_printf(Perl_debug_log, "\n");
2233     });
2234     Safefree(q);
2235     /*RExC_seen |= REG_SEEN_TRIEDFA;*/
2236 }
2237
2238
2239 /*
2240  * There are strange code-generation bugs caused on sparc64 by gcc-2.95.2.
2241  * These need to be revisited when a newer toolchain becomes available.
2242  */
2243 #if defined(__sparc64__) && defined(__GNUC__)
2244 #   if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
2245 #       undef  SPARC64_GCC_WORKAROUND
2246 #       define SPARC64_GCC_WORKAROUND 1
2247 #   endif
2248 #endif
2249
2250 #define DEBUG_PEEP(str,scan,depth) \
2251     DEBUG_OPTIMISE_r({if (scan){ \
2252        SV * const mysv=sv_newmortal(); \
2253        regnode *Next = regnext(scan); \
2254        regprop(RExC_rx, mysv, scan); \
2255        PerlIO_printf(Perl_debug_log, "%*s" str ">%3d: %s (%d)\n", \
2256        (int)depth*2, "", REG_NODE_NUM(scan), SvPV_nolen_const(mysv),\
2257        Next ? (REG_NODE_NUM(Next)) : 0 ); \
2258    }});
2259
2260
2261
2262
2263
2264 #define JOIN_EXACT(scan,min,flags) \
2265     if (PL_regkind[OP(scan)] == EXACT) \
2266         join_exact(pRExC_state,(scan),(min),(flags),NULL,depth+1)
2267
2268 STATIC U32
2269 S_join_exact(pTHX_ RExC_state_t *pRExC_state, regnode *scan, I32 *min, U32 flags,regnode *val, U32 depth) {
2270     /* Merge several consecutive EXACTish nodes into one. */
2271     regnode *n = regnext(scan);
2272     U32 stringok = 1;
2273     regnode *next = scan + NODE_SZ_STR(scan);
2274     U32 merged = 0;
2275     U32 stopnow = 0;
2276 #ifdef DEBUGGING
2277     regnode *stop = scan;
2278     GET_RE_DEBUG_FLAGS_DECL;
2279 #else
2280     PERL_UNUSED_ARG(depth);
2281 #endif
2282 #ifndef EXPERIMENTAL_INPLACESCAN
2283     PERL_UNUSED_ARG(flags);
2284     PERL_UNUSED_ARG(val);
2285 #endif
2286     DEBUG_PEEP("join",scan,depth);
2287     
2288     /* Skip NOTHING, merge EXACT*. */
2289     while (n &&
2290            ( PL_regkind[OP(n)] == NOTHING ||
2291              (stringok && (OP(n) == OP(scan))))
2292            && NEXT_OFF(n)
2293            && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
2294         
2295         if (OP(n) == TAIL || n > next)
2296             stringok = 0;
2297         if (PL_regkind[OP(n)] == NOTHING) {
2298             DEBUG_PEEP("skip:",n,depth);
2299             NEXT_OFF(scan) += NEXT_OFF(n);
2300             next = n + NODE_STEP_REGNODE;
2301 #ifdef DEBUGGING
2302             if (stringok)
2303                 stop = n;
2304 #endif
2305             n = regnext(n);
2306         }
2307         else if (stringok) {
2308             const unsigned int oldl = STR_LEN(scan);
2309             regnode * const nnext = regnext(n);
2310             
2311             DEBUG_PEEP("merg",n,depth);
2312             
2313             merged++;
2314             if (oldl + STR_LEN(n) > U8_MAX)
2315                 break;
2316             NEXT_OFF(scan) += NEXT_OFF(n);
2317             STR_LEN(scan) += STR_LEN(n);
2318             next = n + NODE_SZ_STR(n);
2319             /* Now we can overwrite *n : */
2320             Move(STRING(n), STRING(scan) + oldl, STR_LEN(n), char);
2321 #ifdef DEBUGGING
2322             stop = next - 1;
2323 #endif
2324             n = nnext;
2325             if (stopnow) break;
2326         }
2327
2328 #ifdef EXPERIMENTAL_INPLACESCAN
2329         if (flags && !NEXT_OFF(n)) {
2330             DEBUG_PEEP("atch", val, depth);
2331             if (reg_off_by_arg[OP(n)]) {
2332                 ARG_SET(n, val - n);
2333             }
2334             else {
2335                 NEXT_OFF(n) = val - n;
2336             }
2337             stopnow = 1;
2338         }
2339 #endif
2340     }
2341     
2342     if (UTF && ( OP(scan) == EXACTF ) && ( STR_LEN(scan) >= 6 ) ) {
2343     /*
2344     Two problematic code points in Unicode casefolding of EXACT nodes:
2345     
2346     U+0390 - GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS
2347     U+03B0 - GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS
2348     
2349     which casefold to
2350     
2351     Unicode                      UTF-8
2352     
2353     U+03B9 U+0308 U+0301         0xCE 0xB9 0xCC 0x88 0xCC 0x81
2354     U+03C5 U+0308 U+0301         0xCF 0x85 0xCC 0x88 0xCC 0x81
2355     
2356     This means that in case-insensitive matching (or "loose matching",
2357     as Unicode calls it), an EXACTF of length six (the UTF-8 encoded byte
2358     length of the above casefolded versions) can match a target string
2359     of length two (the byte length of UTF-8 encoded U+0390 or U+03B0).
2360     This would rather mess up the minimum length computation.
2361     
2362     What we'll do is to look for the tail four bytes, and then peek
2363     at the preceding two bytes to see whether we need to decrease
2364     the minimum length by four (six minus two).
2365     
2366     Thanks to the design of UTF-8, there cannot be false matches:
2367     A sequence of valid UTF-8 bytes cannot be a subsequence of
2368     another valid sequence of UTF-8 bytes.
2369     
2370     */
2371          char * const s0 = STRING(scan), *s, *t;
2372          char * const s1 = s0 + STR_LEN(scan) - 1;
2373          char * const s2 = s1 - 4;
2374 #ifdef EBCDIC /* RD tunifold greek 0390 and 03B0 */
2375          const char t0[] = "\xaf\x49\xaf\x42";
2376 #else
2377          const char t0[] = "\xcc\x88\xcc\x81";
2378 #endif
2379          const char * const t1 = t0 + 3;
2380     
2381          for (s = s0 + 2;
2382               s < s2 && (t = ninstr(s, s1, t0, t1));
2383               s = t + 4) {
2384 #ifdef EBCDIC
2385               if (((U8)t[-1] == 0x68 && (U8)t[-2] == 0xB4) ||
2386                   ((U8)t[-1] == 0x46 && (U8)t[-2] == 0xB5))
2387 #else
2388               if (((U8)t[-1] == 0xB9 && (U8)t[-2] == 0xCE) ||
2389                   ((U8)t[-1] == 0x85 && (U8)t[-2] == 0xCF))
2390 #endif
2391                    *min -= 4;
2392          }
2393     }
2394     
2395 #ifdef DEBUGGING
2396     /* Allow dumping */
2397     n = scan + NODE_SZ_STR(scan);
2398     while (n <= stop) {
2399         if (PL_regkind[OP(n)] != NOTHING || OP(n) == NOTHING) {
2400             OP(n) = OPTIMIZED;
2401             NEXT_OFF(n) = 0;
2402         }
2403         n++;
2404     }
2405 #endif
2406     DEBUG_OPTIMISE_r(if (merged){DEBUG_PEEP("finl",scan,depth)});
2407     return stopnow;
2408 }
2409
2410 /* REx optimizer.  Converts nodes into quickier variants "in place".
2411    Finds fixed substrings.  */
2412
2413 /* Stops at toplevel WHILEM as well as at "last". At end *scanp is set
2414    to the position after last scanned or to NULL. */
2415
2416 #define INIT_AND_WITHP \
2417     assert(!and_withp); \
2418     Newx(and_withp,1,struct regnode_charclass_class); \
2419     SAVEFREEPV(and_withp)
2420
2421 /* this is a chain of data about sub patterns we are processing that
2422    need to be handled seperately/specially in study_chunk. Its so
2423    we can simulate recursion without losing state.  */
2424 struct scan_frame;
2425 typedef struct scan_frame {
2426     regnode *last;  /* last node to process in this frame */
2427     regnode *next;  /* next node to process when last is reached */
2428     struct scan_frame *prev; /*previous frame*/
2429     I32 stop; /* what stopparen do we use */
2430 } scan_frame;
2431
2432
2433 #define SCAN_COMMIT(s, data, m) scan_commit(s, data, m, is_inf)
2434
2435 #define CASE_SYNST_FNC(nAmE)                                       \
2436 case nAmE:                                                         \
2437     if (flags & SCF_DO_STCLASS_AND) {                              \
2438             for (value = 0; value < 256; value++)                  \
2439                 if (!is_ ## nAmE ## _cp(value))                       \
2440                     ANYOF_BITMAP_CLEAR(data->start_class, value);  \
2441     }                                                              \
2442     else {                                                         \
2443             for (value = 0; value < 256; value++)                  \
2444                 if (is_ ## nAmE ## _cp(value))                        \
2445                     ANYOF_BITMAP_SET(data->start_class, value);    \
2446     }                                                              \
2447     break;                                                         \
2448 case N ## nAmE:                                                    \
2449     if (flags & SCF_DO_STCLASS_AND) {                              \
2450             for (value = 0; value < 256; value++)                   \
2451                 if (is_ ## nAmE ## _cp(value))                         \
2452                     ANYOF_BITMAP_CLEAR(data->start_class, value);   \
2453     }                                                               \
2454     else {                                                          \
2455             for (value = 0; value < 256; value++)                   \
2456                 if (!is_ ## nAmE ## _cp(value))                        \
2457                     ANYOF_BITMAP_SET(data->start_class, value);     \
2458     }                                                               \
2459     break
2460
2461
2462
2463 STATIC I32
2464 S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp,
2465                         I32 *minlenp, I32 *deltap,
2466                         regnode *last,
2467                         scan_data_t *data,
2468                         I32 stopparen,
2469                         U8* recursed,
2470                         struct regnode_charclass_class *and_withp,
2471                         U32 flags, U32 depth)
2472                         /* scanp: Start here (read-write). */
2473                         /* deltap: Write maxlen-minlen here. */
2474                         /* last: Stop before this one. */
2475                         /* data: string data about the pattern */
2476                         /* stopparen: treat close N as END */
2477                         /* recursed: which subroutines have we recursed into */
2478                         /* and_withp: Valid if flags & SCF_DO_STCLASS_OR */
2479 {
2480     dVAR;
2481     I32 min = 0, pars = 0, code;
2482     regnode *scan = *scanp, *next;
2483     I32 delta = 0;
2484     int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
2485     int is_inf_internal = 0;            /* The studied chunk is infinite */
2486     I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
2487     scan_data_t data_fake;
2488     SV *re_trie_maxbuff = NULL;
2489     regnode *first_non_open = scan;
2490     I32 stopmin = I32_MAX;
2491     scan_frame *frame = NULL;
2492
2493     GET_RE_DEBUG_FLAGS_DECL;
2494
2495 #ifdef DEBUGGING
2496     StructCopy(&zero_scan_data, &data_fake, scan_data_t);
2497 #endif
2498
2499     if ( depth == 0 ) {
2500         while (first_non_open && OP(first_non_open) == OPEN)
2501             first_non_open=regnext(first_non_open);
2502     }
2503
2504
2505   fake_study_recurse:
2506     while ( scan && OP(scan) != END && scan < last ){
2507         /* Peephole optimizer: */
2508         DEBUG_STUDYDATA("Peep:", data,depth);
2509         DEBUG_PEEP("Peep",scan,depth);
2510         JOIN_EXACT(scan,&min,0);
2511
2512         /* Follow the next-chain of the current node and optimize
2513            away all the NOTHINGs from it.  */
2514         if (OP(scan) != CURLYX) {
2515             const int max = (reg_off_by_arg[OP(scan)]
2516                        ? I32_MAX
2517                        /* I32 may be smaller than U16 on CRAYs! */
2518                        : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
2519             int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
2520             int noff;
2521             regnode *n = scan;
2522         
2523             /* Skip NOTHING and LONGJMP. */
2524             while ((n = regnext(n))
2525                    && ((PL_regkind[OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
2526                        || ((OP(n) == LONGJMP) && (noff = ARG(n))))
2527                    && off + noff < max)
2528                 off += noff;
2529             if (reg_off_by_arg[OP(scan)])
2530                 ARG(scan) = off;
2531             else
2532                 NEXT_OFF(scan) = off;
2533         }
2534
2535
2536
2537         /* The principal pseudo-switch.  Cannot be a switch, since we
2538            look into several different things.  */
2539         if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
2540                    || OP(scan) == IFTHEN) {
2541             next = regnext(scan);
2542             code = OP(scan);
2543             /* demq: the op(next)==code check is to see if we have "branch-branch" AFAICT */
2544         
2545             if (OP(next) == code || code == IFTHEN) {
2546                 /* NOTE - There is similar code to this block below for handling
2547                    TRIE nodes on a re-study.  If you change stuff here check there
2548                    too. */
2549                 I32 max1 = 0, min1 = I32_MAX, num = 0;
2550                 struct regnode_charclass_class accum;
2551                 regnode * const startbranch=scan;
2552                 
2553                 if (flags & SCF_DO_SUBSTR)
2554                     SCAN_COMMIT(pRExC_state, data, minlenp); /* Cannot merge strings after this. */
2555                 if (flags & SCF_DO_STCLASS)
2556                     cl_init_zero(pRExC_state, &accum);
2557
2558                 while (OP(scan) == code) {
2559                     I32 deltanext, minnext, f = 0, fake;
2560                     struct regnode_charclass_class this_class;
2561
2562                     num++;
2563                     data_fake.flags = 0;
2564                     if (data) {
2565                         data_fake.whilem_c = data->whilem_c;
2566                         data_fake.last_closep = data->last_closep;
2567                     }
2568                     else
2569                         data_fake.last_closep = &fake;
2570
2571                     data_fake.pos_delta = delta;
2572                     next = regnext(scan);
2573                     scan = NEXTOPER(scan);
2574                     if (code != BRANCH)
2575                         scan = NEXTOPER(scan);
2576                     if (flags & SCF_DO_STCLASS) {
2577                         cl_init(pRExC_state, &this_class);
2578                         data_fake.start_class = &this_class;
2579                         f = SCF_DO_STCLASS_AND;
2580                     }
2581                     if (flags & SCF_WHILEM_VISITED_POS)
2582                         f |= SCF_WHILEM_VISITED_POS;
2583
2584                     /* we suppose the run is continuous, last=next...*/
2585                     minnext = study_chunk(pRExC_state, &scan, minlenp, &deltanext,
2586                                           next, &data_fake,
2587                                           stopparen, recursed, NULL, f,depth+1);
2588                     if (min1 > minnext)
2589                         min1 = minnext;
2590                     if (max1 < minnext + deltanext)
2591                         max1 = minnext + deltanext;
2592                     if (deltanext == I32_MAX)
2593                         is_inf = is_inf_internal = 1;
2594                     scan = next;
2595                     if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
2596                         pars++;
2597                     if (data_fake.flags & SCF_SEEN_ACCEPT) {
2598                         if ( stopmin > minnext) 
2599                             stopmin = min + min1;
2600                         flags &= ~SCF_DO_SUBSTR;
2601                         if (data)
2602                             data->flags |= SCF_SEEN_ACCEPT;
2603                     }
2604                     if (data) {
2605                         if (data_fake.flags & SF_HAS_EVAL)
2606                             data->flags |= SF_HAS_EVAL;
2607                         data->whilem_c = data_fake.whilem_c;
2608                     }
2609                     if (flags & SCF_DO_STCLASS)
2610                         cl_or(pRExC_state, &accum, &this_class);
2611                 }
2612                 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
2613                     min1 = 0;
2614                 if (flags & SCF_DO_SUBSTR) {
2615                     data->pos_min += min1;
2616                     data->pos_delta += max1 - min1;
2617                     if (max1 != min1 || is_inf)
2618                         data->longest = &(data->longest_float);
2619                 }
2620                 min += min1;
2621                 delta += max1 - min1;
2622                 if (flags & SCF_DO_STCLASS_OR) {
2623                     cl_or(pRExC_state, data->start_class, &accum);
2624                     if (min1) {
2625                         cl_and(data->start_class, and_withp);
2626                         flags &= ~SCF_DO_STCLASS;
2627                     }
2628                 }
2629                 else if (flags & SCF_DO_STCLASS_AND) {
2630                     if (min1) {
2631                         cl_and(data->start_class, &accum);
2632                         flags &= ~SCF_DO_STCLASS;
2633                     }
2634                     else {
2635                         /* Switch to OR mode: cache the old value of
2636                          * data->start_class */
2637                         INIT_AND_WITHP;
2638                         StructCopy(data->start_class, and_withp,
2639                                    struct regnode_charclass_class);
2640                         flags &= ~SCF_DO_STCLASS_AND;
2641                         StructCopy(&accum, data->start_class,
2642                                    struct regnode_charclass_class);
2643                         flags |= SCF_DO_STCLASS_OR;
2644                         data->start_class->flags |= ANYOF_EOS;
2645                     }
2646                 }
2647
2648                 if (PERL_ENABLE_TRIE_OPTIMISATION && OP( startbranch ) == BRANCH ) {
2649                 /* demq.
2650
2651                    Assuming this was/is a branch we are dealing with: 'scan' now
2652                    points at the item that follows the branch sequence, whatever
2653                    it is. We now start at the beginning of the sequence and look
2654                    for subsequences of
2655
2656                    BRANCH->EXACT=>x1
2657                    BRANCH->EXACT=>x2
2658                    tail
2659
2660                    which would be constructed from a pattern like /A|LIST|OF|WORDS/
2661
2662                    If we can find such a subseqence we need to turn the first
2663                    element into a trie and then add the subsequent branch exact
2664                    strings to the trie.
2665
2666                    We have two cases
2667
2668                      1. patterns where the whole set of branch can be converted. 
2669
2670                      2. patterns where only a subset can be converted.
2671
2672                    In case 1 we can replace the whole set with a single regop
2673                    for the trie. In case 2 we need to keep the start and end
2674                    branchs so
2675
2676                      'BRANCH EXACT; BRANCH EXACT; BRANCH X'
2677                      becomes BRANCH TRIE; BRANCH X;
2678
2679                   There is an additional case, that being where there is a 
2680                   common prefix, which gets split out into an EXACT like node
2681                   preceding the TRIE node.
2682
2683                   If x(1..n)==tail then we can do a simple trie, if not we make
2684                   a "jump" trie, such that when we match the appropriate word
2685                   we "jump" to the appopriate tail node. Essentailly we turn
2686                   a nested if into a case structure of sorts.
2687
2688                 */
2689                 
2690                     int made=0;
2691                     if (!re_trie_maxbuff) {
2692                         re_trie_maxbuff = get_sv(RE_TRIE_MAXBUF_NAME, 1);
2693                         if (!SvIOK(re_trie_maxbuff))
2694                             sv_setiv(re_trie_maxbuff, RE_TRIE_MAXBUF_INIT);
2695                     }
2696                     if ( SvIV(re_trie_maxbuff)>=0  ) {
2697                         regnode *cur;
2698                         regnode *first = (regnode *)NULL;
2699                         regnode *last = (regnode *)NULL;
2700                         regnode *tail = scan;
2701                         U8 optype = 0;
2702                         U32 count=0;
2703
2704 #ifdef DEBUGGING
2705                         SV * const mysv = sv_newmortal();       /* for dumping */
2706 #endif
2707                         /* var tail is used because there may be a TAIL
2708                            regop in the way. Ie, the exacts will point to the
2709                            thing following the TAIL, but the last branch will
2710                            point at the TAIL. So we advance tail. If we
2711                            have nested (?:) we may have to move through several
2712                            tails.
2713                          */
2714
2715                         while ( OP( tail ) == TAIL ) {
2716                             /* this is the TAIL generated by (?:) */
2717                             tail = regnext( tail );
2718                         }
2719
2720                         
2721                         DEBUG_OPTIMISE_r({
2722                             regprop(RExC_rx, mysv, tail );
2723                             PerlIO_printf( Perl_debug_log, "%*s%s%s\n",
2724                                 (int)depth * 2 + 2, "", 
2725                                 "Looking for TRIE'able sequences. Tail node is: ", 
2726                                 SvPV_nolen_const( mysv )
2727                             );
2728                         });
2729                         
2730                         /*
2731
2732                            step through the branches, cur represents each
2733                            branch, noper is the first thing to be matched
2734                            as part of that branch and noper_next is the
2735                            regnext() of that node. if noper is an EXACT
2736                            and noper_next is the same as scan (our current
2737                            position in the regex) then the EXACT branch is
2738                            a possible optimization target. Once we have
2739                            two or more consequetive such branches we can
2740                            create a trie of the EXACT's contents and stich
2741                            it in place. If the sequence represents all of
2742                            the branches we eliminate the whole thing and
2743                            replace it with a single TRIE. If it is a
2744                            subsequence then we need to stitch it in. This
2745                            means the first branch has to remain, and needs
2746                            to be repointed at the item on the branch chain
2747                            following the last branch optimized. This could
2748                            be either a BRANCH, in which case the
2749                            subsequence is internal, or it could be the
2750                            item following the branch sequence in which
2751                            case the subsequence is at the end.
2752
2753                         */
2754
2755                         /* dont use tail as the end marker for this traverse */
2756                         for ( cur = startbranch ; cur != scan ; cur = regnext( cur ) ) {
2757                             regnode * const noper = NEXTOPER( cur );
2758 #if defined(DEBUGGING) || defined(NOJUMPTRIE)
2759                             regnode * const noper_next = regnext( noper );
2760 #endif
2761
2762                             DEBUG_OPTIMISE_r({
2763                                 regprop(RExC_rx, mysv, cur);
2764                                 PerlIO_printf( Perl_debug_log, "%*s- %s (%d)",
2765                                    (int)depth * 2 + 2,"", SvPV_nolen_const( mysv ), REG_NODE_NUM(cur) );
2766
2767                                 regprop(RExC_rx, mysv, noper);
2768                                 PerlIO_printf( Perl_debug_log, " -> %s",
2769                                     SvPV_nolen_const(mysv));
2770
2771                                 if ( noper_next ) {
2772                                   regprop(RExC_rx, mysv, noper_next );
2773                                   PerlIO_printf( Perl_debug_log,"\t=> %s\t",
2774                                     SvPV_nolen_const(mysv));
2775                                 }
2776                                 PerlIO_printf( Perl_debug_log, "(First==%d,Last==%d,Cur==%d)\n",
2777                                    REG_NODE_NUM(first), REG_NODE_NUM(last), REG_NODE_NUM(cur) );
2778                             });
2779                             if ( (((first && optype!=NOTHING) ? OP( noper ) == optype
2780                                          : PL_regkind[ OP( noper ) ] == EXACT )
2781                                   || OP(noper) == NOTHING )
2782 #ifdef NOJUMPTRIE
2783                                   && noper_next == tail
2784 #endif
2785                                   && count < U16_MAX)
2786                             {
2787                                 count++;
2788                                 if ( !first || optype == NOTHING ) {
2789                                     if (!first) first = cur;
2790                                     optype = OP( noper );
2791                                 } else {
2792                                     last = cur;
2793                                 }
2794                             } else {
2795 /* 
2796     Currently we assume that the trie can handle unicode and ascii
2797     matches fold cased matches. If this proves true then the following
2798     define will prevent tries in this situation. 
2799     
2800     #define TRIE_TYPE_IS_SAFE (UTF || optype==EXACT)
2801 */
2802 #define TRIE_TYPE_IS_SAFE 1
2803                                 if ( last && TRIE_TYPE_IS_SAFE ) {
2804                                     make_trie( pRExC_state, 
2805                                             startbranch, first, cur, tail, count, 
2806                                             optype, depth+1 );
2807                                 }
2808                                 if ( PL_regkind[ OP( noper ) ] == EXACT
2809 #ifdef NOJUMPTRIE
2810                                      && noper_next == tail
2811 #endif
2812                                 ){
2813                                     count = 1;
2814                                     first = cur;
2815                                     optype = OP( noper );
2816                                 } else {
2817                                     count = 0;
2818                                     first = NULL;
2819                                     optype = 0;
2820                                 }
2821                                 last = NULL;
2822                             }
2823                         }
2824                         DEBUG_OPTIMISE_r({
2825                             regprop(RExC_rx, mysv, cur);
2826                             PerlIO_printf( Perl_debug_log,
2827                               "%*s- %s (%d) <SCAN FINISHED>\n", (int)depth * 2 + 2,
2828                               "", SvPV_nolen_const( mysv ),REG_NODE_NUM(cur));
2829
2830                         });
2831                         
2832                         if ( last && TRIE_TYPE_IS_SAFE ) {
2833                             made= make_trie( pRExC_state, startbranch, first, scan, tail, count, optype, depth+1 );
2834 #ifdef TRIE_STUDY_OPT   
2835                             if ( ((made == MADE_EXACT_TRIE && 
2836                                  startbranch == first) 
2837                                  || ( first_non_open == first )) && 
2838                                  depth==0 ) {
2839                                 flags |= SCF_TRIE_RESTUDY;
2840                                 if ( startbranch == first 
2841                                      && scan == tail ) 
2842                                 {
2843                                     RExC_seen &=~REG_TOP_LEVEL_BRANCHES;
2844                                 }
2845                             }
2846 #endif
2847                         }
2848                     }
2849                     
2850                 } /* do trie */
2851                 
2852             }
2853             else if ( code == BRANCHJ ) {  /* single branch is optimized. */
2854                 scan = NEXTOPER(NEXTOPER(scan));
2855             } else                      /* single branch is optimized. */
2856                 scan = NEXTOPER(scan);
2857             continue;
2858         } else if (OP(scan) == SUSPEND || OP(scan) == GOSUB || OP(scan) == GOSTART) {
2859             scan_frame *newframe = NULL;
2860             I32 paren;
2861             regnode *start;
2862             regnode *end;
2863
2864             if (OP(scan) != SUSPEND) {
2865             /* set the pointer */
2866                 if (OP(scan) == GOSUB) {
2867                     paren = ARG(scan);
2868                     RExC_recurse[ARG2L(scan)] = scan;
2869                     start = RExC_open_parens[paren-1];
2870                     end   = RExC_close_parens[paren-1];
2871                 } else {
2872                     paren = 0;
2873                     start = RExC_rxi->program + 1;
2874                     end   = RExC_opend;
2875                 }
2876                 if (!recursed) {
2877                     Newxz(recursed, (((RExC_npar)>>3) +1), U8);
2878                     SAVEFREEPV(recursed);
2879                 }
2880                 if (!PAREN_TEST(recursed,paren+1)) {
2881                     PAREN_SET(recursed,paren+1);
2882                     Newx(newframe,1,scan_frame);
2883                 } else {
2884                     if (flags & SCF_DO_SUBSTR) {
2885                         SCAN_COMMIT(pRExC_state,data,minlenp);
2886                         data->longest = &(data->longest_float);
2887                     }
2888                     is_inf = is_inf_internal = 1;
2889                     if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
2890                         cl_anything(pRExC_state, data->start_class);
2891                     flags &= ~SCF_DO_STCLASS;
2892                 }
2893             } else {
2894                 Newx(newframe,1,scan_frame);
2895                 paren = stopparen;
2896                 start = scan+2;
2897                 end = regnext(scan);
2898             }
2899             if (newframe) {
2900                 assert(start);
2901                 assert(end);
2902                 SAVEFREEPV(newframe);
2903                 newframe->next = regnext(scan);
2904                 newframe->last = last;
2905                 newframe->stop = stopparen;
2906                 newframe->prev = frame;
2907
2908                 frame = newframe;
2909                 scan =  start;
2910                 stopparen = paren;
2911                 last = end;
2912
2913                 continue;
2914             }
2915         }
2916         else if (OP(scan) == EXACT) {
2917             I32 l = STR_LEN(scan);
2918             UV uc;
2919             if (UTF) {
2920                 const U8 * const s = (U8*)STRING(scan);
2921                 l = utf8_length(s, s + l);
2922                 uc = utf8_to_uvchr(s, NULL);
2923             } else {
2924                 uc = *((U8*)STRING(scan));
2925             }
2926             min += l;
2927             if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
2928                 /* The code below prefers earlier match for fixed
2929                    offset, later match for variable offset.  */
2930                 if (data->last_end == -1) { /* Update the start info. */
2931                     data->last_start_min = data->pos_min;
2932                     data->last_start_max = is_inf
2933                         ? I32_MAX : data->pos_min + data->pos_delta;
2934                 }
2935                 sv_catpvn(data->last_found, STRING(scan), STR_LEN(scan));
2936                 if (UTF)
2937                     SvUTF8_on(data->last_found);
2938                 {
2939                     SV * const sv = data->last_found;
2940                     MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ?
2941                         mg_find(sv, PERL_MAGIC_utf8) : NULL;
2942                     if (mg && mg->mg_len >= 0)
2943                         mg->mg_len += utf8_length((U8*)STRING(scan),
2944                                                   (U8*)STRING(scan)+STR_LEN(scan));
2945                 }
2946                 data->last_end = data->pos_min + l;
2947                 data->pos_min += l; /* As in the first entry. */
2948                 data->flags &= ~SF_BEFORE_EOL;
2949             }
2950             if (flags & SCF_DO_STCLASS_AND) {
2951                 /* Check whether it is compatible with what we know already! */
2952                 int compat = 1;
2953
2954                 if (uc >= 0x100 ||
2955                     (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
2956                     && !ANYOF_BITMAP_TEST(data->start_class, uc)
2957                     && (!(data->start_class->flags & ANYOF_FOLD)
2958                         || !ANYOF_BITMAP_TEST(data->start_class, PL_fold[uc])))
2959                     )
2960                     compat = 0;
2961                 ANYOF_CLASS_ZERO(data->start_class);
2962                 ANYOF_BITMAP_ZERO(data->start_class);
2963                 if (compat)
2964                     ANYOF_BITMAP_SET(data->start_class, uc);
2965                 data->start_class->flags &= ~ANYOF_EOS;
2966                 if (uc < 0x100)
2967                   data->start_class->flags &= ~ANYOF_UNICODE_ALL;
2968             }
2969             else if (flags & SCF_DO_STCLASS_OR) {
2970                 /* false positive possible if the class is case-folded */
2971                 if (uc < 0x100)
2972                     ANYOF_BITMAP_SET(data->start_class, uc);
2973                 else
2974                     data->start_class->flags |= ANYOF_UNICODE_ALL;
2975                 data->start_class->flags &= ~ANYOF_EOS;
2976                 cl_and(data->start_class, and_withp);
2977             }
2978             flags &= ~SCF_DO_STCLASS;
2979         }
2980         else if (PL_regkind[OP(scan)] == EXACT) { /* But OP != EXACT! */
2981             I32 l = STR_LEN(scan);
2982             UV uc = *((U8*)STRING(scan));
2983
2984             /* Search for fixed substrings supports EXACT only. */
2985             if (flags & SCF_DO_SUBSTR) {
2986                 assert(data);
2987                 SCAN_COMMIT(pRExC_state, data, minlenp);
2988             }
2989             if (UTF) {
2990                 const U8 * const s = (U8 *)STRING(scan);
2991                 l = utf8_length(s, s + l);
2992                 uc = utf8_to_uvchr(s, NULL);
2993             }
2994             min += l;
2995             if (flags & SCF_DO_SUBSTR)
2996                 data->pos_min += l;
2997             if (flags & SCF_DO_STCLASS_AND) {
2998                 /* Check whether it is compatible with what we know already! */
2999                 int compat = 1;
3000
3001                 if (uc >= 0x100 ||
3002                     (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
3003                     && !ANYOF_BITMAP_TEST(data->start_class, uc)
3004                      && !ANYOF_BITMAP_TEST(data->start_class, PL_fold[uc])))
3005                     compat = 0;
3006                 ANYOF_CLASS_ZERO(data->start_class);
3007                 ANYOF_BITMAP_ZERO(data->start_class);
3008                 if (compat) {
3009                     ANYOF_BITMAP_SET(data->start_class, uc);
3010                     data->start_class->flags &= ~ANYOF_EOS;
3011                     data->start_class->flags |= ANYOF_FOLD;
3012                     if (OP(scan) == EXACTFL)
3013                         data->start_class->flags |= ANYOF_LOCALE;
3014                 }
3015             }
3016             else if (flags & SCF_DO_STCLASS_OR) {
3017                 if (data->start_class->flags & ANYOF_FOLD) {
3018                     /* false positive possible if the class is case-folded.
3019                        Assume that the locale settings are the same... */
3020                     if (uc < 0x100)
3021                         ANYOF_BITMAP_SET(data->start_class, uc);
3022                     data->start_class->flags &= ~ANYOF_EOS;
3023                 }
3024                 cl_and(data->start_class, and_withp);
3025             }
3026             flags &= ~SCF_DO_STCLASS;
3027         }
3028         else if (strchr((const char*)PL_varies,OP(scan))) {
3029             I32 mincount, maxcount, minnext, deltanext, fl = 0;
3030             I32 f = flags, pos_before = 0;
3031             regnode * const oscan = scan;
3032             struct regnode_charclass_class this_class;
3033             struct regnode_charclass_class *oclass = NULL;
3034             I32 next_is_eval = 0;
3035
3036             switch (PL_regkind[OP(scan)]) {
3037             case WHILEM:                /* End of (?:...)* . */
3038                 scan = NEXTOPER(scan);
3039                 goto finish;
3040             case PLUS:
3041                 if (flags & (SCF_DO_SUBSTR | SCF_DO_STCLASS)) {
3042                     next = NEXTOPER(scan);
3043                     if (OP(next) == EXACT || (flags & SCF_DO_STCLASS)) {
3044                         mincount = 1;
3045                         maxcount = REG_INFTY;
3046                         next = regnext(scan);
3047                         scan = NEXTOPER(scan);
3048                         goto do_curly;
3049                     }
3050                 }
3051                 if (flags & SCF_DO_SUBSTR)
3052                     data->pos_min++;
3053                 min++;
3054                 /* Fall through. */
3055             case STAR:
3056                 if (flags & SCF_DO_STCLASS) {
3057                     mincount = 0;
3058                     maxcount = REG_INFTY;
3059                     next = regnext(scan);
3060                     scan = NEXTOPER(scan);
3061                     goto do_curly;
3062                 }
3063                 is_inf = is_inf_internal = 1;
3064                 scan = regnext(scan);
3065                 if (flags & SCF_DO_SUBSTR) {
3066                     SCAN_COMMIT(pRExC_state, data, minlenp); /* Cannot extend fixed substrings */
3067                     data->longest = &(data->longest_float);
3068                 }
3069                 goto optimize_curly_tail;
3070             case CURLY:
3071                 if (stopparen>0 && (OP(scan)==CURLYN || OP(scan)==CURLYM)
3072                     && (scan->flags == stopparen))
3073                 {
3074                     mincount = 1;
3075                     maxcount = 1;
3076                 } else {
3077                     mincount = ARG1(scan);
3078                     maxcount = ARG2(scan);
3079                 }
3080                 next = regnext(scan);
3081                 if (OP(scan) == CURLYX) {
3082                     I32 lp = (data ? *(data->last_closep) : 0);
3083                     scan->flags = ((lp <= (I32)U8_MAX) ? (U8)lp : U8_MAX);
3084                 }
3085                 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3086                 next_is_eval = (OP(scan) == EVAL);
3087               do_curly:
3088                 if (flags & SCF_DO_SUBSTR) {
3089                     if (mincount == 0) SCAN_COMMIT(pRExC_state,data,minlenp); /* Cannot extend fixed substrings */
3090                     pos_before = data->pos_min;
3091                 }
3092                 if (data) {
3093                     fl = data->flags;
3094                     data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
3095                     if (is_inf)
3096                         data->flags |= SF_IS_INF;
3097                 }
3098                 if (flags & SCF_DO_STCLASS) {
3099                     cl_init(pRExC_state, &this_class);
3100                     oclass = data->start_class;
3101                     data->start_class = &this_class;
3102                     f |= SCF_DO_STCLASS_AND;
3103                     f &= ~SCF_DO_STCLASS_OR;
3104                 }
3105                 /* These are the cases when once a subexpression
3106                    fails at a particular position, it cannot succeed
3107                    even after backtracking at the enclosing scope.
3108                 
3109                    XXXX what if minimal match and we are at the
3110                         initial run of {n,m}? */
3111                 if ((mincount != maxcount - 1) && (maxcount != REG_INFTY))
3112                     f &= ~SCF_WHILEM_VISITED_POS;
3113
3114                 /* This will finish on WHILEM, setting scan, or on NULL: */
3115                 minnext = study_chunk(pRExC_state, &scan, minlenp, &deltanext, 
3116                                       last, data, stopparen, recursed, NULL,
3117                                       (mincount == 0
3118                                         ? (f & ~SCF_DO_SUBSTR) : f),depth+1);
3119
3120                 if (flags & SCF_DO_STCLASS)
3121                     data->start_class = oclass;
3122                 if (mincount == 0 || minnext == 0) {
3123                     if (flags & SCF_DO_STCLASS_OR) {
3124                         cl_or(pRExC_state, data->start_class, &this_class);
3125                     }
3126                     else if (flags & SCF_DO_STCLASS_AND) {
3127                         /* Switch to OR mode: cache the old value of
3128                          * data->start_class */
3129                         INIT_AND_WITHP;
3130                         StructCopy(data->start_class, and_withp,
3131                                    struct regnode_charclass_class);
3132                         flags &= ~SCF_DO_STCLASS_AND;
3133                         StructCopy(&this_class, data->start_class,
3134                                    struct regnode_charclass_class);
3135                         flags |= SCF_DO_STCLASS_OR;
3136                         data->start_class->flags |= ANYOF_EOS;
3137                     }
3138                 } else {                /* Non-zero len */
3139                     if (flags & SCF_DO_STCLASS_OR) {
3140                         cl_or(pRExC_state, data->start_class, &this_class);
3141                         cl_and(data->start_class, and_withp);
3142                     }
3143                     else if (flags & SCF_DO_STCLASS_AND)
3144                         cl_and(data->start_class, &this_class);
3145                     flags &= ~SCF_DO_STCLASS;
3146                 }
3147                 if (!scan)              /* It was not CURLYX, but CURLY. */
3148                     scan = next;
3149                 if ( /* ? quantifier ok, except for (?{ ... }) */
3150                     (next_is_eval || !(mincount == 0 && maxcount == 1))
3151                     && (minnext == 0) && (deltanext == 0)
3152                     && data && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
3153                     && maxcount <= REG_INFTY/3 /* Complement check for big count */
3154                     && ckWARN(WARN_REGEXP))
3155                 {
3156                     vWARN(RExC_parse,
3157                           "Quantifier unexpected on zero-length expression");
3158                 }
3159
3160                 min += minnext * mincount;
3161                 is_inf_internal |= ((maxcount == REG_INFTY
3162                                      && (minnext + deltanext) > 0)
3163                                     || deltanext == I32_MAX);
3164                 is_inf |= is_inf_internal;
3165                 delta += (minnext + deltanext) * maxcount - minnext * mincount;
3166
3167                 /* Try powerful optimization CURLYX => CURLYN. */
3168                 if (  OP(oscan) == CURLYX && data
3169                       && data->flags & SF_IN_PAR
3170                       && !(data->flags & SF_HAS_EVAL)
3171                       && !deltanext && minnext == 1 ) {
3172                     /* Try to optimize to CURLYN.  */
3173                     regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
3174                     regnode * const nxt1 = nxt;
3175 #ifdef DEBUGGING
3176                     regnode *nxt2;
3177 #endif
3178
3179                     /* Skip open. */
3180                     nxt = regnext(nxt);
3181                     if (!strchr((const char*)PL_simple,OP(nxt))
3182                         && !(PL_regkind[OP(nxt)] == EXACT
3183                              && STR_LEN(nxt) == 1))
3184                         goto nogo;
3185 #ifdef DEBUGGING
3186                     nxt2 = nxt;
3187 #endif
3188                     nxt = regnext(nxt);
3189                     if (OP(nxt) != CLOSE)
3190                         goto nogo;
3191                     if (RExC_open_parens) {
3192                         RExC_open_parens[ARG(nxt1)-1]=oscan; /*open->CURLYM*/
3193                         RExC_close_parens[ARG(nxt1)-1]=nxt+2; /*close->while*/
3194                     }
3195                     /* Now we know that nxt2 is the only contents: */
3196                     oscan->flags = (U8)ARG(nxt);
3197                     OP(oscan) = CURLYN;
3198                     OP(nxt1) = NOTHING; /* was OPEN. */
3199
3200 #ifdef DEBUGGING
3201                     OP(nxt1 + 1) = OPTIMIZED; /* was count. */
3202                     NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
3203                     NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
3204                     OP(nxt) = OPTIMIZED;        /* was CLOSE. */
3205                     OP(nxt + 1) = OPTIMIZED; /* was count. */
3206                     NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
3207 #endif
3208                 }
3209               nogo:
3210
3211                 /* Try optimization CURLYX => CURLYM. */
3212                 if (  OP(oscan) == CURLYX && data
3213                       && !(data->flags & SF_HAS_PAR)
3214                       && !(data->flags & SF_HAS_EVAL)
3215                       && !deltanext     /* atom is fixed width */
3216                       && minnext != 0   /* CURLYM can't handle zero width */
3217                 ) {
3218                     /* XXXX How to optimize if data == 0? */
3219                     /* Optimize to a simpler form.  */
3220                     regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
3221                     regnode *nxt2;
3222
3223                     OP(oscan) = CURLYM;
3224                     while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
3225                             && (OP(nxt2) != WHILEM))
3226                         nxt = nxt2;
3227                     OP(nxt2)  = SUCCEED; /* Whas WHILEM */
3228                     /* Need to optimize away parenths. */
3229                     if (data->flags & SF_IN_PAR) {
3230                         /* Set the parenth number.  */
3231                         regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
3232
3233                         if (OP(nxt) != CLOSE)
3234                             FAIL("Panic opt close");
3235                         oscan->flags = (U8)ARG(nxt);
3236                         if (RExC_open_parens) {
3237                             RExC_open_parens[ARG(nxt1)-1]=oscan; /*open->CURLYM*/
3238                             RExC_close_parens[ARG(nxt1)-1]=nxt2+1; /*close->NOTHING*/
3239                         }
3240                         OP(nxt1) = OPTIMIZED;   /* was OPEN. */
3241                         OP(nxt) = OPTIMIZED;    /* was CLOSE. */
3242
3243 #ifdef DEBUGGING
3244                         OP(nxt1 + 1) = OPTIMIZED; /* was count. */
3245                         OP(nxt + 1) = OPTIMIZED; /* was count. */
3246                         NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
3247                         NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
3248 #endif
3249 #if 0
3250                         while ( nxt1 && (OP(nxt1) != WHILEM)) {
3251                             regnode *nnxt = regnext(nxt1);
3252                         
3253                             if (nnxt == nxt) {
3254                                 if (reg_off_by_arg[OP(nxt1)])
3255                                     ARG_SET(nxt1, nxt2 - nxt1);
3256                                 else if (nxt2 - nxt1 < U16_MAX)
3257                                     NEXT_OFF(nxt1) = nxt2 - nxt1;
3258                                 else
3259                                     OP(nxt) = NOTHING;  /* Cannot beautify */
3260                             }
3261                             nxt1 = nnxt;
3262                         }
3263 #endif
3264                         /* Optimize again: */
3265                         study_chunk(pRExC_state, &nxt1, minlenp, &deltanext, nxt,
3266                                     NULL, stopparen, recursed, NULL, 0,depth+1);
3267                     }
3268                     else
3269                         oscan->flags = 0;
3270                 }
3271                 else if ((OP(oscan) == CURLYX)
3272                          && (flags & SCF_WHILEM_VISITED_POS)
3273                          /* See the comment on a similar expression above.
3274                             However, this time it not a subexpression
3275                             we care about, but the expression itself. */
3276                          && (maxcount == REG_INFTY)
3277                          && data && ++data->whilem_c < 16) {
3278                     /* This stays as CURLYX, we can put the count/of pair. */
3279                     /* Find WHILEM (as in regexec.c) */
3280                     regnode *nxt = oscan + NEXT_OFF(oscan);
3281
3282                     if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
3283                         nxt += ARG(nxt);
3284                     PREVOPER(nxt)->flags = (U8)(data->whilem_c
3285                         | (RExC_whilem_seen << 4)); /* On WHILEM */
3286                 }
3287                 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
3288                     pars++;
3289                 if (flags & SCF_DO_SUBSTR) {
3290                     SV *last_str = NULL;
3291                     int counted = mincount != 0;
3292
3293                     if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
3294 #if defined(SPARC64_GCC_WORKAROUND)
3295                         I32 b = 0;
3296                         STRLEN l = 0;
3297                         const char *s = NULL;
3298                         I32 old = 0;
3299
3300                         if (pos_before >= data->last_start_min)
3301                             b = pos_before;
3302                         else
3303                             b = data->last_start_min;
3304
3305                         l = 0;
3306                         s = SvPV_const(data->last_found, l);
3307                         old = b - data->last_start_min;
3308
3309 #else
3310                         I32 b = pos_before >= data->last_start_min
3311                             ? pos_before : data->last_start_min;
3312                         STRLEN l;
3313                         const char * const s = SvPV_const(data->last_found, l);
3314                         I32 old = b - data->last_start_min;
3315 #endif
3316
3317                         if (UTF)
3318                             old = utf8_hop((U8*)s, old) - (U8*)s;
3319                         
3320                         l -= old;
3321                         /* Get the added string: */
3322                         last_str = newSVpvn_utf8(s  + old, l, UTF);
3323                         if (deltanext == 0 && pos_before == b) {
3324                             /* What was added is a constant string */
3325                             if (mincount > 1) {
3326                                 SvGROW(last_str, (mincount * l) + 1);
3327                                 repeatcpy(SvPVX(last_str) + l,
3328                                           SvPVX_const(last_str), l, mincount - 1);
3329                                 SvCUR_set(last_str, SvCUR(last_str) * mincount);
3330                                 /* Add additional parts. */
3331                                 SvCUR_set(data->last_found,
3332                                           SvCUR(data->last_found) - l);
3333                                 sv_catsv(data->last_found, last_str);
3334                                 {
3335                                     SV * sv = data->last_found;
3336                                     MAGIC *mg =
3337                                         SvUTF8(sv) && SvMAGICAL(sv) ?
3338                                         mg_find(sv, PERL_MAGIC_utf8) : NULL;
3339                                     if (mg && mg->mg_len >= 0)
3340                                         mg->mg_len += CHR_SVLEN(last_str) - l;
3341                                 }
3342                                 data->last_end += l * (mincount - 1);
3343                             }
3344                         } else {
3345                             /* start offset must point into the last copy */
3346                             data->last_start_min += minnext * (mincount - 1);
3347                             data->last_start_max += is_inf ? I32_MAX
3348                                 : (maxcount - 1) * (minnext + data->pos_delta);
3349                         }
3350                     }
3351                     /* It is counted once already... */
3352                     data->pos_min += minnext * (mincount - counted);
3353                     data->pos_delta += - counted * deltanext +
3354                         (minnext + deltanext) * maxcount - minnext * mincount;
3355                     if (mincount != maxcount) {
3356                          /* Cannot extend fixed substrings found inside
3357                             the group.  */
3358                         SCAN_COMMIT(pRExC_state,data,minlenp);
3359                         if (mincount && last_str) {
3360                             SV * const sv = data->last_found;
3361                             MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ?
3362                                 mg_find(sv, PERL_MAGIC_utf8) : NULL;
3363
3364                             if (mg)
3365                                 mg->mg_len = -1;
3366                             sv_setsv(sv, last_str);
3367                             data->last_end = data->pos_min;
3368                             data->last_start_min =
3369                                 data->pos_min - CHR_SVLEN(last_str);
3370                             data->last_start_max = is_inf
3371                                 ? I32_MAX
3372                                 : data->pos_min + data->pos_delta
3373                                 - CHR_SVLEN(last_str);
3374                         }
3375                         data->longest = &(data->longest_float);
3376                     }
3377                     SvREFCNT_dec(last_str);
3378                 }
3379                 if (data && (fl & SF_HAS_EVAL))
3380                     data->flags |= SF_HAS_EVAL;
3381               optimize_curly_tail:
3382                 if (OP(oscan) != CURLYX) {
3383                     while (PL_regkind[OP(next = regnext(oscan))] == NOTHING
3384                            && NEXT_OFF(next))
3385                         NEXT_OFF(oscan) += NEXT_OFF(next);
3386                 }
3387                 continue;
3388             default:                    /* REF and CLUMP only? */
3389                 if (flags & SCF_DO_SUBSTR) {
3390                     SCAN_COMMIT(pRExC_state,data,minlenp);      /* Cannot expect anything... */
3391                     data->longest = &(data->longest_float);
3392                 }
3393                 is_inf = is_inf_internal = 1;
3394                 if (flags & SCF_DO_STCLASS_OR)
3395                     cl_anything(pRExC_state, data->start_class);
3396                 flags &= ~SCF_DO_STCLASS;
3397                 break;
3398             }
3399         }
3400         else if (OP(scan) == LNBREAK) {
3401             if (flags & SCF_DO_STCLASS) {
3402                 int value = 0;
3403                 data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */
3404                 if (flags & SCF_DO_STCLASS_AND) {
3405                     for (value = 0; value < 256; value++)
3406                         if (!is_VERTWS_cp(value))
3407                             ANYOF_BITMAP_CLEAR(data->start_class, value);  
3408                 }                                                              
3409                 else {                                                         
3410                     for (value = 0; value < 256; value++)
3411                         if (is_VERTWS_cp(value))
3412                             ANYOF_BITMAP_SET(data->start_class, value);    
3413                 }                                                              
3414                 if (flags & SCF_DO_STCLASS_OR)
3415                     cl_and(data->start_class, and_withp);
3416                 flags &= ~SCF_DO_STCLASS;
3417             }
3418             min += 1;
3419             delta += 1;
3420             if (flags & SCF_DO_SUBSTR) {
3421                 SCAN_COMMIT(pRExC_state,data,minlenp);  /* Cannot expect anything... */
3422                 data->pos_min += 1;
3423                 data->pos_delta += 1;
3424                 data->longest = &(data->longest_float);
3425             }
3426             
3427         }
3428         else if (OP(scan) == FOLDCHAR) {
3429             int d = ARG(scan)==0xDF ? 1 : 2;
3430             flags &= ~SCF_DO_STCLASS;
3431             min += 1;
3432             delta += d;
3433             if (flags & SCF_DO_SUBSTR) {
3434                 SCAN_COMMIT(pRExC_state,data,minlenp);  /* Cannot expect anything... */
3435                 data->pos_min += 1;
3436                 data->pos_delta += d;
3437                 data->longest = &(data->longest_float);
3438             }
3439         }
3440         else if (strchr((const char*)PL_simple,OP(scan))) {
3441             int value = 0;
3442
3443             if (flags & SCF_DO_SUBSTR) {
3444                 SCAN_COMMIT(pRExC_state,data,minlenp);
3445                 data->pos_min++;
3446             }
3447             min++;
3448             if (flags & SCF_DO_STCLASS) {
3449                 data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */
3450
3451                 /* Some of the logic below assumes that switching
3452                    locale on will only add false positives. */
3453                 switch (PL_regkind[OP(scan)]) {
3454                 case SANY:
3455                 default:
3456                   do_default:
3457                     /* Perl_croak(aTHX_ "panic: unexpected simple REx opcode %d", OP(scan)); */
3458                     if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
3459                         cl_anything(pRExC_state, data->start_class);
3460                     break;
3461                 case REG_ANY:
3462                     if (OP(scan) == SANY)
3463                         goto do_default;
3464                     if (flags & SCF_DO_STCLASS_OR) { /* Everything but \n */
3465                         value = (ANYOF_BITMAP_TEST(data->start_class,'\n')
3466                                  || (data->start_class->flags & ANYOF_CLASS));
3467                         cl_anything(pRExC_state, data->start_class);
3468                     }
3469                     if (flags & SCF_DO_STCLASS_AND || !value)
3470                         ANYOF_BITMAP_CLEAR(data->start_class,'\n');
3471                     break;
3472                 case ANYOF:
3473                     if (flags & SCF_DO_STCLASS_AND)
3474                         cl_and(data->start_class,
3475                                (struct regnode_charclass_class*)scan);
3476                     else
3477                         cl_or(pRExC_state, data->start_class,
3478                               (struct regnode_charclass_class*)scan);
3479                     break;
3480                 case ALNUM:
3481                     if (flags & SCF_DO_STCLASS_AND) {
3482                         if (!(data->start_class->flags & ANYOF_LOCALE)) {
3483                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
3484                             for (value = 0; value < 256; value++)
3485                                 if (!isALNUM(value))
3486                                     ANYOF_BITMAP_CLEAR(data->start_class, value);
3487                         }
3488                     }
3489                     else {
3490                         if (data->start_class->flags & ANYOF_LOCALE)
3491                             ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
3492                         else {
3493                             for (value = 0; value < 256; value++)
3494                                 if (isALNUM(value))
3495                                     ANYOF_BITMAP_SET(data->start_class, value);                 
3496                         }
3497                     }
3498                     break;
3499                 case ALNUML:
3500                     if (flags & SCF_DO_STCLASS_AND) {
3501                         if (data->start_class->flags & ANYOF_LOCALE)
3502                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
3503                     }
3504                     else {
3505                         ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
3506                         data->start_class->flags |= ANYOF_LOCALE;
3507                     }
3508                     break;
3509                 case NALNUM:
3510                     if (flags & SCF_DO_STCLASS_AND) {
3511                         if (!(data->start_class->flags & ANYOF_LOCALE)) {
3512                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
3513                             for (value = 0; value < 256; value++)
3514                                 if (isALNUM(value))
3515                                     ANYOF_BITMAP_CLEAR(data->start_class, value);
3516                         }
3517                     }
3518                     else {
3519                         if (data->start_class->flags & ANYOF_LOCALE)
3520                             ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
3521                         else {
3522                             for (value = 0; value < 256; value++)
3523                                 if (!isALNUM(value))
3524                                     ANYOF_BITMAP_SET(data->start_class, value);                 
3525                         }
3526                     }
3527                     break;
3528                 case NALNUML:
3529                     if (flags & SCF_DO_STCLASS_AND) {
3530                         if (data->start_class->flags & ANYOF_LOCALE)
3531                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
3532                     }
3533                     else {
3534                         data->start_class->flags |= ANYOF_LOCALE;
3535                         ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
3536                     }
3537                     break;
3538                 case SPACE:
3539                     if (flags & SCF_DO_STCLASS_AND) {
3540                         if (!(data->start_class->flags & ANYOF_LOCALE)) {
3541                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
3542                             for (value = 0; value < 256; value++)
3543                                 if (!isSPACE(value))
3544                                     ANYOF_BITMAP_CLEAR(data->start_class, value);
3545                         }
3546                     }
3547                     else {
3548                         if (data->start_class->flags & ANYOF_LOCALE)
3549                             ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
3550                         else {
3551                             for (value = 0; value < 256; value++)
3552                                 if (isSPACE(value))
3553                                     ANYOF_BITMAP_SET(data->start_class, value);                 
3554                         }
3555                     }
3556                     break;
3557                 case SPACEL:
3558                     if (flags & SCF_DO_STCLASS_AND) {
3559                         if (data->start_class->flags & ANYOF_LOCALE)
3560                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
3561                     }
3562                     else {
3563                         data->start_class->flags |= ANYOF_LOCALE;
3564                         ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
3565                     }
3566                     break;
3567                 case NSPACE:
3568                     if (flags & SCF_DO_STCLASS_AND) {
3569                         if (!(data->start_class->flags & ANYOF_LOCALE)) {
3570                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
3571                             for (value = 0; value < 256; value++)
3572                                 if (isSPACE(value))
3573                                     ANYOF_BITMAP_CLEAR(data->start_class, value);
3574                         }
3575                     }
3576                     else {
3577                         if (data->start_class->flags & ANYOF_LOCALE)
3578                             ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
3579                         else {
3580                             for (value = 0; value < 256; value++)
3581                                 if (!isSPACE(value))
3582                                     ANYOF_BITMAP_SET(data->start_class, value);                 
3583                         }
3584                     }
3585                     break;
3586                 case NSPACEL:
3587                     if (flags & SCF_DO_STCLASS_AND) {
3588                         if (data->start_class->flags & ANYOF_LOCALE) {
3589                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
3590                             for (value = 0; value < 256; value++)
3591                                 if (!isSPACE(value))
3592                                     ANYOF_BITMAP_CLEAR(data->start_class, value);
3593                         }
3594                     }
3595                     else {
3596                         data->start_class->flags |= ANYOF_LOCALE;
3597                         ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
3598                     }
3599                     break;
3600                 case DIGIT:
3601                     if (flags & SCF_DO_STCLASS_AND) {
3602                         ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NDIGIT);
3603                         for (value = 0; value < 256; value++)
3604                             if (!isDIGIT(value))
3605                                 ANYOF_BITMAP_CLEAR(data->start_class, value);
3606                     }
3607                     else {
3608                         if (data->start_class->flags & ANYOF_LOCALE)
3609                             ANYOF_CLASS_SET(data->start_class,ANYOF_DIGIT);
3610                         else {
3611                             for (value = 0; value < 256; value++)
3612                                 if (isDIGIT(value))
3613                                     ANYOF_BITMAP_SET(data->start_class, value);                 
3614                         }
3615                     }
3616                     break;
3617                 case NDIGIT:
3618                     if (flags & SCF_DO_STCLASS_AND) {
3619                         ANYOF_CLASS_CLEAR(data->start_class,ANYOF_DIGIT);
3620                         for (value = 0; value < 256; value++)
3621                             if (isDIGIT(value))
3622                                 ANYOF_BITMAP_CLEAR(data->start_class, value);
3623                     }
3624                     else {
3625                         if (data->start_class->flags & ANYOF_LOCALE)
3626                             ANYOF_CLASS_SET(data->start_class,ANYOF_NDIGIT);
3627                         else {
3628                             for (value = 0; value < 256; value++)
3629                                 if (!isDIGIT(value))
3630                                     ANYOF_BITMAP_SET(data->start_class, value);                 
3631                         }
3632                     }
3633                     break;
3634                 CASE_SYNST_FNC(VERTWS);
3635                 CASE_SYNST_FNC(HORIZWS);
3636                 
3637                 }
3638                 if (flags & SCF_DO_STCLASS_OR)
3639                     cl_and(data->start_class, and_withp);
3640                 flags &= ~SCF_DO_STCLASS;
3641             }
3642         }
3643         else if (PL_regkind[OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
3644             data->flags |= (OP(scan) == MEOL
3645                             ? SF_BEFORE_MEOL
3646                             : SF_BEFORE_SEOL);
3647         }
3648         else if (  PL_regkind[OP(scan)] == BRANCHJ
3649                  /* Lookbehind, or need to calculate parens/evals/stclass: */
3650                    && (scan->flags || data || (flags & SCF_DO_STCLASS))
3651                    && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
3652             if ( !PERL_ENABLE_POSITIVE_ASSERTION_STUDY 
3653                 || OP(scan) == UNLESSM )
3654             {
3655                 /* Negative Lookahead/lookbehind
3656                    In this case we can't do fixed string optimisation.
3657                 */
3658
3659                 I32 deltanext, minnext, fake = 0;
3660                 regnode *nscan;
3661                 struct regnode_charclass_class intrnl;
3662                 int f = 0;
3663
3664                 data_fake.flags = 0;
3665                 if (data) {
3666                     data_fake.whilem_c = data->whilem_c;
3667                     data_fake.last_closep = data->last_closep;
3668                 }
3669                 else
3670                     data_fake.last_closep = &fake;
3671                 data_fake.pos_delta = delta;
3672                 if ( flags & SCF_DO_STCLASS && !scan->flags
3673                      && OP(scan) == IFMATCH ) { /* Lookahead */
3674                     cl_init(pRExC_state, &intrnl);
3675                     data_fake.start_class = &intrnl;
3676                     f |= SCF_DO_STCLASS_AND;
3677                 }
3678                 if (flags & SCF_WHILEM_VISITED_POS)
3679                     f |= SCF_WHILEM_VISITED_POS;
3680                 next = regnext(scan);
3681                 nscan = NEXTOPER(NEXTOPER(scan));
3682                 minnext = study_chunk(pRExC_state, &nscan, minlenp, &deltanext, 
3683                     last, &data_fake, stopparen, recursed, NULL, f, depth+1);
3684                 if (scan->flags) {
3685                     if (deltanext) {
3686                         FAIL("Variable length lookbehind not implemented");
3687                     }
3688                     else if (minnext > (I32)U8_MAX) {
3689                         FAIL2("Lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
3690                     }
3691                     scan->flags = (U8)minnext;
3692                 }
3693                 if (data) {
3694                     if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
3695                         pars++;
3696                     if (data_fake.flags & SF_HAS_EVAL)
3697                         data->flags |= SF_HAS_EVAL;
3698                     data->whilem_c = data_fake.whilem_c;
3699                 }
3700                 if (f & SCF_DO_STCLASS_AND) {
3701                     const int was = (data->start_class->flags & ANYOF_EOS);
3702
3703                     cl_and(data->start_class, &intrnl);
3704                     if (was)
3705                         data->start_class->flags |= ANYOF_EOS;
3706                 }
3707             }
3708 #if PERL_ENABLE_POSITIVE_ASSERTION_STUDY
3709             else {
3710                 /* Positive Lookahead/lookbehind
3711                    In this case we can do fixed string optimisation,
3712                    but we must be careful about it. Note in the case of
3713                    lookbehind the positions will be offset by the minimum
3714                    length of the pattern, something we won't know about
3715                    until after the recurse.
3716                 */
3717                 I32 deltanext, fake = 0;
3718                 regnode *nscan;
3719                 struct regnode_charclass_class intrnl;
3720                 int f = 0;
3721                 /* We use SAVEFREEPV so that when the full compile 
3722                     is finished perl will clean up the allocated 
3723                     minlens when its all done. This was we don't
3724                     have to worry about freeing them when we know
3725                     they wont be used, which would be a pain.
3726                  */
3727                 I32 *minnextp;
3728                 Newx( minnextp, 1, I32 );
3729                 SAVEFREEPV(minnextp);
3730
3731                 if (data) {
3732                     StructCopy(data, &data_fake, scan_data_t);
3733                     if ((flags & SCF_DO_SUBSTR) && data->last_found) {
3734                         f |= SCF_DO_SUBSTR;
3735                         if (scan->flags) 
3736                             SCAN_COMMIT(pRExC_state, &data_fake,minlenp);
3737                         data_fake.last_found=newSVsv(data->last_found);
3738                     }
3739                 }
3740                 else
3741                     data_fake.last_closep = &fake;
3742                 data_fake.flags = 0;
3743                 data_fake.pos_delta = delta;
3744                 if (is_inf)
3745                     data_fake.flags |= SF_IS_INF;
3746                 if ( flags & SCF_DO_STCLASS && !scan->flags
3747                      && OP(scan) == IFMATCH ) { /* Lookahead */
3748                     cl_init(pRExC_state, &intrnl);
3749                     data_fake.start_class = &intrnl;
3750                     f |= SCF_DO_STCLASS_AND;
3751                 }
3752                 if (flags & SCF_WHILEM_VISITED_POS)
3753                     f |= SCF_WHILEM_VISITED_POS;
3754                 next = regnext(scan);
3755                 nscan = NEXTOPER(NEXTOPER(scan));
3756
3757                 *minnextp = study_chunk(pRExC_state, &nscan, minnextp, &deltanext, 
3758                     last, &data_fake, stopparen, recursed, NULL, f,depth+1);
3759                 if (scan->flags) {
3760                     if (deltanext) {
3761                         FAIL("Variable length lookbehind not implemented");
3762                     }
3763                     else if (*minnextp > (I32)U8_MAX) {
3764                         FAIL2("Lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
3765                     }
3766                     scan->flags = (U8)*minnextp;
3767                 }
3768
3769                 *minnextp += min;
3770
3771                 if (f & SCF_DO_STCLASS_AND) {
3772                     const int was = (data->start_class->flags & ANYOF_EOS);
3773
3774                     cl_and(data->start_class, &intrnl);
3775                     if (was)
3776                         data->start_class->flags |= ANYOF_EOS;
3777                 }
3778                 if (data) {
3779                     if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
3780                         pars++;
3781                     if (data_fake.flags & SF_HAS_EVAL)
3782                         data->flags |= SF_HAS_EVAL;
3783                     data->whilem_c = data_fake.whilem_c;
3784                     if ((flags & SCF_DO_SUBSTR) && data_fake.last_found) {
3785                         if (RExC_rx->minlen<*minnextp)
3786                             RExC_rx->minlen=*minnextp;
3787                         SCAN_COMMIT(pRExC_state, &data_fake, minnextp);
3788                         SvREFCNT_dec(data_fake.last_found);
3789                         
3790                         if ( data_fake.minlen_fixed != minlenp ) 
3791                         {
3792                             data->offset_fixed= data_fake.offset_fixed;
3793                             data->minlen_fixed= data_fake.minlen_fixed;
3794                             data->lookbehind_fixed+= scan->flags;
3795                         }
3796                         if ( data_fake.minlen_float != minlenp )
3797                         {
3798                             data->minlen_float= data_fake.minlen_float;
3799                             data->offset_float_min=data_fake.offset_float_min;
3800                             data->offset_float_max=data_fake.offset_float_max;
3801                             data->lookbehind_float+= scan->flags;
3802                         }
3803                     }
3804                 }
3805
3806
3807             }
3808 #endif
3809         }
3810         else if (OP(scan) == OPEN) {
3811             if (stopparen != (I32)ARG(scan))
3812                 pars++;
3813         }
3814         else if (OP(scan) == CLOSE) {
3815             if (stopparen == (I32)ARG(scan)) {
3816                 break;
3817             }
3818             if ((I32)ARG(scan) == is_par) {
3819                 next = regnext(scan);
3820
3821                 if ( next && (OP(next) != WHILEM) && next < last)
3822                     is_par = 0;         /* Disable optimization */
3823             }
3824             if (data)
3825                 *(data->last_closep) = ARG(scan);
3826         }
3827         else if (OP(scan) == EVAL) {
3828                 if (data)
3829                     data->flags |= SF_HAS_EVAL;
3830         }
3831         else if ( PL_regkind[OP(scan)] == ENDLIKE ) {
3832             if (flags & SCF_DO_SUBSTR) {
3833                 SCAN_COMMIT(pRExC_state,data,minlenp);
3834                 flags &= ~SCF_DO_SUBSTR;
3835             }
3836             if (data && OP(scan)==ACCEPT) {
3837                 data->flags |= SCF_SEEN_ACCEPT;
3838                 if (stopmin > min)
3839                     stopmin = min;
3840             }
3841         }
3842         else if (OP(scan) == LOGICAL && scan->flags == 2) /* Embedded follows */
3843         {
3844                 if (flags & SCF_DO_SUBSTR) {
3845                     SCAN_COMMIT(pRExC_state,data,minlenp);
3846                     data->longest = &(data->longest_float);
3847                 }
3848                 is_inf = is_inf_internal = 1;
3849                 if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
3850                     cl_anything(pRExC_state, data->start_class);
3851                 flags &= ~SCF_DO_STCLASS;
3852         }
3853         else if (OP(scan) == GPOS) {
3854             if (!(RExC_rx->extflags & RXf_GPOS_FLOAT) &&
3855                 !(delta || is_inf || (data && data->pos_delta))) 
3856             {
3857                 if (!(RExC_rx->extflags & RXf_ANCH) && (flags & SCF_DO_SUBSTR))
3858                     RExC_rx->extflags |= RXf_ANCH_GPOS;
3859                 if (RExC_rx->gofs < (U32)min)
3860                     RExC_rx->gofs = min;
3861             } else {
3862                 RExC_rx->extflags |= RXf_GPOS_FLOAT;
3863                 RExC_rx->gofs = 0;
3864             }       
3865         }
3866 #ifdef TRIE_STUDY_OPT
3867 #ifdef FULL_TRIE_STUDY
3868         else if (PL_regkind[OP(scan)] == TRIE) {
3869             /* NOTE - There is similar code to this block above for handling
3870                BRANCH nodes on the initial study.  If you change stuff here
3871                check there too. */
3872             regnode *trie_node= scan;
3873             regnode *tail= regnext(scan);
3874             reg_trie_data *trie = (reg_trie_data*)RExC_rxi->data->data[ ARG(scan) ];
3875             I32 max1 = 0, min1 = I32_MAX;
3876             struct regnode_charclass_class accum;
3877
3878             if (flags & SCF_DO_SUBSTR) /* XXXX Add !SUSPEND? */
3879                 SCAN_COMMIT(pRExC_state, data,minlenp); /* Cannot merge strings after this. */
3880             if (flags & SCF_DO_STCLASS)
3881                 cl_init_zero(pRExC_state, &accum);
3882                 
3883             if (!trie->jump) {
3884                 min1= trie->minlen;
3885                 max1= trie->maxlen;
3886             } else {
3887                 const regnode *nextbranch= NULL;
3888                 U32 word;
3889                 
3890                 for ( word=1 ; word <= trie->wordcount ; word++) 
3891                 {
3892                     I32 deltanext=0, minnext=0, f = 0, fake;
3893                     struct regnode_charclass_class this_class;
3894                     
3895                     data_fake.flags = 0;
3896                     if (data) {
3897                         data_fake.whilem_c = data->whilem_c;
3898                         data_fake.last_closep = data->last_closep;
3899                     }
3900                     else
3901                         data_fake.last_closep = &fake;
3902                     data_fake.pos_delta = delta;
3903                     if (flags & SCF_DO_STCLASS) {
3904                         cl_init(pRExC_state, &this_class);
3905                         data_fake.start_class = &this_class;
3906                         f = SCF_DO_STCLASS_AND;
3907                     }
3908                     if (flags & SCF_WHILEM_VISITED_POS)
3909                         f |= SCF_WHILEM_VISITED_POS;
3910     
3911                     if (trie->jump[word]) {
3912                         if (!nextbranch)
3913                             nextbranch = trie_node + trie->jump[0];
3914                         scan= trie_node + trie->jump[word];
3915                         /* We go from the jump point to the branch that follows
3916                            it. Note this means we need the vestigal unused branches
3917                            even though they arent otherwise used.
3918                          */
3919                         minnext = study_chunk(pRExC_state, &scan, minlenp, 
3920                             &deltanext, (regnode *)nextbranch, &data_fake, 
3921                             stopparen, recursed, NULL, f,depth+1);
3922                     }
3923                     if (nextbranch && PL_regkind[OP(nextbranch)]==BRANCH)
3924                         nextbranch= regnext((regnode*)nextbranch);
3925                     
3926                     if (min1 > (I32)(minnext + trie->minlen))
3927                         min1 = minnext + trie->minlen;
3928                     if (max1 < (I32)(minnext + deltanext + trie->maxlen))
3929                         max1 = minnext + deltanext + trie->maxlen;
3930                     if (deltanext == I32_MAX)
3931                         is_inf = is_inf_internal = 1;
3932                     
3933                     if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
3934                         pars++;
3935                     if (data_fake.flags & SCF_SEEN_ACCEPT) {
3936                         if ( stopmin > min + min1) 
3937                             stopmin = min + min1;
3938                         flags &= ~SCF_DO_SUBSTR;
3939                         if (data)
3940                             data->flags |= SCF_SEEN_ACCEPT;
3941                     }
3942                     if (data) {
3943                         if (data_fake.flags & SF_HAS_EVAL)
3944                             data->flags |= SF_HAS_EVAL;
3945                         data->whilem_c = data_fake.whilem_c;
3946                     }
3947                     if (flags & SCF_DO_STCLASS)
3948                         cl_or(pRExC_state, &accum, &this_class);
3949                 }
3950             }
3951             if (flags & SCF_DO_SUBSTR) {
3952                 data->pos_min += min1;
3953                 data->pos_delta += max1 - min1;
3954                 if (max1 != min1 || is_inf)
3955                     data->longest = &(data->longest_float);
3956             }
3957             min += min1;
3958             delta += max1 - min1;
3959             if (flags & SCF_DO_STCLASS_OR) {
3960                 cl_or(pRExC_state, data->start_class, &accum);
3961                 if (min1) {
3962                     cl_and(data->start_class, and_withp);
3963                     flags &= ~SCF_DO_STCLASS;
3964                 }
3965             }
3966             else if (flags & SCF_DO_STCLASS_AND) {
3967                 if (min1) {
3968                     cl_and(data->start_class, &accum);
3969                     flags &= ~SCF_DO_STCLASS;
3970                 }
3971                 else {
3972                     /* Switch to OR mode: cache the old value of
3973                      * data->start_class */
3974                     INIT_AND_WITHP;
3975                     StructCopy(data->start_class, and_withp,
3976                                struct regnode_charclass_class);
3977                     flags &= ~SCF_DO_STCLASS_AND;
3978                     StructCopy(&accum, data->start_class,
3979                                struct regnode_charclass_class);
3980                     flags |= SCF_DO_STCLASS_OR;
3981                     data->start_class->flags |= ANYOF_EOS;
3982                 }
3983             }
3984             scan= tail;
3985             continue;
3986         }
3987 #else
3988         else if (PL_regkind[OP(scan)] == TRIE) {
3989             reg_trie_data *trie = (reg_trie_data*)RExC_rxi->data->data[ ARG(scan) ];
3990             U8*bang=NULL;
3991             
3992             min += trie->minlen;
3993             delta += (trie->maxlen - trie->minlen);
3994             flags &= ~SCF_DO_STCLASS; /* xxx */
3995             if (flags & SCF_DO_SUBSTR) {
3996                 SCAN_COMMIT(pRExC_state,data,minlenp);  /* Cannot expect anything... */
3997                 data->pos_min += trie->minlen;
3998                 data->pos_delta += (trie->maxlen - trie->minlen);
3999                 if (trie->maxlen != trie->minlen)
4000                     data->longest = &(data->longest_float);
4001             }
4002             if (trie->jump) /* no more substrings -- for now /grr*/
4003                 flags &= ~SCF_DO_SUBSTR; 
4004         }
4005 #endif /* old or new */
4006 #endif /* TRIE_STUDY_OPT */     
4007
4008         /* Else: zero-length, ignore. */
4009         scan = regnext(scan);
4010     }
4011     if (frame) {
4012         last = frame->last;
4013         scan = frame->next;
4014         stopparen = frame->stop;
4015         frame = frame->prev;
4016         goto fake_study_recurse;
4017     }
4018
4019   finish:
4020     assert(!frame);
4021     DEBUG_STUDYDATA("pre-fin:",data,depth);
4022
4023     *scanp = scan;
4024     *deltap = is_inf_internal ? I32_MAX : delta;
4025     if (flags & SCF_DO_SUBSTR && is_inf)
4026         data->pos_delta = I32_MAX - data->pos_min;
4027     if (is_par > (I32)U8_MAX)
4028         is_par = 0;
4029     if (is_par && pars==1 && data) {
4030         data->flags |= SF_IN_PAR;
4031         data->flags &= ~SF_HAS_PAR;
4032     }
4033     else if (pars && data) {
4034         data->flags |= SF_HAS_PAR;
4035         data->flags &= ~SF_IN_PAR;
4036     }
4037     if (flags & SCF_DO_STCLASS_OR)
4038         cl_and(data->start_class, and_withp);
4039     if (flags & SCF_TRIE_RESTUDY)
4040         data->flags |=  SCF_TRIE_RESTUDY;
4041     
4042     DEBUG_STUDYDATA("post-fin:",data,depth);
4043     
4044     return min < stopmin ? min : stopmin;
4045 }
4046
4047 STATIC U32
4048 S_add_data(RExC_state_t *pRExC_state, U32 n, const char *s)
4049 {
4050     U32 count = RExC_rxi->data ? RExC_rxi->data->count : 0;
4051
4052     Renewc(RExC_rxi->data,
4053            sizeof(*RExC_rxi->data) + sizeof(void*) * (count + n - 1),
4054            char, struct reg_data);
4055     if(count)
4056         Renew(RExC_rxi->data->what, count + n, U8);
4057     else
4058         Newx(RExC_rxi->data->what, n, U8);
4059     RExC_rxi->data->count = count + n;
4060     Copy(s, RExC_rxi->data->what + count, n, U8);
4061     return count;
4062 }
4063
4064 /*XXX: todo make this not included in a non debugging perl */
4065 #ifndef PERL_IN_XSUB_RE
4066 void
4067 Perl_reginitcolors(pTHX)
4068 {
4069     dVAR;
4070     const char * const s = PerlEnv_getenv("PERL_RE_COLORS");
4071     if (s) {
4072         char *t = savepv(s);
4073         int i = 0;
4074         PL_colors[0] = t;
4075         while (++i < 6) {
4076             t = strchr(t, '\t');
4077             if (t) {
4078                 *t = '\0';
4079                 PL_colors[i] = ++t;
4080             }
4081             else
4082                 PL_colors[i] = t = (char *)"";
4083         }
4084     } else {
4085         int i = 0;
4086         while (i < 6)
4087             PL_colors[i++] = (char *)"";
4088     }
4089     PL_colorset = 1;
4090 }
4091 #endif
4092
4093
4094 #ifdef TRIE_STUDY_OPT
4095 #define CHECK_RESTUDY_GOTO                                  \
4096         if (                                                \
4097               (data.flags & SCF_TRIE_RESTUDY)               \
4098               && ! restudied++                              \
4099         )     goto reStudy
4100 #else
4101 #define CHECK_RESTUDY_GOTO
4102 #endif        
4103
4104 /*
4105  - pregcomp - compile a regular expression into internal code
4106  *
4107  * We can't allocate space until we know how big the compiled form will be,
4108  * but we can't compile it (and thus know how big it is) until we've got a
4109  * place to put the code.  So we cheat:  we compile it twice, once with code
4110  * generation turned off and size counting turned on, and once "for real".
4111  * This also means that we don't allocate space until we are sure that the
4112  * thing really will compile successfully, and we never have to move the
4113  * code and thus invalidate pointers into it.  (Note that it has to be in
4114  * one piece because free() must be able to free it all.) [NB: not true in perl]
4115  *
4116  * Beware that the optimization-preparation code in here knows about some
4117  * of the structure of the compiled regexp.  [I'll say.]
4118  */
4119
4120
4121
4122 #ifndef PERL_IN_XSUB_RE
4123 #define RE_ENGINE_PTR &PL_core_reg_engine
4124 #else
4125 extern const struct regexp_engine my_reg_engine;
4126 #define RE_ENGINE_PTR &my_reg_engine
4127 #endif
4128
4129 #ifndef PERL_IN_XSUB_RE 
4130 REGEXP *
4131 Perl_pregcomp(pTHX_ const SV * const pattern, const U32 flags)
4132 {
4133     dVAR;
4134     HV * const table = GvHV(PL_hintgv);
4135     /* Dispatch a request to compile a regexp to correct 
4136        regexp engine. */
4137     if (table) {
4138         SV **ptr= hv_fetchs(table, "regcomp", FALSE);
4139         GET_RE_DEBUG_FLAGS_DECL;
4140         if (ptr && SvIOK(*ptr) && SvIV(*ptr)) {
4141             const regexp_engine *eng=INT2PTR(regexp_engine*,SvIV(*ptr));
4142             DEBUG_COMPILE_r({
4143                 PerlIO_printf(Perl_debug_log, "Using engine %"UVxf"\n",
4144                     SvIV(*ptr));
4145             });            
4146             return CALLREGCOMP_ENG(eng, pattern, flags);
4147         } 
4148     }
4149     return Perl_re_compile(aTHX_ pattern, flags);
4150 }
4151 #endif
4152
4153 REGEXP *
4154 Perl_re_compile(pTHX_ const SV * const pattern, const U32 pm_flags)
4155 {
4156     dVAR;
4157     REGEXP *rx;
4158     struct regexp *r;
4159     register regexp_internal *ri;
4160     STRLEN plen;
4161     char*  exp = SvPV((SV*)pattern, plen);
4162     char* xend = exp + plen;
4163     regnode *scan;
4164     I32 flags;
4165     I32 minlen = 0;
4166     I32 sawplus = 0;
4167     I32 sawopen = 0;
4168     scan_data_t data;
4169     RExC_state_t RExC_state;
4170     RExC_state_t * const pRExC_state = &RExC_state;
4171 #ifdef TRIE_STUDY_OPT    
4172     int restudied= 0;
4173     RExC_state_t copyRExC_state;
4174 #endif    
4175     GET_RE_DEBUG_FLAGS_DECL;
4176     DEBUG_r(if (!PL_colorset) reginitcolors());
4177
4178     RExC_utf8 = RExC_orig_utf8 = pm_flags & RXf_UTF8;
4179
4180     DEBUG_COMPILE_r({
4181         SV *dsv= sv_newmortal();
4182         RE_PV_QUOTED_DECL(s, RExC_utf8,
4183             dsv, exp, plen, 60);
4184         PerlIO_printf(Perl_debug_log, "%sCompiling REx%s %s\n",
4185                        PL_colors[4],PL_colors[5],s);
4186     });
4187
4188 redo_first_pass:
4189     RExC_precomp = exp;
4190     RExC_flags = pm_flags;
4191     RExC_sawback = 0;
4192
4193     RExC_seen = 0;
4194     RExC_seen_zerolen = *exp == '^' ? -1 : 0;
4195     RExC_seen_evals = 0;
4196     RExC_extralen = 0;
4197
4198     /* First pass: determine size, legality. */
4199     RExC_parse = exp;
4200     RExC_start = exp;
4201     RExC_end = xend;
4202     RExC_naughty = 0;
4203     RExC_npar = 1;
4204     RExC_nestroot = 0;
4205     RExC_size = 0L;
4206     RExC_emit = &PL_regdummy;
4207     RExC_whilem_seen = 0;
4208     RExC_charnames = NULL;
4209     RExC_open_parens = NULL;
4210     RExC_close_parens = NULL;
4211     RExC_opend = NULL;
4212     RExC_paren_names = NULL;
4213 #ifdef DEBUGGING
4214     RExC_paren_name_list = NULL;
4215 #endif
4216     RExC_recurse = NULL;
4217     RExC_recurse_count = 0;
4218
4219 #if 0 /* REGC() is (currently) a NOP at the first pass.
4220        * Clever compilers notice this and complain. --jhi */
4221     REGC((U8)REG_MAGIC, (char*)RExC_emit);
4222 #endif
4223     DEBUG_PARSE_r(PerlIO_printf(Perl_debug_log, "Starting first pass (sizing)\n"));
4224     if (reg(pRExC_state, 0, &flags,1) == NULL) {
4225         RExC_precomp = NULL;
4226         return(NULL);
4227     }
4228     if (RExC_utf8 && !RExC_orig_utf8) {
4229         /* It's possible to write a regexp in ascii that represents Unicode
4230         codepoints outside of the byte range, such as via \x{100}. If we
4231         detect such a sequence we have to convert the entire pattern to utf8
4232         and then recompile, as our sizing calculation will have been based
4233         on 1 byte == 1 character, but we will need to use utf8 to encode
4234         at least some part of the pattern, and therefore must convert the whole
4235         thing.
4236         XXX: somehow figure out how to make this less expensive...
4237         -- dmq */
4238         STRLEN len = plen;
4239         DEBUG_PARSE_r(PerlIO_printf(Perl_debug_log,
4240             "UTF8 mismatch! Converting to utf8 for resizing and compile\n"));
4241         exp = (char*)Perl_bytes_to_utf8(aTHX_ (U8*)exp, &len);
4242         xend = exp + len;
4243         RExC_orig_utf8 = RExC_utf8;
4244         SAVEFREEPV(exp);
4245         goto redo_first_pass;
4246     }
4247     DEBUG_PARSE_r({
4248         PerlIO_printf(Perl_debug_log, 
4249             "Required size %"IVdf" nodes\n"
4250             "Starting second pass (creation)\n", 
4251             (IV)RExC_size);
4252         RExC_lastnum=0; 
4253         RExC_lastparse=NULL; 
4254     });
4255     /* Small enough for pointer-storage convention?
4256        If extralen==0, this means that we will not need long jumps. */
4257     if (RExC_size >= 0x10000L && RExC_extralen)
4258         RExC_size += RExC_extralen;
4259     else
4260         RExC_extralen = 0;
4261     if (RExC_whilem_seen > 15)
4262         RExC_whilem_seen = 15;
4263
4264     /* Allocate space and zero-initialize. Note, the two step process 
4265        of zeroing when in debug mode, thus anything assigned has to 
4266        happen after that */
4267     rx = newSV_type(SVt_REGEXP);
4268     r = (struct regexp*)SvANY(rx);
4269     Newxc(ri, sizeof(regexp_internal) + (unsigned)RExC_size * sizeof(regnode),
4270          char, regexp_internal);
4271     if ( r == NULL || ri == NULL )
4272         FAIL("Regexp out of space");
4273 #ifdef DEBUGGING
4274     /* avoid reading uninitialized memory in DEBUGGING code in study_chunk() */
4275     Zero(ri, sizeof(regexp_internal) + (unsigned)RExC_size * sizeof(regnode), char);
4276 #else 
4277     /* bulk initialize base fields with 0. */
4278     Zero(ri, sizeof(regexp_internal), char);        
4279 #endif
4280
4281     /* non-zero initialization begins here */
4282     RXi_SET( r, ri );
4283     r->engine= RE_ENGINE_PTR;
4284     r->extflags = pm_flags;
4285     {
4286         bool has_p     = ((r->extflags & RXf_PMf_KEEPCOPY) == RXf_PMf_KEEPCOPY);
4287         bool has_minus = ((r->extflags & RXf_PMf_STD_PMMOD) != RXf_PMf_STD_PMMOD);
4288         bool has_runon = ((RExC_seen & REG_SEEN_RUN_ON_COMMENT)==REG_SEEN_RUN_ON_COMMENT);
4289         U16 reganch = (U16)((r->extflags & RXf_PMf_STD_PMMOD)
4290                             >> RXf_PMf_STD_PMMOD_SHIFT);
4291         const char *fptr = STD_PAT_MODS;        /*"msix"*/
4292         char *p;
4293         RXp_WRAPLEN(r) = plen + has_minus + has_p + has_runon
4294             + (sizeof(STD_PAT_MODS) - 1)
4295             + (sizeof("(?:)") - 1);
4296
4297         Newx(RXp_WRAPPED(r), RXp_WRAPLEN(r) + 1, char );
4298         p = RXp_WRAPPED(r);
4299         *p++='('; *p++='?';
4300         if (has_p)
4301             *p++ = KEEPCOPY_PAT_MOD; /*'p'*/
4302         {
4303             char *r = p + (sizeof(STD_PAT_MODS) - 1) + has_minus - 1;
4304             char *colon = r + 1;
4305             char ch;
4306
4307             while((ch = *fptr++)) {
4308                 if(reganch & 1)
4309                     *p++ = ch;
4310                 else
4311                     *r-- = ch;
4312                 reganch >>= 1;
4313             }
4314             if(has_minus) {
4315                 *r = '-';
4316                 p = colon;
4317             }
4318         }
4319
4320         *p++ = ':';
4321         Copy(RExC_precomp, p, plen, char);
4322         assert ((RXp_WRAPPED(r) - p) < 16);
4323         r->pre_prefix = p - RXp_WRAPPED(r);
4324         p += plen;
4325         if (has_runon)
4326             *p++ = '\n';
4327         *p++ = ')';
4328         *p = 0;
4329     }
4330
4331     r->intflags = 0;
4332     r->nparens = RExC_npar - 1; /* set early to validate backrefs */
4333     
4334     if (RExC_seen & REG_SEEN_RECURSE) {
4335         Newxz(RExC_open_parens, RExC_npar,regnode *);
4336         SAVEFREEPV(RExC_open_parens);
4337         Newxz(RExC_close_parens,RExC_npar,regnode *);
4338         SAVEFREEPV(RExC_close_parens);
4339     }
4340
4341     /* Useful during FAIL. */
4342 #ifdef RE_TRACK_PATTERN_OFFSETS
4343     Newxz(ri->u.offsets, 2*RExC_size+1, U32); /* MJD 20001228 */
4344     DEBUG_OFFSETS_r(PerlIO_printf(Perl_debug_log,
4345                           "%s %"UVuf" bytes for offset annotations.\n",
4346                           ri->u.offsets ? "Got" : "Couldn't get",
4347                           (UV)((2*RExC_size+1) * sizeof(U32))));
4348 #endif
4349     SetProgLen(ri,RExC_size);
4350     RExC_rx_sv = rx;
4351     RExC_rx = r;
4352     RExC_rxi = ri;
4353
4354     /* Second pass: emit code. */
4355     RExC_flags = pm_flags;      /* don't let top level (?i) bleed */
4356     RExC_parse = exp;
4357     RExC_end = xend;
4358     RExC_naughty = 0;
4359     RExC_npar = 1;
4360     RExC_emit_start = ri->program;
4361     RExC_emit = ri->program;
4362     RExC_emit_bound = ri->program + RExC_size + 1;
4363
4364     /* Store the count of eval-groups for security checks: */
4365     RExC_rx->seen_evals = RExC_seen_evals;
4366     REGC((U8)REG_MAGIC, (char*) RExC_emit++);
4367     if (reg(pRExC_state, 0, &flags,1) == NULL) {
4368         ReREFCNT_dec(rx);   
4369         return(NULL);
4370     }
4371     /* XXXX To minimize changes to RE engine we always allocate
4372        3-units-long substrs field. */
4373     Newx(r->substrs, 1, struct reg_substr_data);
4374     if (RExC_recurse_count) {
4375         Newxz(RExC_recurse,RExC_recurse_count,regnode *);
4376         SAVEFREEPV(RExC_recurse);
4377     }
4378
4379 reStudy:
4380     r->minlen = minlen = sawplus = sawopen = 0;
4381     Zero(r->substrs, 1, struct reg_substr_data);
4382
4383 #ifdef TRIE_STUDY_OPT
4384     if ( restudied ) {
4385         U32 seen=RExC_seen;
4386         DEBUG_OPTIMISE_r(PerlIO_printf(Perl_debug_log,"Restudying\n"));
4387         
4388         RExC_state = copyRExC_state;
4389         if (seen & REG_TOP_LEVEL_BRANCHES) 
4390             RExC_seen |= REG_TOP_LEVEL_BRANCHES;
4391         else
4392             RExC_seen &= ~REG_TOP_LEVEL_BRANCHES;
4393         if (data.last_found) {
4394             SvREFCNT_dec(data.longest_fixed);
4395             SvREFCNT_dec(data.longest_float);
4396             SvREFCNT_dec(data.last_found);
4397         }
4398         StructCopy(&zero_scan_data, &data, scan_data_t);
4399     } else {
4400         StructCopy(&zero_scan_data, &data, scan_data_t);
4401         copyRExC_state = RExC_state;
4402     }
4403 #else
4404     StructCopy(&zero_scan_data, &data, scan_data_t);
4405 #endif    
4406
4407     /* Dig out information for optimizations. */
4408     r->extflags = RExC_flags; /* was pm_op */
4409     /*dmq: removed as part of de-PMOP: pm->op_pmflags = RExC_flags; */
4410  
4411     if (UTF)
4412         r->extflags |= RXf_UTF8;        /* Unicode in it? */
4413     ri->regstclass = NULL;
4414     if (RExC_naughty >= 10)     /* Probably an expensive pattern. */
4415         r->intflags |= PREGf_NAUGHTY;
4416     scan = ri->program + 1;             /* First BRANCH. */
4417
4418     /* testing for BRANCH here tells us whether there is "must appear"
4419        data in the pattern. If there is then we can use it for optimisations */
4420     if (!(RExC_seen & REG_TOP_LEVEL_BRANCHES)) { /*  Only one top-level choice. */
4421         I32 fake;
4422         STRLEN longest_float_length, longest_fixed_length;
4423         struct regnode_charclass_class ch_class; /* pointed to by data */
4424         int stclass_flag;
4425         I32 last_close = 0; /* pointed to by data */
4426         regnode *first= scan;
4427         regnode *first_next= regnext(first);
4428         
4429         /* Skip introductions and multiplicators >= 1. */
4430         while ((OP(first) == OPEN && (sawopen = 1)) ||
4431                /* An OR of *one* alternative - should not happen now. */
4432             (OP(first) == BRANCH && OP(first_next) != BRANCH) ||
4433             /* for now we can't handle lookbehind IFMATCH*/
4434             (OP(first) == IFMATCH && !first->flags) || 
4435             (OP(first) == PLUS) ||
4436             (OP(first) == MINMOD) ||
4437                /* An {n,m} with n>0 */
4438             (PL_regkind[OP(first)] == CURLY && ARG1(first) > 0) ||
4439             (OP(first) == NOTHING && PL_regkind[OP(first_next)] != END ))
4440         {
4441                 
4442                 if (OP(first) == PLUS)
4443                     sawplus = 1;
4444                 else
4445                     first += regarglen[OP(first)];
4446                 if (OP(first) == IFMATCH) {
4447                     first = NEXTOPER(first);
4448                     first += EXTRA_STEP_2ARGS;
4449                 } else  /* XXX possible optimisation for /(?=)/  */
4450                     first = NEXTOPER(first);
4451                 first_next= regnext(first);
4452         }
4453
4454         /* Starting-point info. */
4455       again:
4456         DEBUG_PEEP("first:",first,0);
4457         /* Ignore EXACT as we deal with it later. */
4458         if (PL_regkind[OP(first)] == EXACT) {
4459             if (OP(first) == EXACT)
4460                 NOOP;   /* Empty, get anchored substr later. */
4461             else if ((OP(first) == EXACTF || OP(first) == EXACTFL))
4462                 ri->regstclass = first;
4463         }
4464 #ifdef TRIE_STCLASS     
4465         else if (PL_regkind[OP(first)] == TRIE &&
4466                 ((reg_trie_data *)ri->data->data[ ARG(first) ])->minlen>0) 
4467         {
4468             regnode *trie_op;
4469             /* this can happen only on restudy */
4470             if ( OP(first) == TRIE ) {
4471                 struct regnode_1 *trieop = (struct regnode_1 *)
4472                     PerlMemShared_calloc(1, sizeof(struct regnode_1));
4473                 StructCopy(first,trieop,struct regnode_1);
4474                 trie_op=(regnode *)trieop;
4475             } else {
4476                 struct regnode_charclass *trieop = (struct regnode_charclass *)
4477                     PerlMemShared_calloc(1, sizeof(struct regnode_charclass));
4478                 StructCopy(first,trieop,struct regnode_charclass);
4479                 trie_op=(regnode *)trieop;
4480             }
4481             OP(trie_op)+=2;
4482             make_trie_failtable(pRExC_state, (regnode *)first, trie_op, 0);
4483             ri->regstclass = trie_op;
4484         }
4485 #endif  
4486         else if (strchr((const char*)PL_simple,OP(first)))
4487             ri->regstclass = first;
4488         else if (PL_regkind[OP(first)] == BOUND ||
4489                  PL_regkind[OP(first)] == NBOUND)
4490             ri->regstclass = first;
4491         else if (PL_regkind[OP(first)] == BOL) {
4492             r->extflags |= (OP(first) == MBOL
4493                            ? RXf_ANCH_MBOL
4494                            : (OP(first) == SBOL
4495                               ? RXf_ANCH_SBOL
4496                               : RXf_ANCH_BOL));
4497             first = NEXTOPER(first);
4498             goto again;
4499         }
4500         else if (OP(first) == GPOS) {
4501             r->extflags |= RXf_ANCH_GPOS;
4502             first = NEXTOPER(first);
4503             goto again;
4504         }
4505         else if ((!sawopen || !RExC_sawback) &&
4506             (OP(first) == STAR &&
4507             PL_regkind[OP(NEXTOPER(first))] == REG_ANY) &&
4508             !(r->extflags & RXf_ANCH) && !(RExC_seen & REG_SEEN_EVAL))
4509         {
4510             /* turn .* into ^.* with an implied $*=1 */
4511             const int type =
4512                 (OP(NEXTOPER(first)) == REG_ANY)
4513                     ? RXf_ANCH_MBOL
4514                     : RXf_ANCH_SBOL;
4515             r->extflags |= type;
4516             r->intflags |= PREGf_IMPLICIT;
4517             first = NEXTOPER(first);
4518             goto again;
4519         }
4520         if (sawplus && (!sawopen || !RExC_sawback)
4521             && !(RExC_seen & REG_SEEN_EVAL)) /* May examine pos and $& */
4522             /* x+ must match at the 1st pos of run of x's */
4523             r->intflags |= PREGf_SKIP;
4524
4525         /* Scan is after the zeroth branch, first is atomic matcher. */
4526 #ifdef TRIE_STUDY_OPT
4527         DEBUG_PARSE_r(
4528             if (!restudied)
4529                 PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n",
4530                               (IV)(first - scan + 1))
4531         );
4532 #else
4533         DEBUG_PARSE_r(
4534             PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n",
4535                 (IV)(first - scan + 1))
4536         );
4537 #endif
4538
4539
4540         /*
4541         * If there's something expensive in the r.e., find the
4542         * longest literal string that must appear and make it the
4543         * regmust.  Resolve ties in favor of later strings, since
4544         * the regstart check works with the beginning of the r.e.
4545         * and avoiding duplication strengthens checking.  Not a
4546         * strong reason, but sufficient in the absence of others.
4547         * [Now we resolve ties in favor of the earlier string if
4548         * it happens that c_offset_min has been invalidated, since the
4549         * earlier string may buy us something the later one won't.]
4550         */
4551         
4552         data.longest_fixed = newSVpvs("");
4553         data.longest_float = newSVpvs("");
4554         data.last_found = newSVpvs("");
4555         data.longest = &(data.longest_fixed);
4556         first = scan;
4557         if (!ri->regstclass) {
4558             cl_init(pRExC_state, &ch_class);
4559             data.start_class = &ch_class;
4560             stclass_flag = SCF_DO_STCLASS_AND;
4561         } else                          /* XXXX Check for BOUND? */
4562             stclass_flag = 0;
4563         data.last_closep = &last_close;
4564         
4565         minlen = study_chunk(pRExC_state, &first, &minlen, &fake, scan + RExC_size, /* Up to end */
4566             &data, -1, NULL, NULL,
4567             SCF_DO_SUBSTR | SCF_WHILEM_VISITED_POS | stclass_flag,0);
4568
4569         
4570         CHECK_RESTUDY_GOTO;
4571
4572
4573         if ( RExC_npar == 1 && data.longest == &(data.longest_fixed)
4574              && data.last_start_min == 0 && data.last_end > 0
4575              && !RExC_seen_zerolen
4576              && !(RExC_seen & REG_SEEN_VERBARG)
4577              && (!(RExC_seen & REG_SEEN_GPOS) || (r->extflags & RXf_ANCH_GPOS)))
4578             r->extflags |= RXf_CHECK_ALL;
4579         scan_commit(pRExC_state, &data,&minlen,0);
4580         SvREFCNT_dec(data.last_found);
4581
4582         /* Note that code very similar to this but for anchored string 
4583            follows immediately below, changes may need to be made to both. 
4584            Be careful. 
4585          */
4586         longest_float_length = CHR_SVLEN(data.longest_float);
4587         if (longest_float_length
4588             || (data.flags & SF_FL_BEFORE_EOL
4589                 && (!(data.flags & SF_FL_BEFORE_MEOL)
4590                     || (RExC_flags & RXf_PMf_MULTILINE)))) 
4591         {
4592             I32 t,ml;
4593
4594             if (SvCUR(data.longest_fixed)  /* ok to leave SvCUR */
4595                 && data.offset_fixed == data.offset_float_min
4596                 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
4597                     goto remove_float;          /* As in (a)+. */
4598
4599             /* copy the information about the longest float from the reg_scan_data
4600                over to the program. */
4601             if (SvUTF8(data.longest_float)) {
4602                 r->float_utf8 = data.longest_float;
4603                 r->float_substr = NULL;
4604             } else {
4605                 r->float_substr = data.longest_float;
4606                 r->float_utf8 = NULL;
4607             }
4608             /* float_end_shift is how many chars that must be matched that 
4609                follow this item. We calculate it ahead of time as once the
4610                lookbehind offset is added in we lose the ability to correctly
4611                calculate it.*/
4612             ml = data.minlen_float ? *(data.minlen_float) 
4613                                    : (I32)longest_float_length;
4614             r->float_end_shift = ml - data.offset_float_min
4615                 - longest_float_length + (SvTAIL(data.longest_float) != 0)
4616                 + data.lookbehind_float;
4617             r->float_min_offset = data.offset_float_min - data.lookbehind_float;
4618             r->float_max_offset = data.offset_float_max;
4619             if (data.offset_float_max < I32_MAX) /* Don't offset infinity */
4620                 r->float_max_offset -= data.lookbehind_float;
4621             
4622             t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
4623                        && (!(data.flags & SF_FL_BEFORE_MEOL)
4624                            || (RExC_flags & RXf_PMf_MULTILINE)));
4625             fbm_compile(data.longest_float, t ? FBMcf_TAIL : 0);
4626         }
4627         else {
4628           remove_float:
4629             r->float_substr = r->float_utf8 = NULL;
4630             SvREFCNT_dec(data.longest_float);
4631             longest_float_length = 0;
4632         }
4633
4634         /* Note that code very similar to this but for floating string 
4635            is immediately above, changes may need to be made to both. 
4636            Be careful. 
4637          */
4638         longest_fixed_length = CHR_SVLEN(data.longest_fixed);
4639         if (longest_fixed_length
4640             || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
4641                 && (!(data.flags & SF_FIX_BEFORE_MEOL)
4642                     || (RExC_flags & RXf_PMf_MULTILINE)))) 
4643         {
4644             I32 t,ml;
4645
4646             /* copy the information about the longest fixed 
4647                from the reg_scan_data over to the program. */
4648             if (SvUTF8(data.longest_fixed)) {
4649                 r->anchored_utf8 = data.longest_fixed;
4650                 r->anchored_substr = NULL;
4651             } else {
4652                 r->anchored_substr = data.longest_fixed;
4653                 r->anchored_utf8 = NULL;
4654             }
4655             /* fixed_end_shift is how many chars that must be matched that 
4656                follow this item. We calculate it ahead of time as once the
4657                lookbehind offset is added in we lose the ability to correctly
4658                calculate it.*/
4659             ml = data.minlen_fixed ? *(data.minlen_fixed) 
4660                                    : (I32)longest_fixed_length;
4661             r->anchored_end_shift = ml - data.offset_fixed
4662                 - longest_fixed_length + (SvTAIL(data.longest_fixed) != 0)
4663                 + data.lookbehind_fixed;
4664             r->anchored_offset = data.offset_fixed - data.lookbehind_fixed;
4665
4666             t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
4667                  && (!(data.flags & SF_FIX_BEFORE_MEOL)
4668                      || (RExC_flags & RXf_PMf_MULTILINE)));
4669             fbm_compile(data.longest_fixed, t ? FBMcf_TAIL : 0);
4670         }
4671         else {
4672             r->anchored_substr = r->anchored_utf8 = NULL;
4673             SvREFCNT_dec(data.longest_fixed);
4674             longest_fixed_length = 0;
4675         }
4676         if (ri->regstclass
4677             && (OP(ri->regstclass) == REG_ANY || OP(ri->regstclass) == SANY))
4678             ri->regstclass = NULL;
4679         if ((!(r->anchored_substr || r->anchored_utf8) || r->anchored_offset)
4680             && stclass_flag
4681             && !(data.start_class->flags & ANYOF_EOS)
4682             && !cl_is_anything(data.start_class))
4683         {
4684             const U32 n = add_data(pRExC_state, 1, "f");
4685
4686             Newx(RExC_rxi->data->data[n], 1,
4687                 struct regnode_charclass_class);
4688             StructCopy(data.start_class,
4689                        (struct regnode_charclass_class*)RExC_rxi->data->data[n],
4690                        struct regnode_charclass_class);
4691             ri->regstclass = (regnode*)RExC_rxi->data->data[n];
4692             r->intflags &= ~PREGf_SKIP; /* Used in find_byclass(). */
4693             DEBUG_COMPILE_r({ SV *sv = sv_newmortal();
4694                       regprop(r, sv, (regnode*)data.start_class);
4695                       PerlIO_printf(Perl_debug_log,
4696                                     "synthetic stclass \"%s\".\n",
4697                                     SvPVX_const(sv));});
4698         }
4699
4700         /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
4701         if (longest_fixed_length > longest_float_length) {
4702             r->check_end_shift = r->anchored_end_shift;
4703             r->check_substr = r->anchored_substr;
4704             r->check_utf8 = r->anchored_utf8;
4705             r->check_offset_min = r->check_offset_max = r->anchored_offset;
4706             if (r->extflags & RXf_ANCH_SINGLE)
4707                 r->extflags |= RXf_NOSCAN;
4708         }
4709         else {
4710             r->check_end_shift = r->float_end_shift;
4711             r->check_substr = r->float_substr;
4712             r->check_utf8 = r->float_utf8;
4713             r->check_offset_min = r->float_min_offset;
4714             r->check_offset_max = r->float_max_offset;
4715         }
4716         /* XXXX Currently intuiting is not compatible with ANCH_GPOS.
4717            This should be changed ASAP!  */
4718         if ((r->check_substr || r->check_utf8) && !(r->extflags & RXf_ANCH_GPOS)) {
4719             r->extflags |= RXf_USE_INTUIT;
4720             if (SvTAIL(r->check_substr ? r->check_substr : r->check_utf8))
4721                 r->extflags |= RXf_INTUIT_TAIL;
4722         }
4723         /* XXX Unneeded? dmq (shouldn't as this is handled elsewhere)
4724         if ( (STRLEN)minlen < longest_float_length )
4725             minlen= longest_float_length;
4726         if ( (STRLEN)minlen < longest_fixed_length )
4727             minlen= longest_fixed_length;     
4728         */
4729     }
4730     else {
4731         /* Several toplevels. Best we can is to set minlen. */
4732         I32 fake;
4733         struct regnode_charclass_class ch_class;
4734         I32 last_close = 0;
4735         
4736         DEBUG_PARSE_r(PerlIO_printf(Perl_debug_log, "\nMulti Top Level\n"));
4737
4738         scan = ri->program + 1;
4739         cl_init(pRExC_state, &ch_class);
4740         data.start_class = &ch_class;
4741         data.last_closep = &last_close;
4742
4743         
4744         minlen = study_chunk(pRExC_state, &scan, &minlen, &fake, scan + RExC_size,
4745             &data, -1, NULL, NULL, SCF_DO_STCLASS_AND|SCF_WHILEM_VISITED_POS,0);
4746         
4747         CHECK_RESTUDY_GOTO;
4748
4749         r->check_substr = r->check_utf8 = r->anchored_substr = r->anchored_utf8
4750                 = r->float_substr = r->float_utf8 = NULL;
4751         if (!(data.start_class->flags & ANYOF_EOS)
4752             && !cl_is_anything(data.start_class))
4753         {
4754             const U32 n = add_data(pRExC_state, 1, "f");
4755
4756             Newx(RExC_rxi->data->data[n], 1,
4757                 struct regnode_charclass_class);
4758             StructCopy(data.start_class,
4759                        (struct regnode_charclass_class*)RExC_rxi->data->data[n],
4760                        struct regnode_charclass_class);
4761             ri->regstclass = (regnode*)RExC_rxi->data->data[n];
4762             r->intflags &= ~PREGf_SKIP; /* Used in find_byclass(). */
4763             DEBUG_COMPILE_r({ SV* sv = sv_newmortal();
4764                       regprop(r, sv, (regnode*)data.start_class);
4765                       PerlIO_printf(Perl_debug_log,
4766                                     "synthetic stclass \"%s\".\n",
4767                                     SvPVX_const(sv));});
4768         }
4769     }
4770
4771     /* Guard against an embedded (?=) or (?<=) with a longer minlen than
4772        the "real" pattern. */
4773     DEBUG_OPTIMISE_r({
4774         PerlIO_printf(Perl_debug_log,"minlen: %"IVdf" r->minlen:%"IVdf"\n",
4775                       (IV)minlen, (IV)r->minlen);
4776     });
4777     r->minlenret = minlen;
4778     if (r->minlen < minlen) 
4779         r->minlen = minlen;
4780     
4781     if (RExC_seen & REG_SEEN_GPOS)
4782         r->extflags |= RXf_GPOS_SEEN;
4783     if (RExC_seen & REG_SEEN_LOOKBEHIND)
4784         r->extflags |= RXf_LOOKBEHIND_SEEN;
4785     if (RExC_seen & REG_SEEN_EVAL)
4786         r->extflags |= RXf_EVAL_SEEN;
4787     if (RExC_seen & REG_SEEN_CANY)
4788         r->extflags |= RXf_CANY_SEEN;
4789     if (RExC_seen & REG_SEEN_VERBARG)
4790         r->intflags |= PREGf_VERBARG_SEEN;
4791     if (RExC_seen & REG_SEEN_CUTGROUP)
4792         r->intflags |= PREGf_CUTGROUP_SEEN;
4793     if (RExC_paren_names)
4794         r->paren_names = (HV*)SvREFCNT_inc(RExC_paren_names);
4795     else
4796         r->paren_names = NULL;
4797
4798 #ifdef STUPID_PATTERN_CHECKS            
4799     if (RX_PRELEN(r) == 0)
4800         r->extflags |= RXf_NULL;
4801     if (r->extflags & RXf_SPLIT && RX_PRELEN(r) == 1 && RXp_PRECOMP(r)[0] == ' ')
4802         /* XXX: this should happen BEFORE we compile */
4803         r->extflags |= (RXf_SKIPWHITE|RXf_WHITE); 
4804     else if (RX_PRELEN(r) == 3 && memEQ("\\s+", RXp_PRECOMP(r), 3))
4805         r->extflags |= RXf_WHITE;
4806     else if (RX_PRELEN(r) == 1 && RXp_PRECOMP(r)[0] == '^')
4807         r->extflags |= RXf_START_ONLY;
4808 #else
4809     if (r->extflags & RXf_SPLIT && RXp_PRELEN(r) == 1 && RXp_PRECOMP(r)[0] == ' ')
4810             /* XXX: this should happen BEFORE we compile */
4811             r->extflags |= (RXf_SKIPWHITE|RXf_WHITE); 
4812     else {
4813         regnode *first = ri->program + 1;
4814         U8 fop = OP(first);
4815         U8 nop = OP(NEXTOPER(first));
4816         
4817         if (PL_regkind[fop] == NOTHING && nop == END)
4818             r->extflags |= RXf_NULL;
4819         else if (PL_regkind[fop] == BOL && nop == END)
4820             r->extflags |= RXf_START_ONLY;
4821         else if (fop == PLUS && nop ==SPACE && OP(regnext(first))==END)
4822             r->extflags |= RXf_WHITE;    
4823     }
4824 #endif
4825 #ifdef DEBUGGING
4826     if (RExC_paren_names) {
4827         ri->name_list_idx = add_data( pRExC_state, 1, "p" );
4828         ri->data->data[ri->name_list_idx] = (void*)SvREFCNT_inc(RExC_paren_name_list);
4829     } else
4830 #endif
4831         ri->name_list_idx = 0;
4832
4833     if (RExC_recurse_count) {
4834         for ( ; RExC_recurse_count ; RExC_recurse_count-- ) {
4835             const regnode *scan = RExC_recurse[RExC_recurse_count-1];
4836             ARG2L_SET( scan, RExC_open_parens[ARG(scan)-1] - scan );
4837         }
4838     }
4839     Newxz(r->offs, RExC_npar, regexp_paren_pair);
4840     /* assume we don't need to swap parens around before we match */
4841
4842     DEBUG_DUMP_r({
4843         PerlIO_printf(Perl_debug_log,"Final program:\n");
4844         regdump(r);
4845     });
4846 #ifdef RE_TRACK_PATTERN_OFFSETS
4847     DEBUG_OFFSETS_r(if (ri->u.offsets) {
4848         const U32 len = ri->u.offsets[0];
4849         U32 i;
4850         GET_RE_DEBUG_FLAGS_DECL;
4851         PerlIO_printf(Perl_debug_log, "Offsets: [%"UVuf"]\n\t", (UV)ri->u.offsets[0]);
4852         for (i = 1; i <= len; i++) {
4853             if (ri->u.offsets[i*2-1] || ri->u.offsets[i*2])
4854                 PerlIO_printf(Perl_debug_log, "%"UVuf":%"UVuf"[%"UVuf"] ",
4855                 (UV)i, (UV)ri->u.offsets[i*2-1], (UV)ri->u.offsets[i*2]);
4856             }
4857         PerlIO_printf(Perl_debug_log, "\n");
4858     });
4859 #endif
4860     return rx;
4861 }
4862
4863 #undef RE_ENGINE_PTR
4864
4865
4866 SV*
4867 Perl_reg_named_buff(pTHX_ REGEXP * const rx, SV * const key, SV * const value,
4868                     const U32 flags)
4869 {
4870     PERL_UNUSED_ARG(value);
4871
4872     if (flags & RXapif_FETCH) {
4873         return reg_named_buff_fetch(rx, key, flags);
4874     } else if (flags & (RXapif_STORE | RXapif_DELETE | RXapif_CLEAR)) {
4875         Perl_croak(aTHX_ PL_no_modify);
4876         return NULL;
4877     } else if (flags & RXapif_EXISTS) {
4878         return reg_named_buff_exists(rx, key, flags)
4879             ? &PL_sv_yes
4880             : &PL_sv_no;
4881     } else if (flags & RXapif_REGNAMES) {
4882         return reg_named_buff_all(rx, flags);
4883     } else if (flags & (RXapif_SCALAR | RXapif_REGNAMES_COUNT)) {
4884         return reg_named_buff_scalar(rx, flags);
4885     } else {
4886         Perl_croak(aTHX_ "panic: Unknown flags %d in named_buff", (int)flags);
4887         return NULL;
4888     }
4889 }
4890
4891 SV*
4892 Perl_reg_named_buff_iter(pTHX_ REGEXP * const rx, const SV * const lastkey,
4893                          const U32 flags)
4894 {
4895     PERL_UNUSED_ARG(lastkey);
4896
4897     if (flags & RXapif_FIRSTKEY)
4898         return reg_named_buff_firstkey(rx, flags);
4899     else if (flags & RXapif_NEXTKEY)
4900         return reg_named_buff_nextkey(rx, flags);
4901     else {
4902         Perl_croak(aTHX_ "panic: Unknown flags %d in named_buff_iter", (int)flags);
4903         return NULL;
4904     }
4905 }
4906
4907 SV*
4908 Perl_reg_named_buff_fetch(pTHX_ REGEXP * const r, SV * const namesv,
4909                           const U32 flags)
4910 {
4911     AV *retarray = NULL;
4912     SV *ret;
4913     struct regexp *const rx = (struct regexp *)SvANY(r);
4914     if (flags & RXapif_ALL)
4915         retarray=newAV();
4916
4917     if (rx && rx->paren_names) {
4918         HE *he_str = hv_fetch_ent( rx->paren_names, namesv, 0, 0 );
4919         if (he_str) {
4920             IV i;
4921             SV* sv_dat=HeVAL(he_str);
4922             I32 *nums=(I32*)SvPVX(sv_dat);
4923             for ( i=0; i<SvIVX(sv_dat); i++ ) {
4924                 if ((I32)(rx->nparens) >= nums[i]
4925                     && rx->offs[nums[i]].start != -1
4926                     && rx->offs[nums[i]].end != -1)
4927                 {
4928                     ret = newSVpvs("");
4929                     CALLREG_NUMBUF_FETCH(r,nums[i],ret);
4930                     if (!retarray)
4931                         return ret;
4932                 } else {
4933                     ret = newSVsv(&PL_sv_undef);
4934                 }
4935                 if (retarray) {
4936                     SvREFCNT_inc_simple_void(ret);
4937                     av_push(retarray, ret);
4938                 }
4939             }
4940             if (retarray)
4941                 return newRV((SV*)retarray);
4942         }
4943     }
4944     return NULL;
4945 }
4946
4947 bool
4948 Perl_reg_named_buff_exists(pTHX_ REGEXP * const r, SV * const key,
4949                            const U32 flags)
4950 {
4951     struct regexp *const rx = (struct regexp *)SvANY(r);
4952     if (rx && rx->paren_names) {
4953         if (flags & RXapif_ALL) {
4954             return hv_exists_ent(rx->paren_names, key, 0);
4955         } else {
4956             SV *sv = CALLREG_NAMED_BUFF_FETCH(r, key, flags);
4957             if (sv) {
4958                 SvREFCNT_dec(sv);
4959                 return TRUE;
4960             } else {
4961                 return FALSE;
4962             }
4963         }
4964     } else {
4965         return FALSE;
4966     }
4967 }
4968
4969 SV*
4970 Perl_reg_named_buff_firstkey(pTHX_ REGEXP * const r, const U32 flags)
4971 {
4972     struct regexp *const rx = (struct regexp *)SvANY(r);
4973     if ( rx && rx->paren_names ) {
4974         (void)hv_iterinit(rx->paren_names);
4975
4976         return CALLREG_NAMED_BUFF_NEXTKEY(r, NULL, flags & ~RXapif_FIRSTKEY);
4977     } else {
4978         return FALSE;
4979     }
4980 }
4981
4982 SV*
4983 Perl_reg_named_buff_nextkey(pTHX_ REGEXP * const r, const U32 flags)
4984 {
4985     struct regexp *const rx = (struct regexp *)SvANY(r);
4986     if (rx && rx->paren_names) {
4987         HV *hv = rx->paren_names;
4988         HE *temphe;
4989         while ( (temphe = hv_iternext_flags(hv,0)) ) {
4990             IV i;
4991             IV parno = 0;
4992             SV* sv_dat = HeVAL(temphe);
4993             I32 *nums = (I32*)SvPVX(sv_dat);
4994             for ( i = 0; i < SvIVX(sv_dat); i++ ) {
4995                 if ((I32)(rx->lastcloseparen) >= nums[i] &&
4996                     rx->offs[nums[i]].start != -1 &&
4997                     rx->offs[nums[i]].end != -1)
4998                 {
4999                     parno = nums[i];
5000                     break;
5001                 }
5002             }
5003             if (parno || flags & RXapif_ALL) {
5004                 return newSVhek(HeKEY_hek(temphe));
5005             }
5006         }
5007     }
5008     return NULL;
5009 }
5010
5011 SV*
5012 Perl_reg_named_buff_scalar(pTHX_ REGEXP * const r, const U32 flags)
5013 {
5014     SV *ret;
5015     AV *av;
5016     I32 length;
5017     struct regexp *const rx = (struct regexp *)SvANY(r);
5018
5019     if (rx && rx->paren_names) {
5020         if (flags & (RXapif_ALL | RXapif_REGNAMES_COUNT)) {
5021             return newSViv(HvTOTALKEYS(rx->paren_names));
5022         } else if (flags & RXapif_ONE) {
5023             ret = CALLREG_NAMED_BUFF_ALL(r, (flags | RXapif_REGNAMES));
5024             av = (AV*)SvRV(ret);
5025             length = av_len(av);
5026             return newSViv(length + 1);
5027         } else {
5028             Perl_croak(aTHX_ "panic: Unknown flags %d in named_buff_scalar", (int)flags);
5029             return NULL;
5030         }
5031     }
5032     return &PL_sv_undef;
5033 }
5034
5035 SV*
5036 Perl_reg_named_buff_all(pTHX_ REGEXP * const r, const U32 flags)
5037 {
5038     struct regexp *const rx = (struct regexp *)SvANY(r);
5039     AV *av = newAV();
5040
5041     if (rx && rx->paren_names) {
5042         HV *hv= rx->paren_names;
5043         HE *temphe;
5044         (void)hv_iterinit(hv);
5045         while ( (temphe = hv_iternext_flags(hv,0)) ) {
5046             IV i;
5047             IV parno = 0;
5048             SV* sv_dat = HeVAL(temphe);
5049             I32 *nums = (I32*)SvPVX(sv_dat);
5050             for ( i = 0; i < SvIVX(sv_dat); i++ ) {
5051                 if ((I32)(rx->lastcloseparen) >= nums[i] &&
5052                     rx->offs[nums[i]].start != -1 &&
5053                     rx->offs[nums[i]].end != -1)
5054                 {
5055                     parno = nums[i];
5056                     break;
5057                 }
5058             }
5059             if (parno || flags & RXapif_ALL) {
5060                 av_push(av, newSVhek(HeKEY_hek(temphe)));
5061             }
5062         }
5063     }
5064
5065     return newRV((SV*)av);
5066 }
5067
5068 void
5069 Perl_reg_numbered_buff_fetch(pTHX_ REGEXP * const r, const I32 paren,
5070                              SV * const sv)
5071 {
5072     struct regexp *const rx = (struct regexp *)SvANY(r);
5073     char *s = NULL;
5074     I32 i = 0;
5075     I32 s1, t1;
5076         
5077     if (!rx->subbeg) {
5078         sv_setsv(sv,&PL_sv_undef);
5079         return;
5080     } 
5081     else               
5082     if (paren == RX_BUFF_IDX_PREMATCH && rx->offs[0].start != -1) {
5083         /* $` */
5084         i = rx->offs[0].start;
5085         s = rx->subbeg;
5086     }
5087     else 
5088     if (paren == RX_BUFF_IDX_POSTMATCH && rx->offs[0].end != -1) {
5089         /* $' */
5090         s = rx->subbeg + rx->offs[0].end;
5091         i = rx->sublen - rx->offs[0].end;
5092     } 
5093     else
5094     if ( 0 <= paren && paren <= (I32)rx->nparens &&
5095         (s1 = rx->offs[paren].start) != -1 &&
5096         (t1 = rx->offs[paren].end) != -1)
5097     {
5098         /* $& $1 ... */
5099         i = t1 - s1;
5100         s = rx->subbeg + s1;
5101     } else {
5102         sv_setsv(sv,&PL_sv_undef);
5103         return;
5104     }          
5105     assert(rx->sublen >= (s - rx->subbeg) + i );
5106     if (i >= 0) {
5107         const int oldtainted = PL_tainted;
5108         TAINT_NOT;
5109         sv_setpvn(sv, s, i);
5110         PL_tainted = oldtainted;
5111         if ( (rx->extflags & RXf_CANY_SEEN)
5112             ? (RXp_MATCH_UTF8(rx)
5113                         && (!i || is_utf8_string((U8*)s, i)))
5114             : (RXp_MATCH_UTF8(rx)) )
5115         {
5116             SvUTF8_on(sv);
5117         }
5118         else
5119             SvUTF8_off(sv);
5120         if (PL_tainting) {
5121             if (RXp_MATCH_TAINTED(rx)) {
5122                 if (SvTYPE(sv) >= SVt_PVMG) {
5123                     MAGIC* const mg = SvMAGIC(sv);
5124                     MAGIC* mgt;
5125                     PL_tainted = 1;
5126                     SvMAGIC_set(sv, mg->mg_moremagic);
5127                     SvTAINT(sv);
5128                     if ((mgt = SvMAGIC(sv))) {
5129                         mg->mg_moremagic = mgt;
5130                         SvMAGIC_set(sv, mg);
5131                     }
5132                 } else {
5133                     PL_tainted = 1;
5134                     SvTAINT(sv);
5135                 }
5136             } else 
5137                 SvTAINTED_off(sv);
5138         }
5139     } else {
5140         sv_setsv(sv,&PL_sv_undef);
5141         return;
5142     }
5143 }
5144
5145 void
5146 Perl_reg_numbered_buff_store(pTHX_ REGEXP * const rx, const I32 paren,
5147                                                          SV const * const value)
5148 {
5149     PERL_UNUSED_ARG(rx);
5150     PERL_UNUSED_ARG(paren);
5151     PERL_UNUSED_ARG(value);
5152
5153     if (!PL_localizing)
5154         Perl_croak(aTHX_ PL_no_modify);
5155 }
5156
5157 I32
5158 Perl_reg_numbered_buff_length(pTHX_ REGEXP * const r, const SV * const sv,
5159                               const I32 paren)
5160 {
5161     struct regexp *const rx = (struct regexp *)SvANY(r);
5162     I32 i;
5163     I32 s1, t1;
5164
5165     /* Some of this code was originally in C<Perl_magic_len> in F<mg.c> */
5166         switch (paren) {
5167       /* $` / ${^PREMATCH} */
5168       case RX_BUFF_IDX_PREMATCH:
5169         if (rx->offs[0].start != -1) {
5170                         i = rx->offs[0].start;
5171                         if (i > 0) {
5172                                 s1 = 0;
5173                                 t1 = i;
5174                                 goto getlen;
5175                         }
5176             }
5177         return 0;
5178       /* $' / ${^POSTMATCH} */
5179       case RX_BUFF_IDX_POSTMATCH:
5180             if (rx->offs[0].end != -1) {
5181                         i = rx->sublen - rx->offs[0].end;
5182                         if (i > 0) {
5183                                 s1 = rx->offs[0].end;
5184                                 t1 = rx->sublen;
5185                                 goto getlen;
5186                         }
5187             }
5188         return 0;
5189       /* $& / ${^MATCH}, $1, $2, ... */
5190       default:
5191             if (paren <= (I32)rx->nparens &&
5192             (s1 = rx->offs[paren].start) != -1 &&
5193             (t1 = rx->offs[paren].end) != -1)
5194             {
5195             i = t1 - s1;
5196             goto getlen;
5197         } else {
5198             if (ckWARN(WARN_UNINITIALIZED))
5199                 report_uninit((SV*)sv);
5200             return 0;
5201         }
5202     }
5203   getlen:
5204     if (i > 0 && RXp_MATCH_UTF8(rx)) {
5205         const char * const s = rx->subbeg + s1;
5206         const U8 *ep;
5207         STRLEN el;
5208
5209         i = t1 - s1;
5210         if (is_utf8_string_loclen((U8*)s, i, &ep, &el))
5211                         i = el;
5212     }
5213     return i;
5214 }
5215
5216 SV*
5217 Perl_reg_qr_package(pTHX_ REGEXP * const rx)
5218 {
5219         PERL_UNUSED_ARG(rx);
5220         return NULL;
5221 }
5222
5223 /* Scans the name of a named buffer from the pattern.
5224  * If flags is REG_RSN_RETURN_NULL returns null.
5225  * If flags is REG_RSN_RETURN_NAME returns an SV* containing the name
5226  * If flags is REG_RSN_RETURN_DATA returns the data SV* corresponding
5227  * to the parsed name as looked up in the RExC_paren_names hash.
5228  * If there is an error throws a vFAIL().. type exception.
5229  */
5230
5231 #define REG_RSN_RETURN_NULL    0
5232 #define REG_RSN_RETURN_NAME    1
5233 #define REG_RSN_RETURN_DATA    2
5234
5235 STATIC SV*
5236 S_reg_scan_name(pTHX_ RExC_state_t *pRExC_state, U32 flags) {
5237     char *name_start = RExC_parse;
5238
5239     if (isIDFIRST_lazy_if(RExC_parse, UTF)) {
5240          /* skip IDFIRST by using do...while */
5241         if (UTF)
5242             do {
5243                 RExC_parse += UTF8SKIP(RExC_parse);
5244             } while (isALNUM_utf8((U8*)RExC_parse));
5245         else
5246             do {
5247                 RExC_parse++;
5248             } while (isALNUM(*RExC_parse));
5249     }
5250
5251     if ( flags ) {
5252         SV* sv_name = sv_2mortal(newSVpvn_utf8(name_start,
5253             (int)(RExC_parse - name_start), UTF));
5254         if ( flags == REG_RSN_RETURN_NAME)
5255             return sv_name;
5256         else if (flags==REG_RSN_RETURN_DATA) {
5257             HE *he_str = NULL;
5258             SV *sv_dat = NULL;
5259             if ( ! sv_name )      /* should not happen*/
5260                 Perl_croak(aTHX_ "panic: no svname in reg_scan_name");
5261             if (RExC_paren_names)
5262                 he_str = hv_fetch_ent( RExC_paren_names, sv_name, 0, 0 );
5263             if ( he_str )
5264                 sv_dat = HeVAL(he_str);
5265             if ( ! sv_dat )
5266                 vFAIL("Reference to nonexistent named group");
5267             return sv_dat;
5268         }
5269         else {
5270             Perl_croak(aTHX_ "panic: bad flag in reg_scan_name");
5271         }
5272         /* NOT REACHED */
5273     }
5274     return NULL;
5275 }
5276
5277 #define DEBUG_PARSE_MSG(funcname)     DEBUG_PARSE_r({           \
5278     int rem=(int)(RExC_end - RExC_parse);                       \
5279     int cut;                                                    \
5280     int num;                                                    \
5281     int iscut=0;                                                \
5282     if (rem>10) {                                               \
5283         rem=10;                                                 \
5284         iscut=1;                                                \
5285     }                                                           \
5286     cut=10-rem;                                                 \
5287     if (RExC_lastparse!=RExC_parse)                             \
5288         PerlIO_printf(Perl_debug_log," >%.*s%-*s",              \
5289             rem, RExC_parse,                                    \
5290             cut + 4,                                            \
5291             iscut ? "..." : "<"                                 \
5292         );                                                      \
5293     else                                                        \
5294         PerlIO_printf(Perl_debug_log,"%16s","");                \
5295                                                                 \
5296     if (SIZE_ONLY)                                              \
5297        num = RExC_size + 1;                                     \
5298     else                                                        \
5299        num=REG_NODE_NUM(RExC_emit);                             \
5300     if (RExC_lastnum!=num)                                      \
5301        PerlIO_printf(Perl_debug_log,"|%4d",num);                \
5302     else                                                        \
5303        PerlIO_printf(Perl_debug_log,"|%4s","");                 \
5304     PerlIO_printf(Perl_debug_log,"|%*s%-4s",                    \
5305         (int)((depth*2)), "",                                   \
5306         (funcname)                                              \
5307     );                                                          \
5308     RExC_lastnum=num;                                           \
5309     RExC_lastparse=RExC_parse;                                  \
5310 })
5311
5312
5313
5314 #define DEBUG_PARSE(funcname)     DEBUG_PARSE_r({           \
5315     DEBUG_PARSE_MSG((funcname));                            \
5316     PerlIO_printf(Perl_debug_log,"%4s","\n");               \
5317 })
5318 #define DEBUG_PARSE_FMT(funcname,fmt,args)     DEBUG_PARSE_r({           \
5319     DEBUG_PARSE_MSG((funcname));                            \
5320     PerlIO_printf(Perl_debug_log,fmt "\n",args);               \
5321 })
5322 /*
5323  - reg - regular expression, i.e. main body or parenthesized thing
5324  *
5325  * Caller must absorb opening parenthesis.
5326  *
5327  * Combining parenthesis handling with the base level of regular expression
5328  * is a trifle forced, but the need to tie the tails of the branches to what
5329  * follows makes it hard to avoid.
5330  */
5331 #define REGTAIL(x,y,z) regtail((x),(y),(z),depth+1)
5332 #ifdef DEBUGGING
5333 #define REGTAIL_STUDY(x,y,z) regtail_study((x),(y),(z),depth+1)
5334 #else
5335 #define REGTAIL_STUDY(x,y,z) regtail((x),(y),(z),depth+1)
5336 #endif
5337
5338 STATIC regnode *
5339 S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp,U32 depth)
5340     /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
5341 {
5342     dVAR;
5343     register regnode *ret;              /* Will be the head of the group. */
5344     register regnode *br;
5345     register regnode *lastbr;
5346     register regnode *ender = NULL;
5347     register I32 parno = 0;
5348     I32 flags;
5349     U32 oregflags = RExC_flags;
5350     bool have_branch = 0;
5351     bool is_open = 0;
5352     I32 freeze_paren = 0;
5353     I32 after_freeze = 0;
5354
5355     /* for (?g), (?gc), and (?o) warnings; warning
5356        about (?c) will warn about (?g) -- japhy    */
5357
5358 #define WASTED_O  0x01
5359 #define WASTED_G  0x02
5360 #define WASTED_C  0x04
5361 #define WASTED_GC (0x02|0x04)
5362     I32 wastedflags = 0x00;
5363
5364     char * parse_start = RExC_parse; /* MJD */
5365     char * const oregcomp_parse = RExC_parse;
5366
5367     GET_RE_DEBUG_FLAGS_DECL;
5368     DEBUG_PARSE("reg ");
5369
5370     *flagp = 0;                         /* Tentatively. */
5371
5372
5373     /* Make an OPEN node, if parenthesized. */
5374     if (paren) {
5375         if ( *RExC_parse == '*') { /* (*VERB:ARG) */
5376             char *start_verb = RExC_parse;
5377             STRLEN verb_len = 0;
5378             char *start_arg = NULL;
5379             unsigned char op = 0;
5380             int argok = 1;
5381             int internal_argval = 0; /* internal_argval is only useful if !argok */
5382             while ( *RExC_parse && *RExC_parse != ')' ) {
5383                 if ( *RExC_parse == ':' ) {
5384                     start_arg = RExC_parse + 1;
5385                     break;
5386                 }
5387                 RExC_parse++;
5388             }
5389             ++start_verb;
5390             verb_len = RExC_parse - start_verb;
5391             if ( start_arg ) {
5392                 RExC_parse++;
5393                 while ( *RExC_parse && *RExC_parse != ')' ) 
5394                     RExC_parse++;
5395                 if ( *RExC_parse != ')' ) 
5396                     vFAIL("Unterminated verb pattern argument");
5397                 if ( RExC_parse == start_arg )
5398                     start_arg = NULL;
5399             } else {
5400                 if ( *RExC_parse != ')' )
5401                     vFAIL("Unterminated verb pattern");
5402             }
5403             
5404             switch ( *start_verb ) {
5405             case 'A':  /* (*ACCEPT) */
5406                 if ( memEQs(start_verb,verb_len,"ACCEPT") ) {
5407                     op = ACCEPT;
5408                     internal_argval = RExC_nestroot;
5409                 }
5410                 break;
5411             case 'C':  /* (*COMMIT) */
5412                 if ( memEQs(start_verb,verb_len,"COMMIT") )
5413                     op = COMMIT;
5414                 break;
5415             case 'F':  /* (*FAIL) */
5416                 if ( verb_len==1 || memEQs(start_verb,verb_len,"FAIL") ) {
5417                     op = OPFAIL;
5418                     argok = 0;
5419                 }
5420                 break;
5421             case ':':  /* (*:NAME) */
5422             case 'M':  /* (*MARK:NAME) */
5423                 if ( verb_len==0 || memEQs(start_verb,verb_len,"MARK") ) {
5424                     op = MARKPOINT;
5425                     argok = -1;
5426                 }
5427                 break;
5428             case 'P':  /* (*PRUNE) */
5429                 if ( memEQs(start_verb,verb_len,"PRUNE") )
5430                     op = PRUNE;
5431                 break;
5432             case 'S':   /* (*SKIP) */  
5433                 if ( memEQs(start_verb,verb_len,"SKIP") ) 
5434                     op = SKIP;
5435                 break;
5436             case 'T':  /* (*THEN) */
5437                 /* [19:06] <TimToady> :: is then */
5438                 if ( memEQs(start_verb,verb_len,"THEN") ) {
5439                     op = CUTGROUP;
5440                     RExC_seen |= REG_SEEN_CUTGROUP;
5441                 }
5442                 break;
5443             }
5444             if ( ! op ) {
5445                 RExC_parse++;
5446                 vFAIL3("Unknown verb pattern '%.*s'",
5447                     verb_len, start_verb);
5448             }
5449             if ( argok ) {
5450                 if ( start_arg && internal_argval ) {
5451                     vFAIL3("Verb pattern '%.*s' may not have an argument",
5452                         verb_len, start_verb); 
5453                 } else if ( argok < 0 && !start_arg ) {
5454                     vFAIL3("Verb pattern '%.*s' has a mandatory argument",
5455                         verb_len, start_verb);    
5456                 } else {
5457                     ret = reganode(pRExC_state, op, internal_argval);
5458                     if ( ! internal_argval && ! SIZE_ONLY ) {
5459                         if (start_arg) {
5460                             SV *sv = newSVpvn( start_arg, RExC_parse - start_arg);
5461                             ARG(ret) = add_data( pRExC_state, 1, "S" );
5462                             RExC_rxi->data->data[ARG(ret)]=(void*)sv;
5463                             ret->flags = 0;
5464                         } else {
5465                             ret->flags = 1; 
5466                         }
5467                     }               
5468                 }
5469                 if (!internal_argval)
5470                     RExC_seen |= REG_SEEN_VERBARG;
5471             } else if ( start_arg ) {
5472                 vFAIL3("Verb pattern '%.*s' may not have an argument",
5473                         verb_len, start_verb);    
5474             } else {
5475                 ret = reg_node(pRExC_state, op);
5476             }
5477             nextchar(pRExC_state);
5478             return ret;
5479         } else 
5480         if (*RExC_parse == '?') { /* (?...) */
5481             bool is_logical = 0;
5482             const char * const seqstart = RExC_parse;
5483
5484             RExC_parse++;
5485             paren = *RExC_parse++;
5486             ret = NULL;                 /* For look-ahead/behind. */
5487             switch (paren) {
5488
5489             case 'P':   /* (?P...) variants for those used to PCRE/Python */
5490                 paren = *RExC_parse++;
5491                 if ( paren == '<')         /* (?P<...>) named capture */
5492                     goto named_capture;
5493                 else if (paren == '>') {   /* (?P>name) named recursion */
5494                     goto named_recursion;
5495                 }
5496                 else if (paren == '=') {   /* (?P=...)  named backref */
5497                     /* this pretty much dupes the code for \k<NAME> in regatom(), if
5498                        you change this make sure you change that */
5499                     char* name_start = RExC_parse;
5500                     U32 num = 0;
5501                     SV *sv_dat = reg_scan_name(pRExC_state,
5502                         SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
5503                     if (RExC_parse == name_start || *RExC_parse != ')')
5504                         vFAIL2("Sequence %.3s... not terminated",parse_start);
5505
5506                     if (!SIZE_ONLY) {
5507                         num = add_data( pRExC_state, 1, "S" );
5508                         RExC_rxi->data->data[num]=(void*)sv_dat;
5509                         SvREFCNT_inc_simple_void(sv_dat);
5510                     }
5511                     RExC_sawback = 1;
5512                     ret = reganode(pRExC_state,
5513                            (U8)(FOLD ? (LOC ? NREFFL : NREFF) : NREF),
5514                            num);
5515                     *flagp |= HASWIDTH;
5516
5517                     Set_Node_Offset(ret, parse_start+1);
5518                     Set_Node_Cur_Length(ret); /* MJD */
5519
5520                     nextchar(pRExC_state);
5521                     return ret;
5522                 }
5523                 RExC_parse++;
5524                 vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
5525                 /*NOTREACHED*/
5526             case '<':           /* (?<...) */
5527                 if (*RExC_parse == '!')
5528                     paren = ',';
5529                 else if (*RExC_parse != '=') 
5530               named_capture:
5531                 {               /* (?<...>) */
5532                     char *name_start;
5533                     SV *svname;
5534                     paren= '>';
5535             case '\'':          /* (?'...') */
5536                     name_start= RExC_parse;
5537                     svname = reg_scan_name(pRExC_state,
5538                         SIZE_ONLY ?  /* reverse test from the others */
5539                         REG_RSN_RETURN_NAME : 
5540                         REG_RSN_RETURN_NULL);
5541                     if (RExC_parse == name_start) {
5542                         RExC_parse++;
5543                         vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
5544                         /*NOTREACHED*/
5545                     }
5546                     if (*RExC_parse != paren)
5547                         vFAIL2("Sequence (?%c... not terminated",
5548                             paren=='>' ? '<' : paren);
5549                     if (SIZE_ONLY) {
5550                         HE *he_str;
5551                         SV *sv_dat = NULL;
5552                         if (!svname) /* shouldnt happen */
5553                             Perl_croak(aTHX_
5554                                 "panic: reg_scan_name returned NULL");
5555                         if (!RExC_paren_names) {
5556                             RExC_paren_names= newHV();
5557                             sv_2mortal((SV*)RExC_paren_names);
5558 #ifdef DEBUGGING
5559                             RExC_paren_name_list= newAV();
5560                             sv_2mortal((SV*)RExC_paren_name_list);
5561 #endif
5562                         }
5563                         he_str = hv_fetch_ent( RExC_paren_names, svname, 1, 0 );
5564                         if ( he_str )
5565                             sv_dat = HeVAL(he_str);
5566                         if ( ! sv_dat ) {
5567                             /* croak baby croak */
5568                             Perl_croak(aTHX_
5569                                 "panic: paren_name hash element allocation failed");
5570                         } else if ( SvPOK(sv_dat) ) {
5571                             /* (?|...) can mean we have dupes so scan to check
5572                                its already been stored. Maybe a flag indicating
5573                                we are inside such a construct would be useful,
5574                                but the arrays are likely to be quite small, so
5575                                for now we punt -- dmq */
5576                             IV count = SvIV(sv_dat);
5577                             I32 *pv = (I32*)SvPVX(sv_dat);
5578                             IV i;
5579                             for ( i = 0 ; i < count ; i++ ) {
5580                                 if ( pv[i] == RExC_npar ) {
5581                                     count = 0;
5582                                     break;
5583                                 }
5584                             }
5585                             if ( count ) {
5586                                 pv = (I32*)SvGROW(sv_dat, SvCUR(sv_dat) + sizeof(I32)+1);
5587                                 SvCUR_set(sv_dat, SvCUR(sv_dat) + sizeof(I32));
5588                                 pv[count] = RExC_npar;
5589                                 SvIVX(sv_dat)++;
5590                             }
5591                         } else {
5592                             (void)SvUPGRADE(sv_dat,SVt_PVNV);
5593                             sv_setpvn(sv_dat, (char *)&(RExC_npar), sizeof(I32));
5594                             SvIOK_on(sv_dat);
5595                             SvIVX(sv_dat)= 1;
5596                         }
5597 #ifdef DEBUGGING
5598                         if (!av_store(RExC_paren_name_list, RExC_npar, SvREFCNT_inc(svname)))
5599                             SvREFCNT_dec(svname);
5600 #endif
5601
5602                         /*sv_dump(sv_dat);*/
5603                     }
5604                     nextchar(pRExC_state);
5605                     paren = 1;
5606                     goto capturing_parens;
5607                 }
5608                 RExC_seen |= REG_SEEN_LOOKBEHIND;
5609                 RExC_parse++;
5610             case '=':           /* (?=...) */
5611             case '!':           /* (?!...) */
5612                 RExC_seen_zerolen++;
5613                 if (*RExC_parse == ')') {
5614                     ret=reg_node(pRExC_state, OPFAIL);
5615                     nextchar(pRExC_state);
5616                     return ret;
5617                 }
5618                 break;
5619             case '|':           /* (?|...) */
5620                 /* branch reset, behave like a (?:...) except that
5621                    buffers in alternations share the same numbers */
5622                 paren = ':'; 
5623                 after_freeze = freeze_paren = RExC_npar;
5624                 break;
5625             case ':':           /* (?:...) */
5626             case '>':           /* (?>...) */
5627                 break;
5628             case '$':           /* (?$...) */
5629             case '@':           /* (?@...) */
5630                 vFAIL2("Sequence (?%c...) not implemented", (int)paren);
5631                 break;
5632             case '#':           /* (?#...) */
5633                 while (*RExC_parse && *RExC_parse != ')')
5634                     RExC_parse++;
5635                 if (*RExC_parse != ')')
5636                     FAIL("Sequence (?#... not terminated");
5637                 nextchar(pRExC_state);
5638                 *flagp = TRYAGAIN;
5639                 return NULL;
5640             case '0' :           /* (?0) */
5641             case 'R' :           /* (?R) */
5642                 if (*RExC_parse != ')')
5643                     FAIL("Sequence (?R) not terminated");
5644                 ret = reg_node(pRExC_state, GOSTART);
5645                 *flagp |= POSTPONED;
5646                 nextchar(pRExC_state);
5647                 return ret;
5648                 /*notreached*/
5649             { /* named and numeric backreferences */
5650                 I32 num;
5651             case '&':            /* (?&NAME) */
5652                 parse_start = RExC_parse - 1;
5653               named_recursion:
5654                 {
5655                     SV *sv_dat = reg_scan_name(pRExC_state,
5656                         SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
5657                      num = sv_dat ? *((I32 *)SvPVX(sv_dat)) : 0;
5658                 }
5659                 goto gen_recurse_regop;
5660                 /* NOT REACHED */
5661             case '+':
5662                 if (!(RExC_parse[0] >= '1' && RExC_parse[0] <= '9')) {
5663                     RExC_parse++;
5664                     vFAIL("Illegal pattern");
5665                 }
5666                 goto parse_recursion;
5667                 /* NOT REACHED*/
5668             case '-': /* (?-1) */
5669                 if (!(RExC_parse[0] >= '1' && RExC_parse[0] <= '9')) {
5670                     RExC_parse--; /* rewind to let it be handled later */
5671                     goto parse_flags;
5672                 } 
5673                 /*FALLTHROUGH */
5674             case '1': case '2': case '3': case '4': /* (?1) */
5675             case '5': case '6': case '7': case '8': case '9':
5676                 RExC_parse--;
5677               parse_recursion:
5678                 num = atoi(RExC_parse);
5679                 parse_start = RExC_parse - 1; /* MJD */
5680                 if (*RExC_parse == '-')
5681                     RExC_parse++;
5682                 while (isDIGIT(*RExC_parse))
5683                         RExC_parse++;
5684                 if (*RExC_parse!=')') 
5685                     vFAIL("Expecting close bracket");
5686                         
5687               gen_recurse_regop:
5688                 if ( paren == '-' ) {
5689                     /*
5690                     Diagram of capture buffer numbering.
5691                     Top line is the normal capture buffer numbers
5692                     Botton line is the negative indexing as from
5693                     the X (the (?-2))
5694
5695                     +   1 2    3 4 5 X          6 7
5696                        /(a(x)y)(a(b(c(?-2)d)e)f)(g(h))/
5697                     -   5 4    3 2 1 X          x x
5698
5699                     */
5700                     num = RExC_npar + num;
5701                     if (num < 1)  {
5702                         RExC_parse++;
5703                         vFAIL("Reference to nonexistent group");
5704                     }
5705                 } else if ( paren == '+' ) {
5706                     num = RExC_npar + num - 1;
5707                 }
5708
5709                 ret = reganode(pRExC_state, GOSUB, num);
5710                 if (!SIZE_ONLY) {
5711                     if (num > (I32)RExC_rx->nparens) {
5712                         RExC_parse++;
5713                         vFAIL("Reference to nonexistent group");
5714                     }
5715                     ARG2L_SET( ret, RExC_recurse_count++);
5716                     RExC_emit++;
5717                     DEBUG_OPTIMISE_MORE_r(PerlIO_printf(Perl_debug_log,
5718                         "Recurse #%"UVuf" to %"IVdf"\n", (UV)ARG(ret), (IV)ARG2L(ret)));
5719                 } else {
5720                     RExC_size++;
5721                 }
5722                 RExC_seen |= REG_SEEN_RECURSE;
5723                 Set_Node_Length(ret, 1 + regarglen[OP(ret)]); /* MJD */
5724                 Set_Node_Offset(ret, parse_start); /* MJD */
5725
5726                 *flagp |= POSTPONED;
5727                 nextchar(pRExC_state);
5728                 return ret;
5729             } /* named and numeric backreferences */
5730             /* NOT REACHED */
5731
5732             case '?':           /* (??...) */
5733                 is_logical = 1;
5734                 if (*RExC_parse != '{') {
5735                     RExC_parse++;
5736                     vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
5737                     /*NOTREACHED*/
5738                 }
5739                 *flagp |= POSTPONED;
5740                 paren = *RExC_parse++;
5741                 /* FALL THROUGH */
5742             case '{':           /* (?{...}) */
5743             {
5744                 I32 count = 1;
5745                 U32 n = 0;
5746                 char c;
5747                 char *s = RExC_parse;
5748
5749                 RExC_seen_zerolen++;
5750                 RExC_seen |= REG_SEEN_EVAL;
5751                 while (count && (c = *RExC_parse)) {
5752                     if (c == '\\') {
5753                         if (RExC_parse[1])
5754                             RExC_parse++;
5755                     }
5756                     else if (c == '{')
5757                         count++;
5758                     else if (c == '}')
5759                         count--;
5760                     RExC_parse++;
5761                 }
5762                 if (*RExC_parse != ')') {
5763                     RExC_parse = s;             
5764                     vFAIL("Sequence (?{...}) not terminated or not {}-balanced");
5765                 }
5766                 if (!SIZE_ONLY) {
5767                     PAD *pad;
5768                     OP_4tree *sop, *rop;
5769                     SV * const sv = newSVpvn(s, RExC_parse - 1 - s);
5770
5771                     ENTER;
5772                     Perl_save_re_context(aTHX);
5773                     rop = sv_compile_2op(sv, &sop, "re", &pad);
5774                     sop->op_private |= OPpREFCOUNTED;
5775                     /* re_dup will OpREFCNT_inc */
5776                     OpREFCNT_set(sop, 1);
5777                     LEAVE;
5778
5779                     n = add_data(pRExC_state, 3, "nop");
5780                     RExC_rxi->data->data[n] = (void*)rop;
5781                     RExC_rxi->data->data[n+1] = (void*)sop;
5782                     RExC_rxi->data->data[n+2] = (void*)pad;
5783                     SvREFCNT_dec(sv);
5784                 }
5785                 else {                                          /* First pass */
5786                     if (PL_reginterp_cnt < ++RExC_seen_evals
5787                         && IN_PERL_RUNTIME)
5788                         /* No compiled RE interpolated, has runtime
5789                            components ===> unsafe.  */
5790                         FAIL("Eval-group not allowed at runtime, use re 'eval'");
5791                     if (PL_tainting && PL_tainted)
5792                         FAIL("Eval-group in insecure regular expression");
5793 #if PERL_VERSION > 8
5794                     if (IN_PERL_COMPILETIME)
5795                         PL_cv_has_eval = 1;
5796 #endif
5797                 }
5798
5799                 nextchar(pRExC_state);
5800                 if (is_logical) {
5801                     ret = reg_node(pRExC_state, LOGICAL);
5802                     if (!SIZE_ONLY)
5803                         ret->flags = 2;
5804                     REGTAIL(pRExC_state, ret, reganode(pRExC_state, EVAL, n));
5805                     /* deal with the length of this later - MJD */
5806                     return ret;
5807                 }
5808                 ret = reganode(pRExC_state, EVAL, n);
5809                 Set_Node_Length(ret, RExC_parse - parse_start + 1);
5810                 Set_Node_Offset(ret, parse_start);
5811                 return ret;
5812             }
5813             case '(':           /* (?(?{...})...) and (?(?=...)...) */
5814             {
5815                 int is_define= 0;
5816                 if (RExC_parse[0] == '?') {        /* (?(?...)) */
5817                     if (RExC_parse[1] == '=' || RExC_parse[1] == '!'
5818                         || RExC_parse[1] == '<'
5819                         || RExC_parse[1] == '{') { /* Lookahead or eval. */
5820                         I32 flag;
5821                         
5822                         ret = reg_node(pRExC_state, LOGICAL);
5823                         if (!SIZE_ONLY)
5824                             ret->flags = 1;
5825                         REGTAIL(pRExC_state, ret, reg(pRExC_state, 1, &flag,depth+1));
5826                         goto insert_if;
5827                     }
5828                 }
5829                 else if ( RExC_parse[0] == '<'     /* (?(<NAME>)...) */
5830                          || RExC_parse[0] == '\'' ) /* (?('NAME')...) */
5831                 {
5832                     char ch = RExC_parse[0] == '<' ? '>' : '\'';
5833                     char *name_start= RExC_parse++;
5834                     U32 num = 0;
5835                     SV *sv_dat=reg_scan_name(pRExC_state,
5836                         SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
5837                     if (RExC_parse == name_start || *RExC_parse != ch)
5838                         vFAIL2("Sequence (?(%c... not terminated",
5839                             (ch == '>' ? '<' : ch));
5840                     RExC_parse++;
5841                     if (!SIZE_ONLY) {
5842                         num = add_data( pRExC_state, 1, "S" );
5843                         RExC_rxi->data->data[num]=(void*)sv_dat;
5844                         SvREFCNT_inc_simple_void(sv_dat);
5845                     }
5846                     ret = reganode(pRExC_state,NGROUPP,num);
5847                     goto insert_if_check_paren;
5848                 }
5849                 else if (RExC_parse[0] == 'D' &&
5850                          RExC_parse[1] == 'E' &&
5851                          RExC_parse[2] == 'F' &&
5852                          RExC_parse[3] == 'I' &&
5853                          RExC_parse[4] == 'N' &&
5854                          RExC_parse[5] == 'E')
5855                 {
5856                     ret = reganode(pRExC_state,DEFINEP,0);
5857                     RExC_parse +=6 ;
5858                     is_define = 1;
5859                     goto insert_if_check_paren;
5860                 }
5861                 else if (RExC_parse[0] == 'R') {
5862                     RExC_parse++;
5863                     parno = 0;
5864                     if (RExC_parse[0] >= '1' && RExC_parse[0] <= '9' ) {
5865                         parno = atoi(RExC_parse++);
5866                         while (isDIGIT(*RExC_parse))
5867                             RExC_parse++;
5868                     } else if (RExC_parse[0] == '&') {
5869                         SV *sv_dat;
5870                         RExC_parse++;
5871                         sv_dat = reg_scan_name(pRExC_state,
5872                             SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
5873                         parno = sv_dat ? *((I32 *)SvPVX(sv_dat)) : 0;
5874                     }
5875                     ret = reganode(pRExC_state,INSUBP,parno); 
5876                     goto insert_if_check_paren;
5877                 }
5878                 else if (RExC_parse[0] >= '1' && RExC_parse[0] <= '9' ) {
5879                     /* (?(1)...) */
5880                     char c;
5881                     parno = atoi(RExC_parse++);
5882
5883                     while (isDIGIT(*RExC_parse))
5884                         RExC_parse++;
5885                     ret = reganode(pRExC_state, GROUPP, parno);
5886
5887                  insert_if_check_paren:
5888                     if ((c = *nextchar(pRExC_state)) != ')')
5889                         vFAIL("Switch condition not recognized");
5890                   insert_if:
5891                     REGTAIL(pRExC_state, ret, reganode(pRExC_state, IFTHEN, 0));
5892                     br = regbranch(pRExC_state, &flags, 1,depth+1);
5893                     if (br == NULL)
5894                         br = reganode(pRExC_state, LONGJMP, 0);
5895                     else
5896                         REGTAIL(pRExC_state, br, reganode(pRExC_state, LONGJMP, 0));
5897                     c = *nextchar(pRExC_state);
5898                     if (flags&HASWIDTH)
5899                         *flagp |= HASWIDTH;
5900                     if (c == '|') {
5901                         if (is_define) 
5902                             vFAIL("(?(DEFINE)....) does not allow branches");
5903                         lastbr = reganode(pRExC_state, IFTHEN, 0); /* Fake one for optimizer. */
5904                         regbranch(pRExC_state, &flags, 1,depth+1);
5905                         REGTAIL(pRExC_state, ret, lastbr);
5906                         if (flags&HASWIDTH)
5907                             *flagp |= HASWIDTH;
5908                         c = *nextchar(pRExC_state);
5909                     }
5910                     else
5911                         lastbr = NULL;
5912                     if (c != ')')
5913                         vFAIL("Switch (?(condition)... contains too many branches");
5914                     ender = reg_node(pRExC_state, TAIL);
5915                     REGTAIL(pRExC_state, br, ender);
5916                     if (lastbr) {
5917                         REGTAIL(pRExC_state, lastbr, ender);
5918                         REGTAIL(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender);
5919                     }
5920                     else
5921                         REGTAIL(pRExC_state, ret, ender);
5922                     RExC_size++; /* XXX WHY do we need this?!!
5923                                     For large programs it seems to be required
5924                                     but I can't figure out why. -- dmq*/
5925                     return ret;
5926                 }
5927                 else {
5928                     vFAIL2("Unknown switch condition (?(%.2s", RExC_parse);
5929                 }
5930             }
5931             case 0:
5932                 RExC_parse--; /* for vFAIL to print correctly */
5933                 vFAIL("Sequence (? incomplete");
5934                 break;
5935             default:
5936                 --RExC_parse;
5937                 parse_flags:      /* (?i) */  
5938             {
5939                 U32 posflags = 0, negflags = 0;
5940                 U32 *flagsp = &posflags;
5941
5942                 while (*RExC_parse) {
5943                     /* && strchr("iogcmsx", *RExC_parse) */
5944                     /* (?g), (?gc) and (?o) are useless here
5945                        and must be globally applied -- japhy */
5946                     switch (*RExC_parse) {
5947                     CASE_STD_PMMOD_FLAGS_PARSE_SET(flagsp);
5948                     case ONCE_PAT_MOD: /* 'o' */
5949                     case GLOBAL_PAT_MOD: /* 'g' */
5950                         if (SIZE_ONLY && ckWARN(WARN_REGEXP)) {
5951                             const I32 wflagbit = *RExC_parse == 'o' ? WASTED_O : WASTED_G;
5952                             if (! (wastedflags & wflagbit) ) {
5953                                 wastedflags |= wflagbit;
5954                                 vWARN5(
5955                                     RExC_parse + 1,
5956                                     "Useless (%s%c) - %suse /%c modifier",
5957                                     flagsp == &negflags ? "?-" : "?",
5958                                     *RExC_parse,
5959                                     flagsp == &negflags ? "don't " : "",
5960                                     *RExC_parse
5961                                 );
5962                             }
5963                         }
5964                         break;
5965                         
5966                     case CONTINUE_PAT_MOD: /* 'c' */
5967                         if (SIZE_ONLY && ckWARN(WARN_REGEXP)) {
5968                             if (! (wastedflags & WASTED_C) ) {
5969                                 wastedflags |= WASTED_GC;
5970                                 vWARN3(
5971                                     RExC_parse + 1,
5972                                     "Useless (%sc) - %suse /gc modifier",
5973                                     flagsp == &negflags ? "?-" : "?",
5974                                     flagsp == &negflags ? "don't " : ""
5975                                 );
5976                             }
5977                         }
5978                         break;
5979                     case KEEPCOPY_PAT_MOD: /* 'p' */
5980                         if (flagsp == &negflags) {
5981                             if (SIZE_ONLY && ckWARN(WARN_REGEXP))
5982                                 vWARN(RExC_parse + 1,"Useless use of (?-p)");
5983                         } else {
5984                             *flagsp |= RXf_PMf_KEEPCOPY;
5985                         }
5986                         break;
5987                     case '-':
5988                         if (flagsp == &negflags) {
5989                             RExC_parse++;
5990                             vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
5991                             /*NOTREACHED*/
5992                         }
5993                         flagsp = &negflags;
5994                         wastedflags = 0;  /* reset so (?g-c) warns twice */
5995                         break;
5996                     case ':':
5997                         paren = ':';
5998                         /*FALLTHROUGH*/
5999                     case ')':
6000                         RExC_flags |= posflags;
6001                         RExC_flags &= ~negflags;
6002                         if (paren != ':') {
6003                             oregflags |= posflags;
6004                             oregflags &= ~negflags;
6005                         }
6006                         nextchar(pRExC_state);
6007                         if (paren != ':') {
6008                             *flagp = TRYAGAIN;
6009                             return NULL;
6010                         } else {
6011                             ret = NULL;
6012                             goto parse_rest;
6013                         }
6014                         /*NOTREACHED*/
6015                     default:
6016                         RExC_parse++;
6017                         vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
6018                         /*NOTREACHED*/
6019                     }                           
6020                     ++RExC_parse;
6021                 }
6022             }} /* one for the default block, one for the switch */
6023         }
6024         else {                  /* (...) */
6025           capturing_parens:
6026             parno = RExC_npar;
6027             RExC_npar++;
6028             
6029             ret = reganode(pRExC_state, OPEN, parno);
6030             if (!SIZE_ONLY ){
6031                 if (!RExC_nestroot) 
6032                     RExC_nestroot = parno;
6033                 if (RExC_seen & REG_SEEN_RECURSE
6034                     && !RExC_open_parens[parno-1])
6035                 {
6036                     DEBUG_OPTIMISE_MORE_r(PerlIO_printf(Perl_debug_log,
6037                         "Setting open paren #%"IVdf" to %d\n", 
6038                         (IV)parno, REG_NODE_NUM(ret)));
6039                     RExC_open_parens[parno-1]= ret;
6040                 }
6041             }
6042             Set_Node_Length(ret, 1); /* MJD */
6043             Set_Node_Offset(ret, RExC_parse); /* MJD */
6044             is_open = 1;
6045         }
6046     }
6047     else                        /* ! paren */
6048         ret = NULL;
6049    
6050    parse_rest:
6051     /* Pick up the branches, linking them together. */
6052     parse_start = RExC_parse;   /* MJD */
6053     br = regbranch(pRExC_state, &flags, 1,depth+1);
6054     /*     branch_len = (paren != 0); */
6055
6056     if (br == NULL)
6057         return(NULL);
6058     if (*RExC_parse == '|') {
6059         if (!SIZE_ONLY && RExC_extralen) {
6060             reginsert(pRExC_state, BRANCHJ, br, depth+1);
6061         }
6062         else {                  /* MJD */
6063             reginsert(pRExC_state, BRANCH, br, depth+1);
6064             Set_Node_Length(br, paren != 0);
6065             Set_Node_Offset_To_R(br-RExC_emit_start, parse_start-RExC_start);
6066         }
6067         have_branch = 1;
6068         if (SIZE_ONLY)
6069             RExC_extralen += 1;         /* For BRANCHJ-BRANCH. */
6070     }
6071     else if (paren == ':') {
6072         *flagp |= flags&SIMPLE;
6073     }
6074     if (is_open) {                              /* Starts with OPEN. */
6075         REGTAIL(pRExC_state, ret, br);          /* OPEN -> first. */
6076     }
6077     else if (paren != '?')              /* Not Conditional */
6078         ret = br;
6079     *flagp |= flags & (SPSTART | HASWIDTH | POSTPONED);
6080     lastbr = br;
6081     while (*RExC_parse == '|') {
6082         if (!SIZE_ONLY && RExC_extralen) {
6083             ender = reganode(pRExC_state, LONGJMP,0);
6084             REGTAIL(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
6085         }
6086         if (SIZE_ONLY)
6087             RExC_extralen += 2;         /* Account for LONGJMP. */
6088         nextchar(pRExC_state);
6089         if (freeze_paren) {
6090             if (RExC_npar > after_freeze)
6091                 after_freeze = RExC_npar;
6092             RExC_npar = freeze_paren;       
6093         }
6094         br = regbranch(pRExC_state, &flags, 0, depth+1);
6095
6096         if (br == NULL)
6097             return(NULL);
6098         REGTAIL(pRExC_state, lastbr, br);               /* BRANCH -> BRANCH. */
6099         lastbr = br;
6100         *flagp |= flags & (SPSTART | HASWIDTH | POSTPONED);
6101     }
6102
6103     if (have_branch || paren != ':') {
6104         /* Make a closing node, and hook it on the end. */
6105         switch (paren) {
6106         case ':':
6107             ender = reg_node(pRExC_state, TAIL);
6108             break;
6109         case 1:
6110             ender = reganode(pRExC_state, CLOSE, parno);
6111             if (!SIZE_ONLY && RExC_seen & REG_SEEN_RECURSE) {
6112                 DEBUG_OPTIMISE_MORE_r(PerlIO_printf(Perl_debug_log,
6113                         "Setting close paren #%"IVdf" to %d\n", 
6114                         (IV)parno, REG_NODE_NUM(ender)));
6115                 RExC_close_parens[parno-1]= ender;
6116                 if (RExC_nestroot == parno) 
6117                     RExC_nestroot = 0;
6118             }       
6119             Set_Node_Offset(ender,RExC_parse+1); /* MJD */
6120             Set_Node_Length(ender,1); /* MJD */
6121             break;
6122         case '<':
6123         case ',':
6124         case '=':
6125         case '!':
6126             *flagp &= ~HASWIDTH;
6127             /* FALL THROUGH */
6128         case '>':
6129             ender = reg_node(pRExC_state, SUCCEED);
6130             break;
6131         case 0:
6132             ender = reg_node(pRExC_state, END);
6133             if (!SIZE_ONLY) {
6134                 assert(!RExC_opend); /* there can only be one! */
6135                 RExC_opend = ender;
6136             }
6137             break;
6138         }
6139         REGTAIL(pRExC_state, lastbr, ender);
6140
6141         if (have_branch && !SIZE_ONLY) {
6142             if (depth==1)
6143                 RExC_seen |= REG_TOP_LEVEL_BRANCHES;
6144
6145             /* Hook the tails of the branches to the closing node. */
6146             for (br = ret; br; br = regnext(br)) {
6147                 const U8 op = PL_regkind[OP(br)];
6148                 if (op == BRANCH) {
6149                     REGTAIL_STUDY(pRExC_state, NEXTOPER(br), ender);
6150                 }
6151                 else if (op == BRANCHJ) {
6152                     REGTAIL_STUDY(pRExC_state, NEXTOPER(NEXTOPER(br)), ender);
6153                 }
6154             }
6155         }
6156     }
6157
6158     {
6159         const char *p;
6160         static const char parens[] = "=!<,>";
6161
6162         if (paren && (p = strchr(parens, paren))) {
6163             U8 node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
6164             int flag = (p - parens) > 1;
6165
6166             if (paren == '>')
6167                 node = SUSPEND, flag = 0;
6168             reginsert(pRExC_state, node,ret, depth+1);
6169             Set_Node_Cur_Length(ret);
6170             Set_Node_Offset(ret, parse_start + 1);
6171             ret->flags = flag;
6172             REGTAIL_STUDY(pRExC_state, ret, reg_node(pRExC_state, TAIL));
6173         }
6174     }
6175
6176     /* Check for proper termination. */
6177     if (paren) {
6178         RExC_flags = oregflags;
6179         if (RExC_parse >= RExC_end || *nextchar(pRExC_state) != ')') {
6180             RExC_parse = oregcomp_parse;
6181             vFAIL("Unmatched (");
6182         }
6183     }
6184     else if (!paren && RExC_parse < RExC_end) {
6185         if (*RExC_parse == ')') {
6186             RExC_parse++;
6187             vFAIL("Unmatched )");
6188         }
6189         else
6190             FAIL("Junk on end of regexp");      /* "Can't happen". */
6191         /* NOTREACHED */
6192     }
6193     if (after_freeze)
6194         RExC_npar = after_freeze;
6195     return(ret);
6196 }
6197
6198 /*
6199  - regbranch - one alternative of an | operator
6200  *
6201  * Implements the concatenation operator.
6202  */
6203 STATIC regnode *
6204 S_regbranch(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, I32 first, U32 depth)
6205 {
6206     dVAR;
6207     register regnode *ret;
6208     register regnode *chain = NULL;
6209     register regnode *latest;
6210     I32 flags = 0, c = 0;
6211     GET_RE_DEBUG_FLAGS_DECL;
6212     DEBUG_PARSE("brnc");
6213
6214     if (first)
6215         ret = NULL;
6216     else {
6217         if (!SIZE_ONLY && RExC_extralen)
6218             ret = reganode(pRExC_state, BRANCHJ,0);
6219         else {
6220             ret = reg_node(pRExC_state, BRANCH);
6221             Set_Node_Length(ret, 1);
6222         }
6223     }
6224         
6225     if (!first && SIZE_ONLY)
6226         RExC_extralen += 1;                     /* BRANCHJ */
6227
6228     *flagp = WORST;                     /* Tentatively. */
6229
6230     RExC_parse--;
6231     nextchar(pRExC_state);
6232     while (RExC_parse < RExC_end && *RExC_parse != '|' && *RExC_parse != ')') {
6233         flags &= ~TRYAGAIN;
6234         latest = regpiece(pRExC_state, &flags,depth+1);
6235         if (latest == NULL) {
6236             if (flags & TRYAGAIN)
6237                 continue;
6238             return(NULL);
6239         }
6240         else if (ret == NULL)
6241             ret = latest;
6242         *flagp |= flags&(HASWIDTH|POSTPONED);
6243         if (chain == NULL)      /* First piece. */
6244             *flagp |= flags&SPSTART;
6245         else {
6246             RExC_naughty++;
6247             REGTAIL(pRExC_state, chain, latest);
6248         }
6249         chain = latest;
6250         c++;
6251     }
6252     if (chain == NULL) {        /* Loop ran zero times. */
6253         chain = reg_node(pRExC_state, NOTHING);
6254         if (ret == NULL)
6255             ret = chain;
6256     }
6257     if (c == 1) {
6258         *flagp |= flags&SIMPLE;
6259     }
6260
6261     return ret;
6262 }
6263
6264 /*
6265  - regpiece - something followed by possible [*+?]
6266  *
6267  * Note that the branching code sequences used for ? and the general cases
6268  * of * and + are somewhat optimized:  they use the same NOTHING node as
6269  * both the endmarker for their branch list and the body of the last branch.
6270  * It might seem that this node could be dispensed with entirely, but the
6271  * endmarker role is not redundant.
6272  */
6273 STATIC regnode *
6274 S_regpiece(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, U32 depth)
6275 {
6276     dVAR;
6277     register regnode *ret;
6278     register char op;
6279     register char *next;
6280     I32 flags;
6281     const char * const origparse = RExC_parse;
6282     I32 min;
6283     I32 max = REG_INFTY;
6284     char *parse_start;
6285     const char *maxpos = NULL;
6286     GET_RE_DEBUG_FLAGS_DECL;
6287     DEBUG_PARSE("piec");
6288
6289     ret = regatom(pRExC_state, &flags,depth+1);
6290     if (ret == NULL) {
6291         if (flags & TRYAGAIN)
6292             *flagp |= TRYAGAIN;
6293         return(NULL);
6294     }
6295
6296     op = *RExC_parse;
6297
6298     if (op == '{' && regcurly(RExC_parse)) {
6299         maxpos = NULL;
6300         parse_start = RExC_parse; /* MJD */
6301         next = RExC_parse + 1;
6302         while (isDIGIT(*next) || *next == ',') {
6303             if (*next == ',') {
6304                 if (maxpos)
6305                     break;
6306                 else
6307                     maxpos = next;
6308             }
6309             next++;
6310         }
6311         if (*next == '}') {             /* got one */
6312             if (!maxpos)
6313                 maxpos = next;
6314             RExC_parse++;
6315             min = atoi(RExC_parse);
6316             if (*maxpos == ',')
6317                 maxpos++;
6318             else
6319                 maxpos = RExC_parse;
6320             max = atoi(maxpos);
6321             if (!max && *maxpos != '0')
6322                 max = REG_INFTY;                /* meaning "infinity" */
6323             else if (max >= REG_INFTY)
6324                 vFAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
6325             RExC_parse = next;
6326             nextchar(pRExC_state);
6327
6328         do_curly:
6329             if ((flags&SIMPLE)) {
6330                 RExC_naughty += 2 + RExC_naughty / 2;
6331                 reginsert(pRExC_state, CURLY, ret, depth+1);
6332                 Set_Node_Offset(ret, parse_start+1); /* MJD */
6333                 Set_Node_Cur_Length(ret);
6334             }
6335             else {
6336                 regnode * const w = reg_node(pRExC_state, WHILEM);
6337
6338                 w->flags = 0;
6339                 REGTAIL(pRExC_state, ret, w);
6340                 if (!SIZE_ONLY && RExC_extralen) {
6341                     reginsert(pRExC_state, LONGJMP,ret, depth+1);
6342                     reginsert(pRExC_state, NOTHING,ret, depth+1);
6343                     NEXT_OFF(ret) = 3;  /* Go over LONGJMP. */
6344                 }
6345                 reginsert(pRExC_state, CURLYX,ret, depth+1);
6346                                 /* MJD hk */
6347                 Set_Node_Offset(ret, parse_start+1);
6348                 Set_Node_Length(ret,
6349                                 op == '{' ? (RExC_parse - parse_start) : 1);
6350
6351                 if (!SIZE_ONLY && RExC_extralen)
6352                     NEXT_OFF(ret) = 3;  /* Go over NOTHING to LONGJMP. */
6353                 REGTAIL(pRExC_state, ret, reg_node(pRExC_state, NOTHING));
6354                 if (SIZE_ONLY)
6355                     RExC_whilem_seen++, RExC_extralen += 3;
6356                 RExC_naughty += 4 + RExC_naughty;       /* compound interest */
6357             }
6358             ret->flags = 0;
6359
6360             if (min > 0)
6361                 *flagp = WORST;
6362             if (max > 0)
6363                 *flagp |= HASWIDTH;
6364             if (max && max < min)
6365                 vFAIL("Can't do {n,m} with n > m");
6366             if (!SIZE_ONLY) {
6367                 ARG1_SET(ret, (U16)min);
6368                 ARG2_SET(ret, (U16)max);
6369             }
6370
6371             goto nest_check;
6372         }
6373     }
6374
6375     if (!ISMULT1(op)) {
6376         *flagp = flags;
6377         return(ret);
6378     }
6379
6380 #if 0                           /* Now runtime fix should be reliable. */
6381
6382     /* if this is reinstated, don't forget to put this back into perldiag:
6383
6384             =item Regexp *+ operand could be empty at {#} in regex m/%s/
6385
6386            (F) The part of the regexp subject to either the * or + quantifier
6387            could match an empty string. The {#} shows in the regular
6388            expression about where the problem was discovered.
6389
6390     */
6391
6392     if (!(flags&HASWIDTH) && op != '?')
6393       vFAIL("Regexp *+ operand could be empty");
6394 #endif
6395
6396     parse_start = RExC_parse;
6397     nextchar(pRExC_state);
6398
6399     *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
6400
6401     if (op == '*' && (flags&SIMPLE)) {
6402         reginsert(pRExC_state, STAR, ret, depth+1);
6403         ret->flags = 0;
6404         RExC_naughty += 4;
6405     }
6406     else if (op == '*') {
6407         min = 0;
6408         goto do_curly;
6409     }
6410     else if (op == '+' && (flags&SIMPLE)) {
6411         reginsert(pRExC_state, PLUS, ret, depth+1);
6412         ret->flags = 0;
6413         RExC_naughty += 3;
6414     }
6415     else if (op == '+') {
6416         min = 1;
6417         goto do_curly;
6418     }
6419     else if (op == '?') {
6420         min = 0; max = 1;
6421         goto do_curly;
6422     }
6423   nest_check:
6424     if (!SIZE_ONLY && !(flags&(HASWIDTH|POSTPONED)) && max > REG_INFTY/3 && ckWARN(WARN_REGEXP)) {
6425         vWARN3(RExC_parse,
6426                "%.*s matches null string many times",
6427                (int)(RExC_parse >= origparse ? RExC_parse - origparse : 0),
6428                origparse);
6429     }
6430
6431     if (RExC_parse < RExC_end && *RExC_parse == '?') {
6432         nextchar(pRExC_state);
6433         reginsert(pRExC_state, MINMOD, ret, depth+1);
6434         REGTAIL(pRExC_state, ret, ret + NODE_STEP_REGNODE);
6435     }
6436 #ifndef REG_ALLOW_MINMOD_SUSPEND
6437     else
6438 #endif
6439     if (RExC_parse < RExC_end && *RExC_parse == '+') {
6440         regnode *ender;
6441         nextchar(pRExC_state);
6442         ender = reg_node(pRExC_state, SUCCEED);
6443         REGTAIL(pRExC_state, ret, ender);
6444         reginsert(pRExC_state, SUSPEND, ret, depth+1);
6445         ret->flags = 0;
6446         ender = reg_node(pRExC_state, TAIL);
6447         REGTAIL(pRExC_state, ret, ender);
6448         /*ret= ender;*/
6449     }
6450
6451     if (RExC_parse < RExC_end && ISMULT2(RExC_parse)) {
6452         RExC_parse++;
6453         vFAIL("Nested quantifiers");
6454     }
6455
6456     return(ret);
6457 }
6458
6459
6460 /* reg_namedseq(pRExC_state,UVp)
6461    
6462    This is expected to be called by a parser routine that has 
6463    recognized'\N' and needs to handle the rest. RExC_parse is 
6464    expected to point at the first char following the N at the time
6465    of the call.
6466    
6467    If valuep is non-null then it is assumed that we are parsing inside 
6468    of a charclass definition and the first codepoint in the resolved
6469    string is returned via *valuep and the routine will return NULL. 
6470    In this mode if a multichar string is returned from the charnames 
6471    handler a warning will be issued, and only the first char in the 
6472    sequence will be examined. If the string returned is zero length
6473    then the value of *valuep is undefined and NON-NULL will 
6474    be returned to indicate failure. (This will NOT be a valid pointer 
6475    to a regnode.)
6476    
6477    If value is null then it is assumed that we are parsing normal text
6478    and inserts a new EXACT node into the program containing the resolved
6479    string and returns a pointer to the new node. If the string is 
6480    zerolength a NOTHING node is emitted.
6481    
6482    On success RExC_parse is set to the char following the endbrace.
6483    Parsing failures will generate a fatal errorvia vFAIL(...)
6484    
6485    NOTE: We cache all results from the charnames handler locally in 
6486    the RExC_charnames hash (created on first use) to prevent a charnames 
6487    handler from playing silly-buggers and returning a short string and 
6488    then a long string for a given pattern. Since the regexp program 
6489    size is calculated during an initial parse this would result
6490    in a buffer overrun so we cache to prevent the charname result from
6491    changing during the course of the parse.
6492    
6493  */
6494 STATIC regnode *
6495 S_reg_namedseq(pTHX_ RExC_state_t *pRExC_state, UV *valuep) 
6496 {
6497     char * name;        /* start of the content of the name */
6498     char * endbrace;    /* endbrace following the name */
6499     SV *sv_str = NULL;  
6500     SV *sv_name = NULL;
6501     STRLEN len; /* this has various purposes throughout the code */
6502     bool cached = 0; /* if this is true then we shouldn't refcount dev sv_str */
6503     regnode *ret = NULL;
6504     
6505     if (*RExC_parse != '{') {
6506         vFAIL("Missing braces on \\N{}");
6507     }
6508     name = RExC_parse+1;
6509     endbrace = strchr(RExC_parse, '}');
6510     if ( ! endbrace ) {
6511         RExC_parse++;
6512         vFAIL("Missing right brace on \\N{}");
6513     } 
6514     RExC_parse = endbrace + 1;  
6515     
6516     
6517     /* RExC_parse points at the beginning brace, 
6518        endbrace points at the last */
6519     if ( name[0]=='U' && name[1]=='+' ) {
6520         /* its a "Unicode hex" notation {U+89AB} */
6521         I32 fl = PERL_SCAN_ALLOW_UNDERSCORES
6522             | PERL_SCAN_DISALLOW_PREFIX
6523             | (SIZE_ONLY ? PERL_SCAN_SILENT_ILLDIGIT : 0);
6524         UV cp;
6525         char string;
6526         len = (STRLEN)(endbrace - name - 2);
6527         cp = grok_hex(name + 2, &len, &fl, NULL);
6528         if ( len != (STRLEN)(endbrace - name - 2) ) {
6529             cp = 0xFFFD;
6530         }    
6531         if (cp > 0xff)
6532             RExC_utf8 = 1;
6533         if ( valuep ) {
6534             *valuep = cp;
6535             return NULL;
6536         }
6537         string = (char)cp;
6538         sv_str= newSVpvn(&string, 1);
6539     } else {
6540         /* fetch the charnames handler for this scope */
6541         HV * const table = GvHV(PL_hintgv);
6542         SV **cvp= table ? 
6543             hv_fetchs(table, "charnames", FALSE) :
6544             NULL;
6545         SV *cv= cvp ? *cvp : NULL;
6546         HE *he_str;
6547         int count;
6548         /* create an SV with the name as argument */
6549         sv_name = newSVpvn(name, endbrace - name);
6550         
6551         if (!table || !(PL_hints & HINT_LOCALIZE_HH)) {
6552             vFAIL2("Constant(\\N{%s}) unknown: "
6553                   "(possibly a missing \"use charnames ...\")",
6554                   SvPVX(sv_name));
6555         }
6556         if (!cvp || !SvOK(*cvp)) { /* when $^H{charnames} = undef; */
6557             vFAIL2("Constant(\\N{%s}): "
6558                   "$^H{charnames} is not defined",SvPVX(sv_name));
6559         }
6560         
6561         
6562         
6563         if (!RExC_charnames) {
6564             /* make sure our cache is allocated */
6565             RExC_charnames = newHV();
6566             sv_2mortal((SV*)RExC_charnames);
6567         } 
6568             /* see if we have looked this one up before */
6569         he_str = hv_fetch_ent( RExC_charnames, sv_name, 0, 0 );
6570         if ( he_str ) {
6571             sv_str = HeVAL(he_str);
6572             cached = 1;
6573         } else {
6574             dSP ;
6575
6576             ENTER ;
6577             SAVETMPS ;
6578             PUSHMARK(SP) ;
6579             
6580             XPUSHs(sv_name);
6581             
6582             PUTBACK ;
6583             
6584             count= call_sv(cv, G_SCALAR);
6585             
6586             if (count == 1) { /* XXXX is this right? dmq */
6587                 sv_str = POPs;
6588                 SvREFCNT_inc_simple_void(sv_str);
6589             } 
6590             
6591             SPAGAIN ;
6592             PUTBACK ;
6593             FREETMPS ;
6594             LEAVE ;
6595             
6596             if ( !sv_str || !SvOK(sv_str) ) {
6597                 vFAIL2("Constant(\\N{%s}): Call to &{$^H{charnames}} "
6598                       "did not return a defined value",SvPVX(sv_name));
6599             }
6600             if (hv_store_ent( RExC_charnames, sv_name, sv_str, 0))
6601                 cached = 1;
6602         }
6603     }
6604     if (valuep) {
6605         char *p = SvPV(sv_str, len);
6606         if (len) {
6607             STRLEN numlen = 1;
6608             if ( SvUTF8(sv_str) ) {
6609                 *valuep = utf8_to_uvchr((U8*)p, &numlen);
6610                 if (*valuep > 0x7F)
6611                     RExC_utf8 = 1; 
6612                 /* XXXX
6613                   We have to turn on utf8 for high bit chars otherwise
6614                   we get failures with
6615                   
6616                    "ss" =~ /[\N{LATIN SMALL LETTER SHARP S}]/i
6617                    "SS" =~ /[\N{LATIN SMALL LETTER SHARP S}]/i
6618                 
6619                   This is different from what \x{} would do with the same
6620                   codepoint, where the condition is > 0xFF.
6621                   - dmq
6622                 */
6623                 
6624                 
6625             } else {
6626                 *valuep = (UV)*p;
6627                 /* warn if we havent used the whole string? */
6628             }
6629             if (numlen<len && SIZE_ONLY && ckWARN(WARN_REGEXP)) {
6630                 vWARN2(RExC_parse,
6631                     "Ignoring excess chars from \\N{%s} in character class",
6632                     SvPVX(sv_name)
6633                 );
6634             }        
6635         } else if (SIZE_ONLY && ckWARN(WARN_REGEXP)) {
6636             vWARN2(RExC_parse,
6637                     "Ignoring zero length \\N{%s} in character class",
6638                     SvPVX(sv_name)
6639                 );
6640         }
6641         if (sv_name)    
6642             SvREFCNT_dec(sv_name);    
6643         if (!cached)
6644             SvREFCNT_dec(sv_str);    
6645         return len ? NULL : (regnode *)&len;
6646     } else if(SvCUR(sv_str)) {     
6647         
6648         char *s; 
6649         char *p, *pend;        
6650         STRLEN charlen = 1;
6651 #ifdef DEBUGGING
6652         char * parse_start = name-3; /* needed for the offsets */
6653 #endif
6654         GET_RE_DEBUG_FLAGS_DECL;     /* needed for the offsets */
6655         
6656         ret = reg_node(pRExC_state,
6657             (U8)(FOLD ? (LOC ? EXACTFL : EXACTF) : EXACT));
6658         s= STRING(ret);
6659         
6660         if ( RExC_utf8 && !SvUTF8(sv_str) ) {
6661             sv_utf8_upgrade(sv_str);
6662         } else if ( !RExC_utf8 && SvUTF8(sv_str) ) {
6663             RExC_utf8= 1;
6664         }
6665         
6666         p = SvPV(sv_str, len);
6667         pend = p + len;
6668         /* len is the length written, charlen is the size the char read */
6669         for ( len = 0; p < pend; p += charlen ) {
6670             if (UTF) {
6671                 UV uvc = utf8_to_uvchr((U8*)p, &charlen);
6672                 if (FOLD) {
6673                     STRLEN foldlen,numlen;
6674                     U8 tmpbuf[UTF8_MAXBYTES_CASE+1], *foldbuf;
6675                     uvc = toFOLD_uni(uvc, tmpbuf, &foldlen);
6676                     /* Emit all the Unicode characters. */
6677                     
6678                     for (foldbuf = tmpbuf;
6679                         foldlen;
6680                         foldlen -= numlen) 
6681                     {
6682                         uvc = utf8_to_uvchr(foldbuf, &numlen);
6683                         if (numlen > 0) {
6684                             const STRLEN unilen = reguni(pRExC_state, uvc, s);
6685                             s       += unilen;
6686                             len     += unilen;
6687                             /* In EBCDIC the numlen
6688                             * and unilen can differ. */
6689                             foldbuf += numlen;
6690                             if (numlen >= foldlen)
6691                                 break;
6692                         }
6693                         else
6694                             break; /* "Can't happen." */
6695                     }                          
6696                 } else {
6697                     const STRLEN unilen = reguni(pRExC_state, uvc, s);
6698                     if (unilen > 0) {
6699                        s   += unilen;
6700                        len += unilen;
6701                     }
6702                 }
6703             } else {
6704                 len++;
6705                 REGC(*p, s++);
6706             }
6707         }
6708         if (SIZE_ONLY) {
6709             RExC_size += STR_SZ(len);
6710         } else {
6711             STR_LEN(ret) = len;
6712             RExC_emit += STR_SZ(len);
6713         }
6714         Set_Node_Cur_Length(ret); /* MJD */
6715         RExC_parse--; 
6716         nextchar(pRExC_state);
6717     } else {
6718         ret = reg_node(pRExC_state,NOTHING);
6719     }
6720     if (!cached) {
6721         SvREFCNT_dec(sv_str);
6722     }
6723     if (sv_name) {
6724         SvREFCNT_dec(sv_name); 
6725     }
6726     return ret;
6727
6728 }
6729
6730
6731 /*
6732  * reg_recode
6733  *
6734  * It returns the code point in utf8 for the value in *encp.
6735  *    value: a code value in the source encoding
6736  *    encp:  a pointer to an Encode object
6737  *
6738  * If the result from Encode is not a single character,
6739  * it returns U+FFFD (Replacement character) and sets *encp to NULL.
6740  */
6741 STATIC UV
6742 S_reg_recode(pTHX_ const char value, SV **encp)
6743 {
6744     STRLEN numlen = 1;
6745     SV * const sv = sv_2mortal(newSVpvn(&value, numlen));
6746     const char * const s = *encp ? sv_recode_to_utf8(sv, *encp) : SvPVX(sv);
6747     const STRLEN newlen = SvCUR(sv);
6748     UV uv = UNICODE_REPLACEMENT;
6749
6750     if (newlen)
6751         uv = SvUTF8(sv)
6752              ? utf8n_to_uvchr((U8*)s, newlen, &numlen, UTF8_ALLOW_DEFAULT)
6753              : *(U8*)s;
6754
6755     if (!newlen || numlen != newlen) {
6756         uv = UNICODE_REPLACEMENT;
6757         *encp = NULL;
6758     }
6759     return uv;
6760 }
6761
6762
6763 /*
6764  - regatom - the lowest level
6765
6766    Try to identify anything special at the start of the pattern. If there
6767    is, then handle it as required. This may involve generating a single regop,
6768    such as for an assertion; or it may involve recursing, such as to
6769    handle a () structure.
6770
6771    If the string doesn't start with something special then we gobble up
6772    as much literal text as we can.
6773
6774    Once we have been able to handle whatever type of thing started the
6775    sequence, we return.
6776
6777    Note: we have to be careful with escapes, as they can be both literal
6778    and special, and in the case of \10 and friends can either, depending
6779    on context. Specifically there are two seperate switches for handling
6780    escape sequences, with the one for handling literal escapes requiring
6781    a dummy entry for all of the special escapes that are actually handled
6782    by the other.
6783 */
6784
6785 STATIC regnode *
6786 S_regatom(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, U32 depth)
6787 {
6788     dVAR;
6789     register regnode *ret = NULL;
6790     I32 flags;
6791     char *parse_start = RExC_parse;
6792     GET_RE_DEBUG_FLAGS_DECL;
6793     DEBUG_PARSE("atom");
6794     *flagp = WORST;             /* Tentatively. */
6795
6796
6797 tryagain:
6798     switch ((U8)*RExC_parse) {
6799     case '^':
6800         RExC_seen_zerolen++;
6801         nextchar(pRExC_state);
6802         if (RExC_flags & RXf_PMf_MULTILINE)
6803             ret = reg_node(pRExC_state, MBOL);
6804         else if (RExC_flags & RXf_PMf_SINGLELINE)
6805             ret = reg_node(pRExC_state, SBOL);
6806         else
6807             ret = reg_node(pRExC_state, BOL);
6808         Set_Node_Length(ret, 1); /* MJD */
6809         break;
6810     case '$':
6811         nextchar(pRExC_state);
6812         if (*RExC_parse)
6813             RExC_seen_zerolen++;
6814         if (RExC_flags & RXf_PMf_MULTILINE)
6815             ret = reg_node(pRExC_state, MEOL);
6816         else if (RExC_flags & RXf_PMf_SINGLELINE)
6817             ret = reg_node(pRExC_state, SEOL);
6818         else
6819             ret = reg_node(pRExC_state, EOL);
6820         Set_Node_Length(ret, 1); /* MJD */
6821         break;
6822     case '.':
6823         nextchar(pRExC_state);
6824         if (RExC_flags & RXf_PMf_SINGLELINE)
6825             ret = reg_node(pRExC_state, SANY);
6826         else
6827             ret = reg_node(pRExC_state, REG_ANY);
6828         *flagp |= HASWIDTH|SIMPLE;
6829         RExC_naughty++;
6830         Set_Node_Length(ret, 1); /* MJD */
6831         break;
6832     case '[':
6833     {
6834         char * const oregcomp_parse = ++RExC_parse;
6835         ret = regclass(pRExC_state,depth+1);
6836         if (*RExC_parse != ']') {
6837             RExC_parse = oregcomp_parse;
6838             vFAIL("Unmatched [");
6839         }
6840         nextchar(pRExC_state);
6841         *flagp |= HASWIDTH|SIMPLE;
6842         Set_Node_Length(ret, RExC_parse - oregcomp_parse + 1); /* MJD */
6843         break;
6844     }
6845     case '(':
6846         nextchar(pRExC_state);
6847         ret = reg(pRExC_state, 1, &flags,depth+1);
6848         if (ret == NULL) {
6849                 if (flags & TRYAGAIN) {
6850                     if (RExC_parse == RExC_end) {
6851                          /* Make parent create an empty node if needed. */
6852                         *flagp |= TRYAGAIN;
6853                         return(NULL);
6854                     }
6855                     goto tryagain;
6856                 }
6857                 return(NULL);
6858         }
6859         *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE|POSTPONED);
6860         break;
6861     case '|':
6862     case ')':
6863         if (flags & TRYAGAIN) {
6864             *flagp |= TRYAGAIN;
6865             return NULL;
6866         }
6867         vFAIL("Internal urp");
6868                                 /* Supposed to be caught earlier. */
6869         break;
6870     case '{':
6871         if (!regcurly(RExC_parse)) {
6872             RExC_parse++;
6873             goto defchar;
6874         }
6875         /* FALL THROUGH */
6876     case '?':
6877     case '+':
6878     case '*':
6879         RExC_parse++;
6880         vFAIL("Quantifier follows nothing");
6881         break;
6882     case 0xDF:
6883     case 0xC3:
6884     case 0xCE:
6885         do_foldchar:
6886         if (!LOC && FOLD) {
6887             U32 len,cp;
6888             len=0; /* silence a spurious compiler warning */
6889             if ((cp = what_len_TRICKYFOLD_safe(RExC_parse,RExC_end,UTF,len))) {
6890                 *flagp |= HASWIDTH; /* could be SIMPLE too, but needs a handler in regexec.regrepeat */
6891                 RExC_parse+=len-1; /* we get one from nextchar() as well. :-( */
6892                 ret = reganode(pRExC_state, FOLDCHAR, cp);
6893                 Set_Node_Length(ret, 1); /* MJD */
6894                 nextchar(pRExC_state); /* kill whitespace under /x */
6895                 return ret;
6896             }
6897         }
6898         goto outer_default;
6899     case '\\':
6900         /* Special Escapes
6901
6902            This switch handles escape sequences that resolve to some kind
6903            of special regop and not to literal text. Escape sequnces that
6904            resolve to literal text are handled below in the switch marked
6905            "Literal Escapes".
6906
6907            Every entry in this switch *must* have a corresponding entry
6908            in the literal escape switch. However, the opposite is not
6909            required, as the default for this switch is to jump to the
6910            literal text handling code.
6911         */
6912         switch ((U8)*++RExC_parse) {
6913         case 0xDF:
6914         case 0xC3:
6915         case 0xCE:
6916                    goto do_foldchar;        
6917         /* Special Escapes */
6918         case 'A':
6919             RExC_seen_zerolen++;
6920             ret = reg_node(pRExC_state, SBOL);
6921             *flagp |= SIMPLE;
6922             goto finish_meta_pat;
6923         case 'G':
6924             ret = reg_node(pRExC_state, GPOS);
6925             RExC_seen |= REG_SEEN_GPOS;
6926             *flagp |= SIMPLE;
6927             goto finish_meta_pat;
6928         case 'K':
6929             RExC_seen_zerolen++;
6930             ret = reg_node(pRExC_state, KEEPS);
6931             *flagp |= SIMPLE;
6932             /* XXX:dmq : disabling in-place substitution seems to
6933              * be necessary here to avoid cases of memory corruption, as
6934              * with: C<$_="x" x 80; s/x\K/y/> -- rgs
6935              */
6936             RExC_seen |= REG_SEEN_LOOKBEHIND;
6937             goto finish_meta_pat;
6938         case 'Z':
6939             ret = reg_node(pRExC_state, SEOL);
6940             *flagp |= SIMPLE;
6941             RExC_seen_zerolen++;                /* Do not optimize RE away */
6942             goto finish_meta_pat;
6943         case 'z':
6944             ret = reg_node(pRExC_state, EOS);
6945             *flagp |= SIMPLE;
6946             RExC_seen_zerolen++;                /* Do not optimize RE away */
6947             goto finish_meta_pat;
6948         case 'C':
6949             ret = reg_node(pRExC_state, CANY);
6950             RExC_seen |= REG_SEEN_CANY;
6951             *flagp |= HASWIDTH|SIMPLE;
6952             goto finish_meta_pat;
6953         case 'X':
6954             ret = reg_node(pRExC_state, CLUMP);
6955             *flagp |= HASWIDTH;
6956             goto finish_meta_pat;
6957         case 'w':
6958             ret = reg_node(pRExC_state, (U8)(LOC ? ALNUML     : ALNUM));
6959             *flagp |= HASWIDTH|SIMPLE;
6960             goto finish_meta_pat;
6961         case 'W':
6962             ret = reg_node(pRExC_state, (U8)(LOC ? NALNUML    : NALNUM));
6963             *flagp |= HASWIDTH|SIMPLE;
6964             goto finish_meta_pat;
6965         case 'b':
6966             RExC_seen_zerolen++;
6967             RExC_seen |= REG_SEEN_LOOKBEHIND;
6968             ret = reg_node(pRExC_state, (U8)(LOC ? BOUNDL     : BOUND));
6969             *flagp |= SIMPLE;
6970             goto finish_meta_pat;
6971         case 'B':
6972             RExC_seen_zerolen++;
6973             RExC_seen |= REG_SEEN_LOOKBEHIND;
6974             ret = reg_node(pRExC_state, (U8)(LOC ? NBOUNDL    : NBOUND));
6975             *flagp |= SIMPLE;
6976             goto finish_meta_pat;
6977         case 's':
6978             ret = reg_node(pRExC_state, (U8)(LOC ? SPACEL     : SPACE));
6979             *flagp |= HASWIDTH|SIMPLE;
6980             goto finish_meta_pat;
6981         case 'S':
6982             ret = reg_node(pRExC_state, (U8)(LOC ? NSPACEL    : NSPACE));
6983             *flagp |= HASWIDTH|SIMPLE;
6984             goto finish_meta_pat;
6985         case 'd':
6986             ret = reg_node(pRExC_state, DIGIT);
6987             *flagp |= HASWIDTH|SIMPLE;
6988             goto finish_meta_pat;
6989         case 'D':
6990             ret = reg_node(pRExC_state, NDIGIT);
6991             *flagp |= HASWIDTH|SIMPLE;
6992             goto finish_meta_pat;
6993         case 'R':
6994             ret = reg_node(pRExC_state, LNBREAK);
6995             *flagp |= HASWIDTH|SIMPLE;
6996             goto finish_meta_pat;
6997         case 'h':
6998             ret = reg_node(pRExC_state, HORIZWS);
6999             *flagp |= HASWIDTH|SIMPLE;
7000             goto finish_meta_pat;
7001         case 'H':
7002             ret = reg_node(pRExC_state, NHORIZWS);
7003             *flagp |= HASWIDTH|SIMPLE;
7004             goto finish_meta_pat;
7005         case 'v':
7006             ret = reg_node(pRExC_state, VERTWS);
7007             *flagp |= HASWIDTH|SIMPLE;
7008             goto finish_meta_pat;
7009         case 'V':
7010             ret = reg_node(pRExC_state, NVERTWS);
7011             *flagp |= HASWIDTH|SIMPLE;
7012          finish_meta_pat:           
7013             nextchar(pRExC_state);
7014             Set_Node_Length(ret, 2); /* MJD */
7015             break;          
7016         case 'p':
7017         case 'P':
7018             {   
7019                 char* const oldregxend = RExC_end;
7020 #ifdef DEBUGGING
7021                 char* parse_start = RExC_parse - 2;
7022 #endif
7023
7024                 if (RExC_parse[1] == '{') {
7025                   /* a lovely hack--pretend we saw [\pX] instead */
7026                     RExC_end = strchr(RExC_parse, '}');
7027                     if (!RExC_end) {
7028                         const U8 c = (U8)*RExC_parse;
7029                         RExC_parse += 2;
7030                         RExC_end = oldregxend;
7031                         vFAIL2("Missing right brace on \\%c{}", c);
7032                     }
7033                     RExC_end++;
7034                 }
7035                 else {
7036                     RExC_end = RExC_parse + 2;
7037                     if (RExC_end > oldregxend)
7038                         RExC_end = oldregxend;
7039                 }
7040                 RExC_parse--;
7041
7042                 ret = regclass(pRExC_state,depth+1);
7043
7044                 RExC_end = oldregxend;
7045                 RExC_parse--;
7046
7047                 Set_Node_Offset(ret, parse_start + 2);
7048                 Set_Node_Cur_Length(ret);
7049                 nextchar(pRExC_state);
7050                 *flagp |= HASWIDTH|SIMPLE;
7051             }
7052             break;
7053         case 'N': 
7054             /* Handle \N{NAME} here and not below because it can be 
7055             multicharacter. join_exact() will join them up later on. 
7056             Also this makes sure that things like /\N{BLAH}+/ and 
7057             \N{BLAH} being multi char Just Happen. dmq*/
7058             ++RExC_parse;
7059             ret= reg_namedseq(pRExC_state, NULL); 
7060             break;
7061         case 'k':    /* Handle \k<NAME> and \k'NAME' */
7062         parse_named_seq:
7063         {   
7064             char ch= RExC_parse[1];         
7065             if (ch != '<' && ch != '\'' && ch != '{') {
7066                 RExC_parse++;
7067                 vFAIL2("Sequence %.2s... not terminated",parse_start);
7068             } else {
7069                 /* this pretty much dupes the code for (?P=...) in reg(), if
7070                    you change this make sure you change that */
7071                 char* name_start = (RExC_parse += 2);
7072                 U32 num = 0;
7073                 SV *sv_dat = reg_scan_name(pRExC_state,
7074                     SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
7075                 ch= (ch == '<') ? '>' : (ch == '{') ? '}' : '\'';
7076                 if (RExC_parse == name_start || *RExC_parse != ch)
7077                     vFAIL2("Sequence %.3s... not terminated",parse_start);
7078
7079                 if (!SIZE_ONLY) {
7080                     num = add_data( pRExC_state, 1, "S" );
7081                     RExC_rxi->data->data[num]=(void*)sv_dat;
7082                     SvREFCNT_inc_simple_void(sv_dat);
7083                 }
7084
7085                 RExC_sawback = 1;
7086                 ret = reganode(pRExC_state,
7087                            (U8)(FOLD ? (LOC ? NREFFL : NREFF) : NREF),
7088                            num);
7089                 *flagp |= HASWIDTH;
7090
7091                 /* override incorrect value set in reganode MJD */
7092                 Set_Node_Offset(ret, parse_start+1);
7093                 Set_Node_Cur_Length(ret); /* MJD */
7094                 nextchar(pRExC_state);
7095
7096             }
7097             break;
7098         }
7099         case 'g': 
7100         case '1': case '2': case '3': case '4':
7101         case '5': case '6': case '7': case '8': case '9':
7102             {
7103                 I32 num;
7104                 bool isg = *RExC_parse == 'g';
7105                 bool isrel = 0; 
7106                 bool hasbrace = 0;
7107                 if (isg) {
7108                     RExC_parse++;
7109                     if (*RExC_parse == '{') {
7110                         RExC_parse++;
7111                         hasbrace = 1;
7112                     }
7113                     if (*RExC_parse == '-') {
7114                         RExC_parse++;
7115                         isrel = 1;
7116                     }
7117                     if (hasbrace && !isDIGIT(*RExC_parse)) {
7118                         if (isrel) RExC_parse--;
7119                         RExC_parse -= 2;                            
7120                         goto parse_named_seq;
7121                 }   }
7122                 num = atoi(RExC_parse);
7123                 if (isg && num == 0)
7124                     vFAIL("Reference to invalid group 0");
7125                 if (isrel) {
7126                     num = RExC_npar - num;
7127                     if (num < 1)
7128                         vFAIL("Reference to nonexistent or unclosed group");
7129                 }
7130                 if (!isg && num > 9 && num >= RExC_npar)
7131                     goto defchar;
7132                 else {
7133                     char * const parse_start = RExC_parse - 1; /* MJD */
7134                     while (isDIGIT(*RExC_parse))
7135                         RExC_parse++;
7136                     if (parse_start == RExC_parse - 1) 
7137                         vFAIL("Unterminated \\g... pattern");
7138                     if (hasbrace) {
7139                         if (*RExC_parse != '}') 
7140                             vFAIL("Unterminated \\g{...} pattern");
7141                         RExC_parse++;
7142                     }    
7143                     if (!SIZE_ONLY) {
7144                         if (num > (I32)RExC_rx->nparens)
7145                             vFAIL("Reference to nonexistent group");
7146                     }
7147                     RExC_sawback = 1;
7148                     ret = reganode(pRExC_state,
7149                                    (U8)(FOLD ? (LOC ? REFFL : REFF) : REF),
7150                                    num);
7151                     *flagp |= HASWIDTH;
7152
7153                     /* override incorrect value set in reganode MJD */
7154                     Set_Node_Offset(ret, parse_start+1);
7155                     Set_Node_Cur_Length(ret); /* MJD */
7156                     RExC_parse--;
7157                     nextchar(pRExC_state);
7158                 }
7159             }
7160             break;
7161         case '\0':
7162             if (RExC_parse >= RExC_end)
7163                 FAIL("Trailing \\");
7164             /* FALL THROUGH */
7165         default:
7166             /* Do not generate "unrecognized" warnings here, we fall
7167                back into the quick-grab loop below */
7168             parse_start--;
7169             goto defchar;
7170         }
7171         break;
7172
7173     case '#':
7174         if (RExC_flags & RXf_PMf_EXTENDED) {
7175             if ( reg_skipcomment( pRExC_state ) )
7176                 goto tryagain;
7177         }
7178         /* FALL THROUGH */
7179
7180     default:
7181         outer_default:{
7182             register STRLEN len;
7183             register UV ender;
7184             register char *p;
7185             char *s;
7186             STRLEN foldlen;
7187             U8 tmpbuf[UTF8_MAXBYTES_CASE+1], *foldbuf;
7188
7189             parse_start = RExC_parse - 1;
7190
7191             RExC_parse++;
7192
7193         defchar:
7194             ender = 0;
7195             ret = reg_node(pRExC_state,
7196                            (U8)(FOLD ? (LOC ? EXACTFL : EXACTF) : EXACT));
7197             s = STRING(ret);
7198             for (len = 0, p = RExC_parse - 1;
7199               len < 127 && p < RExC_end;
7200               len++)
7201             {
7202                 char * const oldp = p;
7203
7204                 if (RExC_flags & RXf_PMf_EXTENDED)
7205                     p = regwhite( pRExC_state, p );
7206                 switch ((U8)*p) {
7207                 case 0xDF:
7208                 case 0xC3:
7209                 case 0xCE:
7210                            if (LOC || !FOLD || !is_TRICKYFOLD_safe(p,RExC_end,UTF))
7211                                 goto normal_default;
7212                 case '^':
7213                 case '$':
7214                 case '.':
7215                 case '[':
7216                 case '(':
7217                 case ')':
7218                 case '|':
7219                     goto loopdone;
7220                 case '\\':
7221                     /* Literal Escapes Switch
7222
7223                        This switch is meant to handle escape sequences that
7224                        resolve to a literal character.
7225
7226                        Every escape sequence that represents something
7227                        else, like an assertion or a char class, is handled
7228                        in the switch marked 'Special Escapes' above in this
7229                        routine, but also has an entry here as anything that
7230                        isn't explicitly mentioned here will be treated as
7231                        an unescaped equivalent literal.
7232                     */
7233
7234                     switch ((U8)*++p) {
7235                     /* These are all the special escapes. */
7236                     case 0xDF:
7237                     case 0xC3:
7238                     case 0xCE:
7239                            if (LOC || !FOLD || !is_TRICKYFOLD_safe(p,RExC_end,UTF))
7240                                 goto normal_default;                
7241                     case 'A':             /* Start assertion */
7242                     case 'b': case 'B':   /* Word-boundary assertion*/
7243                     case 'C':             /* Single char !DANGEROUS! */
7244                     case 'd': case 'D':   /* digit class */
7245                     case 'g': case 'G':   /* generic-backref, pos assertion */
7246                     case 'h': case 'H':   /* HORIZWS */
7247                     case 'k': case 'K':   /* named backref, keep marker */
7248                     case 'N':             /* named char sequence */
7249                     case 'p': case 'P':   /* Unicode property */
7250                               case 'R':   /* LNBREAK */
7251                     case 's': case 'S':   /* space class */
7252                     case 'v': case 'V':   /* VERTWS */
7253                     case 'w': case 'W':   /* word class */
7254                     case 'X':             /* eXtended Unicode "combining character sequence" */
7255                     case 'z': case 'Z':   /* End of line/string assertion */
7256                         --p;
7257                         goto loopdone;
7258
7259                     /* Anything after here is an escape that resolves to a
7260                        literal. (Except digits, which may or may not)
7261                      */
7262                     case 'n':
7263                         ender = '\n';
7264                         p++;
7265                         break;
7266                     case 'r':
7267                         ender = '\r';
7268                         p++;
7269                         break;
7270                     case 't':
7271                         ender = '\t';
7272                         p++;
7273                         break;
7274                     case 'f':
7275                         ender = '\f';
7276                         p++;
7277                         break;
7278                     case 'e':
7279                           ender = ASCII_TO_NATIVE('\033');
7280                         p++;
7281                         break;
7282                     case 'a':
7283                           ender = ASCII_TO_NATIVE('\007');
7284                         p++;
7285                         break;
7286                     case 'x':
7287                         if (*++p == '{') {
7288                             char* const e = strchr(p, '}');
7289         
7290                             if (!e) {
7291                                 RExC_parse = p + 1;
7292                                 vFAIL("Missing right brace on \\x{}");
7293                             }
7294                             else {
7295                                 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
7296                                     | PERL_SCAN_DISALLOW_PREFIX;
7297                                 STRLEN numlen = e - p - 1;
7298                                 ender = grok_hex(p + 1, &numlen, &flags, NULL);
7299                                 if (ender > 0xff)
7300                                     RExC_utf8 = 1;
7301                                 p = e + 1;
7302                             }
7303                         }
7304                         else {
7305                             I32 flags = PERL_SCAN_DISALLOW_PREFIX;
7306                             STRLEN numlen = 2;
7307                             ender = grok_hex(p, &numlen, &flags, NULL);
7308                             p += numlen;
7309                         }
7310                         if (PL_encoding && ender < 0x100)
7311                             goto recode_encoding;
7312                         break;
7313                     case 'c':
7314                         p++;
7315                         ender = UCHARAT(p++);
7316                         ender = toCTRL(ender);
7317                         break;
7318                     case '0': case '1': case '2': case '3':case '4':
7319                     case '5': case '6': case '7': case '8':case '9':
7320                         if (*p == '0' ||
7321                           (isDIGIT(p[1]) && atoi(p) >= RExC_npar) ) {
7322                             I32 flags = 0;
7323                             STRLEN numlen = 3;
7324                             ender = grok_oct(p, &numlen, &flags, NULL);
7325                             p += numlen;
7326                         }
7327                         else {
7328                             --p;
7329                             goto loopdone;
7330                         }
7331                         if (PL_encoding && ender < 0x100)
7332                             goto recode_encoding;
7333                         break;
7334                     recode_encoding:
7335                         {
7336                             SV* enc = PL_encoding;
7337                             ender = reg_recode((const char)(U8)ender, &enc);
7338                             if (!enc && SIZE_ONLY && ckWARN(WARN_REGEXP))
7339                                 vWARN(p, "Invalid escape in the specified encoding");
7340                             RExC_utf8 = 1;
7341                         }
7342                         break;
7343                     case '\0':
7344                         if (p >= RExC_end)
7345                             FAIL("Trailing \\");
7346                         /* FALL THROUGH */
7347                     default:
7348                         if (!SIZE_ONLY&& isALPHA(*p) && ckWARN(WARN_REGEXP))
7349                             vWARN2(p + 1, "Unrecognized escape \\%c passed through", UCHARAT(p));
7350                         goto normal_default;
7351                     }
7352                     break;
7353                 default:
7354                   normal_default:
7355                     if (UTF8_IS_START(*p) && UTF) {
7356                         STRLEN numlen;
7357                         ender = utf8n_to_uvchr((U8*)p, RExC_end - p,
7358                                                &numlen, UTF8_ALLOW_DEFAULT);
7359                         p += numlen;
7360                     }
7361                     else
7362                         ender = *p++;
7363                     break;
7364                 }
7365                 if ( RExC_flags & RXf_PMf_EXTENDED)
7366                     p = regwhite( pRExC_state, p );
7367                 if (UTF && FOLD) {
7368                     /* Prime the casefolded buffer. */
7369                     ender = toFOLD_uni(ender, tmpbuf, &foldlen);
7370                 }
7371                 if (p < RExC_end && ISMULT2(p)) { /* Back off on ?+*. */
7372                     if (len)
7373                         p = oldp;
7374                     else if (UTF) {
7375                          if (FOLD) {
7376                               /* Emit all the Unicode characters. */
7377                               STRLEN numlen;
7378                               for (foldbuf = tmpbuf;
7379                                    foldlen;
7380                                    foldlen -= numlen) {
7381                                    ender = utf8_to_uvchr(foldbuf, &numlen);
7382                                    if (numlen > 0) {
7383                                         const STRLEN unilen = reguni(pRExC_state, ender, s);
7384                                         s       += unilen;
7385                                         len     += unilen;
7386                                         /* In EBCDIC the numlen
7387                                          * and unilen can differ. */
7388                                         foldbuf += numlen;
7389                                         if (numlen >= foldlen)
7390                                              break;
7391                                    }
7392                                    else
7393                                         break; /* "Can't happen." */
7394                               }
7395                          }
7396                          else {
7397                               const STRLEN unilen = reguni(pRExC_state, ender, s);
7398                               if (unilen > 0) {
7399                                    s   += unilen;
7400                                    len += unilen;
7401                               }
7402                          }
7403                     }
7404                     else {
7405                         len++;
7406                         REGC((char)ender, s++);
7407                     }
7408                     break;
7409                 }
7410                 if (UTF) {
7411                      if (FOLD) {
7412                           /* Emit all the Unicode characters. */
7413                           STRLEN numlen;
7414                           for (foldbuf = tmpbuf;
7415                                foldlen;
7416                                foldlen -= numlen) {
7417                                ender = utf8_to_uvchr(foldbuf, &numlen);
7418                                if (numlen > 0) {
7419                                     const STRLEN unilen = reguni(pRExC_state, ender, s);
7420                                     len     += unilen;
7421                                     s       += unilen;
7422                                     /* In EBCDIC the numlen
7423                                      * and unilen can differ. */
7424                                     foldbuf += numlen;
7425                                     if (numlen >= foldlen)
7426                                          break;
7427                                }
7428                                else
7429                                     break;
7430                           }
7431                      }
7432                      else {
7433                           const STRLEN unilen = reguni(pRExC_state, ender, s);
7434                           if (unilen > 0) {
7435                                s   += unilen;
7436                                len += unilen;
7437                           }
7438                      }
7439                      len--;
7440                 }
7441                 else
7442                     REGC((char)ender, s++);
7443             }
7444         loopdone:
7445             RExC_parse = p - 1;
7446             Set_Node_Cur_Length(ret); /* MJD */
7447             nextchar(pRExC_state);
7448             {
7449                 /* len is STRLEN which is unsigned, need to copy to signed */
7450                 IV iv = len;
7451                 if (iv < 0)
7452                     vFAIL("Internal disaster");
7453             }
7454             if (len > 0)
7455                 *flagp |= HASWIDTH;
7456             if (len == 1 && UNI_IS_INVARIANT(ender))
7457                 *flagp |= SIMPLE;
7458                 
7459             if (SIZE_ONLY)
7460                 RExC_size += STR_SZ(len);
7461             else {
7462                 STR_LEN(ret) = len;
7463                 RExC_emit += STR_SZ(len);
7464             }
7465         }
7466         break;
7467     }
7468
7469     return(ret);
7470 }
7471
7472 STATIC char *
7473 S_regwhite( RExC_state_t *pRExC_state, char *p )
7474 {
7475     const char *e = RExC_end;
7476     while (p < e) {
7477         if (isSPACE(*p))
7478             ++p;
7479         else if (*p == '#') {
7480             bool ended = 0;
7481             do {
7482                 if (*p++ == '\n') {
7483                     ended = 1;
7484                     break;
7485                 }
7486             } while (p < e);
7487             if (!ended)
7488                 RExC_seen |= REG_SEEN_RUN_ON_COMMENT;
7489         }
7490         else
7491             break;
7492     }
7493     return p;
7494 }
7495
7496 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
7497    Character classes ([:foo:]) can also be negated ([:^foo:]).
7498    Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
7499    Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
7500    but trigger failures because they are currently unimplemented. */
7501
7502 #define POSIXCC_DONE(c)   ((c) == ':')
7503 #define POSIXCC_NOTYET(c) ((c) == '=' || (c) == '.')
7504 #define POSIXCC(c) (POSIXCC_DONE(c) || POSIXCC_NOTYET(c))
7505
7506 STATIC I32
7507 S_regpposixcc(pTHX_ RExC_state_t *pRExC_state, I32 value)
7508 {
7509     dVAR;
7510     I32 namedclass = OOB_NAMEDCLASS;
7511
7512     if (value == '[' && RExC_parse + 1 < RExC_end &&
7513         /* I smell either [: or [= or [. -- POSIX has been here, right? */
7514         POSIXCC(UCHARAT(RExC_parse))) {
7515         const char c = UCHARAT(RExC_parse);
7516         char* const s = RExC_parse++;
7517         
7518         while (RExC_parse < RExC_end && UCHARAT(RExC_parse) != c)
7519             RExC_parse++;
7520         if (RExC_parse == RExC_end)
7521             /* Grandfather lone [:, [=, [. */
7522             RExC_parse = s;
7523         else {
7524             const char* const t = RExC_parse++; /* skip over the c */
7525             assert(*t == c);
7526
7527             if (UCHARAT(RExC_parse) == ']') {
7528                 const char *posixcc = s + 1;
7529                 RExC_parse++; /* skip over the ending ] */
7530
7531                 if (*s == ':') {
7532                     const I32 complement = *posixcc == '^' ? *posixcc++ : 0;
7533                     const I32 skip = t - posixcc;
7534
7535                     /* Initially switch on the length of the name.  */
7536                     switch (skip) {
7537                     case 4:
7538                         if (memEQ(posixcc, "word", 4)) /* this is not POSIX, this is the Perl \w */
7539                             namedclass = complement ? ANYOF_NALNUM : ANYOF_ALNUM;
7540                         break;
7541                     case 5:
7542                         /* Names all of length 5.  */
7543                         /* alnum alpha ascii blank cntrl digit graph lower
7544                            print punct space upper  */
7545                         /* Offset 4 gives the best switch position.  */
7546                         switch (posixcc[4]) {
7547                         case 'a':
7548                             if (memEQ(posixcc, "alph", 4)) /* alpha */
7549                                 namedclass = complement ? ANYOF_NALPHA : ANYOF_ALPHA;
7550                             break;
7551                         case 'e':
7552                             if (memEQ(posixcc, "spac", 4)) /* space */
7553                                 namedclass = complement ? ANYOF_NPSXSPC : ANYOF_PSXSPC;
7554                             break;
7555                         case 'h':
7556                             if (memEQ(posixcc, "grap", 4)) /* graph */
7557                                 namedclass = complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
7558                             break;
7559                         case 'i':
7560                             if (memEQ(posixcc, "asci", 4)) /* ascii */
7561                                 namedclass = complement ? ANYOF_NASCII : ANYOF_ASCII;
7562                             break;
7563                         case 'k':
7564                             if (memEQ(posixcc, "blan", 4)) /* blank */
7565                                 namedclass = complement ? ANYOF_NBLANK : ANYOF_BLANK;
7566                             break;
7567                         case 'l':
7568                             if (memEQ(posixcc, "cntr", 4)) /* cntrl */
7569                                 namedclass = complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
7570                             break;
7571                         case 'm':
7572                             if (memEQ(posixcc, "alnu", 4)) /* alnum */
7573                                 namedclass = complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
7574                             break;
7575                         case 'r':
7576                             if (memEQ(posixcc, "lowe", 4)) /* lower */
7577                                 namedclass = complement ? ANYOF_NLOWER : ANYOF_LOWER;
7578                             else if (memEQ(posixcc, "uppe", 4)) /* upper */
7579                                 namedclass = complement ? ANYOF_NUPPER : ANYOF_UPPER;
7580                             break;
7581                         case 't':
7582                             if (memEQ(posixcc, "digi", 4)) /* digit */
7583                                 namedclass = complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
7584                             else if (memEQ(posixcc, "prin", 4)) /* print */
7585                                 namedclass = complement ? ANYOF_NPRINT : ANYOF_PRINT;
7586                             else if (memEQ(posixcc, "punc", 4)) /* punct */
7587                                 namedclass = complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
7588                             break;
7589                         }
7590                         break;
7591                     case 6:
7592                         if (memEQ(posixcc, "xdigit", 6))
7593                             namedclass = complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
7594                         break;
7595                     }
7596
7597                     if (namedclass == OOB_NAMEDCLASS)
7598                         Simple_vFAIL3("POSIX class [:%.*s:] unknown",
7599                                       t - s - 1, s + 1);
7600                     assert (posixcc[skip] == ':');
7601                     assert (posixcc[skip+1] == ']');
7602                 } else if (!SIZE_ONLY) {
7603                     /* [[=foo=]] and [[.foo.]] are still future. */
7604
7605                     /* adjust RExC_parse so the warning shows after
7606                        the class closes */
7607                     while (UCHARAT(RExC_parse) && UCHARAT(RExC_parse) != ']')
7608                         RExC_parse++;
7609                     Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
7610                 }
7611             } else {
7612                 /* Maternal grandfather:
7613                  * "[:" ending in ":" but not in ":]" */
7614                 RExC_parse = s;
7615             }
7616         }
7617     }
7618
7619     return namedclass;
7620 }
7621
7622 STATIC void
7623 S_checkposixcc(pTHX_ RExC_state_t *pRExC_state)
7624 {
7625     dVAR;
7626     if (POSIXCC(UCHARAT(RExC_parse))) {
7627         const char *s = RExC_parse;
7628         const char  c = *s++;
7629
7630         while (isALNUM(*s))
7631             s++;
7632         if (*s && c == *s && s[1] == ']') {
7633             if (ckWARN(WARN_REGEXP))
7634                 vWARN3(s+2,
7635                         "POSIX syntax [%c %c] belongs inside character classes",
7636                         c, c);
7637
7638             /* [[=foo=]] and [[.foo.]] are still future. */
7639             if (POSIXCC_NOTYET(c)) {
7640                 /* adjust RExC_parse so the error shows after
7641                    the class closes */
7642                 while (UCHARAT(RExC_parse) && UCHARAT(RExC_parse++) != ']')
7643                     NOOP;
7644                 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
7645             }
7646         }
7647     }
7648 }
7649
7650
7651 #define _C_C_T_(NAME,TEST,WORD)                         \
7652 ANYOF_##NAME:                                           \
7653     if (LOC)                                            \
7654         ANYOF_CLASS_SET(ret, ANYOF_##NAME);             \
7655     else {                                              \
7656         for (value = 0; value < 256; value++)           \
7657             if (TEST)                                   \
7658                 ANYOF_BITMAP_SET(ret, value);           \
7659     }                                                   \
7660     yesno = '+';                                        \
7661     what = WORD;                                        \
7662     break;                                              \
7663 case ANYOF_N##NAME:                                     \
7664     if (LOC)                                            \
7665         ANYOF_CLASS_SET(ret, ANYOF_N##NAME);            \
7666     else {                                              \
7667         for (value = 0; value < 256; value++)           \
7668             if (!TEST)                                  \
7669                 ANYOF_BITMAP_SET(ret, value);           \
7670     }                                                   \
7671     yesno = '!';                                        \
7672     what = WORD;                                        \
7673     break
7674
7675 #define _C_C_T_NOLOC_(NAME,TEST,WORD)                   \
7676 ANYOF_##NAME:                                           \
7677         for (value = 0; value < 256; value++)           \
7678             if (TEST)                                   \
7679                 ANYOF_BITMAP_SET(ret, value);           \
7680     yesno = '+';                                        \
7681     what = WORD;                                        \
7682     break;                                              \
7683 case ANYOF_N##NAME:                                     \
7684         for (value = 0; value < 256; value++)           \
7685             if (!TEST)                                  \
7686                 ANYOF_BITMAP_SET(ret, value);           \
7687     yesno = '!';                                        \
7688     what = WORD;                                        \
7689     break
7690
7691 /*
7692    parse a class specification and produce either an ANYOF node that
7693    matches the pattern or if the pattern matches a single char only and
7694    that char is < 256 and we are case insensitive then we produce an 
7695    EXACT node instead.
7696 */
7697
7698 STATIC regnode *
7699 S_regclass(pTHX_ RExC_state_t *pRExC_state, U32 depth)
7700 {
7701     dVAR;
7702     register UV nextvalue;
7703     register IV prevvalue = OOB_UNICODE;
7704     register IV range = 0;
7705     UV value = 0; /* XXX:dmq: needs to be referenceable (unfortunately) */
7706     register regnode *ret;
7707     STRLEN numlen;
7708     IV namedclass;
7709     char *rangebegin = NULL;
7710     bool need_class = 0;
7711     SV *listsv = NULL;
7712     UV n;
7713     bool optimize_invert   = TRUE;
7714     AV* unicode_alternate  = NULL;
7715 #ifdef EBCDIC
7716     UV literal_endpoint = 0;
7717 #endif
7718     UV stored = 0;  /* number of chars stored in the class */
7719
7720     regnode * const orig_emit = RExC_emit; /* Save the original RExC_emit in
7721         case we need to change the emitted regop to an EXACT. */
7722     const char * orig_parse = RExC_parse;
7723     GET_RE_DEBUG_FLAGS_DECL;
7724 #ifndef DEBUGGING
7725     PERL_UNUSED_ARG(depth);
7726 #endif
7727
7728     DEBUG_PARSE("clas");
7729
7730     /* Assume we are going to generate an ANYOF node. */
7731     ret = reganode(pRExC_state, ANYOF, 0);
7732
7733     if (!SIZE_ONLY)
7734         ANYOF_FLAGS(ret) = 0;
7735
7736     if (UCHARAT(RExC_parse) == '^') {   /* Complement of range. */
7737         RExC_naughty++;
7738         RExC_parse++;
7739         if (!SIZE_ONLY)
7740             ANYOF_FLAGS(ret) |= ANYOF_INVERT;
7741     }
7742
7743     if (SIZE_ONLY) {
7744         RExC_size += ANYOF_SKIP;
7745         listsv = &PL_sv_undef; /* For code scanners: listsv always non-NULL. */
7746     }
7747     else {
7748         RExC_emit += ANYOF_SKIP;
7749         if (FOLD)
7750             ANYOF_FLAGS(ret) |= ANYOF_FOLD;
7751         if (LOC)
7752             ANYOF_FLAGS(ret) |= ANYOF_LOCALE;
7753         ANYOF_BITMAP_ZERO(ret);
7754         listsv = newSVpvs("# comment\n");
7755     }
7756
7757     nextvalue = RExC_parse < RExC_end ? UCHARAT(RExC_parse) : 0;
7758
7759     if (!SIZE_ONLY && POSIXCC(nextvalue))
7760         checkposixcc(pRExC_state);
7761
7762     /* allow 1st char to be ] (allowing it to be - is dealt with later) */
7763     if (UCHARAT(RExC_parse) == ']')
7764         goto charclassloop;
7765
7766 parseit:
7767     while (RExC_parse < RExC_end && UCHARAT(RExC_parse) != ']') {
7768
7769     charclassloop:
7770
7771         namedclass = OOB_NAMEDCLASS; /* initialize as illegal */
7772
7773         if (!range)
7774             rangebegin = RExC_parse;
7775         if (UTF) {
7776             value = utf8n_to_uvchr((U8*)RExC_parse,
7777                                    RExC_end - RExC_parse,
7778                                    &numlen, UTF8_ALLOW_DEFAULT);
7779             RExC_parse += numlen;
7780         }
7781         else
7782             value = UCHARAT(RExC_parse++);
7783
7784         nextvalue = RExC_parse < RExC_end ? UCHARAT(RExC_parse) : 0;
7785         if (value == '[' && POSIXCC(nextvalue))
7786             namedclass = regpposixcc(pRExC_state, value);
7787         else if (value == '\\') {
7788             if (UTF) {
7789                 value = utf8n_to_uvchr((U8*)RExC_parse,
7790                                    RExC_end - RExC_parse,
7791                                    &numlen, UTF8_ALLOW_DEFAULT);
7792                 RExC_parse += numlen;
7793             }
7794             else
7795                 value = UCHARAT(RExC_parse++);
7796             /* Some compilers cannot handle switching on 64-bit integer
7797              * values, therefore value cannot be an UV.  Yes, this will
7798              * be a problem later if we want switch on Unicode.
7799              * A similar issue a little bit later when switching on
7800              * namedclass. --jhi */
7801             switch ((I32)value) {
7802             case 'w':   namedclass = ANYOF_ALNUM;       break;
7803             case 'W':   namedclass = ANYOF_NALNUM;      break;
7804             case 's':   namedclass = ANYOF_SPACE;       break;
7805             case 'S':   namedclass = ANYOF_NSPACE;      break;
7806             case 'd':   namedclass = ANYOF_DIGIT;       break;
7807             case 'D':   namedclass = ANYOF_NDIGIT;      break;
7808             case 'v':   namedclass = ANYOF_VERTWS;      break;
7809             case 'V':   namedclass = ANYOF_NVERTWS;     break;
7810             case 'h':   namedclass = ANYOF_HORIZWS;     break;
7811             case 'H':   namedclass = ANYOF_NHORIZWS;    break;
7812             case 'N':  /* Handle \N{NAME} in class */
7813                 {
7814                     /* We only pay attention to the first char of 
7815                     multichar strings being returned. I kinda wonder
7816                     if this makes sense as it does change the behaviour
7817                     from earlier versions, OTOH that behaviour was broken
7818                     as well. */
7819                     UV v; /* value is register so we cant & it /grrr */
7820                     if (reg_namedseq(pRExC_state, &v)) {
7821                         goto parseit;
7822                     }
7823                     value= v; 
7824                 }
7825                 break;
7826             case 'p':
7827             case 'P':
7828                 {
7829                 char *e;
7830                 if (RExC_parse >= RExC_end)
7831                     vFAIL2("Empty \\%c{}", (U8)value);
7832                 if (*RExC_parse == '{') {
7833                     const U8 c = (U8)value;
7834                     e = strchr(RExC_parse++, '}');
7835                     if (!e)
7836                         vFAIL2("Missing right brace on \\%c{}", c);
7837                     while (isSPACE(UCHARAT(RExC_parse)))
7838                         RExC_parse++;
7839                     if (e == RExC_parse)
7840                         vFAIL2("Empty \\%c{}", c);
7841                     n = e - RExC_parse;
7842                     while (isSPACE(UCHARAT(RExC_parse + n - 1)))
7843                         n--;
7844                 }
7845                 else {
7846                     e = RExC_parse;
7847                     n = 1;
7848                 }
7849                 if (!SIZE_ONLY) {
7850                     if (UCHARAT(RExC_parse) == '^') {
7851                          RExC_parse++;
7852                          n--;
7853                          value = value == 'p' ? 'P' : 'p'; /* toggle */
7854                          while (isSPACE(UCHARAT(RExC_parse))) {
7855                               RExC_parse++;
7856                               n--;
7857                          }
7858                     }
7859                     Perl_sv_catpvf(aTHX_ listsv, "%cutf8::%.*s\n",
7860                         (value=='p' ? '+' : '!'), (int)n, RExC_parse);
7861                 }
7862                 RExC_parse = e + 1;
7863                 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
7864                 namedclass = ANYOF_MAX;  /* no official name, but it's named */
7865                 }
7866                 break;
7867             case 'n':   value = '\n';                   break;
7868             case 'r':   value = '\r';                   break;
7869             case 't':   value = '\t';                   break;
7870             case 'f':   value = '\f';                   break;
7871             case 'b':   value = '\b';                   break;
7872             case 'e':   value = ASCII_TO_NATIVE('\033');break;
7873             case 'a':   value = ASCII_TO_NATIVE('\007');break;
7874             case 'x':
7875                 if (*RExC_parse == '{') {
7876                     I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
7877                         | PERL_SCAN_DISALLOW_PREFIX;
7878                     char * const e = strchr(RExC_parse++, '}');
7879                     if (!e)
7880                         vFAIL("Missing right brace on \\x{}");
7881
7882                     numlen = e - RExC_parse;
7883                     value = grok_hex(RExC_parse, &numlen, &flags, NULL);
7884                     RExC_parse = e + 1;
7885                 }
7886                 else {
7887                     I32 flags = PERL_SCAN_DISALLOW_PREFIX;
7888                     numlen = 2;
7889                     value = grok_hex(RExC_parse, &numlen, &flags, NULL);
7890                     RExC_parse += numlen;
7891                 }
7892                 if (PL_encoding && value < 0x100)
7893                     goto recode_encoding;
7894                 break;
7895             case 'c':
7896                 value = UCHARAT(RExC_parse++);
7897                 value = toCTRL(value);
7898                 break;
7899             case '0': case '1': case '2': case '3': case '4':
7900             case '5': case '6': case '7': case '8': case '9':
7901                 {
7902                     I32 flags = 0;
7903                     numlen = 3;
7904                     value = grok_oct(--RExC_parse, &numlen, &flags, NULL);
7905                     RExC_parse += numlen;
7906                     if (PL_encoding && value < 0x100)
7907                         goto recode_encoding;
7908                     break;
7909                 }
7910             recode_encoding:
7911                 {
7912                     SV* enc = PL_encoding;
7913                     value = reg_recode((const char)(U8)value, &enc);
7914                     if (!enc && SIZE_ONLY && ckWARN(WARN_REGEXP))
7915                         vWARN(RExC_parse,
7916                               "Invalid escape in the specified encoding");
7917                     break;
7918                 }
7919             default:
7920                 if (!SIZE_ONLY && isALPHA(value) && ckWARN(WARN_REGEXP))
7921                     vWARN2(RExC_parse,
7922                            "Unrecognized escape \\%c in character class passed through",
7923                            (int)value);
7924                 break;
7925             }
7926         } /* end of \blah */
7927 #ifdef EBCDIC
7928         else
7929             literal_endpoint++;
7930 #endif
7931
7932         if (namedclass > OOB_NAMEDCLASS) { /* this is a named class \blah */
7933
7934             if (!SIZE_ONLY && !need_class)
7935                 ANYOF_CLASS_ZERO(ret);
7936
7937             need_class = 1;
7938
7939             /* a bad range like a-\d, a-[:digit:] ? */
7940             if (range) {
7941                 if (!SIZE_ONLY) {
7942                     if (ckWARN(WARN_REGEXP)) {
7943                         const int w =
7944                             RExC_parse >= rangebegin ?
7945                             RExC_parse - rangebegin : 0;
7946                         vWARN4(RExC_parse,
7947                                "False [] range \"%*.*s\"",
7948                                w, w, rangebegin);
7949                     }
7950                     if (prevvalue < 256) {
7951                         ANYOF_BITMAP_SET(ret, prevvalue);
7952                         ANYOF_BITMAP_SET(ret, '-');
7953                     }
7954                     else {
7955                         ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
7956                         Perl_sv_catpvf(aTHX_ listsv,
7957                                        "%04"UVxf"\n%04"UVxf"\n", (UV)prevvalue, (UV) '-');
7958                     }
7959                 }
7960
7961                 range = 0; /* this was not a true range */
7962             }
7963
7964
7965     
7966             if (!SIZE_ONLY) {
7967                 const char *what = NULL;
7968                 char yesno = 0;
7969
7970                 if (namedclass > OOB_NAMEDCLASS)
7971                     optimize_invert = FALSE;
7972                 /* Possible truncation here but in some 64-bit environments
7973                  * the compiler gets heartburn about switch on 64-bit values.
7974                  * A similar issue a little earlier when switching on value.
7975                  * --jhi */
7976                 switch ((I32)namedclass) {
7977                 case _C_C_T_(ALNUM, isALNUM(value), "Word");
7978                 case _C_C_T_(ALNUMC, isALNUMC(value), "Alnum");
7979                 case _C_C_T_(ALPHA, isALPHA(value), "Alpha");
7980                 case _C_C_T_(BLANK, isBLANK(value), "Blank");
7981                 case _C_C_T_(CNTRL, isCNTRL(value), "Cntrl");
7982                 case _C_C_T_(GRAPH, isGRAPH(value), "Graph");
7983                 case _C_C_T_(LOWER, isLOWER(value), "Lower");
7984                 case _C_C_T_(PRINT, isPRINT(value), "Print");
7985                 case _C_C_T_(PSXSPC, isPSXSPC(value), "Space");
7986                 case _C_C_T_(PUNCT, isPUNCT(value), "Punct");
7987                 case _C_C_T_(SPACE, isSPACE(value), "SpacePerl");
7988                 case _C_C_T_(UPPER, isUPPER(value), "Upper");
7989                 case _C_C_T_(XDIGIT, isXDIGIT(value), "XDigit");
7990                 case _C_C_T_NOLOC_(VERTWS, is_VERTWS_latin1(&value), "VertSpace");
7991                 case _C_C_T_NOLOC_(HORIZWS, is_HORIZWS_latin1(&value), "HorizSpace");
7992                 case ANYOF_ASCII:
7993                     if (LOC)
7994                         ANYOF_CLASS_SET(ret, ANYOF_ASCII);
7995                     else {
7996 #ifndef EBCDIC
7997                         for (value = 0; value < 128; value++)
7998                             ANYOF_BITMAP_SET(ret, value);
7999 #else  /* EBCDIC */
8000                         for (value = 0; value < 256; value++) {
8001                             if (isASCII(value))
8002                                 ANYOF_BITMAP_SET(ret, value);
8003                         }
8004 #endif /* EBCDIC */
8005                     }
8006                     yesno = '+';
8007                     what = "ASCII";
8008                     break;
8009                 case ANYOF_NASCII:
8010                     if (LOC)
8011                         ANYOF_CLASS_SET(ret, ANYOF_NASCII);
8012                     else {
8013 #ifndef EBCDIC
8014                         for (value = 128; value < 256; value++)
8015                             ANYOF_BITMAP_SET(ret, value);
8016 #else  /* EBCDIC */
8017                         for (value = 0; value < 256; value++) {
8018                             if (!isASCII(value))
8019                                 ANYOF_BITMAP_SET(ret, value);
8020                         }
8021 #endif /* EBCDIC */
8022                     }
8023                     yesno = '!';
8024                     what = "ASCII";
8025                     break;              
8026                 case ANYOF_DIGIT:
8027                     if (LOC)
8028                         ANYOF_CLASS_SET(ret, ANYOF_DIGIT);
8029                     else {
8030                         /* consecutive digits assumed */
8031                         for (value = '0'; value <= '9'; value++)
8032                             ANYOF_BITMAP_SET(ret, value);
8033                     }
8034                     yesno = '+';
8035                     what = "Digit";
8036                     break;
8037                 case ANYOF_NDIGIT:
8038                     if (LOC)
8039                         ANYOF_CLASS_SET(ret, ANYOF_NDIGIT);
8040                     else {
8041                         /* consecutive digits assumed */
8042                         for (value = 0; value < '0'; value++)
8043                             ANYOF_BITMAP_SET(ret, value);
8044                         for (value = '9' + 1; value < 256; value++)
8045                             ANYOF_BITMAP_SET(ret, value);
8046                     }
8047                     yesno = '!';
8048                     what = "Digit";
8049                     break;              
8050                 case ANYOF_MAX:
8051                     /* this is to handle \p and \P */
8052                     break;
8053                 default:
8054                     vFAIL("Invalid [::] class");
8055                     break;
8056                 }
8057                 if (what) {
8058                     /* Strings such as "+utf8::isWord\n" */
8059                     Perl_sv_catpvf(aTHX_ listsv, "%cutf8::Is%s\n", yesno, what);
8060                 }
8061                 if (LOC)
8062                     ANYOF_FLAGS(ret) |= ANYOF_CLASS;
8063                 continue;
8064             }
8065         } /* end of namedclass \blah */
8066
8067         if (range) {
8068             if (prevvalue > (IV)value) /* b-a */ {
8069                 const int w = RExC_parse - rangebegin;
8070                 Simple_vFAIL4("Invalid [] range \"%*.*s\"", w, w, rangebegin);
8071                 range = 0; /* not a valid range */
8072             }
8073         }
8074         else {
8075             prevvalue = value; /* save the beginning of the range */
8076             if (*RExC_parse == '-' && RExC_parse+1 < RExC_end &&
8077                 RExC_parse[1] != ']') {
8078                 RExC_parse++;
8079
8080                 /* a bad range like \w-, [:word:]- ? */
8081                 if (namedclass > OOB_NAMEDCLASS) {
8082                     if (ckWARN(WARN_REGEXP)) {
8083                         const int w =
8084                             RExC_parse >= rangebegin ?
8085                             RExC_parse - rangebegin : 0;
8086                         vWARN4(RExC_parse,
8087                                "False [] range \"%*.*s\"",
8088                                w, w, rangebegin);
8089                     }
8090                     if (!SIZE_ONLY)
8091                         ANYOF_BITMAP_SET(ret, '-');
8092                 } else
8093                     range = 1;  /* yeah, it's a range! */
8094                 continue;       /* but do it the next time */
8095             }
8096         }
8097
8098         /* now is the next time */
8099         /*stored += (value - prevvalue + 1);*/
8100         if (!SIZE_ONLY) {
8101             if (prevvalue < 256) {
8102                 const IV ceilvalue = value < 256 ? value : 255;
8103                 IV i;
8104 #ifdef EBCDIC
8105                 /* In EBCDIC [\x89-\x91] should include
8106                  * the \x8e but [i-j] should not. */
8107                 if (literal_endpoint == 2 &&
8108                     ((isLOWER(prevvalue) && isLOWER(ceilvalue)) ||
8109                      (isUPPER(prevvalue) && isUPPER(ceilvalue))))
8110                 {
8111                     if (isLOWER(prevvalue)) {
8112                         for (i = prevvalue; i <= ceilvalue; i++)
8113                             if (isLOWER(i) && !ANYOF_BITMAP_TEST(ret,i)) {
8114                                 stored++;
8115                                 ANYOF_BITMAP_SET(ret, i);
8116                             }
8117                     } else {
8118                         for (i = prevvalue; i <= ceilvalue; i++)
8119                             if (isUPPER(i) && !ANYOF_BITMAP_TEST(ret,i)) {
8120                                 stored++;
8121                                 ANYOF_BITMAP_SET(ret, i);
8122                             }
8123                     }
8124                 }
8125                 else
8126 #endif
8127                       for (i = prevvalue; i <= ceilvalue; i++) {
8128                         if (!ANYOF_BITMAP_TEST(ret,i)) {
8129                             stored++;  
8130                             ANYOF_BITMAP_SET(ret, i);
8131                         }
8132                       }
8133           }
8134           if (value > 255 || UTF) {
8135                 const UV prevnatvalue  = NATIVE_TO_UNI(prevvalue);
8136                 const UV natvalue      = NATIVE_TO_UNI(value);
8137                 stored+=2; /* can't optimize this class */
8138                 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
8139                 if (prevnatvalue < natvalue) { /* what about > ? */
8140                     Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\t%04"UVxf"\n",
8141                                    prevnatvalue, natvalue);
8142                 }
8143                 else if (prevnatvalue == natvalue) {
8144                     Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n", natvalue);
8145                     if (FOLD) {
8146                          U8 foldbuf[UTF8_MAXBYTES_CASE+1];
8147                          STRLEN foldlen;
8148                          const UV f = to_uni_fold(natvalue, foldbuf, &foldlen);
8149
8150 #ifdef EBCDIC /* RD t/uni/fold ff and 6b */
8151                          if (RExC_precomp[0] == ':' &&
8152                              RExC_precomp[1] == '[' &&
8153                              (f == 0xDF || f == 0x92)) {
8154                              f = NATIVE_TO_UNI(f);
8155                         }
8156 #endif
8157                          /* If folding and foldable and a single
8158                           * character, insert also the folded version
8159                           * to the charclass. */
8160                          if (f != value) {
8161 #ifdef EBCDIC /* RD tunifold ligatures s,t fb05, fb06 */
8162                              if ((RExC_precomp[0] == ':' &&
8163                                   RExC_precomp[1] == '[' &&
8164                                   (f == 0xA2 &&
8165                                    (value == 0xFB05 || value == 0xFB06))) ?
8166                                  foldlen == ((STRLEN)UNISKIP(f) - 1) :
8167                                  foldlen == (STRLEN)UNISKIP(f) )
8168 #else
8169                               if (foldlen == (STRLEN)UNISKIP(f))
8170 #endif
8171                                   Perl_sv_catpvf(aTHX_ listsv,
8172                                                  "%04"UVxf"\n", f);
8173                               else {
8174                                   /* Any multicharacter foldings
8175                                    * require the following transform:
8176                                    * [ABCDEF] -> (?:[ABCabcDEFd]|pq|rst)
8177                                    * where E folds into "pq" and F folds
8178                                    * into "rst", all other characters
8179                                    * fold to single characters.  We save
8180                                    * away these multicharacter foldings,
8181                                    * to be later saved as part of the
8182                                    * additional "s" data. */
8183                                   SV *sv;
8184
8185                                   if (!unicode_alternate)
8186                                       unicode_alternate = newAV();
8187                                   sv = newSVpvn_utf8((char*)foldbuf, foldlen,
8188                                                      TRUE);
8189                                   av_push(unicode_alternate, sv);
8190                               }
8191                          }
8192
8193                          /* If folding and the value is one of the Greek
8194                           * sigmas insert a few more sigmas to make the
8195                           * folding rules of the sigmas to work right.
8196                           * Note that not all the possible combinations
8197                           * are handled here: some of them are handled
8198                           * by the standard folding rules, and some of
8199                           * them (literal or EXACTF cases) are handled
8200                           * during runtime in regexec.c:S_find_byclass(). */
8201                          if (value == UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA) {
8202                               Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n",
8203                                              (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA);
8204                               Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n",
8205                                              (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA);
8206                          }
8207                          else if (value == UNICODE_GREEK_CAPITAL_LETTER_SIGMA)
8208                               Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n",
8209                                              (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA);
8210                     }
8211                 }
8212             }
8213 #ifdef EBCDIC
8214             literal_endpoint = 0;
8215 #endif
8216         }
8217
8218         range = 0; /* this range (if it was one) is done now */
8219     }
8220
8221     if (need_class) {
8222         ANYOF_FLAGS(ret) |= ANYOF_LARGE;
8223         if (SIZE_ONLY)
8224             RExC_size += ANYOF_CLASS_ADD_SKIP;
8225         else
8226             RExC_emit += ANYOF_CLASS_ADD_SKIP;
8227     }
8228
8229
8230     if (SIZE_ONLY)
8231         return ret;
8232     /****** !SIZE_ONLY AFTER HERE *********/
8233
8234     if( stored == 1 && (value < 128 || (value < 256 && !UTF))
8235         && !( ANYOF_FLAGS(ret) & ( ANYOF_FLAGS_ALL ^ ANYOF_FOLD ) )
8236     ) {
8237         /* optimize single char class to an EXACT node
8238            but *only* when its not a UTF/high char  */
8239         const char * cur_parse= RExC_parse;
8240         RExC_emit = (regnode *)orig_emit;
8241         RExC_parse = (char *)orig_parse;
8242         ret = reg_node(pRExC_state,
8243                        (U8)((ANYOF_FLAGS(ret) & ANYOF_FOLD) ? EXACTF : EXACT));
8244         RExC_parse = (char *)cur_parse;
8245         *STRING(ret)= (char)value;
8246         STR_LEN(ret)= 1;
8247         RExC_emit += STR_SZ(1);
8248         return ret;
8249     }
8250     /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
8251     if ( /* If the only flag is folding (plus possibly inversion). */
8252         ((ANYOF_FLAGS(ret) & (ANYOF_FLAGS_ALL ^ ANYOF_INVERT)) == ANYOF_FOLD)
8253        ) {
8254         for (value = 0; value < 256; ++value) {
8255             if (ANYOF_BITMAP_TEST(ret, value)) {
8256                 UV fold = PL_fold[value];
8257
8258                 if (fold != value)
8259                     ANYOF_BITMAP_SET(ret, fold);
8260             }
8261         }
8262         ANYOF_FLAGS(ret) &= ~ANYOF_FOLD;
8263     }
8264
8265     /* optimize inverted simple patterns (e.g. [^a-z]) */
8266     if (optimize_invert &&
8267         /* If the only flag is inversion. */
8268         (ANYOF_FLAGS(ret) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
8269         for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
8270             ANYOF_BITMAP(ret)[value] ^= ANYOF_FLAGS_ALL;
8271         ANYOF_FLAGS(ret) = ANYOF_UNICODE_ALL;
8272     }
8273     {
8274         AV * const av = newAV();
8275         SV *rv;
8276         /* The 0th element stores the character class description
8277          * in its textual form: used later (regexec.c:Perl_regclass_swash())
8278          * to initialize the appropriate swash (which gets stored in
8279          * the 1st element), and also useful for dumping the regnode.
8280          * The 2nd element stores the multicharacter foldings,
8281          * used later (regexec.c:S_reginclass()). */
8282         av_store(av, 0, listsv);
8283         av_store(av, 1, NULL);
8284         av_store(av, 2, (SV*)unicode_alternate);
8285         rv = newRV_noinc((SV*)av);
8286         n = add_data(pRExC_state, 1, "s");
8287         RExC_rxi->data->data[n] = (void*)rv;
8288         ARG_SET(ret, n);
8289     }
8290     return ret;
8291 }
8292 #undef _C_C_T_
8293
8294
8295 /* reg_skipcomment()
8296
8297    Absorbs an /x style # comments from the input stream.
8298    Returns true if there is more text remaining in the stream.
8299    Will set the REG_SEEN_RUN_ON_COMMENT flag if the comment
8300    terminates the pattern without including a newline.
8301
8302    Note its the callers responsibility to ensure that we are
8303    actually in /x mode
8304
8305 */
8306
8307 STATIC bool
8308 S_reg_skipcomment(pTHX_ RExC_state_t *pRExC_state)
8309 {
8310     bool ended = 0;
8311     while (RExC_parse < RExC_end)
8312         if (*RExC_parse++ == '\n') {
8313             ended = 1;
8314             break;
8315         }
8316     if (!ended) {
8317         /* we ran off the end of the pattern without ending
8318            the comment, so we have to add an \n when wrapping */
8319         RExC_seen |= REG_SEEN_RUN_ON_COMMENT;
8320         return 0;
8321     } else
8322         return 1;
8323 }
8324
8325 /* nextchar()
8326
8327    Advance that parse position, and optionall absorbs
8328    "whitespace" from the inputstream.
8329
8330    Without /x "whitespace" means (?#...) style comments only,
8331    with /x this means (?#...) and # comments and whitespace proper.
8332
8333    Returns the RExC_parse point from BEFORE the scan occurs.
8334
8335    This is the /x friendly way of saying RExC_parse++.
8336 */
8337
8338 STATIC char*
8339 S_nextchar(pTHX_ RExC_state_t *pRExC_state)
8340 {
8341     char* const retval = RExC_parse++;
8342
8343     for (;;) {
8344         if (*RExC_parse == '(' && RExC_parse[1] == '?' &&
8345                 RExC_parse[2] == '#') {
8346             while (*RExC_parse != ')') {
8347                 if (RExC_parse == RExC_end)
8348                     FAIL("Sequence (?#... not terminated");
8349                 RExC_parse++;
8350             }
8351             RExC_parse++;
8352             continue;
8353         }
8354         if (RExC_flags & RXf_PMf_EXTENDED) {
8355             if (isSPACE(*RExC_parse)) {
8356                 RExC_parse++;
8357                 continue;
8358             }
8359             else if (*RExC_parse == '#') {
8360                 if ( reg_skipcomment( pRExC_state ) )
8361                     continue;
8362             }
8363         }
8364         return retval;
8365     }
8366 }
8367
8368 /*
8369 - reg_node - emit a node
8370 */
8371 STATIC regnode *                        /* Location. */
8372 S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op)
8373 {
8374     dVAR;
8375     register regnode *ptr;
8376     regnode * const ret = RExC_emit;
8377     GET_RE_DEBUG_FLAGS_DECL;
8378
8379     if (SIZE_ONLY) {
8380         SIZE_ALIGN(RExC_size);
8381         RExC_size += 1;
8382         return(ret);
8383     }
8384     if (RExC_emit >= RExC_emit_bound)
8385         Perl_croak(aTHX_ "panic: reg_node overrun trying to emit %d", op);
8386
8387     NODE_ALIGN_FILL(ret);
8388     ptr = ret;
8389     FILL_ADVANCE_NODE(ptr, op);
8390 #ifdef RE_TRACK_PATTERN_OFFSETS
8391     if (RExC_offsets) {         /* MJD */
8392         MJD_OFFSET_DEBUG(("%s:%d: (op %s) %s %"UVuf" (len %"UVuf") (max %"UVuf").\n", 
8393               "reg_node", __LINE__, 
8394               PL_reg_name[op],
8395               (UV)(RExC_emit - RExC_emit_start) > RExC_offsets[0] 
8396                 ? "Overwriting end of array!\n" : "OK",
8397               (UV)(RExC_emit - RExC_emit_start),
8398               (UV)(RExC_parse - RExC_start),
8399               (UV)RExC_offsets[0])); 
8400         Set_Node_Offset(RExC_emit, RExC_parse + (op == END));
8401     }
8402 #endif
8403     RExC_emit = ptr;
8404     return(ret);
8405 }
8406
8407 /*
8408 - reganode - emit a node with an argument
8409 */
8410 STATIC regnode *                        /* Location. */
8411 S_reganode(pTHX_ RExC_state_t *pRExC_state, U8 op, U32 arg)
8412 {
8413     dVAR;
8414     register regnode *ptr;
8415     regnode * const ret = RExC_emit;
8416     GET_RE_DEBUG_FLAGS_DECL;
8417
8418     if (SIZE_ONLY) {
8419         SIZE_ALIGN(RExC_size);
8420         RExC_size += 2;
8421         /* 
8422            We can't do this:
8423            
8424            assert(2==regarglen[op]+1); 
8425         
8426            Anything larger than this has to allocate the extra amount.
8427            If we changed this to be:
8428            
8429            RExC_size += (1 + regarglen[op]);
8430            
8431            then it wouldn't matter. Its not clear what side effect
8432            might come from that so its not done so far.
8433            -- dmq
8434         */
8435         return(ret);
8436     }
8437     if (RExC_emit >= RExC_emit_bound)
8438         Perl_croak(aTHX_ "panic: reg_node overrun trying to emit %d", op);
8439
8440     NODE_ALIGN_FILL(ret);
8441     ptr = ret;
8442     FILL_ADVANCE_NODE_ARG(ptr, op, arg);
8443 #ifdef RE_TRACK_PATTERN_OFFSETS
8444     if (RExC_offsets) {         /* MJD */
8445         MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s %"UVuf" <- %"UVuf" (max %"UVuf").\n", 
8446               "reganode",
8447               __LINE__,
8448               PL_reg_name[op],
8449               (UV)(RExC_emit - RExC_emit_start) > RExC_offsets[0] ? 
8450               "Overwriting end of array!\n" : "OK",
8451               (UV)(RExC_emit - RExC_emit_start),
8452               (UV)(RExC_parse - RExC_start),
8453               (UV)RExC_offsets[0])); 
8454         Set_Cur_Node_Offset;
8455     }
8456 #endif            
8457     RExC_emit = ptr;
8458     return(ret);
8459 }
8460
8461 /*
8462 - reguni - emit (if appropriate) a Unicode character
8463 */
8464 STATIC STRLEN
8465 S_reguni(pTHX_ const RExC_state_t *pRExC_state, UV uv, char* s)
8466 {
8467     dVAR;
8468     return SIZE_ONLY ? UNISKIP(uv) : (uvchr_to_utf8((U8*)s, uv) - (U8*)s);
8469 }
8470
8471 /*
8472 - reginsert - insert an operator in front of already-emitted operand
8473 *
8474 * Means relocating the operand.
8475 */
8476 STATIC void
8477 S_reginsert(pTHX_ RExC_state_t *pRExC_state, U8 op, regnode *opnd, U32 depth)
8478 {
8479     dVAR;
8480     register regnode *src;
8481     register regnode *dst;
8482     register regnode *place;
8483     const int offset = regarglen[(U8)op];
8484     const int size = NODE_STEP_REGNODE + offset;
8485     GET_RE_DEBUG_FLAGS_DECL;
8486     PERL_UNUSED_ARG(depth);
8487 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
8488     DEBUG_PARSE_FMT("inst"," - %s",PL_reg_name[op]);
8489     if (SIZE_ONLY) {
8490         RExC_size += size;
8491         return;
8492     }
8493
8494     src = RExC_emit;
8495     RExC_emit += size;
8496     dst = RExC_emit;
8497     if (RExC_open_parens) {
8498         int paren;
8499         /*DEBUG_PARSE_FMT("inst"," - %"IVdf, (IV)RExC_npar);*/
8500         for ( paren=0 ; paren < RExC_npar ; paren++ ) {
8501             if ( RExC_open_parens[paren] >= opnd ) {
8502                 /*DEBUG_PARSE_FMT("open"," - %d",size);*/
8503                 RExC_open_parens[paren] += size;
8504             } else {
8505                 /*DEBUG_PARSE_FMT("open"," - %s","ok");*/
8506             }
8507             if ( RExC_close_parens[paren] >= opnd ) {
8508                 /*DEBUG_PARSE_FMT("close"," - %d",size);*/
8509                 RExC_close_parens[paren] += size;
8510             } else {
8511                 /*DEBUG_PARSE_FMT("close"," - %s","ok");*/
8512             }
8513         }
8514     }
8515
8516     while (src > opnd) {
8517         StructCopy(--src, --dst, regnode);
8518 #ifdef RE_TRACK_PATTERN_OFFSETS
8519         if (RExC_offsets) {     /* MJD 20010112 */
8520             MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s copy %"UVuf" -> %"UVuf" (max %"UVuf").\n",
8521                   "reg_insert",
8522                   __LINE__,
8523                   PL_reg_name[op],
8524                   (UV)(dst - RExC_emit_start) > RExC_offsets[0] 
8525                     ? "Overwriting end of array!\n" : "OK",
8526                   (UV)(src - RExC_emit_start),
8527                   (UV)(dst - RExC_emit_start),
8528                   (UV)RExC_offsets[0])); 
8529             Set_Node_Offset_To_R(dst-RExC_emit_start, Node_Offset(src));
8530             Set_Node_Length_To_R(dst-RExC_emit_start, Node_Length(src));
8531         }
8532 #endif
8533     }
8534     
8535
8536     place = opnd;               /* Op node, where operand used to be. */
8537 #ifdef RE_TRACK_PATTERN_OFFSETS
8538     if (RExC_offsets) {         /* MJD */
8539         MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s %"UVuf" <- %"UVuf" (max %"UVuf").\n", 
8540               "reginsert",
8541               __LINE__,
8542               PL_reg_name[op],
8543               (UV)(place - RExC_emit_start) > RExC_offsets[0] 
8544               ? "Overwriting end of array!\n" : "OK",
8545               (UV)(place - RExC_emit_start),
8546               (UV)(RExC_parse - RExC_start),
8547               (UV)RExC_offsets[0]));
8548         Set_Node_Offset(place, RExC_parse);
8549         Set_Node_Length(place, 1);
8550     }
8551 #endif    
8552     src = NEXTOPER(place);
8553     FILL_ADVANCE_NODE(place, op);
8554     Zero(src, offset, regnode);
8555 }
8556
8557 /*
8558 - regtail - set the next-pointer at the end of a node chain of p to val.
8559 - SEE ALSO: regtail_study
8560 */
8561 /* TODO: All three parms should be const */
8562 STATIC void
8563 S_regtail(pTHX_ RExC_state_t *pRExC_state, regnode *p, const regnode *val,U32 depth)
8564 {
8565     dVAR;
8566     register regnode *scan;
8567     GET_RE_DEBUG_FLAGS_DECL;
8568 #ifndef DEBUGGING
8569     PERL_UNUSED_ARG(depth);
8570 #endif
8571
8572     if (SIZE_ONLY)
8573         return;
8574
8575     /* Find last node. */
8576     scan = p;
8577     for (;;) {
8578         regnode * const temp = regnext(scan);
8579         DEBUG_PARSE_r({
8580             SV * const mysv=sv_newmortal();
8581             DEBUG_PARSE_MSG((scan==p ? "tail" : ""));
8582             regprop(RExC_rx, mysv, scan);
8583             PerlIO_printf(Perl_debug_log, "~ %s (%d) %s %s\n",
8584                 SvPV_nolen_const(mysv), REG_NODE_NUM(scan),
8585                     (temp == NULL ? "->" : ""),
8586                     (temp == NULL ? PL_reg_name[OP(val)] : "")
8587             );
8588         });
8589         if (temp == NULL)
8590             break;
8591         scan = temp;
8592     }
8593
8594     if (reg_off_by_arg[OP(scan)]) {
8595         ARG_SET(scan, val - scan);
8596     }
8597     else {
8598         NEXT_OFF(scan) = val - scan;
8599     }
8600 }
8601
8602 #ifdef DEBUGGING
8603 /*
8604 - regtail_study - set the next-pointer at the end of a node chain of p to val.
8605 - Look for optimizable sequences at the same time.
8606 - currently only looks for EXACT chains.
8607
8608 This is expermental code. The idea is to use this routine to perform 
8609 in place optimizations on branches and groups as they are constructed,
8610 with the long term intention of removing optimization from study_chunk so
8611 that it is purely analytical.
8612
8613 Currently only used when in DEBUG mode. The macro REGTAIL_STUDY() is used
8614 to control which is which.
8615
8616 */
8617 /* TODO: All four parms should be const */
8618
8619 STATIC U8
8620 S_regtail_study(pTHX_ RExC_state_t *pRExC_state, regnode *p, const regnode *val,U32 depth)
8621 {
8622     dVAR;
8623     register regnode *scan;
8624     U8 exact = PSEUDO;
8625 #ifdef EXPERIMENTAL_INPLACESCAN
8626     I32 min = 0;
8627 #endif
8628
8629     GET_RE_DEBUG_FLAGS_DECL;
8630
8631
8632     if (SIZE_ONLY)
8633         return exact;
8634
8635     /* Find last node. */
8636
8637     scan = p;
8638     for (;;) {
8639         regnode * const temp = regnext(scan);
8640 #ifdef EXPERIMENTAL_INPLACESCAN
8641         if (PL_regkind[OP(scan)] == EXACT)
8642             if (join_exact(pRExC_state,scan,&min,1,val,depth+1))
8643                 return EXACT;
8644 #endif
8645         if ( exact ) {
8646             switch (OP(scan)) {
8647                 case EXACT:
8648                 case EXACTF:
8649                 case EXACTFL:
8650                         if( exact == PSEUDO )
8651                             exact= OP(scan);
8652                         else if ( exact != OP(scan) )
8653                             exact= 0;
8654                 case NOTHING:
8655                     break;
8656                 default:
8657                     exact= 0;
8658             }
8659         }
8660         DEBUG_PARSE_r({
8661             SV * const mysv=sv_newmortal();
8662             DEBUG_PARSE_MSG((scan==p ? "tsdy" : ""));
8663             regprop(RExC_rx, mysv, scan);
8664             PerlIO_printf(Perl_debug_log, "~ %s (%d) -> %s\n",
8665                 SvPV_nolen_const(mysv),
8666                 REG_NODE_NUM(scan),
8667                 PL_reg_name[exact]);
8668         });
8669         if (temp == NULL)
8670             break;
8671         scan = temp;
8672     }
8673     DEBUG_PARSE_r({
8674         SV * const mysv_val=sv_newmortal();
8675         DEBUG_PARSE_MSG("");
8676         regprop(RExC_rx, mysv_val, val);
8677         PerlIO_printf(Perl_debug_log, "~ attach to %s (%"IVdf") offset to %"IVdf"\n",
8678                       SvPV_nolen_const(mysv_val),
8679                       (IV)REG_NODE_NUM(val),
8680                       (IV)(val - scan)
8681         );
8682     });
8683     if (reg_off_by_arg[OP(scan)]) {
8684         ARG_SET(scan, val - scan);
8685     }
8686     else {
8687         NEXT_OFF(scan) = val - scan;
8688     }
8689
8690     return exact;
8691 }
8692 #endif
8693
8694 /*
8695  - regcurly - a little FSA that accepts {\d+,?\d*}
8696  */
8697 STATIC I32
8698 S_regcurly(register const char *s)
8699 {
8700     if (*s++ != '{')
8701         return FALSE;
8702     if (!isDIGIT(*s))
8703         return FALSE;
8704     while (isDIGIT(*s))
8705         s++;
8706     if (*s == ',')
8707         s++;
8708     while (isDIGIT(*s))
8709         s++;
8710     if (*s != '}')
8711         return FALSE;
8712     return TRUE;
8713 }
8714
8715
8716 /*
8717  - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
8718  */
8719 #ifdef DEBUGGING
8720 void 
8721 S_regdump_extflags(pTHX_ const char *lead, const U32 flags) {
8722     int bit;
8723     int set=0;
8724     for (bit=0; bit<32; bit++) {
8725         if (flags & (1<<bit)) {
8726             if (!set++ && lead) 
8727                 PerlIO_printf(Perl_debug_log, "%s",lead);
8728             PerlIO_printf(Perl_debug_log, "%s ",PL_reg_extflags_name[bit]);
8729         }               
8730     }      
8731     if (lead)  {
8732         if (set) 
8733             PerlIO_printf(Perl_debug_log, "\n");
8734         else 
8735             PerlIO_printf(Perl_debug_log, "%s[none-set]\n",lead);
8736     }            
8737 }   
8738 #endif
8739
8740 void
8741 Perl_regdump(pTHX_ const regexp *r)
8742 {
8743 #ifdef DEBUGGING
8744     dVAR;
8745     SV * const sv = sv_newmortal();
8746     SV *dsv= sv_newmortal();
8747     RXi_GET_DECL(r,ri);
8748     GET_RE_DEBUG_FLAGS_DECL;
8749
8750     (void)dumpuntil(r, ri->program, ri->program + 1, NULL, NULL, sv, 0, 0);
8751
8752     /* Header fields of interest. */
8753     if (r->anchored_substr) {
8754         RE_PV_QUOTED_DECL(s, 0, dsv, SvPVX_const(r->anchored_substr), 
8755             RE_SV_DUMPLEN(r->anchored_substr), 30);
8756         PerlIO_printf(Perl_debug_log,
8757                       "anchored %s%s at %"IVdf" ",
8758                       s, RE_SV_TAIL(r->anchored_substr),
8759                       (IV)r->anchored_offset);
8760     } else if (r->anchored_utf8) {
8761         RE_PV_QUOTED_DECL(s, 1, dsv, SvPVX_const(r->anchored_utf8), 
8762             RE_SV_DUMPLEN(r->anchored_utf8), 30);
8763         PerlIO_printf(Perl_debug_log,
8764                       "anchored utf8 %s%s at %"IVdf" ",
8765                       s, RE_SV_TAIL(r->anchored_utf8),
8766                       (IV)r->anchored_offset);
8767     }                 
8768     if (r->float_substr) {
8769         RE_PV_QUOTED_DECL(s, 0, dsv, SvPVX_const(r->float_substr), 
8770             RE_SV_DUMPLEN(r->float_substr), 30);
8771         PerlIO_printf(Perl_debug_log,
8772                       "floating %s%s at %"IVdf"..%"UVuf" ",
8773                       s, RE_SV_TAIL(r->float_substr),
8774                       (IV)r->float_min_offset, (UV)r->float_max_offset);
8775     } else if (r->float_utf8) {
8776         RE_PV_QUOTED_DECL(s, 1, dsv, SvPVX_const(r->float_utf8), 
8777             RE_SV_DUMPLEN(r->float_utf8), 30);
8778         PerlIO_printf(Perl_debug_log,
8779                       "floating utf8 %s%s at %"IVdf"..%"UVuf" ",
8780                       s, RE_SV_TAIL(r->float_utf8),
8781                       (IV)r->float_min_offset, (UV)r->float_max_offset);
8782     }
8783     if (r->check_substr || r->check_utf8)
8784         PerlIO_printf(Perl_debug_log,
8785                       (const char *)
8786                       (r->check_substr == r->float_substr
8787                        && r->check_utf8 == r->float_utf8
8788                        ? "(checking floating" : "(checking anchored"));
8789     if (r->extflags & RXf_NOSCAN)
8790         PerlIO_printf(Perl_debug_log, " noscan");
8791     if (r->extflags & RXf_CHECK_ALL)
8792         PerlIO_printf(Perl_debug_log, " isall");
8793     if (r->check_substr || r->check_utf8)
8794         PerlIO_printf(Perl_debug_log, ") ");
8795
8796     if (ri->regstclass) {
8797         regprop(r, sv, ri->regstclass);
8798         PerlIO_printf(Perl_debug_log, "stclass %s ", SvPVX_const(sv));
8799     }
8800     if (r->extflags & RXf_ANCH) {
8801         PerlIO_printf(Perl_debug_log, "anchored");
8802         if (r->extflags & RXf_ANCH_BOL)
8803             PerlIO_printf(Perl_debug_log, "(BOL)");
8804         if (r->extflags & RXf_ANCH_MBOL)
8805             PerlIO_printf(Perl_debug_log, "(MBOL)");
8806         if (r->extflags & RXf_ANCH_SBOL)
8807             PerlIO_printf(Perl_debug_log, "(SBOL)");
8808         if (r->extflags & RXf_ANCH_GPOS)
8809             PerlIO_printf(Perl_debug_log, "(GPOS)");
8810         PerlIO_putc(Perl_debug_log, ' ');
8811     }
8812     if (r->extflags & RXf_GPOS_SEEN)
8813         PerlIO_printf(Perl_debug_log, "GPOS:%"UVuf" ", (UV)r->gofs);
8814     if (r->intflags & PREGf_SKIP)
8815         PerlIO_printf(Perl_debug_log, "plus ");
8816     if (r->intflags & PREGf_IMPLICIT)
8817         PerlIO_printf(Perl_debug_log, "implicit ");
8818     PerlIO_printf(Perl_debug_log, "minlen %"IVdf" ", (IV)r->minlen);
8819     if (r->extflags & RXf_EVAL_SEEN)
8820         PerlIO_printf(Perl_debug_log, "with eval ");
8821     PerlIO_printf(Perl_debug_log, "\n");
8822     DEBUG_FLAGS_r(regdump_extflags("r->extflags: ",r->extflags));            
8823 #else
8824     PERL_UNUSED_CONTEXT;
8825     PERL_UNUSED_ARG(r);
8826 #endif  /* DEBUGGING */
8827 }
8828
8829 /*
8830 - regprop - printable representation of opcode
8831 */
8832 void
8833 Perl_regprop(pTHX_ const regexp *prog, SV *sv, const regnode *o)
8834 {
8835 #ifdef DEBUGGING
8836     dVAR;
8837     register int k;
8838     RXi_GET_DECL(prog,progi);
8839     GET_RE_DEBUG_FLAGS_DECL;
8840     
8841
8842     sv_setpvn(sv, "", 0);
8843
8844     if (OP(o) > REGNODE_MAX)            /* regnode.type is unsigned */
8845         /* It would be nice to FAIL() here, but this may be called from
8846            regexec.c, and it would be hard to supply pRExC_state. */
8847         Perl_croak(aTHX_ "Corrupted regexp opcode %d > %d", (int)OP(o), (int)REGNODE_MAX);
8848     sv_catpv(sv, PL_reg_name[OP(o)]); /* Take off const! */
8849
8850     k = PL_regkind[OP(o)];
8851
8852     if (k == EXACT) {
8853         sv_catpvs(sv, " ");
8854         /* Using is_utf8_string() (via PERL_PV_UNI_DETECT) 
8855          * is a crude hack but it may be the best for now since 
8856          * we have no flag "this EXACTish node was UTF-8" 
8857          * --jhi */
8858         pv_pretty(sv, STRING(o), STR_LEN(o), 60, PL_colors[0], PL_colors[1],
8859                   PERL_PV_ESCAPE_UNI_DETECT |
8860                   PERL_PV_PRETTY_ELLIPSES   |
8861                   PERL_PV_PRETTY_LTGT       |
8862                   PERL_PV_PRETTY_NOCLEAR
8863                   );
8864     } else if (k == TRIE) {
8865         /* print the details of the trie in dumpuntil instead, as
8866          * progi->data isn't available here */
8867         const char op = OP(o);
8868         const U32 n = ARG(o);
8869         const reg_ac_data * const ac = IS_TRIE_AC(op) ?
8870                (reg_ac_data *)progi->data->data[n] :
8871                NULL;
8872         const reg_trie_data * const trie
8873             = (reg_trie_data*)progi->data->data[!IS_TRIE_AC(op) ? n : ac->trie];
8874         
8875         Perl_sv_catpvf(aTHX_ sv, "-%s",PL_reg_name[o->flags]);
8876         DEBUG_TRIE_COMPILE_r(
8877             Perl_sv_catpvf(aTHX_ sv,
8878                 "<S:%"UVuf"/%"IVdf" W:%"UVuf" L:%"UVuf"/%"UVuf" C:%"UVuf"/%"UVuf">",
8879                 (UV)trie->startstate,
8880                 (IV)trie->statecount-1, /* -1 because of the unused 0 element */
8881                 (UV)trie->wordcount,
8882                 (UV)trie->minlen,
8883                 (UV)trie->maxlen,
8884                 (UV)TRIE_CHARCOUNT(trie),
8885                 (UV)trie->uniquecharcount
8886             )
8887         );
8888         if ( IS_ANYOF_TRIE(op) || trie->bitmap ) {
8889             int i;
8890             int rangestart = -1;
8891             U8* bitmap = IS_ANYOF_TRIE(op) ? (U8*)ANYOF_BITMAP(o) : (U8*)TRIE_BITMAP(trie);
8892             sv_catpvs(sv, "[");
8893             for (i = 0; i <= 256; i++) {
8894                 if (i < 256 && BITMAP_TEST(bitmap,i)) {
8895                     if (rangestart == -1)
8896                         rangestart = i;
8897                 } else if (rangestart != -1) {
8898                     if (i <= rangestart + 3)
8899                         for (; rangestart < i; rangestart++)
8900                             put_byte(sv, rangestart);
8901                     else {
8902                         put_byte(sv, rangestart);
8903                         sv_catpvs(sv, "-");
8904                         put_byte(sv, i - 1);
8905                     }
8906                     rangestart = -1;
8907                 }
8908             }
8909             sv_catpvs(sv, "]");
8910         } 
8911          
8912     } else if (k == CURLY) {
8913         if (OP(o) == CURLYM || OP(o) == CURLYN || OP(o) == CURLYX)
8914             Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
8915         Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
8916     }
8917     else if (k == WHILEM && o->flags)                   /* Ordinal/of */
8918         Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
8919     else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP || OP(o)==ACCEPT) {
8920         Perl_sv_catpvf(aTHX_ sv, "%d", (int)ARG(o));    /* Parenth number */
8921         if ( prog->paren_names ) {
8922             if ( k != REF || OP(o) < NREF) {        
8923                 AV *list= (AV *)progi->data->data[progi->name_list_idx];
8924                 SV **name= av_fetch(list, ARG(o), 0 );
8925                 if (name)
8926                     Perl_sv_catpvf(aTHX_ sv, " '%"SVf"'", SVfARG(*name));
8927             }       
8928             else {
8929                 AV *list= (AV *)progi->data->data[ progi->name_list_idx ];
8930                 SV *sv_dat=(SV*)progi->data->data[ ARG( o ) ];
8931                 I32 *nums=(I32*)SvPVX(sv_dat);
8932                 SV **name= av_fetch(list, nums[0], 0 );
8933                 I32 n;
8934                 if (name) {
8935                     for ( n=0; n<SvIVX(sv_dat); n++ ) {
8936                         Perl_sv_catpvf(aTHX_ sv, "%s%"IVdf,
8937                                     (n ? "," : ""), (IV)nums[n]);
8938                     }
8939                     Perl_sv_catpvf(aTHX_ sv, " '%"SVf"'", SVfARG(*name));
8940                 }
8941             }
8942         }            
8943     } else if (k == GOSUB) 
8944         Perl_sv_catpvf(aTHX_ sv, "%d[%+d]", (int)ARG(o),(int)ARG2L(o)); /* Paren and offset */
8945     else if (k == VERB) {
8946         if (!o->flags) 
8947             Perl_sv_catpvf(aTHX_ sv, ":%"SVf, 
8948                 SVfARG((SV*)progi->data->data[ ARG( o ) ]));
8949     } else if (k == LOGICAL)
8950         Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags);     /* 2: embedded, otherwise 1 */
8951     else if (k == FOLDCHAR)
8952         Perl_sv_catpvf(aTHX_ sv, "[0x%"UVXf"]", PTR2UV(ARG(o)) );
8953     else if (k == ANYOF) {
8954         int i, rangestart = -1;
8955         const U8 flags = ANYOF_FLAGS(o);
8956
8957         /* Should be synchronized with * ANYOF_ #xdefines in regcomp.h */
8958         static const char * const anyofs[] = {
8959             "\\w",
8960             "\\W",
8961             "\\s",
8962             "\\S",
8963             "\\d",
8964             "\\D",
8965             "[:alnum:]",
8966             "[:^alnum:]",
8967             "[:alpha:]",
8968             "[:^alpha:]",
8969             "[:ascii:]",
8970             "[:^ascii:]",
8971             "[:ctrl:]",
8972             "[:^ctrl:]",
8973             "[:graph:]",
8974             "[:^graph:]",
8975             "[:lower:]",
8976             "[:^lower:]",
8977             "[:print:]",
8978             "[:^print:]",
8979             "[:punct:]",
8980             "[:^punct:]",
8981             "[:upper:]",
8982             "[:^upper:]",
8983             "[:xdigit:]",
8984             "[:^xdigit:]",
8985             "[:space:]",
8986             "[:^space:]",
8987             "[:blank:]",
8988             "[:^blank:]"
8989         };
8990
8991         if (flags & ANYOF_LOCALE)
8992             sv_catpvs(sv, "{loc}");
8993         if (flags & ANYOF_FOLD)
8994             sv_catpvs(sv, "{i}");
8995         Perl_sv_catpvf(aTHX_ sv, "[%s", PL_colors[0]);
8996         if (flags & ANYOF_INVERT)
8997             sv_catpvs(sv, "^");
8998         for (i = 0; i <= 256; i++) {
8999             if (i < 256 && ANYOF_BITMAP_TEST(o,i)) {
9000                 if (rangestart == -1)
9001                     rangestart = i;
9002             } else if (rangestart != -1) {
9003                 if (i <= rangestart + 3)
9004                     for (; rangestart < i; rangestart++)
9005                         put_byte(sv, rangestart);
9006                 else {
9007                     put_byte(sv, rangestart);
9008                     sv_catpvs(sv, "-");
9009                     put_byte(sv, i - 1);
9010                 }
9011                 rangestart = -1;
9012             }
9013         }
9014
9015         if (o->flags & ANYOF_CLASS)
9016             for (i = 0; i < (int)(sizeof(anyofs)/sizeof(char*)); i++)
9017                 if (ANYOF_CLASS_TEST(o,i))
9018                     sv_catpv(sv, anyofs[i]);
9019
9020         if (flags & ANYOF_UNICODE)
9021             sv_catpvs(sv, "{unicode}");
9022         else if (flags & ANYOF_UNICODE_ALL)
9023             sv_catpvs(sv, "{unicode_all}");
9024
9025         {
9026             SV *lv;
9027             SV * const sw = regclass_swash(prog, o, FALSE, &lv, 0);
9028         
9029             if (lv) {
9030                 if (sw) {
9031                     U8 s[UTF8_MAXBYTES_CASE+1];
9032                 
9033                     for (i = 0; i <= 256; i++) { /* just the first 256 */
9034                         uvchr_to_utf8(s, i);
9035                         
9036                         if (i < 256 && swash_fetch(sw, s, TRUE)) {
9037                             if (rangestart == -1)
9038                                 rangestart = i;
9039                         } else if (rangestart != -1) {
9040                             if (i <= rangestart + 3)
9041                                 for (; rangestart < i; rangestart++) {
9042                                     const U8 * const e = uvchr_to_utf8(s,rangestart);
9043                                     U8 *p;
9044                                     for(p = s; p < e; p++)
9045                                         put_byte(sv, *p);
9046                                 }
9047                             else {
9048                                 const U8 *e = uvchr_to_utf8(s,rangestart);
9049                                 U8 *p;
9050                                 for (p = s; p < e; p++)
9051                                     put_byte(sv, *p);
9052                                 sv_catpvs(sv, "-");
9053                                 e = uvchr_to_utf8(s, i-1);
9054                                 for (p = s; p < e; p++)
9055                                     put_byte(sv, *p);
9056                                 }
9057                                 rangestart = -1;
9058                             }
9059                         }
9060                         
9061                     sv_catpvs(sv, "..."); /* et cetera */
9062                 }
9063
9064                 {
9065                     char *s = savesvpv(lv);
9066                     char * const origs = s;
9067                 
9068                     while (*s && *s != '\n')
9069                         s++;
9070                 
9071                     if (*s == '\n') {
9072                         const char * const t = ++s;
9073                         
9074                         while (*s) {
9075                             if (*s == '\n')
9076                                 *s = ' ';
9077                             s++;
9078                         }
9079                         if (s[-1] == ' ')
9080                             s[-1] = 0;
9081                         
9082                         sv_catpv(sv, t);
9083                     }
9084                 
9085                     Safefree(origs);
9086                 }
9087             }
9088         }
9089
9090         Perl_sv_catpvf(aTHX_ sv, "%s]", PL_colors[1]);
9091     }
9092     else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
9093         Perl_sv_catpvf(aTHX_ sv, "[%d]", -(o->flags));
9094 #else
9095     PERL_UNUSED_CONTEXT;
9096     PERL_UNUSED_ARG(sv);
9097     PERL_UNUSED_ARG(o);
9098     PERL_UNUSED_ARG(prog);
9099 #endif  /* DEBUGGING */
9100 }
9101
9102 SV *
9103 Perl_re_intuit_string(pTHX_ REGEXP * const r)
9104 {                               /* Assume that RE_INTUIT is set */
9105     dVAR;
9106     struct regexp *const prog = (struct regexp *)SvANY(r);
9107     GET_RE_DEBUG_FLAGS_DECL;
9108     PERL_UNUSED_CONTEXT;
9109
9110     DEBUG_COMPILE_r(
9111         {
9112             const char * const s = SvPV_nolen_const(prog->check_substr
9113                       ? prog->check_substr : prog->check_utf8);
9114
9115             if (!PL_colorset) reginitcolors();
9116             PerlIO_printf(Perl_debug_log,
9117                       "%sUsing REx %ssubstr:%s \"%s%.60s%s%s\"\n",
9118                       PL_colors[4],
9119                       prog->check_substr ? "" : "utf8 ",
9120                       PL_colors[5],PL_colors[0],
9121                       s,
9122                       PL_colors[1],
9123                       (strlen(s) > 60 ? "..." : ""));
9124         } );
9125
9126     return prog->check_substr ? prog->check_substr : prog->check_utf8;
9127 }
9128
9129 /* 
9130    pregfree() 
9131    
9132    handles refcounting and freeing the perl core regexp structure. When 
9133    it is necessary to actually free the structure the first thing it 
9134    does is call the 'free' method of the regexp_engine associated to to 
9135    the regexp, allowing the handling of the void *pprivate; member 
9136    first. (This routine is not overridable by extensions, which is why 
9137    the extensions free is called first.)
9138    
9139    See regdupe and regdupe_internal if you change anything here. 
9140 */
9141 #ifndef PERL_IN_XSUB_RE
9142 void
9143 Perl_pregfree(pTHX_ REGEXP *r)
9144 {
9145     SvREFCNT_dec(r);
9146 }
9147
9148 void
9149 Perl_pregfree2(pTHX_ REGEXP *rx)
9150 {
9151     dVAR;
9152     struct regexp *const r = (struct regexp *)SvANY(rx);
9153     GET_RE_DEBUG_FLAGS_DECL;
9154
9155     if (r->mother_re) {
9156         ReREFCNT_dec(r->mother_re);
9157     } else {
9158         CALLREGFREE_PVT(rx); /* free the private data */
9159         if (r->paren_names)
9160             SvREFCNT_dec(r->paren_names);
9161         Safefree(RXp_WRAPPED(r));
9162     }        
9163     if (r->substrs) {
9164         if (r->anchored_substr)
9165             SvREFCNT_dec(r->anchored_substr);
9166         if (r->anchored_utf8)
9167             SvREFCNT_dec(r->anchored_utf8);
9168         if (r->float_substr)
9169             SvREFCNT_dec(r->float_substr);
9170         if (r->float_utf8)
9171             SvREFCNT_dec(r->float_utf8);
9172         Safefree(r->substrs);
9173     }
9174     RX_MATCH_COPY_FREE(rx);
9175 #ifdef PERL_OLD_COPY_ON_WRITE
9176     if (r->saved_copy)
9177         SvREFCNT_dec(r->saved_copy);
9178 #endif
9179     Safefree(r->swap);
9180     Safefree(r->offs);
9181 }
9182
9183 /*  reg_temp_copy()
9184     
9185     This is a hacky workaround to the structural issue of match results
9186     being stored in the regexp structure which is in turn stored in
9187     PL_curpm/PL_reg_curpm. The problem is that due to qr// the pattern
9188     could be PL_curpm in multiple contexts, and could require multiple
9189     result sets being associated with the pattern simultaneously, such
9190     as when doing a recursive match with (??{$qr})
9191     
9192     The solution is to make a lightweight copy of the regexp structure 
9193     when a qr// is returned from the code executed by (??{$qr}) this
9194     lightweight copy doesnt actually own any of its data except for
9195     the starp/end and the actual regexp structure itself. 
9196     
9197 */    
9198     
9199     
9200 REGEXP *
9201 Perl_reg_temp_copy (pTHX_ REGEXP *rx) {
9202     REGEXP *ret_x = newSV_type(SVt_REGEXP);
9203     struct regexp *ret = (struct regexp *)SvANY(ret_x);
9204     struct regexp *const r = (struct regexp *)SvANY(rx);
9205     register const I32 npar = r->nparens+1;
9206     (void)ReREFCNT_inc(rx);
9207     /* FIXME ORANGE (once we start actually using the regular SV fields.) */
9208     StructCopy(r, ret, regexp);
9209     Newx(ret->offs, npar, regexp_paren_pair);
9210     Copy(r->offs, ret->offs, npar, regexp_paren_pair);
9211     if (r->substrs) {
9212         Newx(ret->substrs, 1, struct reg_substr_data);
9213         StructCopy(r->substrs, ret->substrs, struct reg_substr_data);
9214
9215         SvREFCNT_inc_void(ret->anchored_substr);
9216         SvREFCNT_inc_void(ret->anchored_utf8);
9217         SvREFCNT_inc_void(ret->float_substr);
9218         SvREFCNT_inc_void(ret->float_utf8);
9219
9220         /* check_substr and check_utf8, if non-NULL, point to either their
9221            anchored or float namesakes, and don't hold a second reference.  */
9222     }
9223     RX_MATCH_COPIED_off(ret_x);
9224 #ifdef PERL_OLD_COPY_ON_WRITE
9225     ret->saved_copy = NULL;
9226 #endif
9227     ret->mother_re = rx;
9228     ret->swap = NULL;
9229     
9230     return ret_x;
9231 }
9232 #endif
9233
9234 /* regfree_internal() 
9235
9236    Free the private data in a regexp. This is overloadable by 
9237    extensions. Perl takes care of the regexp structure in pregfree(), 
9238    this covers the *pprivate pointer which technically perldoesnt 
9239    know about, however of course we have to handle the 
9240    regexp_internal structure when no extension is in use. 
9241    
9242    Note this is called before freeing anything in the regexp 
9243    structure. 
9244  */
9245  
9246 void
9247 Perl_regfree_internal(pTHX_ REGEXP * const rx)
9248 {
9249     dVAR;
9250     struct regexp *const r = (struct regexp *)SvANY(rx);
9251     RXi_GET_DECL(r,ri);
9252     GET_RE_DEBUG_FLAGS_DECL;
9253     
9254     DEBUG_COMPILE_r({
9255         if (!PL_colorset)
9256             reginitcolors();
9257         {
9258             SV *dsv= sv_newmortal();
9259             RE_PV_QUOTED_DECL(s, (r->extflags & RXf_UTF8),
9260                 dsv, RXp_PRECOMP(r), RXp_PRELEN(r), 60);
9261             PerlIO_printf(Perl_debug_log,"%sFreeing REx:%s %s\n", 
9262                 PL_colors[4],PL_colors[5],s);
9263         }
9264     });
9265 #ifdef RE_TRACK_PATTERN_OFFSETS
9266     if (ri->u.offsets)
9267         Safefree(ri->u.offsets);             /* 20010421 MJD */
9268 #endif
9269     if (ri->data) {
9270         int n = ri->data->count;
9271         PAD* new_comppad = NULL;
9272         PAD* old_comppad;
9273         PADOFFSET refcnt;
9274
9275         while (--n >= 0) {
9276           /* If you add a ->what type here, update the comment in regcomp.h */
9277             switch (ri->data->what[n]) {
9278             case 's':
9279             case 'S':
9280             case 'u':
9281                 SvREFCNT_dec((SV*)ri->data->data[n]);
9282                 break;
9283             case 'f':
9284                 Safefree(ri->data->data[n]);
9285                 break;
9286             case 'p':
9287                 new_comppad = (AV*)ri->data->data[n];
9288                 break;
9289             case 'o':
9290                 if (new_comppad == NULL)
9291                     Perl_croak(aTHX_ "panic: pregfree comppad");
9292                 PAD_SAVE_LOCAL(old_comppad,
9293                     /* Watch out for global destruction's random ordering. */
9294                     (SvTYPE(new_comppad) == SVt_PVAV) ? new_comppad : NULL
9295                 );
9296                 OP_REFCNT_LOCK;
9297                 refcnt = OpREFCNT_dec((OP_4tree*)ri->data->data[n]);
9298                 OP_REFCNT_UNLOCK;
9299                 if (!refcnt)
9300                     op_free((OP_4tree*)ri->data->data[n]);
9301
9302                 PAD_RESTORE_LOCAL(old_comppad);
9303                 SvREFCNT_dec((SV*)new_comppad);
9304                 new_comppad = NULL;
9305                 break;
9306             case 'n':
9307                 break;
9308             case 'T':           
9309                 { /* Aho Corasick add-on structure for a trie node.
9310                      Used in stclass optimization only */
9311                     U32 refcount;
9312                     reg_ac_data *aho=(reg_ac_data*)ri->data->data[n];
9313                     OP_REFCNT_LOCK;
9314                     refcount = --aho->refcount;
9315                     OP_REFCNT_UNLOCK;
9316                     if ( !refcount ) {
9317                         PerlMemShared_free(aho->states);
9318                         PerlMemShared_free(aho->fail);
9319                          /* do this last!!!! */
9320                         PerlMemShared_free(ri->data->data[n]);
9321                         PerlMemShared_free(ri->regstclass);
9322                     }
9323                 }
9324                 break;
9325             case 't':
9326                 {
9327                     /* trie structure. */
9328                     U32 refcount;
9329                     reg_trie_data *trie=(reg_trie_data*)ri->data->data[n];
9330                     OP_REFCNT_LOCK;
9331                     refcount = --trie->refcount;
9332                     OP_REFCNT_UNLOCK;
9333                     if ( !refcount ) {
9334                         PerlMemShared_free(trie->charmap);
9335                         PerlMemShared_free(trie->states);
9336                         PerlMemShared_free(trie->trans);
9337                         if (trie->bitmap)
9338                             PerlMemShared_free(trie->bitmap);
9339                         if (trie->wordlen)
9340                             PerlMemShared_free(trie->wordlen);
9341                         if (trie->jump)
9342                             PerlMemShared_free(trie->jump);
9343                         if (trie->nextword)
9344                             PerlMemShared_free(trie->nextword);
9345                         /* do this last!!!! */
9346                         PerlMemShared_free(ri->data->data[n]);
9347                     }
9348                 }
9349                 break;
9350             default:
9351                 Perl_croak(aTHX_ "panic: regfree data code '%c'", ri->data->what[n]);
9352             }
9353         }
9354         Safefree(ri->data->what);
9355         Safefree(ri->data);
9356     }
9357
9358     Safefree(ri);
9359 }
9360
9361 #define sv_dup_inc(s,t) SvREFCNT_inc(sv_dup(s,t))
9362 #define av_dup_inc(s,t) (AV*)SvREFCNT_inc(sv_dup((SV*)s,t))
9363 #define hv_dup_inc(s,t) (HV*)SvREFCNT_inc(sv_dup((SV*)s,t))
9364 #define SAVEPVN(p,n)    ((p) ? savepvn(p,n) : NULL)
9365
9366 /* 
9367    re_dup - duplicate a regexp. 
9368    
9369    This routine is expected to clone a given regexp structure. It is not
9370    compiler under USE_ITHREADS.
9371
9372    After all of the core data stored in struct regexp is duplicated
9373    the regexp_engine.dupe method is used to copy any private data
9374    stored in the *pprivate pointer. This allows extensions to handle
9375    any duplication it needs to do.
9376
9377    See pregfree() and regfree_internal() if you change anything here. 
9378 */
9379 #if defined(USE_ITHREADS)
9380 #ifndef PERL_IN_XSUB_RE
9381 void
9382 Perl_re_dup_guts(pTHX_ const REGEXP *sstr, REGEXP *dstr, CLONE_PARAMS *param)
9383 {
9384     dVAR;
9385     I32 npar;
9386     const struct regexp *r = (const struct regexp *)SvANY(sstr);
9387     struct regexp *ret = (struct regexp *)SvANY(dstr);
9388     
9389     npar = r->nparens+1;
9390     Newx(ret->offs, npar, regexp_paren_pair);
9391     Copy(r->offs, ret->offs, npar, regexp_paren_pair);
9392     if(ret->swap) {
9393         /* no need to copy these */
9394         Newx(ret->swap, npar, regexp_paren_pair);
9395     }
9396
9397     if (ret->substrs) {
9398         /* Do it this way to avoid reading from *r after the StructCopy().
9399            That way, if any of the sv_dup_inc()s dislodge *r from the L1
9400            cache, it doesn't matter.  */
9401         const bool anchored = r->check_substr == r->anchored_substr;
9402         Newx(ret->substrs, 1, struct reg_substr_data);
9403         StructCopy(r->substrs, ret->substrs, struct reg_substr_data);
9404
9405         ret->anchored_substr = sv_dup_inc(ret->anchored_substr, param);
9406         ret->anchored_utf8 = sv_dup_inc(ret->anchored_utf8, param);
9407         ret->float_substr = sv_dup_inc(ret->float_substr, param);
9408         ret->float_utf8 = sv_dup_inc(ret->float_utf8, param);
9409
9410         /* check_substr and check_utf8, if non-NULL, point to either their
9411            anchored or float namesakes, and don't hold a second reference.  */
9412
9413         if (ret->check_substr) {
9414             if (anchored) {
9415                 assert(r->check_utf8 == r->anchored_utf8);
9416                 ret->check_substr = ret->anchored_substr;
9417                 ret->check_utf8 = ret->anchored_utf8;
9418             } else {
9419                 assert(r->check_substr == r->float_substr);
9420                 assert(r->check_utf8 == r->float_utf8);
9421                 ret->check_substr = ret->float_substr;
9422                 ret->check_utf8 = ret->float_utf8;
9423             }
9424         }
9425     }
9426
9427     RXp_WRAPPED(ret)    = SAVEPVN(RXp_WRAPPED(ret), RXp_WRAPLEN(ret)+1);
9428     ret->paren_names    = hv_dup_inc(ret->paren_names, param);
9429
9430     if (ret->pprivate)
9431         RXi_SET(ret,CALLREGDUPE_PVT(dstr,param));
9432
9433     if (RX_MATCH_COPIED(dstr))
9434         ret->subbeg  = SAVEPVN(ret->subbeg, ret->sublen);
9435     else
9436         ret->subbeg = NULL;
9437 #ifdef PERL_OLD_COPY_ON_WRITE
9438     ret->saved_copy = NULL;
9439 #endif
9440
9441     ret->mother_re      = NULL;
9442     ret->gofs = 0;
9443     ret->seen_evals = 0;
9444 }
9445 #endif /* PERL_IN_XSUB_RE */
9446
9447 /*
9448    regdupe_internal()
9449    
9450    This is the internal complement to regdupe() which is used to copy
9451    the structure pointed to by the *pprivate pointer in the regexp.
9452    This is the core version of the extension overridable cloning hook.
9453    The regexp structure being duplicated will be copied by perl prior
9454    to this and will be provided as the regexp *r argument, however 
9455    with the /old/ structures pprivate pointer value. Thus this routine
9456    may override any copying normally done by perl.
9457    
9458    It returns a pointer to the new regexp_internal structure.
9459 */
9460
9461 void *
9462 Perl_regdupe_internal(pTHX_ REGEXP * const rx, CLONE_PARAMS *param)
9463 {
9464     dVAR;
9465     struct regexp *const r = (struct regexp *)SvANY(rx);
9466     regexp_internal *reti;
9467     int len, npar;
9468     RXi_GET_DECL(r,ri);
9469     
9470     npar = r->nparens+1;
9471     len = ProgLen(ri);
9472     
9473     Newxc(reti, sizeof(regexp_internal) + (len+1)*sizeof(regnode), char, regexp_internal);
9474     Copy(ri->program, reti->program, len+1, regnode);
9475     
9476
9477     reti->regstclass = NULL;
9478
9479     if (ri->data) {
9480         struct reg_data *d;
9481         const int count = ri->data->count;
9482         int i;
9483
9484         Newxc(d, sizeof(struct reg_data) + count*sizeof(void *),
9485                 char, struct reg_data);
9486         Newx(d->what, count, U8);
9487
9488         d->count = count;
9489         for (i = 0; i < count; i++) {
9490             d->what[i] = ri->data->what[i];
9491             switch (d->what[i]) {
9492                 /* legal options are one of: sSfpontTu
9493                    see also regcomp.h and pregfree() */
9494             case 's':
9495             case 'S':
9496             case 'p': /* actually an AV, but the dup function is identical.  */
9497             case 'u': /* actually an HV, but the dup function is identical.  */
9498                 d->data[i] = sv_dup_inc((SV *)ri->data->data[i], param);
9499                 break;
9500             case 'f':
9501                 /* This is cheating. */
9502                 Newx(d->data[i], 1, struct regnode_charclass_class);
9503                 StructCopy(ri->data->data[i], d->data[i],
9504                             struct regnode_charclass_class);
9505                 reti->regstclass = (regnode*)d->data[i];
9506                 break;
9507             case 'o':
9508                 /* Compiled op trees are readonly and in shared memory,
9509                    and can thus be shared without duplication. */
9510                 OP_REFCNT_LOCK;
9511                 d->data[i] = (void*)OpREFCNT_inc((OP*)ri->data->data[i]);
9512                 OP_REFCNT_UNLOCK;
9513                 break;
9514             case 'T':
9515                 /* Trie stclasses are readonly and can thus be shared
9516                  * without duplication. We free the stclass in pregfree
9517                  * when the corresponding reg_ac_data struct is freed.
9518                  */
9519                 reti->regstclass= ri->regstclass;
9520                 /* Fall through */
9521             case 't':
9522                 OP_REFCNT_LOCK;
9523                 ((reg_trie_data*)ri->data->data[i])->refcount++;
9524                 OP_REFCNT_UNLOCK;
9525                 /* Fall through */
9526             case 'n':
9527                 d->data[i] = ri->data->data[i];
9528                 break;
9529             default:
9530                 Perl_croak(aTHX_ "panic: re_dup unknown data code '%c'", ri->data->what[i]);
9531             }
9532         }
9533
9534         reti->data = d;
9535     }
9536     else
9537         reti->data = NULL;
9538
9539     reti->name_list_idx = ri->name_list_idx;
9540
9541 #ifdef RE_TRACK_PATTERN_OFFSETS
9542     if (ri->u.offsets) {
9543         Newx(reti->u.offsets, 2*len+1, U32);
9544         Copy(ri->u.offsets, reti->u.offsets, 2*len+1, U32);
9545     }
9546 #else
9547     SetProgLen(reti,len);
9548 #endif
9549
9550     return (void*)reti;
9551 }
9552
9553 #endif    /* USE_ITHREADS */
9554
9555 /* 
9556    reg_stringify() 
9557    
9558    converts a regexp embedded in a MAGIC struct to its stringified form, 
9559    caching the converted form in the struct and returns the cached 
9560    string. 
9561
9562    If lp is nonnull then it is used to return the length of the 
9563    resulting string
9564    
9565    If flags is nonnull and the returned string contains UTF8 then 
9566    (*flags & 1) will be true.
9567    
9568    If haseval is nonnull then it is used to return whether the pattern 
9569    contains evals.
9570    
9571    Normally called via macro: 
9572    
9573         CALLREG_STRINGIFY(mg,&len,&utf8);
9574         
9575    And internally with
9576    
9577         CALLREG_AS_STR(mg,&lp,&flags,&haseval)        
9578     
9579    See sv_2pv_flags() in sv.c for an example of internal usage.
9580     
9581  */
9582 #ifndef PERL_IN_XSUB_RE
9583
9584 char *
9585 Perl_reg_stringify(pTHX_ MAGIC *mg, STRLEN *lp, U32 *flags, I32 *haseval ) {
9586     dVAR;
9587     const REGEXP * const re = (REGEXP *)mg->mg_obj;
9588     if (haseval) 
9589         *haseval = RX_SEEN_EVALS(re);
9590     if (flags)    
9591         *flags = ((RX_EXTFLAGS(re) & RXf_UTF8) ? 1 : 0);
9592     if (lp)
9593         *lp = RX_WRAPLEN(re);
9594     return RX_WRAPPED(re);
9595 }
9596
9597 /*
9598  - regnext - dig the "next" pointer out of a node
9599  */
9600 regnode *
9601 Perl_regnext(pTHX_ register regnode *p)
9602 {
9603     dVAR;
9604     register I32 offset;
9605
9606     if (!p)
9607         return(NULL);
9608
9609     offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
9610     if (offset == 0)
9611         return(NULL);
9612
9613     return(p+offset);
9614 }
9615 #endif
9616
9617 STATIC void     
9618 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
9619 {
9620     va_list args;
9621     STRLEN l1 = strlen(pat1);
9622     STRLEN l2 = strlen(pat2);
9623     char buf[512];
9624     SV *msv;
9625     const char *message;
9626
9627     if (l1 > 510)
9628         l1 = 510;
9629     if (l1 + l2 > 510)
9630         l2 = 510 - l1;
9631     Copy(pat1, buf, l1 , char);
9632     Copy(pat2, buf + l1, l2 , char);
9633     buf[l1 + l2] = '\n';
9634     buf[l1 + l2 + 1] = '\0';
9635 #ifdef I_STDARG
9636     /* ANSI variant takes additional second argument */
9637     va_start(args, pat2);
9638 #else
9639     va_start(args);
9640 #endif
9641     msv = vmess(buf, &args);
9642     va_end(args);
9643     message = SvPV_const(msv,l1);
9644     if (l1 > 512)
9645         l1 = 512;
9646     Copy(message, buf, l1 , char);
9647     buf[l1-1] = '\0';                   /* Overwrite \n */
9648     Perl_croak(aTHX_ "%s", buf);
9649 }
9650
9651 /* XXX Here's a total kludge.  But we need to re-enter for swash routines. */
9652
9653 #ifndef PERL_IN_XSUB_RE
9654 void
9655 Perl_save_re_context(pTHX)
9656 {
9657     dVAR;
9658
9659     struct re_save_state *state;
9660
9661     SAVEVPTR(PL_curcop);
9662     SSGROW(SAVESTACK_ALLOC_FOR_RE_SAVE_STATE + 1);
9663
9664     state = (struct re_save_state *)(PL_savestack + PL_savestack_ix);
9665     PL_savestack_ix += SAVESTACK_ALLOC_FOR_RE_SAVE_STATE;
9666     SSPUSHINT(SAVEt_RE_STATE);
9667
9668     Copy(&PL_reg_state, state, 1, struct re_save_state);
9669
9670     PL_reg_start_tmp = 0;
9671     PL_reg_start_tmpl = 0;
9672     PL_reg_oldsaved = NULL;
9673     PL_reg_oldsavedlen = 0;
9674     PL_reg_maxiter = 0;
9675     PL_reg_leftiter = 0;
9676     PL_reg_poscache = NULL;
9677     PL_reg_poscache_size = 0;
9678 #ifdef PERL_OLD_COPY_ON_WRITE
9679     PL_nrs = NULL;
9680 #endif
9681
9682     /* Save $1..$n (#18107: UTF-8 s/(\w+)/uc($1)/e); AMS 20021106. */
9683     if (PL_curpm) {
9684         const REGEXP * const rx = PM_GETRE(PL_curpm);
9685         if (rx) {
9686             U32 i;
9687             for (i = 1; i <= RX_NPARENS(rx); i++) {
9688                 char digits[TYPE_CHARS(long)];
9689                 const STRLEN len = my_snprintf(digits, sizeof(digits), "%lu", (long)i);
9690                 GV *const *const gvp
9691                     = (GV**)hv_fetch(PL_defstash, digits, len, 0);
9692
9693                 if (gvp) {
9694                     GV * const gv = *gvp;
9695                     if (SvTYPE(gv) == SVt_PVGV && GvSV(gv))
9696                         save_scalar(gv);
9697                 }
9698             }
9699         }
9700     }
9701 }
9702 #endif
9703
9704 static void
9705 clear_re(pTHX_ void *r)
9706 {
9707     dVAR;
9708     ReREFCNT_dec((REGEXP *)r);
9709 }
9710
9711 #ifdef DEBUGGING
9712
9713 STATIC void
9714 S_put_byte(pTHX_ SV *sv, int c)
9715 {
9716     /* Our definition of isPRINT() ignores locales, so only bytes that are
9717        not part of UTF-8 are considered printable. I assume that the same
9718        holds for UTF-EBCDIC.
9719        Also, code point 255 is not printable in either (it's E0 in EBCDIC,
9720        which Wikipedia says:
9721
9722        EO, or Eight Ones, is an 8-bit EBCDIC character code represented as all
9723        ones (binary 1111 1111, hexadecimal FF). It is similar, but not
9724        identical, to the ASCII delete (DEL) or rubout control character.
9725        ) So the old condition can be simplified to !isPRINT(c)  */
9726     if (!isPRINT(c))
9727         Perl_sv_catpvf(aTHX_ sv, "\\%o", c);
9728     else {
9729         const char string = c;
9730         if (c == '-' || c == ']' || c == '\\' || c == '^')
9731             sv_catpvs(sv, "\\");
9732         sv_catpvn(sv, &string, 1);
9733     }
9734 }
9735
9736
9737 #define CLEAR_OPTSTART \
9738     if (optstart) STMT_START { \
9739             DEBUG_OPTIMISE_r(PerlIO_printf(Perl_debug_log, " (%"IVdf" nodes)\n", (IV)(node - optstart))); \
9740             optstart=NULL; \
9741     } STMT_END
9742
9743 #define DUMPUNTIL(b,e) CLEAR_OPTSTART; node=dumpuntil(r,start,(b),(e),last,sv,indent+1,depth+1);
9744
9745 STATIC const regnode *
9746 S_dumpuntil(pTHX_ const regexp *r, const regnode *start, const regnode *node,
9747             const regnode *last, const regnode *plast, 
9748             SV* sv, I32 indent, U32 depth)
9749 {
9750     dVAR;
9751     register U8 op = PSEUDO;    /* Arbitrary non-END op. */
9752     register const regnode *next;
9753     const regnode *optstart= NULL;
9754     
9755     RXi_GET_DECL(r,ri);
9756     GET_RE_DEBUG_FLAGS_DECL;
9757     
9758 #ifdef DEBUG_DUMPUNTIL
9759     PerlIO_printf(Perl_debug_log, "--- %d : %d - %d - %d\n",indent,node-start,
9760         last ? last-start : 0,plast ? plast-start : 0);
9761 #endif
9762             
9763     if (plast && plast < last) 
9764         last= plast;
9765
9766     while (PL_regkind[op] != END && (!last || node < last)) {
9767         /* While that wasn't END last time... */
9768         NODE_ALIGN(node);
9769         op = OP(node);
9770         if (op == CLOSE || op == WHILEM)
9771             indent--;
9772         next = regnext((regnode *)node);
9773
9774         /* Where, what. */
9775         if (OP(node) == OPTIMIZED) {
9776             if (!optstart && RE_DEBUG_FLAG(RE_DEBUG_COMPILE_OPTIMISE))
9777                 optstart = node;
9778             else
9779                 goto after_print;
9780         } else
9781             CLEAR_OPTSTART;
9782         
9783         regprop(r, sv, node);
9784         PerlIO_printf(Perl_debug_log, "%4"IVdf":%*s%s", (IV)(node - start),
9785                       (int)(2*indent + 1), "", SvPVX_const(sv));
9786         
9787         if (OP(node) != OPTIMIZED) {                  
9788             if (next == NULL)           /* Next ptr. */
9789                 PerlIO_printf(Perl_debug_log, " (0)");
9790             else if (PL_regkind[(U8)op] == BRANCH && PL_regkind[OP(next)] != BRANCH )
9791                 PerlIO_printf(Perl_debug_log, " (FAIL)");
9792             else 
9793                 PerlIO_printf(Perl_debug_log, " (%"IVdf")", (IV)(next - start));
9794             (void)PerlIO_putc(Perl_debug_log, '\n'); 
9795         }
9796         
9797       after_print:
9798         if (PL_regkind[(U8)op] == BRANCHJ) {
9799             assert(next);
9800             {
9801                 register const regnode *nnode = (OP(next) == LONGJMP
9802                                              ? regnext((regnode *)next)
9803                                              : next);
9804                 if (last && nnode > last)
9805                     nnode = last;
9806                 DUMPUNTIL(NEXTOPER(NEXTOPER(node)), nnode);
9807             }
9808         }
9809         else if (PL_regkind[(U8)op] == BRANCH) {
9810             assert(next);
9811             DUMPUNTIL(NEXTOPER(node), next);
9812         }
9813         else if ( PL_regkind[(U8)op]  == TRIE ) {
9814             const regnode *this_trie = node;
9815             const char op = OP(node);
9816             const U32 n = ARG(node);
9817             const reg_ac_data * const ac = op>=AHOCORASICK ?
9818                (reg_ac_data *)ri->data->data[n] :
9819                NULL;
9820             const reg_trie_data * const trie =
9821                 (reg_trie_data*)ri->data->data[op<AHOCORASICK ? n : ac->trie];
9822 #ifdef DEBUGGING
9823             AV *const trie_words = (AV *) ri->data->data[n + TRIE_WORDS_OFFSET];
9824 #endif
9825             const regnode *nextbranch= NULL;
9826             I32 word_idx;
9827             sv_setpvn(sv, "", 0);
9828             for (word_idx= 0; word_idx < (I32)trie->wordcount; word_idx++) {
9829                 SV ** const elem_ptr = av_fetch(trie_words,word_idx,0);
9830                 
9831                 PerlIO_printf(Perl_debug_log, "%*s%s ",
9832                    (int)(2*(indent+3)), "",
9833                     elem_ptr ? pv_pretty(sv, SvPV_nolen_const(*elem_ptr), SvCUR(*elem_ptr), 60,
9834                             PL_colors[0], PL_colors[1],
9835                             (SvUTF8(*elem_ptr) ? PERL_PV_ESCAPE_UNI : 0) |
9836                             PERL_PV_PRETTY_ELLIPSES    |
9837                             PERL_PV_PRETTY_LTGT
9838                             )
9839                             : "???"
9840                 );
9841                 if (trie->jump) {
9842                     U16 dist= trie->jump[word_idx+1];
9843                     PerlIO_printf(Perl_debug_log, "(%"UVuf")\n",
9844                                   (UV)((dist ? this_trie + dist : next) - start));
9845                     if (dist) {
9846                         if (!nextbranch)
9847                             nextbranch= this_trie + trie->jump[0];    
9848                         DUMPUNTIL(this_trie + dist, nextbranch);
9849                     }
9850                     if (nextbranch && PL_regkind[OP(nextbranch)]==BRANCH)
9851                         nextbranch= regnext((regnode *)nextbranch);
9852                 } else {
9853                     PerlIO_printf(Perl_debug_log, "\n");
9854                 }
9855             }
9856             if (last && next > last)
9857                 node= last;
9858             else
9859                 node= next;
9860         }
9861         else if ( op == CURLY ) {   /* "next" might be very big: optimizer */
9862             DUMPUNTIL(NEXTOPER(node) + EXTRA_STEP_2ARGS,
9863                     NEXTOPER(node) + EXTRA_STEP_2ARGS + 1);
9864         }
9865         else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
9866             assert(next);
9867             DUMPUNTIL(NEXTOPER(node) + EXTRA_STEP_2ARGS, next);
9868         }
9869         else if ( op == PLUS || op == STAR) {
9870             DUMPUNTIL(NEXTOPER(node), NEXTOPER(node) + 1);
9871         }
9872         else if (op == ANYOF) {
9873             /* arglen 1 + class block */
9874             node += 1 + ((ANYOF_FLAGS(node) & ANYOF_LARGE)
9875                     ? ANYOF_CLASS_SKIP : ANYOF_SKIP);
9876             node = NEXTOPER(node);
9877         }
9878         else if (PL_regkind[(U8)op] == EXACT) {
9879             /* Literal string, where present. */
9880             node += NODE_SZ_STR(node) - 1;
9881             node = NEXTOPER(node);
9882         }
9883         else {
9884             node = NEXTOPER(node);
9885             node += regarglen[(U8)op];
9886         }
9887         if (op == CURLYX || op == OPEN)
9888             indent++;
9889     }
9890     CLEAR_OPTSTART;
9891 #ifdef DEBUG_DUMPUNTIL    
9892     PerlIO_printf(Perl_debug_log, "--- %d\n", (int)indent);
9893 #endif
9894     return node;
9895 }
9896
9897 #endif  /* DEBUGGING */
9898
9899 /*
9900  * Local variables:
9901  * c-indentation-style: bsd
9902  * c-basic-offset: 4
9903  * indent-tabs-mode: t
9904  * End:
9905  *
9906  * ex: set ts=8 sts=4 sw=4 noet:
9907  */