]> git.vpit.fr Git - perl/modules/re-engine-Hooks.git/blob - src/5016003/regcomp.c
7b13e4374e06d4a9604b79092b7cda6db6311830
[perl/modules/re-engine-Hooks.git] / src / 5016003 / regcomp.c
1 /*    regcomp.c
2  */
3
4 /*
5  * 'A fair jaw-cracker dwarf-language must be.'            --Samwise Gamgee
6  *
7  *     [p.285 of _The Lord of the Rings_, II/iii: "The Ring Goes South"]
8  */
9
10 /* This file contains functions for compiling a regular expression.  See
11  * also regexec.c which funnily enough, contains functions for executing
12  * a regular expression.
13  *
14  * This file is also copied at build time to ext/re/re_comp.c, where
15  * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT.
16  * This causes the main functions to be compiled under new names and with
17  * debugging support added, which makes "use re 'debug'" work.
18  */
19
20 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
21  * confused with the original package (see point 3 below).  Thanks, Henry!
22  */
23
24 /* Additional note: this code is very heavily munged from Henry's version
25  * in places.  In some spots I've traded clarity for efficiency, so don't
26  * blame Henry for some of the lack of readability.
27  */
28
29 /* The names of the functions have been changed from regcomp and
30  * regexec to pregcomp and pregexec in order to avoid conflicts
31  * with the POSIX routines of the same names.
32 */
33
34 #ifdef PERL_EXT_RE_BUILD
35 #include "re_top.h"
36 #endif
37
38 /*
39  * pregcomp and pregexec -- regsub and regerror are not used in perl
40  *
41  * Copyright (c) 1986 by University of Toronto.
42  * Written by Henry Spencer.  Not derived from licensed software.
43  *
44  * Permission is granted to anyone to use this software for any
45  * purpose on any computer system, and to redistribute it freely,
46  * subject to the following restrictions:
47  *
48  * 1. The author is not responsible for the consequences of use of
49  *  this software, no matter how awful, even if they arise
50  *  from defects in it.
51  *
52  * 2. The origin of this software must not be misrepresented, either
53  *  by explicit claim or by omission.
54  *
55  * 3. Altered versions must be plainly marked as such, and must not
56  *  be misrepresented as being the original software.
57  *
58  *
59  ****    Alterations to Henry's code are...
60  ****
61  ****    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
62  ****    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
63  ****    by Larry Wall and others
64  ****
65  ****    You may distribute under the terms of either the GNU General Public
66  ****    License or the Artistic License, as specified in the README file.
67
68  *
69  * Beware that some of this code is subtly aware of the way operator
70  * precedence is structured in regular expressions.  Serious changes in
71  * regular-expression syntax might require a total rethink.
72  */
73 #include "EXTERN.h"
74 #define PERL_IN_REGCOMP_C
75 #undef PERL_IN_XSUB_RE
76 #define PERL_IN_XSUB_RE 1
77 #include "perl.h"
78 #undef PERL_IN_XSUB_RE
79
80 #ifndef PERL_IN_XSUB_RE
81 #include "re_defs.h"
82 #endif
83
84 #define REG_COMP_C
85 #ifdef PERL_IN_XSUB_RE
86 #  include "re_comp.h"
87 #else
88 #  include "regcomp.h"
89 #endif
90
91 #include "dquote_static.c"
92 #ifndef PERL_IN_XSUB_RE
93 #  include "charclass_invlists.h"
94 #endif
95
96 #ifdef op
97 #undef op
98 #endif /* op */
99
100 #ifdef MSDOS
101 #  if defined(BUGGY_MSC6)
102  /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
103 #    pragma optimize("a",off)
104  /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
105 #    pragma optimize("w",on )
106 #  endif /* BUGGY_MSC6 */
107 #endif /* MSDOS */
108
109 #ifndef STATIC
110 #define STATIC static
111 #endif
112
113 typedef struct RExC_state_t {
114  U32  flags;   /* are we folding, multilining? */
115  char *precomp;  /* uncompiled string. */
116  REGEXP *rx_sv;   /* The SV that is the regexp. */
117  regexp *rx;                    /* perl core regexp structure */
118  regexp_internal *rxi;           /* internal data for regexp object pprivate field */
119  char *start;   /* Start of input for compile */
120  char *end;   /* End of input for compile */
121  char *parse;   /* Input-scan pointer. */
122  I32  whilem_seen;  /* number of WHILEM in this expr */
123  regnode *emit_start;  /* Start of emitted-code area */
124  regnode *emit_bound;  /* First regnode outside of the allocated space */
125  regnode *emit;   /* Code-emit pointer; &regdummy = don't = compiling */
126  I32  naughty;  /* How bad is this pattern? */
127  I32  sawback;  /* Did we see \1, ...? */
128  U32  seen;
129  I32  size;   /* Code size. */
130  I32  npar;   /* Capture buffer count, (OPEN). */
131  I32  cpar;   /* Capture buffer count, (CLOSE). */
132  I32  nestroot;  /* root parens we are in - used by accept */
133  I32  extralen;
134  I32  seen_zerolen;
135  I32  seen_evals;
136  regnode **open_parens;  /* pointers to open parens */
137  regnode **close_parens;  /* pointers to close parens */
138  regnode *opend;   /* END node in program */
139  I32  utf8;  /* whether the pattern is utf8 or not */
140  I32  orig_utf8; /* whether the pattern was originally in utf8 */
141         /* XXX use this for future optimisation of case
142         * where pattern must be upgraded to utf8. */
143  I32  uni_semantics; /* If a d charset modifier should use unicode
144         rules, even if the pattern is not in
145         utf8 */
146  HV  *paren_names;  /* Paren names */
147
148  regnode **recurse;  /* Recurse regops */
149  I32  recurse_count;  /* Number of recurse regops */
150  I32  in_lookbehind;
151  I32  contains_locale;
152  I32  override_recoding;
153 #if ADD_TO_REGEXEC
154  char  *starttry;  /* -Dr: where regtry was called. */
155 #define RExC_starttry (pRExC_state->starttry)
156 #endif
157 #ifdef DEBUGGING
158  const char  *lastparse;
159  I32         lastnum;
160  AV          *paren_name_list;       /* idx -> name */
161 #define RExC_lastparse (pRExC_state->lastparse)
162 #define RExC_lastnum (pRExC_state->lastnum)
163 #define RExC_paren_name_list    (pRExC_state->paren_name_list)
164 #endif
165 } RExC_state_t;
166
167 #define RExC_flags (pRExC_state->flags)
168 #define RExC_precomp (pRExC_state->precomp)
169 #define RExC_rx_sv (pRExC_state->rx_sv)
170 #define RExC_rx  (pRExC_state->rx)
171 #define RExC_rxi (pRExC_state->rxi)
172 #define RExC_start (pRExC_state->start)
173 #define RExC_end (pRExC_state->end)
174 #define RExC_parse (pRExC_state->parse)
175 #define RExC_whilem_seen (pRExC_state->whilem_seen)
176 #ifdef RE_TRACK_PATTERN_OFFSETS
177 #define RExC_offsets (pRExC_state->rxi->u.offsets) /* I am not like the others */
178 #endif
179 #define RExC_emit (pRExC_state->emit)
180 #define RExC_emit_start (pRExC_state->emit_start)
181 #define RExC_emit_bound (pRExC_state->emit_bound)
182 #define RExC_naughty (pRExC_state->naughty)
183 #define RExC_sawback (pRExC_state->sawback)
184 #define RExC_seen (pRExC_state->seen)
185 #define RExC_size (pRExC_state->size)
186 #define RExC_npar (pRExC_state->npar)
187 #define RExC_nestroot   (pRExC_state->nestroot)
188 #define RExC_extralen (pRExC_state->extralen)
189 #define RExC_seen_zerolen (pRExC_state->seen_zerolen)
190 #define RExC_seen_evals (pRExC_state->seen_evals)
191 #define RExC_utf8 (pRExC_state->utf8)
192 #define RExC_uni_semantics (pRExC_state->uni_semantics)
193 #define RExC_orig_utf8 (pRExC_state->orig_utf8)
194 #define RExC_open_parens (pRExC_state->open_parens)
195 #define RExC_close_parens (pRExC_state->close_parens)
196 #define RExC_opend (pRExC_state->opend)
197 #define RExC_paren_names (pRExC_state->paren_names)
198 #define RExC_recurse (pRExC_state->recurse)
199 #define RExC_recurse_count (pRExC_state->recurse_count)
200 #define RExC_in_lookbehind (pRExC_state->in_lookbehind)
201 #define RExC_contains_locale (pRExC_state->contains_locale)
202 #define RExC_override_recoding (pRExC_state->override_recoding)
203
204
205 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
206 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
207   ((*s) == '{' && regcurly(s)))
208
209 #ifdef SPSTART
210 #undef SPSTART  /* dratted cpp namespace... */
211 #endif
212 /*
213  * Flags to be passed up and down.
214  */
215 #define WORST  0 /* Worst case. */
216 #define HASWIDTH 0x01 /* Known to match non-null strings. */
217
218 /* Simple enough to be STAR/PLUS operand, in an EXACT node must be a single
219  * character, and if utf8, must be invariant.  Note that this is not the same thing as REGNODE_SIMPLE */
220 #define SIMPLE  0x02
221 #define SPSTART  0x04 /* Starts with * or +. */
222 #define TRYAGAIN 0x08 /* Weeded out a declaration. */
223 #define POSTPONED 0x10    /* (?1),(?&name), (??{...}) or similar */
224
225 #define REG_NODE_NUM(x) ((x) ? (int)((x)-RExC_emit_start) : -1)
226
227 /* whether trie related optimizations are enabled */
228 #if PERL_ENABLE_EXTENDED_TRIE_OPTIMISATION
229 #define TRIE_STUDY_OPT
230 #define FULL_TRIE_STUDY
231 #define TRIE_STCLASS
232 #endif
233
234
235
236 #define PBYTE(u8str,paren) ((U8*)(u8str))[(paren) >> 3]
237 #define PBITVAL(paren) (1 << ((paren) & 7))
238 #define PAREN_TEST(u8str,paren) ( PBYTE(u8str,paren) & PBITVAL(paren))
239 #define PAREN_SET(u8str,paren) PBYTE(u8str,paren) |= PBITVAL(paren)
240 #define PAREN_UNSET(u8str,paren) PBYTE(u8str,paren) &= (~PBITVAL(paren))
241
242 /* If not already in utf8, do a longjmp back to the beginning */
243 #define UTF8_LONGJMP 42 /* Choose a value not likely to ever conflict */
244 #define REQUIRE_UTF8 STMT_START {                                       \
245          if (! UTF) JMPENV_JUMP(UTF8_LONGJMP); \
246       } STMT_END
247
248 /* About scan_data_t.
249
250   During optimisation we recurse through the regexp program performing
251   various inplace (keyhole style) optimisations. In addition study_chunk
252   and scan_commit populate this data structure with information about
253   what strings MUST appear in the pattern. We look for the longest
254   string that must appear at a fixed location, and we look for the
255   longest string that may appear at a floating location. So for instance
256   in the pattern:
257
258  /FOO[xX]A.*B[xX]BAR/
259
260   Both 'FOO' and 'A' are fixed strings. Both 'B' and 'BAR' are floating
261   strings (because they follow a .* construct). study_chunk will identify
262   both FOO and BAR as being the longest fixed and floating strings respectively.
263
264   The strings can be composites, for instance
265
266  /(f)(o)(o)/
267
268   will result in a composite fixed substring 'foo'.
269
270   For each string some basic information is maintained:
271
272   - offset or min_offset
273  This is the position the string must appear at, or not before.
274  It also implicitly (when combined with minlenp) tells us how many
275  characters must match before the string we are searching for.
276  Likewise when combined with minlenp and the length of the string it
277  tells us how many characters must appear after the string we have
278  found.
279
280   - max_offset
281  Only used for floating strings. This is the rightmost point that
282  the string can appear at. If set to I32 max it indicates that the
283  string can occur infinitely far to the right.
284
285   - minlenp
286  A pointer to the minimum length of the pattern that the string
287  was found inside. This is important as in the case of positive
288  lookahead or positive lookbehind we can have multiple patterns
289  involved. Consider
290
291  /(?=FOO).*F/
292
293  The minimum length of the pattern overall is 3, the minimum length
294  of the lookahead part is 3, but the minimum length of the part that
295  will actually match is 1. So 'FOO's minimum length is 3, but the
296  minimum length for the F is 1. This is important as the minimum length
297  is used to determine offsets in front of and behind the string being
298  looked for.  Since strings can be composites this is the length of the
299  pattern at the time it was committed with a scan_commit. Note that
300  the length is calculated by study_chunk, so that the minimum lengths
301  are not known until the full pattern has been compiled, thus the
302  pointer to the value.
303
304   - lookbehind
305
306  In the case of lookbehind the string being searched for can be
307  offset past the start point of the final matching string.
308  If this value was just blithely removed from the min_offset it would
309  invalidate some of the calculations for how many chars must match
310  before or after (as they are derived from min_offset and minlen and
311  the length of the string being searched for).
312  When the final pattern is compiled and the data is moved from the
313  scan_data_t structure into the regexp structure the information
314  about lookbehind is factored in, with the information that would
315  have been lost precalculated in the end_shift field for the
316  associated string.
317
318   The fields pos_min and pos_delta are used to store the minimum offset
319   and the delta to the maximum offset at the current point in the pattern.
320
321 */
322
323 typedef struct scan_data_t {
324  /*I32 len_min;      unused */
325  /*I32 len_delta;    unused */
326  I32 pos_min;
327  I32 pos_delta;
328  SV *last_found;
329  I32 last_end;     /* min value, <0 unless valid. */
330  I32 last_start_min;
331  I32 last_start_max;
332  SV **longest;     /* Either &l_fixed, or &l_float. */
333  SV *longest_fixed;      /* longest fixed string found in pattern */
334  I32 offset_fixed;       /* offset where it starts */
335  I32 *minlen_fixed;      /* pointer to the minlen relevant to the string */
336  I32 lookbehind_fixed;   /* is the position of the string modfied by LB */
337  SV *longest_float;      /* longest floating string found in pattern */
338  I32 offset_float_min;   /* earliest point in string it can appear */
339  I32 offset_float_max;   /* latest point in string it can appear */
340  I32 *minlen_float;      /* pointer to the minlen relevant to the string */
341  I32 lookbehind_float;   /* is the position of the string modified by LB */
342  I32 flags;
343  I32 whilem_c;
344  I32 *last_closep;
345  struct regnode_charclass_class *start_class;
346 } scan_data_t;
347
348 /*
349  * Forward declarations for pregcomp()'s friends.
350  */
351
352 static const scan_data_t zero_scan_data =
353   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0};
354
355 #define SF_BEFORE_EOL  (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
356 #define SF_BEFORE_SEOL  0x0001
357 #define SF_BEFORE_MEOL  0x0002
358 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
359 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
360
361 #ifdef NO_UNARY_PLUS
362 #  define SF_FIX_SHIFT_EOL (0+2)
363 #  define SF_FL_SHIFT_EOL  (0+4)
364 #else
365 #  define SF_FIX_SHIFT_EOL (+2)
366 #  define SF_FL_SHIFT_EOL  (+4)
367 #endif
368
369 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
370 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
371
372 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
373 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
374 #define SF_IS_INF  0x0040
375 #define SF_HAS_PAR  0x0080
376 #define SF_IN_PAR  0x0100
377 #define SF_HAS_EVAL  0x0200
378 #define SCF_DO_SUBSTR  0x0400
379 #define SCF_DO_STCLASS_AND 0x0800
380 #define SCF_DO_STCLASS_OR 0x1000
381 #define SCF_DO_STCLASS  (SCF_DO_STCLASS_AND|SCF_DO_STCLASS_OR)
382 #define SCF_WHILEM_VISITED_POS 0x2000
383
384 #define SCF_TRIE_RESTUDY        0x4000 /* Do restudy? */
385 #define SCF_SEEN_ACCEPT         0x8000
386
387 #define UTF cBOOL(RExC_utf8)
388
389 /* The enums for all these are ordered so things work out correctly */
390 #define LOC (get_regex_charset(RExC_flags) == REGEX_LOCALE_CHARSET)
391 #define DEPENDS_SEMANTICS (get_regex_charset(RExC_flags) == REGEX_DEPENDS_CHARSET)
392 #define UNI_SEMANTICS (get_regex_charset(RExC_flags) == REGEX_UNICODE_CHARSET)
393 #define AT_LEAST_UNI_SEMANTICS (get_regex_charset(RExC_flags) >= REGEX_UNICODE_CHARSET)
394 #define ASCII_RESTRICTED (get_regex_charset(RExC_flags) == REGEX_ASCII_RESTRICTED_CHARSET)
395 #define MORE_ASCII_RESTRICTED (get_regex_charset(RExC_flags) == REGEX_ASCII_MORE_RESTRICTED_CHARSET)
396 #define AT_LEAST_ASCII_RESTRICTED (get_regex_charset(RExC_flags) >= REGEX_ASCII_RESTRICTED_CHARSET)
397
398 #define FOLD cBOOL(RExC_flags & RXf_PMf_FOLD)
399
400 #define OOB_UNICODE  12345678
401 #define OOB_NAMEDCLASS  -1
402
403 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
404 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
405
406
407 /* length of regex to show in messages that don't mark a position within */
408 #define RegexLengthToShowInErrorMessages 127
409
410 /*
411  * If MARKER[12] are adjusted, be sure to adjust the constants at the top
412  * of t/op/regmesg.t, the tests in t/op/re_tests, and those in
413  * op/pragma/warn/regcomp.
414  */
415 #define MARKER1 "<-- HERE"    /* marker as it appears in the description */
416 #define MARKER2 " <-- HERE "  /* marker as it appears within the regex */
417
418 #define REPORT_LOCATION " in regex; marked by " MARKER1 " in m/%.*s" MARKER2 "%s/"
419
420 /*
421  * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
422  * arg. Show regex, up to a maximum length. If it's too long, chop and add
423  * "...".
424  */
425 #define _FAIL(code) STMT_START {     \
426  const char *ellipses = "";      \
427  IV len = RExC_end - RExC_precomp;     \
428                   \
429  if (!SIZE_ONLY)       \
430   SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv);   \
431  if (len > RegexLengthToShowInErrorMessages) {   \
432   /* chop 10 shorter than the max, to ensure meaning of "..." */ \
433   len = RegexLengthToShowInErrorMessages - 10;   \
434   ellipses = "...";      \
435  }         \
436  code;                                                               \
437 } STMT_END
438
439 #define FAIL(msg) _FAIL(       \
440  Perl_croak(aTHX_ "%s in regex m/%.*s%s/",     \
441    msg, (int)len, RExC_precomp, ellipses))
442
443 #define FAIL2(msg,arg) _FAIL(       \
444  Perl_croak(aTHX_ msg " in regex m/%.*s%s/",     \
445    arg, (int)len, RExC_precomp, ellipses))
446
447 /*
448  * Simple_vFAIL -- like FAIL, but marks the current location in the scan
449  */
450 #define Simple_vFAIL(m) STMT_START {     \
451  const IV offset = RExC_parse - RExC_precomp;   \
452  Perl_croak(aTHX_ "%s" REPORT_LOCATION,    \
453    m, (int)offset, RExC_precomp, RExC_precomp + offset); \
454 } STMT_END
455
456 /*
457  * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL()
458  */
459 #define vFAIL(m) STMT_START {    \
460  if (!SIZE_ONLY)     \
461   SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv); \
462  Simple_vFAIL(m);     \
463 } STMT_END
464
465 /*
466  * Like Simple_vFAIL(), but accepts two arguments.
467  */
468 #define Simple_vFAIL2(m,a1) STMT_START {   \
469  const IV offset = RExC_parse - RExC_precomp;   \
470  S_re_croak2(aTHX_ m, REPORT_LOCATION, a1,   \
471    (int)offset, RExC_precomp, RExC_precomp + offset); \
472 } STMT_END
473
474 /*
475  * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL2().
476  */
477 #define vFAIL2(m,a1) STMT_START {   \
478  if (!SIZE_ONLY)     \
479   SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv); \
480  Simple_vFAIL2(m, a1);    \
481 } STMT_END
482
483
484 /*
485  * Like Simple_vFAIL(), but accepts three arguments.
486  */
487 #define Simple_vFAIL3(m, a1, a2) STMT_START {   \
488  const IV offset = RExC_parse - RExC_precomp;  \
489  S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2,  \
490    (int)offset, RExC_precomp, RExC_precomp + offset); \
491 } STMT_END
492
493 /*
494  * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL3().
495  */
496 #define vFAIL3(m,a1,a2) STMT_START {   \
497  if (!SIZE_ONLY)     \
498   SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv); \
499  Simple_vFAIL3(m, a1, a2);    \
500 } STMT_END
501
502 /*
503  * Like Simple_vFAIL(), but accepts four arguments.
504  */
505 #define Simple_vFAIL4(m, a1, a2, a3) STMT_START {  \
506  const IV offset = RExC_parse - RExC_precomp;  \
507  S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3,  \
508    (int)offset, RExC_precomp, RExC_precomp + offset); \
509 } STMT_END
510
511 #define ckWARNreg(loc,m) STMT_START {     \
512  const IV offset = loc - RExC_precomp;    \
513  Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \
514    (int)offset, RExC_precomp, RExC_precomp + offset);  \
515 } STMT_END
516
517 #define ckWARNregdep(loc,m) STMT_START {    \
518  const IV offset = loc - RExC_precomp;    \
519  Perl_ck_warner_d(aTHX_ packWARN2(WARN_DEPRECATED, WARN_REGEXP), \
520    m REPORT_LOCATION,      \
521    (int)offset, RExC_precomp, RExC_precomp + offset);  \
522 } STMT_END
523
524 #define ckWARN2regdep(loc,m, a1) STMT_START {    \
525  const IV offset = loc - RExC_precomp;    \
526  Perl_ck_warner_d(aTHX_ packWARN2(WARN_DEPRECATED, WARN_REGEXP), \
527    m REPORT_LOCATION,      \
528    a1, (int)offset, RExC_precomp, RExC_precomp + offset); \
529 } STMT_END
530
531 #define ckWARN2reg(loc, m, a1) STMT_START {    \
532  const IV offset = loc - RExC_precomp;    \
533  Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \
534    a1, (int)offset, RExC_precomp, RExC_precomp + offset); \
535 } STMT_END
536
537 #define vWARN3(loc, m, a1, a2) STMT_START {    \
538  const IV offset = loc - RExC_precomp;    \
539  Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,  \
540    a1, a2, (int)offset, RExC_precomp, RExC_precomp + offset); \
541 } STMT_END
542
543 #define ckWARN3reg(loc, m, a1, a2) STMT_START {    \
544  const IV offset = loc - RExC_precomp;    \
545  Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \
546    a1, a2, (int)offset, RExC_precomp, RExC_precomp + offset); \
547 } STMT_END
548
549 #define vWARN4(loc, m, a1, a2, a3) STMT_START {    \
550  const IV offset = loc - RExC_precomp;    \
551  Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,  \
552    a1, a2, a3, (int)offset, RExC_precomp, RExC_precomp + offset); \
553 } STMT_END
554
555 #define ckWARN4reg(loc, m, a1, a2, a3) STMT_START {   \
556  const IV offset = loc - RExC_precomp;    \
557  Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \
558    a1, a2, a3, (int)offset, RExC_precomp, RExC_precomp + offset); \
559 } STMT_END
560
561 #define vWARN5(loc, m, a1, a2, a3, a4) STMT_START {   \
562  const IV offset = loc - RExC_precomp;    \
563  Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,  \
564    a1, a2, a3, a4, (int)offset, RExC_precomp, RExC_precomp + offset); \
565 } STMT_END
566
567
568 /* Allow for side effects in s */
569 #define REGC(c,s) STMT_START {   \
570  if (!SIZE_ONLY) *(s) = (c); else (void)(s); \
571 } STMT_END
572
573 /* Macros for recording node offsets.   20001227 mjd@plover.com
574  * Nodes are numbered 1, 2, 3, 4.  Node #n's position is recorded in
575  * element 2*n-1 of the array.  Element #2n holds the byte length node #n.
576  * Element 0 holds the number n.
577  * Position is 1 indexed.
578  */
579 #ifndef RE_TRACK_PATTERN_OFFSETS
580 #define Set_Node_Offset_To_R(node,byte)
581 #define Set_Node_Offset(node,byte)
582 #define Set_Cur_Node_Offset
583 #define Set_Node_Length_To_R(node,len)
584 #define Set_Node_Length(node,len)
585 #define Set_Node_Cur_Length(node)
586 #define Node_Offset(n)
587 #define Node_Length(n)
588 #define Set_Node_Offset_Length(node,offset,len)
589 #define ProgLen(ri) ri->u.proglen
590 #define SetProgLen(ri,x) ri->u.proglen = x
591 #else
592 #define ProgLen(ri) ri->u.offsets[0]
593 #define SetProgLen(ri,x) ri->u.offsets[0] = x
594 #define Set_Node_Offset_To_R(node,byte) STMT_START {   \
595  if (! SIZE_ONLY) {       \
596   MJD_OFFSET_DEBUG(("** (%d) offset of node %d is %d.\n",  \
597      __LINE__, (int)(node), (int)(byte)));  \
598   if((node) < 0) {      \
599    Perl_croak(aTHX_ "value of node is %d in Offset macro", (int)(node)); \
600   } else {       \
601    RExC_offsets[2*(node)-1] = (byte);    \
602   }        \
603  }         \
604 } STMT_END
605
606 #define Set_Node_Offset(node,byte) \
607  Set_Node_Offset_To_R((node)-RExC_emit_start, (byte)-RExC_start)
608 #define Set_Cur_Node_Offset Set_Node_Offset(RExC_emit, RExC_parse)
609
610 #define Set_Node_Length_To_R(node,len) STMT_START {   \
611  if (! SIZE_ONLY) {       \
612   MJD_OFFSET_DEBUG(("** (%d) size of node %d is %d.\n",  \
613     __LINE__, (int)(node), (int)(len)));   \
614   if((node) < 0) {      \
615    Perl_croak(aTHX_ "value of node is %d in Length macro", (int)(node)); \
616   } else {       \
617    RExC_offsets[2*(node)] = (len);    \
618   }        \
619  }         \
620 } STMT_END
621
622 #define Set_Node_Length(node,len) \
623  Set_Node_Length_To_R((node)-RExC_emit_start, len)
624 #define Set_Cur_Node_Length(len) Set_Node_Length(RExC_emit, len)
625 #define Set_Node_Cur_Length(node) \
626  Set_Node_Length(node, RExC_parse - parse_start)
627
628 /* Get offsets and lengths */
629 #define Node_Offset(n) (RExC_offsets[2*((n)-RExC_emit_start)-1])
630 #define Node_Length(n) (RExC_offsets[2*((n)-RExC_emit_start)])
631
632 #define Set_Node_Offset_Length(node,offset,len) STMT_START { \
633  Set_Node_Offset_To_R((node)-RExC_emit_start, (offset)); \
634  Set_Node_Length_To_R((node)-RExC_emit_start, (len)); \
635 } STMT_END
636 #endif
637
638 #if PERL_ENABLE_EXPERIMENTAL_REGEX_OPTIMISATIONS
639 #define EXPERIMENTAL_INPLACESCAN
640 #endif /*PERL_ENABLE_EXPERIMENTAL_REGEX_OPTIMISATIONS*/
641
642 #define DEBUG_STUDYDATA(str,data,depth)                              \
643 DEBUG_OPTIMISE_MORE_r(if(data){                                      \
644  PerlIO_printf(Perl_debug_log,                                    \
645   "%*s" str "Pos:%"IVdf"/%"IVdf                                \
646   " Flags: 0x%"UVXf" Whilem_c: %"IVdf" Lcp: %"IVdf" %s",       \
647   (int)(depth)*2, "",                                          \
648   (IV)((data)->pos_min),                                       \
649   (IV)((data)->pos_delta),                                     \
650   (UV)((data)->flags),                                         \
651   (IV)((data)->whilem_c),                                      \
652   (IV)((data)->last_closep ? *((data)->last_closep) : -1),     \
653   is_inf ? "INF " : ""                                         \
654  );                                                               \
655  if ((data)->last_found)                                          \
656   PerlIO_printf(Perl_debug_log,                                \
657    "Last:'%s' %"IVdf":%"IVdf"/%"IVdf" %sFixed:'%s' @ %"IVdf \
658    " %sFloat: '%s' @ %"IVdf"/%"IVdf"",                      \
659    SvPVX_const((data)->last_found),                         \
660    (IV)((data)->last_end),                                  \
661    (IV)((data)->last_start_min),                            \
662    (IV)((data)->last_start_max),                            \
663    ((data)->longest &&                                      \
664    (data)->longest==&((data)->longest_fixed)) ? "*" : "",  \
665    SvPVX_const((data)->longest_fixed),                      \
666    (IV)((data)->offset_fixed),                              \
667    ((data)->longest &&                                      \
668    (data)->longest==&((data)->longest_float)) ? "*" : "",  \
669    SvPVX_const((data)->longest_float),                      \
670    (IV)((data)->offset_float_min),                          \
671    (IV)((data)->offset_float_max)                           \
672   );                                                           \
673  PerlIO_printf(Perl_debug_log,"\n");                              \
674 });
675
676 static void clear_re(pTHX_ void *r);
677
678 /* Mark that we cannot extend a found fixed substring at this point.
679    Update the longest found anchored substring and the longest found
680    floating substrings if needed. */
681
682 STATIC void
683 S_scan_commit(pTHX_ const RExC_state_t *pRExC_state, scan_data_t *data, I32 *minlenp, int is_inf)
684 {
685  const STRLEN l = CHR_SVLEN(data->last_found);
686  const STRLEN old_l = CHR_SVLEN(*data->longest);
687  GET_RE_DEBUG_FLAGS_DECL;
688
689  PERL_ARGS_ASSERT_SCAN_COMMIT;
690
691  if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
692   SvSetMagicSV(*data->longest, data->last_found);
693   if (*data->longest == data->longest_fixed) {
694    data->offset_fixed = l ? data->last_start_min : data->pos_min;
695    if (data->flags & SF_BEFORE_EOL)
696     data->flags
697      |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
698    else
699     data->flags &= ~SF_FIX_BEFORE_EOL;
700    data->minlen_fixed=minlenp;
701    data->lookbehind_fixed=0;
702   }
703   else { /* *data->longest == data->longest_float */
704    data->offset_float_min = l ? data->last_start_min : data->pos_min;
705    data->offset_float_max = (l
706          ? data->last_start_max
707          : data->pos_min + data->pos_delta);
708    if (is_inf || (U32)data->offset_float_max > (U32)I32_MAX)
709     data->offset_float_max = I32_MAX;
710    if (data->flags & SF_BEFORE_EOL)
711     data->flags
712      |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
713    else
714     data->flags &= ~SF_FL_BEFORE_EOL;
715    data->minlen_float=minlenp;
716    data->lookbehind_float=0;
717   }
718  }
719  SvCUR_set(data->last_found, 0);
720  {
721   SV * const sv = data->last_found;
722   if (SvUTF8(sv) && SvMAGICAL(sv)) {
723    MAGIC * const mg = mg_find(sv, PERL_MAGIC_utf8);
724    if (mg)
725     mg->mg_len = 0;
726   }
727  }
728  data->last_end = -1;
729  data->flags &= ~SF_BEFORE_EOL;
730  DEBUG_STUDYDATA("commit: ",data,0);
731 }
732
733 /* Can match anything (initialization) */
734 STATIC void
735 S_cl_anything(const RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
736 {
737  PERL_ARGS_ASSERT_CL_ANYTHING;
738
739  ANYOF_BITMAP_SETALL(cl);
740  cl->flags = ANYOF_CLASS|ANYOF_EOS|ANYOF_UNICODE_ALL
741     |ANYOF_LOC_NONBITMAP_FOLD|ANYOF_NON_UTF8_LATIN1_ALL;
742
743  /* If any portion of the regex is to operate under locale rules,
744  * initialization includes it.  The reason this isn't done for all regexes
745  * is that the optimizer was written under the assumption that locale was
746  * all-or-nothing.  Given the complexity and lack of documentation in the
747  * optimizer, and that there are inadequate test cases for locale, so many
748  * parts of it may not work properly, it is safest to avoid locale unless
749  * necessary. */
750  if (RExC_contains_locale) {
751   ANYOF_CLASS_SETALL(cl);     /* /l uses class */
752   cl->flags |= ANYOF_LOCALE;
753  }
754  else {
755   ANYOF_CLASS_ZERO(cl);     /* Only /l uses class now */
756  }
757 }
758
759 /* Can match anything (initialization) */
760 STATIC int
761 S_cl_is_anything(const struct regnode_charclass_class *cl)
762 {
763  int value;
764
765  PERL_ARGS_ASSERT_CL_IS_ANYTHING;
766
767  for (value = 0; value <= ANYOF_MAX; value += 2)
768   if (ANYOF_CLASS_TEST(cl, value) && ANYOF_CLASS_TEST(cl, value + 1))
769    return 1;
770  if (!(cl->flags & ANYOF_UNICODE_ALL))
771   return 0;
772  if (!ANYOF_BITMAP_TESTALLSET((const void*)cl))
773   return 0;
774  return 1;
775 }
776
777 /* Can match anything (initialization) */
778 STATIC void
779 S_cl_init(const RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
780 {
781  PERL_ARGS_ASSERT_CL_INIT;
782
783  Zero(cl, 1, struct regnode_charclass_class);
784  cl->type = ANYOF;
785  cl_anything(pRExC_state, cl);
786  ARG_SET(cl, ANYOF_NONBITMAP_EMPTY);
787 }
788
789 /* These two functions currently do the exact same thing */
790 #define cl_init_zero  S_cl_init
791
792 /* 'AND' a given class with another one.  Can create false positives.  'cl'
793  * should not be inverted.  'and_with->flags & ANYOF_CLASS' should be 0 if
794  * 'and_with' is a regnode_charclass instead of a regnode_charclass_class. */
795 STATIC void
796 S_cl_and(struct regnode_charclass_class *cl,
797   const struct regnode_charclass_class *and_with)
798 {
799  PERL_ARGS_ASSERT_CL_AND;
800
801  assert(and_with->type == ANYOF);
802
803  /* I (khw) am not sure all these restrictions are necessary XXX */
804  if (!(ANYOF_CLASS_TEST_ANY_SET(and_with))
805   && !(ANYOF_CLASS_TEST_ANY_SET(cl))
806   && (and_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
807   && !(and_with->flags & ANYOF_LOC_NONBITMAP_FOLD)
808   && !(cl->flags & ANYOF_LOC_NONBITMAP_FOLD)) {
809   int i;
810
811   if (and_with->flags & ANYOF_INVERT)
812    for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
813     cl->bitmap[i] &= ~and_with->bitmap[i];
814   else
815    for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
816     cl->bitmap[i] &= and_with->bitmap[i];
817  } /* XXXX: logic is complicated otherwise, leave it along for a moment. */
818
819  if (and_with->flags & ANYOF_INVERT) {
820
821   /* Here, the and'ed node is inverted.  Get the AND of the flags that
822   * aren't affected by the inversion.  Those that are affected are
823   * handled individually below */
824   U8 affected_flags = cl->flags & ~INVERSION_UNAFFECTED_FLAGS;
825   cl->flags &= (and_with->flags & INVERSION_UNAFFECTED_FLAGS);
826   cl->flags |= affected_flags;
827
828   /* We currently don't know how to deal with things that aren't in the
829   * bitmap, but we know that the intersection is no greater than what
830   * is already in cl, so let there be false positives that get sorted
831   * out after the synthetic start class succeeds, and the node is
832   * matched for real. */
833
834   /* The inversion of these two flags indicate that the resulting
835   * intersection doesn't have them */
836   if (and_with->flags & ANYOF_UNICODE_ALL) {
837    cl->flags &= ~ANYOF_UNICODE_ALL;
838   }
839   if (and_with->flags & ANYOF_NON_UTF8_LATIN1_ALL) {
840    cl->flags &= ~ANYOF_NON_UTF8_LATIN1_ALL;
841   }
842  }
843  else {   /* and'd node is not inverted */
844   U8 outside_bitmap_but_not_utf8; /* Temp variable */
845
846   if (! ANYOF_NONBITMAP(and_with)) {
847
848    /* Here 'and_with' doesn't match anything outside the bitmap
849    * (except possibly ANYOF_UNICODE_ALL), which means the
850    * intersection can't either, except for ANYOF_UNICODE_ALL, in
851    * which case we don't know what the intersection is, but it's no
852    * greater than what cl already has, so can just leave it alone,
853    * with possible false positives */
854    if (! (and_with->flags & ANYOF_UNICODE_ALL)) {
855     ARG_SET(cl, ANYOF_NONBITMAP_EMPTY);
856     cl->flags &= ~ANYOF_NONBITMAP_NON_UTF8;
857    }
858   }
859   else if (! ANYOF_NONBITMAP(cl)) {
860
861    /* Here, 'and_with' does match something outside the bitmap, and cl
862    * doesn't have a list of things to match outside the bitmap.  If
863    * cl can match all code points above 255, the intersection will
864    * be those above-255 code points that 'and_with' matches.  If cl
865    * can't match all Unicode code points, it means that it can't
866    * match anything outside the bitmap (since the 'if' that got us
867    * into this block tested for that), so we leave the bitmap empty.
868    */
869    if (cl->flags & ANYOF_UNICODE_ALL) {
870     ARG_SET(cl, ARG(and_with));
871
872     /* and_with's ARG may match things that don't require UTF8.
873     * And now cl's will too, in spite of this being an 'and'.  See
874     * the comments below about the kludge */
875     cl->flags |= and_with->flags & ANYOF_NONBITMAP_NON_UTF8;
876    }
877   }
878   else {
879    /* Here, both 'and_with' and cl match something outside the
880    * bitmap.  Currently we do not do the intersection, so just match
881    * whatever cl had at the beginning.  */
882   }
883
884
885   /* Take the intersection of the two sets of flags.  However, the
886   * ANYOF_NONBITMAP_NON_UTF8 flag is treated as an 'or'.  This is a
887   * kludge around the fact that this flag is not treated like the others
888   * which are initialized in cl_anything().  The way the optimizer works
889   * is that the synthetic start class (SSC) is initialized to match
890   * anything, and then the first time a real node is encountered, its
891   * values are AND'd with the SSC's with the result being the values of
892   * the real node.  However, there are paths through the optimizer where
893   * the AND never gets called, so those initialized bits are set
894   * inappropriately, which is not usually a big deal, as they just cause
895   * false positives in the SSC, which will just mean a probably
896   * imperceptible slow down in execution.  However this bit has a
897   * higher false positive consequence in that it can cause utf8.pm,
898   * utf8_heavy.pl ... to be loaded when not necessary, which is a much
899   * bigger slowdown and also causes significant extra memory to be used.
900   * In order to prevent this, the code now takes a different tack.  The
901   * bit isn't set unless some part of the regular expression needs it,
902   * but once set it won't get cleared.  This means that these extra
903   * modules won't get loaded unless there was some path through the
904   * pattern that would have required them anyway, and  so any false
905   * positives that occur by not ANDing them out when they could be
906   * aren't as severe as they would be if we treated this bit like all
907   * the others */
908   outside_bitmap_but_not_utf8 = (cl->flags | and_with->flags)
909          & ANYOF_NONBITMAP_NON_UTF8;
910   cl->flags &= and_with->flags;
911   cl->flags |= outside_bitmap_but_not_utf8;
912  }
913 }
914
915 /* 'OR' a given class with another one.  Can create false positives.  'cl'
916  * should not be inverted.  'or_with->flags & ANYOF_CLASS' should be 0 if
917  * 'or_with' is a regnode_charclass instead of a regnode_charclass_class. */
918 STATIC void
919 S_cl_or(const RExC_state_t *pRExC_state, struct regnode_charclass_class *cl, const struct regnode_charclass_class *or_with)
920 {
921  PERL_ARGS_ASSERT_CL_OR;
922
923  if (or_with->flags & ANYOF_INVERT) {
924
925   /* Here, the or'd node is to be inverted.  This means we take the
926   * complement of everything not in the bitmap, but currently we don't
927   * know what that is, so give up and match anything */
928   if (ANYOF_NONBITMAP(or_with)) {
929    cl_anything(pRExC_state, cl);
930   }
931   /* We do not use
932   * (B1 | CL1) | (!B2 & !CL2) = (B1 | !B2 & !CL2) | (CL1 | (!B2 & !CL2))
933   *   <= (B1 | !B2) | (CL1 | !CL2)
934   * which is wasteful if CL2 is small, but we ignore CL2:
935   *   (B1 | CL1) | (!B2 & !CL2) <= (B1 | CL1) | !B2 = (B1 | !B2) | CL1
936   * XXXX Can we handle case-fold?  Unclear:
937   *   (OK1(i) | OK1(i')) | !(OK1(i) | OK1(i')) =
938   *   (OK1(i) | OK1(i')) | (!OK1(i) & !OK1(i'))
939   */
940   else if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
941    && !(or_with->flags & ANYOF_LOC_NONBITMAP_FOLD)
942    && !(cl->flags & ANYOF_LOC_NONBITMAP_FOLD) ) {
943    int i;
944
945    for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
946     cl->bitmap[i] |= ~or_with->bitmap[i];
947   } /* XXXX: logic is complicated otherwise */
948   else {
949    cl_anything(pRExC_state, cl);
950   }
951
952   /* And, we can just take the union of the flags that aren't affected
953   * by the inversion */
954   cl->flags |= or_with->flags & INVERSION_UNAFFECTED_FLAGS;
955
956   /* For the remaining flags:
957    ANYOF_UNICODE_ALL and inverted means to not match anything above
958      255, which means that the union with cl should just be
959      what cl has in it, so can ignore this flag
960    ANYOF_NON_UTF8_LATIN1_ALL and inverted means if not utf8 and ord
961      is 127-255 to match them, but then invert that, so the
962      union with cl should just be what cl has in it, so can
963      ignore this flag
964   */
965  } else {    /* 'or_with' is not inverted */
966   /* (B1 | CL1) | (B2 | CL2) = (B1 | B2) | (CL1 | CL2)) */
967   if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
968    && (!(or_with->flags & ANYOF_LOC_NONBITMAP_FOLD)
969     || (cl->flags & ANYOF_LOC_NONBITMAP_FOLD)) ) {
970    int i;
971
972    /* OR char bitmap and class bitmap separately */
973    for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
974     cl->bitmap[i] |= or_with->bitmap[i];
975    if (ANYOF_CLASS_TEST_ANY_SET(or_with)) {
976     for (i = 0; i < ANYOF_CLASSBITMAP_SIZE; i++)
977      cl->classflags[i] |= or_with->classflags[i];
978     cl->flags |= ANYOF_CLASS;
979    }
980   }
981   else { /* XXXX: logic is complicated, leave it along for a moment. */
982    cl_anything(pRExC_state, cl);
983   }
984
985   if (ANYOF_NONBITMAP(or_with)) {
986
987    /* Use the added node's outside-the-bit-map match if there isn't a
988    * conflict.  If there is a conflict (both nodes match something
989    * outside the bitmap, but what they match outside is not the same
990    * pointer, and hence not easily compared until XXX we extend
991    * inversion lists this far), give up and allow the start class to
992    * match everything outside the bitmap.  If that stuff is all above
993    * 255, can just set UNICODE_ALL, otherwise caould be anything. */
994    if (! ANYOF_NONBITMAP(cl)) {
995     ARG_SET(cl, ARG(or_with));
996    }
997    else if (ARG(cl) != ARG(or_with)) {
998
999     if ((or_with->flags & ANYOF_NONBITMAP_NON_UTF8)) {
1000      cl_anything(pRExC_state, cl);
1001     }
1002     else {
1003      cl->flags |= ANYOF_UNICODE_ALL;
1004     }
1005    }
1006   }
1007
1008   /* Take the union */
1009   cl->flags |= or_with->flags;
1010  }
1011 }
1012
1013 #define TRIE_LIST_ITEM(state,idx) (trie->states[state].trans.list)[ idx ]
1014 #define TRIE_LIST_CUR(state)  ( TRIE_LIST_ITEM( state, 0 ).forid )
1015 #define TRIE_LIST_LEN(state) ( TRIE_LIST_ITEM( state, 0 ).newstate )
1016 #define TRIE_LIST_USED(idx)  ( trie->states[state].trans.list ? (TRIE_LIST_CUR( idx ) - 1) : 0 )
1017
1018
1019 #ifdef DEBUGGING
1020 /*
1021    dump_trie(trie,widecharmap,revcharmap)
1022    dump_trie_interim_list(trie,widecharmap,revcharmap,next_alloc)
1023    dump_trie_interim_table(trie,widecharmap,revcharmap,next_alloc)
1024
1025    These routines dump out a trie in a somewhat readable format.
1026    The _interim_ variants are used for debugging the interim
1027    tables that are used to generate the final compressed
1028    representation which is what dump_trie expects.
1029
1030    Part of the reason for their existence is to provide a form
1031    of documentation as to how the different representations function.
1032
1033 */
1034
1035 /*
1036   Dumps the final compressed table form of the trie to Perl_debug_log.
1037   Used for debugging make_trie().
1038 */
1039
1040 STATIC void
1041 S_dump_trie(pTHX_ const struct _reg_trie_data *trie, HV *widecharmap,
1042    AV *revcharmap, U32 depth)
1043 {
1044  U32 state;
1045  SV *sv=sv_newmortal();
1046  int colwidth= widecharmap ? 6 : 4;
1047  U16 word;
1048  GET_RE_DEBUG_FLAGS_DECL;
1049
1050  PERL_ARGS_ASSERT_DUMP_TRIE;
1051
1052  PerlIO_printf( Perl_debug_log, "%*sChar : %-6s%-6s%-4s ",
1053   (int)depth * 2 + 2,"",
1054   "Match","Base","Ofs" );
1055
1056  for( state = 0 ; state < trie->uniquecharcount ; state++ ) {
1057   SV ** const tmp = av_fetch( revcharmap, state, 0);
1058   if ( tmp ) {
1059    PerlIO_printf( Perl_debug_log, "%*s",
1060     colwidth,
1061     pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), colwidth,
1062        PL_colors[0], PL_colors[1],
1063        (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) |
1064        PERL_PV_ESCAPE_FIRSTCHAR
1065     )
1066    );
1067   }
1068  }
1069  PerlIO_printf( Perl_debug_log, "\n%*sState|-----------------------",
1070   (int)depth * 2 + 2,"");
1071
1072  for( state = 0 ; state < trie->uniquecharcount ; state++ )
1073   PerlIO_printf( Perl_debug_log, "%.*s", colwidth, "--------");
1074  PerlIO_printf( Perl_debug_log, "\n");
1075
1076  for( state = 1 ; state < trie->statecount ; state++ ) {
1077   const U32 base = trie->states[ state ].trans.base;
1078
1079   PerlIO_printf( Perl_debug_log, "%*s#%4"UVXf"|", (int)depth * 2 + 2,"", (UV)state);
1080
1081   if ( trie->states[ state ].wordnum ) {
1082    PerlIO_printf( Perl_debug_log, " W%4X", trie->states[ state ].wordnum );
1083   } else {
1084    PerlIO_printf( Perl_debug_log, "%6s", "" );
1085   }
1086
1087   PerlIO_printf( Perl_debug_log, " @%4"UVXf" ", (UV)base );
1088
1089   if ( base ) {
1090    U32 ofs = 0;
1091
1092    while( ( base + ofs  < trie->uniquecharcount ) ||
1093     ( base + ofs - trie->uniquecharcount < trie->lasttrans
1094      && trie->trans[ base + ofs - trie->uniquecharcount ].check != state))
1095      ofs++;
1096
1097    PerlIO_printf( Perl_debug_log, "+%2"UVXf"[ ", (UV)ofs);
1098
1099    for ( ofs = 0 ; ofs < trie->uniquecharcount ; ofs++ ) {
1100     if ( ( base + ofs >= trie->uniquecharcount ) &&
1101      ( base + ofs - trie->uniquecharcount < trie->lasttrans ) &&
1102      trie->trans[ base + ofs - trie->uniquecharcount ].check == state )
1103     {
1104     PerlIO_printf( Perl_debug_log, "%*"UVXf,
1105      colwidth,
1106      (UV)trie->trans[ base + ofs - trie->uniquecharcount ].next );
1107     } else {
1108      PerlIO_printf( Perl_debug_log, "%*s",colwidth,"   ." );
1109     }
1110    }
1111
1112    PerlIO_printf( Perl_debug_log, "]");
1113
1114   }
1115   PerlIO_printf( Perl_debug_log, "\n" );
1116  }
1117  PerlIO_printf(Perl_debug_log, "%*sword_info N:(prev,len)=", (int)depth*2, "");
1118  for (word=1; word <= trie->wordcount; word++) {
1119   PerlIO_printf(Perl_debug_log, " %d:(%d,%d)",
1120    (int)word, (int)(trie->wordinfo[word].prev),
1121    (int)(trie->wordinfo[word].len));
1122  }
1123  PerlIO_printf(Perl_debug_log, "\n" );
1124 }
1125 /*
1126   Dumps a fully constructed but uncompressed trie in list form.
1127   List tries normally only are used for construction when the number of
1128   possible chars (trie->uniquecharcount) is very high.
1129   Used for debugging make_trie().
1130 */
1131 STATIC void
1132 S_dump_trie_interim_list(pTHX_ const struct _reg_trie_data *trie,
1133       HV *widecharmap, AV *revcharmap, U32 next_alloc,
1134       U32 depth)
1135 {
1136  U32 state;
1137  SV *sv=sv_newmortal();
1138  int colwidth= widecharmap ? 6 : 4;
1139  GET_RE_DEBUG_FLAGS_DECL;
1140
1141  PERL_ARGS_ASSERT_DUMP_TRIE_INTERIM_LIST;
1142
1143  /* print out the table precompression.  */
1144  PerlIO_printf( Perl_debug_log, "%*sState :Word | Transition Data\n%*s%s",
1145   (int)depth * 2 + 2,"", (int)depth * 2 + 2,"",
1146   "------:-----+-----------------\n" );
1147
1148  for( state=1 ; state < next_alloc ; state ++ ) {
1149   U16 charid;
1150
1151   PerlIO_printf( Perl_debug_log, "%*s %4"UVXf" :",
1152    (int)depth * 2 + 2,"", (UV)state  );
1153   if ( ! trie->states[ state ].wordnum ) {
1154    PerlIO_printf( Perl_debug_log, "%5s| ","");
1155   } else {
1156    PerlIO_printf( Perl_debug_log, "W%4x| ",
1157     trie->states[ state ].wordnum
1158    );
1159   }
1160   for( charid = 1 ; charid <= TRIE_LIST_USED( state ) ; charid++ ) {
1161    SV ** const tmp = av_fetch( revcharmap, TRIE_LIST_ITEM(state,charid).forid, 0);
1162    if ( tmp ) {
1163     PerlIO_printf( Perl_debug_log, "%*s:%3X=%4"UVXf" | ",
1164      colwidth,
1165      pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), colwidth,
1166        PL_colors[0], PL_colors[1],
1167        (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) |
1168        PERL_PV_ESCAPE_FIRSTCHAR
1169      ) ,
1170      TRIE_LIST_ITEM(state,charid).forid,
1171      (UV)TRIE_LIST_ITEM(state,charid).newstate
1172     );
1173     if (!(charid % 10))
1174      PerlIO_printf(Perl_debug_log, "\n%*s| ",
1175       (int)((depth * 2) + 14), "");
1176    }
1177   }
1178   PerlIO_printf( Perl_debug_log, "\n");
1179  }
1180 }
1181
1182 /*
1183   Dumps a fully constructed but uncompressed trie in table form.
1184   This is the normal DFA style state transition table, with a few
1185   twists to facilitate compression later.
1186   Used for debugging make_trie().
1187 */
1188 STATIC void
1189 S_dump_trie_interim_table(pTHX_ const struct _reg_trie_data *trie,
1190       HV *widecharmap, AV *revcharmap, U32 next_alloc,
1191       U32 depth)
1192 {
1193  U32 state;
1194  U16 charid;
1195  SV *sv=sv_newmortal();
1196  int colwidth= widecharmap ? 6 : 4;
1197  GET_RE_DEBUG_FLAGS_DECL;
1198
1199  PERL_ARGS_ASSERT_DUMP_TRIE_INTERIM_TABLE;
1200
1201  /*
1202  print out the table precompression so that we can do a visual check
1203  that they are identical.
1204  */
1205
1206  PerlIO_printf( Perl_debug_log, "%*sChar : ",(int)depth * 2 + 2,"" );
1207
1208  for( charid = 0 ; charid < trie->uniquecharcount ; charid++ ) {
1209   SV ** const tmp = av_fetch( revcharmap, charid, 0);
1210   if ( tmp ) {
1211    PerlIO_printf( Perl_debug_log, "%*s",
1212     colwidth,
1213     pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), colwidth,
1214        PL_colors[0], PL_colors[1],
1215        (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) |
1216        PERL_PV_ESCAPE_FIRSTCHAR
1217     )
1218    );
1219   }
1220  }
1221
1222  PerlIO_printf( Perl_debug_log, "\n%*sState+-",(int)depth * 2 + 2,"" );
1223
1224  for( charid=0 ; charid < trie->uniquecharcount ; charid++ ) {
1225   PerlIO_printf( Perl_debug_log, "%.*s", colwidth,"--------");
1226  }
1227
1228  PerlIO_printf( Perl_debug_log, "\n" );
1229
1230  for( state=1 ; state < next_alloc ; state += trie->uniquecharcount ) {
1231
1232   PerlIO_printf( Perl_debug_log, "%*s%4"UVXf" : ",
1233    (int)depth * 2 + 2,"",
1234    (UV)TRIE_NODENUM( state ) );
1235
1236   for( charid = 0 ; charid < trie->uniquecharcount ; charid++ ) {
1237    UV v=(UV)SAFE_TRIE_NODENUM( trie->trans[ state + charid ].next );
1238    if (v)
1239     PerlIO_printf( Perl_debug_log, "%*"UVXf, colwidth, v );
1240    else
1241     PerlIO_printf( Perl_debug_log, "%*s", colwidth, "." );
1242   }
1243   if ( ! trie->states[ TRIE_NODENUM( state ) ].wordnum ) {
1244    PerlIO_printf( Perl_debug_log, " (%4"UVXf")\n", (UV)trie->trans[ state ].check );
1245   } else {
1246    PerlIO_printf( Perl_debug_log, " (%4"UVXf") W%4X\n", (UV)trie->trans[ state ].check,
1247    trie->states[ TRIE_NODENUM( state ) ].wordnum );
1248   }
1249  }
1250 }
1251
1252 #endif
1253
1254
1255 /* make_trie(startbranch,first,last,tail,word_count,flags,depth)
1256   startbranch: the first branch in the whole branch sequence
1257   first      : start branch of sequence of branch-exact nodes.
1258    May be the same as startbranch
1259   last       : Thing following the last branch.
1260    May be the same as tail.
1261   tail       : item following the branch sequence
1262   count      : words in the sequence
1263   flags      : currently the OP() type we will be building one of /EXACT(|F|Fl)/
1264   depth      : indent depth
1265
1266 Inplace optimizes a sequence of 2 or more Branch-Exact nodes into a TRIE node.
1267
1268 A trie is an N'ary tree where the branches are determined by digital
1269 decomposition of the key. IE, at the root node you look up the 1st character and
1270 follow that branch repeat until you find the end of the branches. Nodes can be
1271 marked as "accepting" meaning they represent a complete word. Eg:
1272
1273   /he|she|his|hers/
1274
1275 would convert into the following structure. Numbers represent states, letters
1276 following numbers represent valid transitions on the letter from that state, if
1277 the number is in square brackets it represents an accepting state, otherwise it
1278 will be in parenthesis.
1279
1280  +-h->+-e->[3]-+-r->(8)-+-s->[9]
1281  |    |
1282  |   (2)
1283  |    |
1284  (1)   +-i->(6)-+-s->[7]
1285  |
1286  +-s->(3)-+-h->(4)-+-e->[5]
1287
1288  Accept Word Mapping: 3=>1 (he),5=>2 (she), 7=>3 (his), 9=>4 (hers)
1289
1290 This shows that when matching against the string 'hers' we will begin at state 1
1291 read 'h' and move to state 2, read 'e' and move to state 3 which is accepting,
1292 then read 'r' and go to state 8 followed by 's' which takes us to state 9 which
1293 is also accepting. Thus we know that we can match both 'he' and 'hers' with a
1294 single traverse. We store a mapping from accepting to state to which word was
1295 matched, and then when we have multiple possibilities we try to complete the
1296 rest of the regex in the order in which they occured in the alternation.
1297
1298 The only prior NFA like behaviour that would be changed by the TRIE support is
1299 the silent ignoring of duplicate alternations which are of the form:
1300
1301  / (DUPE|DUPE) X? (?{ ... }) Y /x
1302
1303 Thus EVAL blocks following a trie may be called a different number of times with
1304 and without the optimisation. With the optimisations dupes will be silently
1305 ignored. This inconsistent behaviour of EVAL type nodes is well established as
1306 the following demonstrates:
1307
1308  'words'=~/(word|word|word)(?{ print $1 })[xyz]/
1309
1310 which prints out 'word' three times, but
1311
1312  'words'=~/(word|word|word)(?{ print $1 })S/
1313
1314 which doesnt print it out at all. This is due to other optimisations kicking in.
1315
1316 Example of what happens on a structural level:
1317
1318 The regexp /(ac|ad|ab)+/ will produce the following debug output:
1319
1320    1: CURLYM[1] {1,32767}(18)
1321    5:   BRANCH(8)
1322    6:     EXACT <ac>(16)
1323    8:   BRANCH(11)
1324    9:     EXACT <ad>(16)
1325   11:   BRANCH(14)
1326   12:     EXACT <ab>(16)
1327   16:   SUCCEED(0)
1328   17:   NOTHING(18)
1329   18: END(0)
1330
1331 This would be optimizable with startbranch=5, first=5, last=16, tail=16
1332 and should turn into:
1333
1334    1: CURLYM[1] {1,32767}(18)
1335    5:   TRIE(16)
1336   [Words:3 Chars Stored:6 Unique Chars:4 States:5 NCP:1]
1337   <ac>
1338   <ad>
1339   <ab>
1340   16:   SUCCEED(0)
1341   17:   NOTHING(18)
1342   18: END(0)
1343
1344 Cases where tail != last would be like /(?foo|bar)baz/:
1345
1346    1: BRANCH(4)
1347    2:   EXACT <foo>(8)
1348    4: BRANCH(7)
1349    5:   EXACT <bar>(8)
1350    7: TAIL(8)
1351    8: EXACT <baz>(10)
1352   10: END(0)
1353
1354 which would be optimizable with startbranch=1, first=1, last=7, tail=8
1355 and would end up looking like:
1356
1357  1: TRIE(8)
1358  [Words:2 Chars Stored:6 Unique Chars:5 States:7 NCP:1]
1359   <foo>
1360   <bar>
1361    7: TAIL(8)
1362    8: EXACT <baz>(10)
1363   10: END(0)
1364
1365  d = uvuni_to_utf8_flags(d, uv, 0);
1366
1367 is the recommended Unicode-aware way of saying
1368
1369  *(d++) = uv;
1370 */
1371
1372 #define TRIE_STORE_REVCHAR(val)                                            \
1373  STMT_START {                                                           \
1374   if (UTF) {          \
1375    SV *zlopp = newSV(7); /* XXX: optimize me */                   \
1376    unsigned char *flrbbbbb = (unsigned char *) SvPVX(zlopp);    \
1377    unsigned const char *const kapow = uvuni_to_utf8(flrbbbbb, val); \
1378    SvCUR_set(zlopp, kapow - flrbbbbb);       \
1379    SvPOK_on(zlopp);         \
1380    SvUTF8_on(zlopp);         \
1381    av_push(revcharmap, zlopp);        \
1382   } else {          \
1383    char ooooff = (char)val;                                           \
1384    av_push(revcharmap, newSVpvn(&ooooff, 1));      \
1385   }           \
1386   } STMT_END
1387
1388 #define TRIE_READ_CHAR STMT_START {                                                     \
1389  wordlen++;                                                                          \
1390  if ( UTF ) {                                                                        \
1391   /* if it is UTF then it is either already folded, or does not need folding */   \
1392   uvc = utf8n_to_uvuni( (const U8*) uc, UTF8_MAXLEN, &len, uniflags);             \
1393  }                                                                                   \
1394  else if (folder == PL_fold_latin1) {                                                \
1395   /* if we use this folder we have to obey unicode rules on latin-1 data */       \
1396   if ( foldlen > 0 ) {                                                            \
1397   uvc = utf8n_to_uvuni( (const U8*) scan, UTF8_MAXLEN, &len, uniflags );       \
1398   foldlen -= len;                                                              \
1399   scan += len;                                                                 \
1400   len = 0;                                                                     \
1401   } else {                                                                        \
1402    len = 1;                                                                    \
1403    uvc = _to_fold_latin1( (U8) *uc, foldbuf, &foldlen, 1);                     \
1404    skiplen = UNISKIP(uvc);                                                     \
1405    foldlen -= skiplen;                                                         \
1406    scan = foldbuf + skiplen;                                                   \
1407   }                                                                               \
1408  } else {                                                                            \
1409   /* raw data, will be folded later if needed */                                  \
1410   uvc = (U32)*uc;                                                                 \
1411   len = 1;                                                                        \
1412  }                                                                                   \
1413 } STMT_END
1414
1415
1416
1417 #define TRIE_LIST_PUSH(state,fid,ns) STMT_START {               \
1418  if ( TRIE_LIST_CUR( state ) >=TRIE_LIST_LEN( state ) ) {    \
1419   U32 ging = TRIE_LIST_LEN( state ) *= 2;                 \
1420   Renew( trie->states[ state ].trans.list, ging, reg_trie_trans_le ); \
1421  }                                                           \
1422  TRIE_LIST_ITEM( state, TRIE_LIST_CUR( state ) ).forid = fid;     \
1423  TRIE_LIST_ITEM( state, TRIE_LIST_CUR( state ) ).newstate = ns;   \
1424  TRIE_LIST_CUR( state )++;                                   \
1425 } STMT_END
1426
1427 #define TRIE_LIST_NEW(state) STMT_START {                       \
1428  Newxz( trie->states[ state ].trans.list,               \
1429   4, reg_trie_trans_le );                                 \
1430  TRIE_LIST_CUR( state ) = 1;                                \
1431  TRIE_LIST_LEN( state ) = 4;                                \
1432 } STMT_END
1433
1434 #define TRIE_HANDLE_WORD(state) STMT_START {                    \
1435  U16 dupe= trie->states[ state ].wordnum;                    \
1436  regnode * const noper_next = regnext( noper );              \
1437                 \
1438  DEBUG_r({                                                   \
1439   /* store the word for dumping */                        \
1440   SV* tmp;                                                \
1441   if (OP(noper) != NOTHING)                               \
1442    tmp = newSVpvn_utf8(STRING(noper), STR_LEN(noper), UTF); \
1443   else                                                    \
1444    tmp = newSVpvn_utf8( "", 0, UTF );   \
1445   av_push( trie_words, tmp );                             \
1446  });                                                         \
1447                 \
1448  curword++;                                                  \
1449  trie->wordinfo[curword].prev   = 0;                         \
1450  trie->wordinfo[curword].len    = wordlen;                   \
1451  trie->wordinfo[curword].accept = state;                     \
1452                 \
1453  if ( noper_next < tail ) {                                  \
1454   if (!trie->jump)                                        \
1455    trie->jump = (U16 *) PerlMemShared_calloc( word_count + 1, sizeof(U16) ); \
1456   trie->jump[curword] = (U16)(noper_next - convert);      \
1457   if (!jumper)                                            \
1458    jumper = noper_next;                                \
1459   if (!nextbranch)                                        \
1460    nextbranch= regnext(cur);                           \
1461  }                                                           \
1462                 \
1463  if ( dupe ) {                                               \
1464   /* It's a dupe. Pre-insert into the wordinfo[].prev   */\
1465   /* chain, so that when the bits of chain are later    */\
1466   /* linked together, the dups appear in the chain      */\
1467   trie->wordinfo[curword].prev = trie->wordinfo[dupe].prev; \
1468   trie->wordinfo[dupe].prev = curword;                    \
1469  } else {                                                    \
1470   /* we haven't inserted this word yet.                */ \
1471   trie->states[ state ].wordnum = curword;                \
1472  }                                                           \
1473 } STMT_END
1474
1475
1476 #define TRIE_TRANS_STATE(state,base,ucharcount,charid,special)  \
1477  ( ( base + charid >=  ucharcount     \
1478   && base + charid < ubound     \
1479   && state == trie->trans[ base - ucharcount + charid ].check \
1480   && trie->trans[ base - ucharcount + charid ].next )  \
1481   ? trie->trans[ base - ucharcount + charid ].next  \
1482   : ( state==1 ? special : 0 )     \
1483  )
1484
1485 #define MADE_TRIE       1
1486 #define MADE_JUMP_TRIE  2
1487 #define MADE_EXACT_TRIE 4
1488
1489 STATIC I32
1490 S_make_trie(pTHX_ RExC_state_t *pRExC_state, regnode *startbranch, regnode *first, regnode *last, regnode *tail, U32 word_count, U32 flags, U32 depth)
1491 {
1492  dVAR;
1493  /* first pass, loop through and scan words */
1494  reg_trie_data *trie;
1495  HV *widecharmap = NULL;
1496  AV *revcharmap = newAV();
1497  regnode *cur;
1498  const U32 uniflags = UTF8_ALLOW_DEFAULT;
1499  STRLEN len = 0;
1500  UV uvc = 0;
1501  U16 curword = 0;
1502  U32 next_alloc = 0;
1503  regnode *jumper = NULL;
1504  regnode *nextbranch = NULL;
1505  regnode *convert = NULL;
1506  U32 *prev_states; /* temp array mapping each state to previous one */
1507  /* we just use folder as a flag in utf8 */
1508  const U8 * folder = NULL;
1509
1510 #ifdef DEBUGGING
1511  const U32 data_slot = add_data( pRExC_state, 4, "tuuu" );
1512  AV *trie_words = NULL;
1513  /* along with revcharmap, this only used during construction but both are
1514  * useful during debugging so we store them in the struct when debugging.
1515  */
1516 #else
1517  const U32 data_slot = add_data( pRExC_state, 2, "tu" );
1518  STRLEN trie_charcount=0;
1519 #endif
1520  SV *re_trie_maxbuff;
1521  GET_RE_DEBUG_FLAGS_DECL;
1522
1523  PERL_ARGS_ASSERT_MAKE_TRIE;
1524 #ifndef DEBUGGING
1525  PERL_UNUSED_ARG(depth);
1526 #endif
1527
1528  switch (flags) {
1529   case EXACT: break;
1530   case EXACTFA:
1531   case EXACTFU_SS:
1532   case EXACTFU_TRICKYFOLD:
1533   case EXACTFU: folder = PL_fold_latin1; break;
1534   case EXACTF:  folder = PL_fold; break;
1535   case EXACTFL: folder = PL_fold_locale; break;
1536   default: Perl_croak( aTHX_ "panic! In trie construction, unknown node type %u %s", (unsigned) flags, PL_reg_name[flags] );
1537  }
1538
1539  trie = (reg_trie_data *) PerlMemShared_calloc( 1, sizeof(reg_trie_data) );
1540  trie->refcount = 1;
1541  trie->startstate = 1;
1542  trie->wordcount = word_count;
1543  RExC_rxi->data->data[ data_slot ] = (void*)trie;
1544  trie->charmap = (U16 *) PerlMemShared_calloc( 256, sizeof(U16) );
1545  if (flags == EXACT)
1546   trie->bitmap = (char *) PerlMemShared_calloc( ANYOF_BITMAP_SIZE, 1 );
1547  trie->wordinfo = (reg_trie_wordinfo *) PerlMemShared_calloc(
1548      trie->wordcount+1, sizeof(reg_trie_wordinfo));
1549
1550  DEBUG_r({
1551   trie_words = newAV();
1552  });
1553
1554  re_trie_maxbuff = get_sv(RE_TRIE_MAXBUF_NAME, 1);
1555  if (!SvIOK(re_trie_maxbuff)) {
1556   sv_setiv(re_trie_maxbuff, RE_TRIE_MAXBUF_INIT);
1557  }
1558  DEBUG_OPTIMISE_r({
1559     PerlIO_printf( Perl_debug_log,
1560     "%*smake_trie start==%d, first==%d, last==%d, tail==%d depth=%d\n",
1561     (int)depth * 2 + 2, "",
1562     REG_NODE_NUM(startbranch),REG_NODE_NUM(first),
1563     REG_NODE_NUM(last), REG_NODE_NUM(tail),
1564     (int)depth);
1565  });
1566
1567    /* Find the node we are going to overwrite */
1568  if ( first == startbranch && OP( last ) != BRANCH ) {
1569   /* whole branch chain */
1570   convert = first;
1571  } else {
1572   /* branch sub-chain */
1573   convert = NEXTOPER( first );
1574  }
1575
1576  /*  -- First loop and Setup --
1577
1578  We first traverse the branches and scan each word to determine if it
1579  contains widechars, and how many unique chars there are, this is
1580  important as we have to build a table with at least as many columns as we
1581  have unique chars.
1582
1583  We use an array of integers to represent the character codes 0..255
1584  (trie->charmap) and we use a an HV* to store Unicode characters. We use the
1585  native representation of the character value as the key and IV's for the
1586  coded index.
1587
1588  *TODO* If we keep track of how many times each character is used we can
1589  remap the columns so that the table compression later on is more
1590  efficient in terms of memory by ensuring the most common value is in the
1591  middle and the least common are on the outside.  IMO this would be better
1592  than a most to least common mapping as theres a decent chance the most
1593  common letter will share a node with the least common, meaning the node
1594  will not be compressible. With a middle is most common approach the worst
1595  case is when we have the least common nodes twice.
1596
1597  */
1598
1599  for ( cur = first ; cur < last ; cur = regnext( cur ) ) {
1600   regnode * const noper = NEXTOPER( cur );
1601   const U8 *uc = (U8*)STRING( noper );
1602   const U8 * const e  = uc + STR_LEN( noper );
1603   STRLEN foldlen = 0;
1604   U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1605   STRLEN skiplen = 0;
1606   const U8 *scan = (U8*)NULL;
1607   U32 wordlen      = 0;         /* required init */
1608   STRLEN chars = 0;
1609   bool set_bit = trie->bitmap ? 1 : 0; /*store the first char in the bitmap?*/
1610
1611   if (OP(noper) == NOTHING) {
1612    trie->minlen= 0;
1613    continue;
1614   }
1615   if ( set_bit ) { /* bitmap only alloced when !(UTF&&Folding) */
1616    TRIE_BITMAP_SET(trie,*uc); /* store the raw first byte
1617           regardless of encoding */
1618    if (OP( noper ) == EXACTFU_SS) {
1619     /* false positives are ok, so just set this */
1620     TRIE_BITMAP_SET(trie,0xDF);
1621    }
1622   }
1623   for ( ; uc < e ; uc += len ) {
1624    TRIE_CHARCOUNT(trie)++;
1625    TRIE_READ_CHAR;
1626    chars++;
1627    if ( uvc < 256 ) {
1628     if ( folder ) {
1629      U8 folded= folder[ (U8) uvc ];
1630      if ( !trie->charmap[ folded ] ) {
1631       trie->charmap[ folded ]=( ++trie->uniquecharcount );
1632       TRIE_STORE_REVCHAR( folded );
1633      }
1634     }
1635     if ( !trie->charmap[ uvc ] ) {
1636      trie->charmap[ uvc ]=( ++trie->uniquecharcount );
1637      TRIE_STORE_REVCHAR( uvc );
1638     }
1639     if ( set_bit ) {
1640      /* store the codepoint in the bitmap, and its folded
1641      * equivalent. */
1642      TRIE_BITMAP_SET(trie, uvc);
1643
1644      /* store the folded codepoint */
1645      if ( folder ) TRIE_BITMAP_SET(trie, folder[(U8) uvc ]);
1646
1647      if ( !UTF ) {
1648       /* store first byte of utf8 representation of
1649       variant codepoints */
1650       if (! UNI_IS_INVARIANT(uvc)) {
1651        TRIE_BITMAP_SET(trie, UTF8_TWO_BYTE_HI(uvc));
1652       }
1653      }
1654      set_bit = 0; /* We've done our bit :-) */
1655     }
1656    } else {
1657     SV** svpp;
1658     if ( !widecharmap )
1659      widecharmap = newHV();
1660
1661     svpp = hv_fetch( widecharmap, (char*)&uvc, sizeof( UV ), 1 );
1662
1663     if ( !svpp )
1664      Perl_croak( aTHX_ "error creating/fetching widecharmap entry for 0x%"UVXf, uvc );
1665
1666     if ( !SvTRUE( *svpp ) ) {
1667      sv_setiv( *svpp, ++trie->uniquecharcount );
1668      TRIE_STORE_REVCHAR(uvc);
1669     }
1670    }
1671   }
1672   if( cur == first ) {
1673    trie->minlen = chars;
1674    trie->maxlen = chars;
1675   } else if (chars < trie->minlen) {
1676    trie->minlen = chars;
1677   } else if (chars > trie->maxlen) {
1678    trie->maxlen = chars;
1679   }
1680   if (OP( noper ) == EXACTFU_SS) {
1681    /* XXX: workaround - 'ss' could match "\x{DF}" so minlen could be 1 and not 2*/
1682    if (trie->minlen > 1)
1683     trie->minlen= 1;
1684   }
1685   if (OP( noper ) == EXACTFU_TRICKYFOLD) {
1686    /* XXX: workround - things like "\x{1FBE}\x{0308}\x{0301}" can match "\x{0390}"
1687    *        - We assume that any such sequence might match a 2 byte string */
1688    if (trie->minlen > 2 )
1689     trie->minlen= 2;
1690   }
1691
1692  } /* end first pass */
1693  DEBUG_TRIE_COMPILE_r(
1694   PerlIO_printf( Perl_debug_log, "%*sTRIE(%s): W:%d C:%d Uq:%d Min:%d Max:%d\n",
1695     (int)depth * 2 + 2,"",
1696     ( widecharmap ? "UTF8" : "NATIVE" ), (int)word_count,
1697     (int)TRIE_CHARCOUNT(trie), trie->uniquecharcount,
1698     (int)trie->minlen, (int)trie->maxlen )
1699  );
1700
1701  /*
1702   We now know what we are dealing with in terms of unique chars and
1703   string sizes so we can calculate how much memory a naive
1704   representation using a flat table  will take. If it's over a reasonable
1705   limit (as specified by ${^RE_TRIE_MAXBUF}) we use a more memory
1706   conservative but potentially much slower representation using an array
1707   of lists.
1708
1709   At the end we convert both representations into the same compressed
1710   form that will be used in regexec.c for matching with. The latter
1711   is a form that cannot be used to construct with but has memory
1712   properties similar to the list form and access properties similar
1713   to the table form making it both suitable for fast searches and
1714   small enough that its feasable to store for the duration of a program.
1715
1716   See the comment in the code where the compressed table is produced
1717   inplace from the flat tabe representation for an explanation of how
1718   the compression works.
1719
1720  */
1721
1722
1723  Newx(prev_states, TRIE_CHARCOUNT(trie) + 2, U32);
1724  prev_states[1] = 0;
1725
1726  if ( (IV)( ( TRIE_CHARCOUNT(trie) + 1 ) * trie->uniquecharcount + 1) > SvIV(re_trie_maxbuff) ) {
1727   /*
1728    Second Pass -- Array Of Lists Representation
1729
1730    Each state will be represented by a list of charid:state records
1731    (reg_trie_trans_le) the first such element holds the CUR and LEN
1732    points of the allocated array. (See defines above).
1733
1734    We build the initial structure using the lists, and then convert
1735    it into the compressed table form which allows faster lookups
1736    (but cant be modified once converted).
1737   */
1738
1739   STRLEN transcount = 1;
1740
1741   DEBUG_TRIE_COMPILE_MORE_r( PerlIO_printf( Perl_debug_log,
1742    "%*sCompiling trie using list compiler\n",
1743    (int)depth * 2 + 2, ""));
1744
1745   trie->states = (reg_trie_state *)
1746    PerlMemShared_calloc( TRIE_CHARCOUNT(trie) + 2,
1747         sizeof(reg_trie_state) );
1748   TRIE_LIST_NEW(1);
1749   next_alloc = 2;
1750
1751   for ( cur = first ; cur < last ; cur = regnext( cur ) ) {
1752
1753    regnode * const noper = NEXTOPER( cur );
1754    U8 *uc           = (U8*)STRING( noper );
1755    const U8 * const e = uc + STR_LEN( noper );
1756    U32 state        = 1;         /* required init */
1757    U16 charid       = 0;         /* sanity init */
1758    U8 *scan         = (U8*)NULL; /* sanity init */
1759    STRLEN foldlen   = 0;         /* required init */
1760    U32 wordlen      = 0;         /* required init */
1761    U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1762    STRLEN skiplen   = 0;
1763
1764    if (OP(noper) != NOTHING) {
1765     for ( ; uc < e ; uc += len ) {
1766
1767      TRIE_READ_CHAR;
1768
1769      if ( uvc < 256 ) {
1770       charid = trie->charmap[ uvc ];
1771      } else {
1772       SV** const svpp = hv_fetch( widecharmap, (char*)&uvc, sizeof( UV ), 0);
1773       if ( !svpp ) {
1774        charid = 0;
1775       } else {
1776        charid=(U16)SvIV( *svpp );
1777       }
1778      }
1779      /* charid is now 0 if we dont know the char read, or nonzero if we do */
1780      if ( charid ) {
1781
1782       U16 check;
1783       U32 newstate = 0;
1784
1785       charid--;
1786       if ( !trie->states[ state ].trans.list ) {
1787        TRIE_LIST_NEW( state );
1788       }
1789       for ( check = 1; check <= TRIE_LIST_USED( state ); check++ ) {
1790        if ( TRIE_LIST_ITEM( state, check ).forid == charid ) {
1791         newstate = TRIE_LIST_ITEM( state, check ).newstate;
1792         break;
1793        }
1794       }
1795       if ( ! newstate ) {
1796        newstate = next_alloc++;
1797        prev_states[newstate] = state;
1798        TRIE_LIST_PUSH( state, charid, newstate );
1799        transcount++;
1800       }
1801       state = newstate;
1802      } else {
1803       Perl_croak( aTHX_ "panic! In trie construction, no char mapping for %"IVdf, uvc );
1804      }
1805     }
1806    }
1807    TRIE_HANDLE_WORD(state);
1808
1809   } /* end second pass */
1810
1811   /* next alloc is the NEXT state to be allocated */
1812   trie->statecount = next_alloc;
1813   trie->states = (reg_trie_state *)
1814    PerlMemShared_realloc( trie->states,
1815         next_alloc
1816         * sizeof(reg_trie_state) );
1817
1818   /* and now dump it out before we compress it */
1819   DEBUG_TRIE_COMPILE_MORE_r(dump_trie_interim_list(trie, widecharmap,
1820               revcharmap, next_alloc,
1821               depth+1)
1822   );
1823
1824   trie->trans = (reg_trie_trans *)
1825    PerlMemShared_calloc( transcount, sizeof(reg_trie_trans) );
1826   {
1827    U32 state;
1828    U32 tp = 0;
1829    U32 zp = 0;
1830
1831
1832    for( state=1 ; state < next_alloc ; state ++ ) {
1833     U32 base=0;
1834
1835     /*
1836     DEBUG_TRIE_COMPILE_MORE_r(
1837      PerlIO_printf( Perl_debug_log, "tp: %d zp: %d ",tp,zp)
1838     );
1839     */
1840
1841     if (trie->states[state].trans.list) {
1842      U16 minid=TRIE_LIST_ITEM( state, 1).forid;
1843      U16 maxid=minid;
1844      U16 idx;
1845
1846      for( idx = 2 ; idx <= TRIE_LIST_USED( state ) ; idx++ ) {
1847       const U16 forid = TRIE_LIST_ITEM( state, idx).forid;
1848       if ( forid < minid ) {
1849        minid=forid;
1850       } else if ( forid > maxid ) {
1851        maxid=forid;
1852       }
1853      }
1854      if ( transcount < tp + maxid - minid + 1) {
1855       transcount *= 2;
1856       trie->trans = (reg_trie_trans *)
1857        PerlMemShared_realloc( trie->trans,
1858              transcount
1859              * sizeof(reg_trie_trans) );
1860       Zero( trie->trans + (transcount / 2), transcount / 2 , reg_trie_trans );
1861      }
1862      base = trie->uniquecharcount + tp - minid;
1863      if ( maxid == minid ) {
1864       U32 set = 0;
1865       for ( ; zp < tp ; zp++ ) {
1866        if ( ! trie->trans[ zp ].next ) {
1867         base = trie->uniquecharcount + zp - minid;
1868         trie->trans[ zp ].next = TRIE_LIST_ITEM( state, 1).newstate;
1869         trie->trans[ zp ].check = state;
1870         set = 1;
1871         break;
1872        }
1873       }
1874       if ( !set ) {
1875        trie->trans[ tp ].next = TRIE_LIST_ITEM( state, 1).newstate;
1876        trie->trans[ tp ].check = state;
1877        tp++;
1878        zp = tp;
1879       }
1880      } else {
1881       for ( idx=1; idx <= TRIE_LIST_USED( state ) ; idx++ ) {
1882        const U32 tid = base -  trie->uniquecharcount + TRIE_LIST_ITEM( state, idx ).forid;
1883        trie->trans[ tid ].next = TRIE_LIST_ITEM( state, idx ).newstate;
1884        trie->trans[ tid ].check = state;
1885       }
1886       tp += ( maxid - minid + 1 );
1887      }
1888      Safefree(trie->states[ state ].trans.list);
1889     }
1890     /*
1891     DEBUG_TRIE_COMPILE_MORE_r(
1892      PerlIO_printf( Perl_debug_log, " base: %d\n",base);
1893     );
1894     */
1895     trie->states[ state ].trans.base=base;
1896    }
1897    trie->lasttrans = tp + 1;
1898   }
1899  } else {
1900   /*
1901   Second Pass -- Flat Table Representation.
1902
1903   we dont use the 0 slot of either trans[] or states[] so we add 1 to each.
1904   We know that we will need Charcount+1 trans at most to store the data
1905   (one row per char at worst case) So we preallocate both structures
1906   assuming worst case.
1907
1908   We then construct the trie using only the .next slots of the entry
1909   structs.
1910
1911   We use the .check field of the first entry of the node temporarily to
1912   make compression both faster and easier by keeping track of how many non
1913   zero fields are in the node.
1914
1915   Since trans are numbered from 1 any 0 pointer in the table is a FAIL
1916   transition.
1917
1918   There are two terms at use here: state as a TRIE_NODEIDX() which is a
1919   number representing the first entry of the node, and state as a
1920   TRIE_NODENUM() which is the trans number. state 1 is TRIE_NODEIDX(1) and
1921   TRIE_NODENUM(1), state 2 is TRIE_NODEIDX(2) and TRIE_NODENUM(3) if there
1922   are 2 entrys per node. eg:
1923
1924    A B       A B
1925   1. 2 4    1. 3 7
1926   2. 0 3    3. 0 5
1927   3. 0 0    5. 0 0
1928   4. 0 0    7. 0 0
1929
1930   The table is internally in the right hand, idx form. However as we also
1931   have to deal with the states array which is indexed by nodenum we have to
1932   use TRIE_NODENUM() to convert.
1933
1934   */
1935   DEBUG_TRIE_COMPILE_MORE_r( PerlIO_printf( Perl_debug_log,
1936    "%*sCompiling trie using table compiler\n",
1937    (int)depth * 2 + 2, ""));
1938
1939   trie->trans = (reg_trie_trans *)
1940    PerlMemShared_calloc( ( TRIE_CHARCOUNT(trie) + 1 )
1941         * trie->uniquecharcount + 1,
1942         sizeof(reg_trie_trans) );
1943   trie->states = (reg_trie_state *)
1944    PerlMemShared_calloc( TRIE_CHARCOUNT(trie) + 2,
1945         sizeof(reg_trie_state) );
1946   next_alloc = trie->uniquecharcount + 1;
1947
1948
1949   for ( cur = first ; cur < last ; cur = regnext( cur ) ) {
1950
1951    regnode * const noper   = NEXTOPER( cur );
1952    const U8 *uc     = (U8*)STRING( noper );
1953    const U8 * const e = uc + STR_LEN( noper );
1954
1955    U32 state        = 1;         /* required init */
1956
1957    U16 charid       = 0;         /* sanity init */
1958    U32 accept_state = 0;         /* sanity init */
1959    U8 *scan         = (U8*)NULL; /* sanity init */
1960
1961    STRLEN foldlen   = 0;         /* required init */
1962    U32 wordlen      = 0;         /* required init */
1963    STRLEN skiplen   = 0;
1964    U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1965
1966
1967    if ( OP(noper) != NOTHING ) {
1968     for ( ; uc < e ; uc += len ) {
1969
1970      TRIE_READ_CHAR;
1971
1972      if ( uvc < 256 ) {
1973       charid = trie->charmap[ uvc ];
1974      } else {
1975       SV* const * const svpp = hv_fetch( widecharmap, (char*)&uvc, sizeof( UV ), 0);
1976       charid = svpp ? (U16)SvIV(*svpp) : 0;
1977      }
1978      if ( charid ) {
1979       charid--;
1980       if ( !trie->trans[ state + charid ].next ) {
1981        trie->trans[ state + charid ].next = next_alloc;
1982        trie->trans[ state ].check++;
1983        prev_states[TRIE_NODENUM(next_alloc)]
1984          = TRIE_NODENUM(state);
1985        next_alloc += trie->uniquecharcount;
1986       }
1987       state = trie->trans[ state + charid ].next;
1988      } else {
1989       Perl_croak( aTHX_ "panic! In trie construction, no char mapping for %"IVdf, uvc );
1990      }
1991      /* charid is now 0 if we dont know the char read, or nonzero if we do */
1992     }
1993    }
1994    accept_state = TRIE_NODENUM( state );
1995    TRIE_HANDLE_WORD(accept_state);
1996
1997   } /* end second pass */
1998
1999   /* and now dump it out before we compress it */
2000   DEBUG_TRIE_COMPILE_MORE_r(dump_trie_interim_table(trie, widecharmap,
2001               revcharmap,
2002               next_alloc, depth+1));
2003
2004   {
2005   /*
2006   * Inplace compress the table.*
2007
2008   For sparse data sets the table constructed by the trie algorithm will
2009   be mostly 0/FAIL transitions or to put it another way mostly empty.
2010   (Note that leaf nodes will not contain any transitions.)
2011
2012   This algorithm compresses the tables by eliminating most such
2013   transitions, at the cost of a modest bit of extra work during lookup:
2014
2015   - Each states[] entry contains a .base field which indicates the
2016   index in the state[] array wheres its transition data is stored.
2017
2018   - If .base is 0 there are no valid transitions from that node.
2019
2020   - If .base is nonzero then charid is added to it to find an entry in
2021   the trans array.
2022
2023   -If trans[states[state].base+charid].check!=state then the
2024   transition is taken to be a 0/Fail transition. Thus if there are fail
2025   transitions at the front of the node then the .base offset will point
2026   somewhere inside the previous nodes data (or maybe even into a node
2027   even earlier), but the .check field determines if the transition is
2028   valid.
2029
2030   XXX - wrong maybe?
2031   The following process inplace converts the table to the compressed
2032   table: We first do not compress the root node 1,and mark all its
2033   .check pointers as 1 and set its .base pointer as 1 as well. This
2034   allows us to do a DFA construction from the compressed table later,
2035   and ensures that any .base pointers we calculate later are greater
2036   than 0.
2037
2038   - We set 'pos' to indicate the first entry of the second node.
2039
2040   - We then iterate over the columns of the node, finding the first and
2041   last used entry at l and m. We then copy l..m into pos..(pos+m-l),
2042   and set the .check pointers accordingly, and advance pos
2043   appropriately and repreat for the next node. Note that when we copy
2044   the next pointers we have to convert them from the original
2045   NODEIDX form to NODENUM form as the former is not valid post
2046   compression.
2047
2048   - If a node has no transitions used we mark its base as 0 and do not
2049   advance the pos pointer.
2050
2051   - If a node only has one transition we use a second pointer into the
2052   structure to fill in allocated fail transitions from other states.
2053   This pointer is independent of the main pointer and scans forward
2054   looking for null transitions that are allocated to a state. When it
2055   finds one it writes the single transition into the "hole".  If the
2056   pointer doesnt find one the single transition is appended as normal.
2057
2058   - Once compressed we can Renew/realloc the structures to release the
2059   excess space.
2060
2061   See "Table-Compression Methods" in sec 3.9 of the Red Dragon,
2062   specifically Fig 3.47 and the associated pseudocode.
2063
2064   demq
2065   */
2066   const U32 laststate = TRIE_NODENUM( next_alloc );
2067   U32 state, charid;
2068   U32 pos = 0, zp=0;
2069   trie->statecount = laststate;
2070
2071   for ( state = 1 ; state < laststate ; state++ ) {
2072    U8 flag = 0;
2073    const U32 stateidx = TRIE_NODEIDX( state );
2074    const U32 o_used = trie->trans[ stateidx ].check;
2075    U32 used = trie->trans[ stateidx ].check;
2076    trie->trans[ stateidx ].check = 0;
2077
2078    for ( charid = 0 ; used && charid < trie->uniquecharcount ; charid++ ) {
2079     if ( flag || trie->trans[ stateidx + charid ].next ) {
2080      if ( trie->trans[ stateidx + charid ].next ) {
2081       if (o_used == 1) {
2082        for ( ; zp < pos ; zp++ ) {
2083         if ( ! trie->trans[ zp ].next ) {
2084          break;
2085         }
2086        }
2087        trie->states[ state ].trans.base = zp + trie->uniquecharcount - charid ;
2088        trie->trans[ zp ].next = SAFE_TRIE_NODENUM( trie->trans[ stateidx + charid ].next );
2089        trie->trans[ zp ].check = state;
2090        if ( ++zp > pos ) pos = zp;
2091        break;
2092       }
2093       used--;
2094      }
2095      if ( !flag ) {
2096       flag = 1;
2097       trie->states[ state ].trans.base = pos + trie->uniquecharcount - charid ;
2098      }
2099      trie->trans[ pos ].next = SAFE_TRIE_NODENUM( trie->trans[ stateidx + charid ].next );
2100      trie->trans[ pos ].check = state;
2101      pos++;
2102     }
2103    }
2104   }
2105   trie->lasttrans = pos + 1;
2106   trie->states = (reg_trie_state *)
2107    PerlMemShared_realloc( trie->states, laststate
2108         * sizeof(reg_trie_state) );
2109   DEBUG_TRIE_COMPILE_MORE_r(
2110     PerlIO_printf( Perl_debug_log,
2111      "%*sAlloc: %d Orig: %"IVdf" elements, Final:%"IVdf". Savings of %%%5.2f\n",
2112      (int)depth * 2 + 2,"",
2113      (int)( ( TRIE_CHARCOUNT(trie) + 1 ) * trie->uniquecharcount + 1 ),
2114      (IV)next_alloc,
2115      (IV)pos,
2116      ( ( next_alloc - pos ) * 100 ) / (double)next_alloc );
2117    );
2118
2119   } /* end table compress */
2120  }
2121  DEBUG_TRIE_COMPILE_MORE_r(
2122    PerlIO_printf(Perl_debug_log, "%*sStatecount:%"UVxf" Lasttrans:%"UVxf"\n",
2123     (int)depth * 2 + 2, "",
2124     (UV)trie->statecount,
2125     (UV)trie->lasttrans)
2126  );
2127  /* resize the trans array to remove unused space */
2128  trie->trans = (reg_trie_trans *)
2129   PerlMemShared_realloc( trie->trans, trie->lasttrans
2130        * sizeof(reg_trie_trans) );
2131
2132  {   /* Modify the program and insert the new TRIE node */
2133   U8 nodetype =(U8)(flags & 0xFF);
2134   char *str=NULL;
2135
2136 #ifdef DEBUGGING
2137   regnode *optimize = NULL;
2138 #ifdef RE_TRACK_PATTERN_OFFSETS
2139
2140   U32 mjd_offset = 0;
2141   U32 mjd_nodelen = 0;
2142 #endif /* RE_TRACK_PATTERN_OFFSETS */
2143 #endif /* DEBUGGING */
2144   /*
2145   This means we convert either the first branch or the first Exact,
2146   depending on whether the thing following (in 'last') is a branch
2147   or not and whther first is the startbranch (ie is it a sub part of
2148   the alternation or is it the whole thing.)
2149   Assuming its a sub part we convert the EXACT otherwise we convert
2150   the whole branch sequence, including the first.
2151   */
2152   /* Find the node we are going to overwrite */
2153   if ( first != startbranch || OP( last ) == BRANCH ) {
2154    /* branch sub-chain */
2155    NEXT_OFF( first ) = (U16)(last - first);
2156 #ifdef RE_TRACK_PATTERN_OFFSETS
2157    DEBUG_r({
2158     mjd_offset= Node_Offset((convert));
2159     mjd_nodelen= Node_Length((convert));
2160    });
2161 #endif
2162    /* whole branch chain */
2163   }
2164 #ifdef RE_TRACK_PATTERN_OFFSETS
2165   else {
2166    DEBUG_r({
2167     const  regnode *nop = NEXTOPER( convert );
2168     mjd_offset= Node_Offset((nop));
2169     mjd_nodelen= Node_Length((nop));
2170    });
2171   }
2172   DEBUG_OPTIMISE_r(
2173    PerlIO_printf(Perl_debug_log, "%*sMJD offset:%"UVuf" MJD length:%"UVuf"\n",
2174     (int)depth * 2 + 2, "",
2175     (UV)mjd_offset, (UV)mjd_nodelen)
2176   );
2177 #endif
2178   /* But first we check to see if there is a common prefix we can
2179   split out as an EXACT and put in front of the TRIE node.  */
2180   trie->startstate= 1;
2181   if ( trie->bitmap && !widecharmap && !trie->jump  ) {
2182    U32 state;
2183    for ( state = 1 ; state < trie->statecount-1 ; state++ ) {
2184     U32 ofs = 0;
2185     I32 idx = -1;
2186     U32 count = 0;
2187     const U32 base = trie->states[ state ].trans.base;
2188
2189     if ( trie->states[state].wordnum )
2190       count = 1;
2191
2192     for ( ofs = 0 ; ofs < trie->uniquecharcount ; ofs++ ) {
2193      if ( ( base + ofs >= trie->uniquecharcount ) &&
2194       ( base + ofs - trie->uniquecharcount < trie->lasttrans ) &&
2195       trie->trans[ base + ofs - trie->uniquecharcount ].check == state )
2196      {
2197       if ( ++count > 1 ) {
2198        SV **tmp = av_fetch( revcharmap, ofs, 0);
2199        const U8 *ch = (U8*)SvPV_nolen_const( *tmp );
2200        if ( state == 1 ) break;
2201        if ( count == 2 ) {
2202         Zero(trie->bitmap, ANYOF_BITMAP_SIZE, char);
2203         DEBUG_OPTIMISE_r(
2204          PerlIO_printf(Perl_debug_log,
2205           "%*sNew Start State=%"UVuf" Class: [",
2206           (int)depth * 2 + 2, "",
2207           (UV)state));
2208         if (idx >= 0) {
2209          SV ** const tmp = av_fetch( revcharmap, idx, 0);
2210          const U8 * const ch = (U8*)SvPV_nolen_const( *tmp );
2211
2212          TRIE_BITMAP_SET(trie,*ch);
2213          if ( folder )
2214           TRIE_BITMAP_SET(trie, folder[ *ch ]);
2215          DEBUG_OPTIMISE_r(
2216           PerlIO_printf(Perl_debug_log, "%s", (char*)ch)
2217          );
2218         }
2219        }
2220        TRIE_BITMAP_SET(trie,*ch);
2221        if ( folder )
2222         TRIE_BITMAP_SET(trie,folder[ *ch ]);
2223        DEBUG_OPTIMISE_r(PerlIO_printf( Perl_debug_log,"%s", ch));
2224       }
2225       idx = ofs;
2226      }
2227     }
2228     if ( count == 1 ) {
2229      SV **tmp = av_fetch( revcharmap, idx, 0);
2230      STRLEN len;
2231      char *ch = SvPV( *tmp, len );
2232      DEBUG_OPTIMISE_r({
2233       SV *sv=sv_newmortal();
2234       PerlIO_printf( Perl_debug_log,
2235        "%*sPrefix State: %"UVuf" Idx:%"UVuf" Char='%s'\n",
2236        (int)depth * 2 + 2, "",
2237        (UV)state, (UV)idx,
2238        pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 6,
2239         PL_colors[0], PL_colors[1],
2240         (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) |
2241         PERL_PV_ESCAPE_FIRSTCHAR
2242        )
2243       );
2244      });
2245      if ( state==1 ) {
2246       OP( convert ) = nodetype;
2247       str=STRING(convert);
2248       STR_LEN(convert)=0;
2249      }
2250      STR_LEN(convert) += len;
2251      while (len--)
2252       *str++ = *ch++;
2253     } else {
2254 #ifdef DEBUGGING
2255      if (state>1)
2256       DEBUG_OPTIMISE_r(PerlIO_printf( Perl_debug_log,"]\n"));
2257 #endif
2258      break;
2259     }
2260    }
2261    trie->prefixlen = (state-1);
2262    if (str) {
2263     regnode *n = convert+NODE_SZ_STR(convert);
2264     NEXT_OFF(convert) = NODE_SZ_STR(convert);
2265     trie->startstate = state;
2266     trie->minlen -= (state - 1);
2267     trie->maxlen -= (state - 1);
2268 #ifdef DEBUGGING
2269    /* At least the UNICOS C compiler choked on this
2270     * being argument to DEBUG_r(), so let's just have
2271     * it right here. */
2272    if (
2273 #ifdef PERL_EXT_RE_BUILD
2274     1
2275 #else
2276     DEBUG_r_TEST
2277 #endif
2278     ) {
2279     regnode *fix = convert;
2280     U32 word = trie->wordcount;
2281     mjd_nodelen++;
2282     Set_Node_Offset_Length(convert, mjd_offset, state - 1);
2283     while( ++fix < n ) {
2284      Set_Node_Offset_Length(fix, 0, 0);
2285     }
2286     while (word--) {
2287      SV ** const tmp = av_fetch( trie_words, word, 0 );
2288      if (tmp) {
2289       if ( STR_LEN(convert) <= SvCUR(*tmp) )
2290        sv_chop(*tmp, SvPV_nolen(*tmp) + STR_LEN(convert));
2291       else
2292        sv_chop(*tmp, SvPV_nolen(*tmp) + SvCUR(*tmp));
2293      }
2294     }
2295    }
2296 #endif
2297     if (trie->maxlen) {
2298      convert = n;
2299     } else {
2300      NEXT_OFF(convert) = (U16)(tail - convert);
2301      DEBUG_r(optimize= n);
2302     }
2303    }
2304   }
2305   if (!jumper)
2306    jumper = last;
2307   if ( trie->maxlen ) {
2308    NEXT_OFF( convert ) = (U16)(tail - convert);
2309    ARG_SET( convert, data_slot );
2310    /* Store the offset to the first unabsorbed branch in
2311    jump[0], which is otherwise unused by the jump logic.
2312    We use this when dumping a trie and during optimisation. */
2313    if (trie->jump)
2314     trie->jump[0] = (U16)(nextbranch - convert);
2315
2316    /* If the start state is not accepting (meaning there is no empty string/NOTHING)
2317    *   and there is a bitmap
2318    *   and the first "jump target" node we found leaves enough room
2319    * then convert the TRIE node into a TRIEC node, with the bitmap
2320    * embedded inline in the opcode - this is hypothetically faster.
2321    */
2322    if ( !trie->states[trie->startstate].wordnum
2323     && trie->bitmap
2324     && ( (char *)jumper - (char *)convert) >= (int)sizeof(struct regnode_charclass) )
2325    {
2326     OP( convert ) = TRIEC;
2327     Copy(trie->bitmap, ((struct regnode_charclass *)convert)->bitmap, ANYOF_BITMAP_SIZE, char);
2328     PerlMemShared_free(trie->bitmap);
2329     trie->bitmap= NULL;
2330    } else
2331     OP( convert ) = TRIE;
2332
2333    /* store the type in the flags */
2334    convert->flags = nodetype;
2335    DEBUG_r({
2336    optimize = convert
2337      + NODE_STEP_REGNODE
2338      + regarglen[ OP( convert ) ];
2339    });
2340    /* XXX We really should free up the resource in trie now,
2341     as we won't use them - (which resources?) dmq */
2342   }
2343   /* needed for dumping*/
2344   DEBUG_r(if (optimize) {
2345    regnode *opt = convert;
2346
2347    while ( ++opt < optimize) {
2348     Set_Node_Offset_Length(opt,0,0);
2349    }
2350    /*
2351     Try to clean up some of the debris left after the
2352     optimisation.
2353    */
2354    while( optimize < jumper ) {
2355     mjd_nodelen += Node_Length((optimize));
2356     OP( optimize ) = OPTIMIZED;
2357     Set_Node_Offset_Length(optimize,0,0);
2358     optimize++;
2359    }
2360    Set_Node_Offset_Length(convert,mjd_offset,mjd_nodelen);
2361   });
2362  } /* end node insert */
2363  REH_CALL_COMP_NODE_HOOK(pRExC_state->rx, convert);
2364
2365  /*  Finish populating the prev field of the wordinfo array.  Walk back
2366  *  from each accept state until we find another accept state, and if
2367  *  so, point the first word's .prev field at the second word. If the
2368  *  second already has a .prev field set, stop now. This will be the
2369  *  case either if we've already processed that word's accept state,
2370  *  or that state had multiple words, and the overspill words were
2371  *  already linked up earlier.
2372  */
2373  {
2374   U16 word;
2375   U32 state;
2376   U16 prev;
2377
2378   for (word=1; word <= trie->wordcount; word++) {
2379    prev = 0;
2380    if (trie->wordinfo[word].prev)
2381     continue;
2382    state = trie->wordinfo[word].accept;
2383    while (state) {
2384     state = prev_states[state];
2385     if (!state)
2386      break;
2387     prev = trie->states[state].wordnum;
2388     if (prev)
2389      break;
2390    }
2391    trie->wordinfo[word].prev = prev;
2392   }
2393   Safefree(prev_states);
2394  }
2395
2396
2397  /* and now dump out the compressed format */
2398  DEBUG_TRIE_COMPILE_r(dump_trie(trie, widecharmap, revcharmap, depth+1));
2399
2400  RExC_rxi->data->data[ data_slot + 1 ] = (void*)widecharmap;
2401 #ifdef DEBUGGING
2402  RExC_rxi->data->data[ data_slot + TRIE_WORDS_OFFSET ] = (void*)trie_words;
2403  RExC_rxi->data->data[ data_slot + 3 ] = (void*)revcharmap;
2404 #else
2405  SvREFCNT_dec(revcharmap);
2406 #endif
2407  return trie->jump
2408   ? MADE_JUMP_TRIE
2409   : trie->startstate>1
2410    ? MADE_EXACT_TRIE
2411    : MADE_TRIE;
2412 }
2413
2414 STATIC void
2415 S_make_trie_failtable(pTHX_ RExC_state_t *pRExC_state, regnode *source,  regnode *stclass, U32 depth)
2416 {
2417 /* The Trie is constructed and compressed now so we can build a fail array if it's needed
2418
2419    This is basically the Aho-Corasick algorithm. Its from exercise 3.31 and 3.32 in the
2420    "Red Dragon" -- Compilers, principles, techniques, and tools. Aho, Sethi, Ullman 1985/88
2421    ISBN 0-201-10088-6
2422
2423    We find the fail state for each state in the trie, this state is the longest proper
2424    suffix of the current state's 'word' that is also a proper prefix of another word in our
2425    trie. State 1 represents the word '' and is thus the default fail state. This allows
2426    the DFA not to have to restart after its tried and failed a word at a given point, it
2427    simply continues as though it had been matching the other word in the first place.
2428    Consider
2429  'abcdgu'=~/abcdefg|cdgu/
2430    When we get to 'd' we are still matching the first word, we would encounter 'g' which would
2431    fail, which would bring us to the state representing 'd' in the second word where we would
2432    try 'g' and succeed, proceeding to match 'cdgu'.
2433  */
2434  /* add a fail transition */
2435  const U32 trie_offset = ARG(source);
2436  reg_trie_data *trie=(reg_trie_data *)RExC_rxi->data->data[trie_offset];
2437  U32 *q;
2438  const U32 ucharcount = trie->uniquecharcount;
2439  const U32 numstates = trie->statecount;
2440  const U32 ubound = trie->lasttrans + ucharcount;
2441  U32 q_read = 0;
2442  U32 q_write = 0;
2443  U32 charid;
2444  U32 base = trie->states[ 1 ].trans.base;
2445  U32 *fail;
2446  reg_ac_data *aho;
2447  const U32 data_slot = add_data( pRExC_state, 1, "T" );
2448  GET_RE_DEBUG_FLAGS_DECL;
2449
2450  PERL_ARGS_ASSERT_MAKE_TRIE_FAILTABLE;
2451 #ifndef DEBUGGING
2452  PERL_UNUSED_ARG(depth);
2453 #endif
2454
2455
2456  ARG_SET( stclass, data_slot );
2457  aho = (reg_ac_data *) PerlMemShared_calloc( 1, sizeof(reg_ac_data) );
2458  RExC_rxi->data->data[ data_slot ] = (void*)aho;
2459  aho->trie=trie_offset;
2460  aho->states=(reg_trie_state *)PerlMemShared_malloc( numstates * sizeof(reg_trie_state) );
2461  Copy( trie->states, aho->states, numstates, reg_trie_state );
2462  Newxz( q, numstates, U32);
2463  aho->fail = (U32 *) PerlMemShared_calloc( numstates, sizeof(U32) );
2464  aho->refcount = 1;
2465  fail = aho->fail;
2466  /* initialize fail[0..1] to be 1 so that we always have
2467  a valid final fail state */
2468  fail[ 0 ] = fail[ 1 ] = 1;
2469
2470  for ( charid = 0; charid < ucharcount ; charid++ ) {
2471   const U32 newstate = TRIE_TRANS_STATE( 1, base, ucharcount, charid, 0 );
2472   if ( newstate ) {
2473    q[ q_write ] = newstate;
2474    /* set to point at the root */
2475    fail[ q[ q_write++ ] ]=1;
2476   }
2477  }
2478  while ( q_read < q_write) {
2479   const U32 cur = q[ q_read++ % numstates ];
2480   base = trie->states[ cur ].trans.base;
2481
2482   for ( charid = 0 ; charid < ucharcount ; charid++ ) {
2483    const U32 ch_state = TRIE_TRANS_STATE( cur, base, ucharcount, charid, 1 );
2484    if (ch_state) {
2485     U32 fail_state = cur;
2486     U32 fail_base;
2487     do {
2488      fail_state = fail[ fail_state ];
2489      fail_base = aho->states[ fail_state ].trans.base;
2490     } while ( !TRIE_TRANS_STATE( fail_state, fail_base, ucharcount, charid, 1 ) );
2491
2492     fail_state = TRIE_TRANS_STATE( fail_state, fail_base, ucharcount, charid, 1 );
2493     fail[ ch_state ] = fail_state;
2494     if ( !aho->states[ ch_state ].wordnum && aho->states[ fail_state ].wordnum )
2495     {
2496       aho->states[ ch_state ].wordnum =  aho->states[ fail_state ].wordnum;
2497     }
2498     q[ q_write++ % numstates] = ch_state;
2499    }
2500   }
2501  }
2502  /* restore fail[0..1] to 0 so that we "fall out" of the AC loop
2503  when we fail in state 1, this allows us to use the
2504  charclass scan to find a valid start char. This is based on the principle
2505  that theres a good chance the string being searched contains lots of stuff
2506  that cant be a start char.
2507  */
2508  fail[ 0 ] = fail[ 1 ] = 0;
2509  DEBUG_TRIE_COMPILE_r({
2510   PerlIO_printf(Perl_debug_log,
2511      "%*sStclass Failtable (%"UVuf" states): 0",
2512      (int)(depth * 2), "", (UV)numstates
2513   );
2514   for( q_read=1; q_read<numstates; q_read++ ) {
2515    PerlIO_printf(Perl_debug_log, ", %"UVuf, (UV)fail[q_read]);
2516   }
2517   PerlIO_printf(Perl_debug_log, "\n");
2518  });
2519  Safefree(q);
2520  /*RExC_seen |= REG_SEEN_TRIEDFA;*/
2521 }
2522
2523
2524 /*
2525  * There are strange code-generation bugs caused on sparc64 by gcc-2.95.2.
2526  * These need to be revisited when a newer toolchain becomes available.
2527  */
2528 #if defined(__sparc64__) && defined(__GNUC__)
2529 #   if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
2530 #       undef  SPARC64_GCC_WORKAROUND
2531 #       define SPARC64_GCC_WORKAROUND 1
2532 #   endif
2533 #endif
2534
2535 #define DEBUG_PEEP(str,scan,depth) \
2536  DEBUG_OPTIMISE_r({if (scan){ \
2537  SV * const mysv=sv_newmortal(); \
2538  regnode *Next = regnext(scan); \
2539  regprop(RExC_rx, mysv, scan); \
2540  PerlIO_printf(Perl_debug_log, "%*s" str ">%3d: %s (%d)\n", \
2541  (int)depth*2, "", REG_NODE_NUM(scan), SvPV_nolen_const(mysv),\
2542  Next ? (REG_NODE_NUM(Next)) : 0 ); \
2543    }});
2544
2545
2546 /* The below joins as many adjacent EXACTish nodes as possible into a single
2547  * one, and looks for problematic sequences of characters whose folds vs.
2548  * non-folds have sufficiently different lengths, that the optimizer would be
2549  * fooled into rejecting legitimate matches of them, and the trie construction
2550  * code can't cope with them.  The joining is only done if:
2551  * 1) there is room in the current conglomerated node to entirely contain the
2552  *    next one.
2553  * 2) they are the exact same node type
2554  *
2555  * The adjacent nodes actually may be separated by NOTHING kind nodes, and
2556  * these get optimized out
2557  *
2558  * If there are problematic code sequences, *min_subtract is set to the delta
2559  * that the minimum size of the node can be less than its actual size.  And,
2560  * the node type of the result is changed to reflect that it contains these
2561  * sequences.
2562  *
2563  * And *has_exactf_sharp_s is set to indicate whether or not the node is EXACTF
2564  * and contains LATIN SMALL LETTER SHARP S
2565  *
2566  * This is as good a place as any to discuss the design of handling these
2567  * problematic sequences.  It's been wrong in Perl for a very long time.  There
2568  * are three code points in Unicode whose folded lengths differ so much from
2569  * the un-folded lengths that it causes problems for the optimizer and trie
2570  * construction.  Why only these are problematic, and not others where lengths
2571  * also differ is something I (khw) do not understand.  New versions of Unicode
2572  * might add more such code points.  Hopefully the logic in fold_grind.t that
2573  * figures out what to test (in part by verifying that each size-combination
2574  * gets tested) will catch any that do come along, so they can be added to the
2575  * special handling below.  The chances of new ones are actually rather small,
2576  * as most, if not all, of the world's scripts that have casefolding have
2577  * already been encoded by Unicode.  Also, a number of Unicode's decisions were
2578  * made to allow compatibility with pre-existing standards, and almost all of
2579  * those have already been dealt with.  These would otherwise be the most
2580  * likely candidates for generating further tricky sequences.  In other words,
2581  * Unicode by itself is unlikely to add new ones unless it is for compatibility
2582  * with pre-existing standards, and there aren't many of those left.
2583  *
2584  * The previous designs for dealing with these involved assigning a special
2585  * node for them.  This approach doesn't work, as evidenced by this example:
2586  *      "\xDFs" =~ /s\xDF/ui    # Used to fail before these patches
2587  * Both these fold to "sss", but if the pattern is parsed to create a node of
2588  * that would match just the \xDF, it won't be able to handle the case where a
2589  * successful match would have to cross the node's boundary.  The new approach
2590  * that hopefully generally solves the problem generates an EXACTFU_SS node
2591  * that is "sss".
2592  *
2593  * There are a number of components to the approach (a lot of work for just
2594  * three code points!):
2595  * 1)   This routine examines each EXACTFish node that could contain the
2596  *      problematic sequences.  It returns in *min_subtract how much to
2597  *      subtract from the the actual length of the string to get a real minimum
2598  *      for one that could match it.  This number is usually 0 except for the
2599  *      problematic sequences.  This delta is used by the caller to adjust the
2600  *      min length of the match, and the delta between min and max, so that the
2601  *      optimizer doesn't reject these possibilities based on size constraints.
2602  * 2)   These sequences are not currently correctly handled by the trie code
2603  *      either, so it changes the joined node type to ops that are not handled
2604  *      by trie's, those new ops being EXACTFU_SS and EXACTFU_TRICKYFOLD.
2605  * 3)   This is sufficient for the two Greek sequences (described below), but
2606  *      the one involving the Sharp s (\xDF) needs more.  The node type
2607  *      EXACTFU_SS is used for an EXACTFU node that contains at least one "ss"
2608  *      sequence in it.  For non-UTF-8 patterns and strings, this is the only
2609  *      case where there is a possible fold length change.  That means that a
2610  *      regular EXACTFU node without UTF-8 involvement doesn't have to concern
2611  *      itself with length changes, and so can be processed faster.  regexec.c
2612  *      takes advantage of this.  Generally, an EXACTFish node that is in UTF-8
2613  *      is pre-folded by regcomp.c.  This saves effort in regex matching.
2614  *      However, probably mostly for historical reasons, the pre-folding isn't
2615  *      done for non-UTF8 patterns (and it can't be for EXACTF and EXACTFL
2616  *      nodes, as what they fold to isn't known until runtime.)  The fold
2617  *      possibilities for the non-UTF8 patterns are quite simple, except for
2618  *      the sharp s.  All the ones that don't involve a UTF-8 target string
2619  *      are members of a fold-pair, and arrays are set up for all of them
2620  *      that quickly find the other member of the pair.  It might actually
2621  *      be faster to pre-fold these, but it isn't currently done, except for
2622  *      the sharp s.  Code elsewhere in this file makes sure that it gets
2623  *      folded to 'ss', even if the pattern isn't UTF-8.  This avoids the
2624  *      issues described in the next item.
2625  * 4)   A problem remains for the sharp s in EXACTF nodes.  Whether it matches
2626  *      'ss' or not is not knowable at compile time.  It will match iff the
2627  *      target string is in UTF-8, unlike the EXACTFU nodes, where it always
2628  *      matches; and the EXACTFL and EXACTFA nodes where it never does.  Thus
2629  *      it can't be folded to "ss" at compile time, unlike EXACTFU does as
2630  *      described in item 3).  An assumption that the optimizer part of
2631  *      regexec.c (probably unwittingly) makes is that a character in the
2632  *      pattern corresponds to at most a single character in the target string.
2633  *      (And I do mean character, and not byte here, unlike other parts of the
2634  *      documentation that have never been updated to account for multibyte
2635  *      Unicode.)  This assumption is wrong only in this case, as all other
2636  *      cases are either 1-1 folds when no UTF-8 is involved; or is true by
2637  *      virtue of having this file pre-fold UTF-8 patterns.   I'm
2638  *      reluctant to try to change this assumption, so instead the code punts.
2639  *      This routine examines EXACTF nodes for the sharp s, and returns a
2640  *      boolean indicating whether or not the node is an EXACTF node that
2641  *      contains a sharp s.  When it is true, the caller sets a flag that later
2642  *      causes the optimizer in this file to not set values for the floating
2643  *      and fixed string lengths, and thus avoids the optimizer code in
2644  *      regexec.c that makes the invalid assumption.  Thus, there is no
2645  *      optimization based on string lengths for EXACTF nodes that contain the
2646  *      sharp s.  This only happens for /id rules (which means the pattern
2647  *      isn't in UTF-8).
2648  */
2649
2650 #define JOIN_EXACT(scan,min_subtract,has_exactf_sharp_s, flags) \
2651  if (PL_regkind[OP(scan)] == EXACT) \
2652   join_exact(pRExC_state,(scan),(min_subtract),has_exactf_sharp_s, (flags),NULL,depth+1)
2653
2654 STATIC U32
2655 S_join_exact(pTHX_ RExC_state_t *pRExC_state, regnode *scan, UV *min_subtract, bool *has_exactf_sharp_s, U32 flags,regnode *val, U32 depth) {
2656  /* Merge several consecutive EXACTish nodes into one. */
2657  regnode *n = regnext(scan);
2658  U32 stringok = 1;
2659  regnode *next = scan + NODE_SZ_STR(scan);
2660  U32 merged = 0;
2661  U32 stopnow = 0;
2662 #ifdef DEBUGGING
2663  regnode *stop = scan;
2664  GET_RE_DEBUG_FLAGS_DECL;
2665 #else
2666  PERL_UNUSED_ARG(depth);
2667 #endif
2668
2669  PERL_ARGS_ASSERT_JOIN_EXACT;
2670 #ifndef EXPERIMENTAL_INPLACESCAN
2671  PERL_UNUSED_ARG(flags);
2672  PERL_UNUSED_ARG(val);
2673 #endif
2674  DEBUG_PEEP("join",scan,depth);
2675
2676  /* Look through the subsequent nodes in the chain.  Skip NOTHING, merge
2677  * EXACT ones that are mergeable to the current one. */
2678  while (n
2679   && (PL_regkind[OP(n)] == NOTHING
2680    || (stringok && OP(n) == OP(scan)))
2681   && NEXT_OFF(n)
2682   && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX)
2683  {
2684
2685   if (OP(n) == TAIL || n > next)
2686    stringok = 0;
2687   if (PL_regkind[OP(n)] == NOTHING) {
2688    DEBUG_PEEP("skip:",n,depth);
2689    NEXT_OFF(scan) += NEXT_OFF(n);
2690    next = n + NODE_STEP_REGNODE;
2691 #ifdef DEBUGGING
2692    if (stringok)
2693     stop = n;
2694 #endif
2695    n = regnext(n);
2696   }
2697   else if (stringok) {
2698    const unsigned int oldl = STR_LEN(scan);
2699    regnode * const nnext = regnext(n);
2700
2701    if (oldl + STR_LEN(n) > U8_MAX)
2702     break;
2703
2704    DEBUG_PEEP("merg",n,depth);
2705    merged++;
2706
2707    NEXT_OFF(scan) += NEXT_OFF(n);
2708    STR_LEN(scan) += STR_LEN(n);
2709    next = n + NODE_SZ_STR(n);
2710    /* Now we can overwrite *n : */
2711    Move(STRING(n), STRING(scan) + oldl, STR_LEN(n), char);
2712 #ifdef DEBUGGING
2713    stop = next - 1;
2714 #endif
2715    n = nnext;
2716    if (stopnow) break;
2717   }
2718
2719 #ifdef EXPERIMENTAL_INPLACESCAN
2720   if (flags && !NEXT_OFF(n)) {
2721    DEBUG_PEEP("atch", val, depth);
2722    if (reg_off_by_arg[OP(n)]) {
2723     ARG_SET(n, val - n);
2724    }
2725    else {
2726     NEXT_OFF(n) = val - n;
2727    }
2728    stopnow = 1;
2729   }
2730 #endif
2731  }
2732
2733  *min_subtract = 0;
2734  *has_exactf_sharp_s = FALSE;
2735
2736  /* Here, all the adjacent mergeable EXACTish nodes have been merged.  We
2737  * can now analyze for sequences of problematic code points.  (Prior to
2738  * this final joining, sequences could have been split over boundaries, and
2739  * hence missed).  The sequences only happen in folding, hence for any
2740  * non-EXACT EXACTish node */
2741  if (OP(scan) != EXACT) {
2742   U8 *s;
2743   U8 * s0 = (U8*) STRING(scan);
2744   U8 * const s_end = s0 + STR_LEN(scan);
2745
2746   /* The below is perhaps overboard, but this allows us to save a test
2747   * each time through the loop at the expense of a mask.  This is
2748   * because on both EBCDIC and ASCII machines, 'S' and 's' differ by a
2749   * single bit.  On ASCII they are 32 apart; on EBCDIC, they are 64.
2750   * This uses an exclusive 'or' to find that bit and then inverts it to
2751   * form a mask, with just a single 0, in the bit position where 'S' and
2752   * 's' differ. */
2753   const U8 S_or_s_mask = (U8) ~ ('S' ^ 's');
2754   const U8 s_masked = 's' & S_or_s_mask;
2755
2756   /* One pass is made over the node's string looking for all the
2757   * possibilities.  to avoid some tests in the loop, there are two main
2758   * cases, for UTF-8 patterns (which can't have EXACTF nodes) and
2759   * non-UTF-8 */
2760   if (UTF) {
2761
2762    /* There are two problematic Greek code points in Unicode
2763    * casefolding
2764    *
2765    * U+0390 - GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS
2766    * U+03B0 - GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS
2767    *
2768    * which casefold to
2769    *
2770    * Unicode                      UTF-8
2771    *
2772    * U+03B9 U+0308 U+0301         0xCE 0xB9 0xCC 0x88 0xCC 0x81
2773    * U+03C5 U+0308 U+0301         0xCF 0x85 0xCC 0x88 0xCC 0x81
2774    *
2775    * This means that in case-insensitive matching (or "loose
2776    * matching", as Unicode calls it), an EXACTF of length six (the
2777    * UTF-8 encoded byte length of the above casefolded versions) can
2778    * match a target string of length two (the byte length of UTF-8
2779    * encoded U+0390 or U+03B0).  This would rather mess up the
2780    * minimum length computation.  (there are other code points that
2781    * also fold to these two sequences, but the delta is smaller)
2782    *
2783    * If these sequences are found, the minimum length is decreased by
2784    * four (six minus two).
2785    *
2786    * Similarly, 'ss' may match the single char and byte LATIN SMALL
2787    * LETTER SHARP S.  We decrease the min length by 1 for each
2788    * occurrence of 'ss' found */
2789
2790 #ifdef EBCDIC /* RD tunifold greek 0390 and 03B0 */
2791 #     define U390_first_byte 0xb4
2792    const U8 U390_tail[] = "\x68\xaf\x49\xaf\x42";
2793 #     define U3B0_first_byte 0xb5
2794    const U8 U3B0_tail[] = "\x46\xaf\x49\xaf\x42";
2795 #else
2796 #     define U390_first_byte 0xce
2797    const U8 U390_tail[] = "\xb9\xcc\x88\xcc\x81";
2798 #     define U3B0_first_byte 0xcf
2799    const U8 U3B0_tail[] = "\x85\xcc\x88\xcc\x81";
2800 #endif
2801    const U8 len = sizeof(U390_tail); /* (-1 for NUL; +1 for 1st byte;
2802             yields a net of 0 */
2803    /* Examine the string for one of the problematic sequences */
2804    for (s = s0;
2805     s < s_end - 1; /* Can stop 1 before the end, as minimum length
2806         * sequence we are looking for is 2 */
2807     s += UTF8SKIP(s))
2808    {
2809
2810     /* Look for the first byte in each problematic sequence */
2811     switch (*s) {
2812      /* We don't have to worry about other things that fold to
2813      * 's' (such as the long s, U+017F), as all above-latin1
2814      * code points have been pre-folded */
2815      case 's':
2816      case 'S':
2817
2818       /* Current character is an 's' or 'S'.  If next one is
2819       * as well, we have the dreaded sequence */
2820       if (((*(s+1) & S_or_s_mask) == s_masked)
2821        /* These two node types don't have special handling
2822        * for 'ss' */
2823        && OP(scan) != EXACTFL && OP(scan) != EXACTFA)
2824       {
2825        *min_subtract += 1;
2826        OP(scan) = EXACTFU_SS;
2827        s++;    /* No need to look at this character again */
2828       }
2829       break;
2830
2831      case U390_first_byte:
2832       if (s_end - s >= len
2833
2834        /* The 1's are because are skipping comparing the
2835        * first byte */
2836        && memEQ(s + 1, U390_tail, len - 1))
2837       {
2838        goto greek_sequence;
2839       }
2840       break;
2841
2842      case U3B0_first_byte:
2843       if (! (s_end - s >= len
2844        && memEQ(s + 1, U3B0_tail, len - 1)))
2845       {
2846        break;
2847       }
2848      greek_sequence:
2849       *min_subtract += 4;
2850
2851       /* This can't currently be handled by trie's, so change
2852       * the node type to indicate this.  If EXACTFA and
2853       * EXACTFL were ever to be handled by trie's, this
2854       * would have to be changed.  If this node has already
2855       * been changed to EXACTFU_SS in this loop, leave it as
2856       * is.  (I (khw) think it doesn't matter in regexec.c
2857       * for UTF patterns, but no need to change it */
2858       if (OP(scan) == EXACTFU) {
2859        OP(scan) = EXACTFU_TRICKYFOLD;
2860       }
2861       s += 6; /* We already know what this sequence is.  Skip
2862         the rest of it */
2863       break;
2864     }
2865    }
2866   }
2867   else if (OP(scan) != EXACTFL && OP(scan) != EXACTFA) {
2868
2869    /* Here, the pattern is not UTF-8.  We need to look only for the
2870    * 'ss' sequence, and in the EXACTF case, the sharp s, which can be
2871    * in the final position.  Otherwise we can stop looking 1 byte
2872    * earlier because have to find both the first and second 's' */
2873    const U8* upper = (OP(scan) == EXACTF) ? s_end : s_end -1;
2874
2875    for (s = s0; s < upper; s++) {
2876     switch (*s) {
2877      case 'S':
2878      case 's':
2879       if (s_end - s > 1
2880        && ((*(s+1) & S_or_s_mask) == s_masked))
2881       {
2882        *min_subtract += 1;
2883
2884        /* EXACTF nodes need to know that the minimum
2885        * length changed so that a sharp s in the string
2886        * can match this ss in the pattern, but they
2887        * remain EXACTF nodes, as they are not trie'able,
2888        * so don't have to invent a new node type to
2889        * exclude them from the trie code */
2890        if (OP(scan) != EXACTF) {
2891         OP(scan) = EXACTFU_SS;
2892        }
2893        s++;
2894       }
2895       break;
2896      case LATIN_SMALL_LETTER_SHARP_S:
2897       if (OP(scan) == EXACTF) {
2898        *has_exactf_sharp_s = TRUE;
2899       }
2900       break;
2901     }
2902    }
2903   }
2904  }
2905
2906 #ifdef DEBUGGING
2907  /* Allow dumping but overwriting the collection of skipped
2908  * ops and/or strings with fake optimized ops */
2909  n = scan + NODE_SZ_STR(scan);
2910  while (n <= stop) {
2911   OP(n) = OPTIMIZED;
2912   FLAGS(n) = 0;
2913   NEXT_OFF(n) = 0;
2914   n++;
2915  }
2916 #endif
2917  DEBUG_OPTIMISE_r(if (merged){DEBUG_PEEP("finl",scan,depth)});
2918  return stopnow;
2919 }
2920
2921 /* REx optimizer.  Converts nodes into quicker variants "in place".
2922    Finds fixed substrings.  */
2923
2924 /* Stops at toplevel WHILEM as well as at "last". At end *scanp is set
2925    to the position after last scanned or to NULL. */
2926
2927 #define INIT_AND_WITHP \
2928  assert(!and_withp); \
2929  Newx(and_withp,1,struct regnode_charclass_class); \
2930  SAVEFREEPV(and_withp)
2931
2932 /* this is a chain of data about sub patterns we are processing that
2933    need to be handled separately/specially in study_chunk. Its so
2934    we can simulate recursion without losing state.  */
2935 struct scan_frame;
2936 typedef struct scan_frame {
2937  regnode *last;  /* last node to process in this frame */
2938  regnode *next;  /* next node to process when last is reached */
2939  struct scan_frame *prev; /*previous frame*/
2940  I32 stop; /* what stopparen do we use */
2941 } scan_frame;
2942
2943
2944 #define SCAN_COMMIT(s, data, m) scan_commit(s, data, m, is_inf)
2945
2946 #define CASE_SYNST_FNC(nAmE)                                       \
2947 case nAmE:                                                         \
2948  if (flags & SCF_DO_STCLASS_AND) {                              \
2949    for (value = 0; value < 256; value++)                  \
2950     if (!is_ ## nAmE ## _cp(value))                       \
2951      ANYOF_BITMAP_CLEAR(data->start_class, value);  \
2952  }                                                              \
2953  else {                                                         \
2954    for (value = 0; value < 256; value++)                  \
2955     if (is_ ## nAmE ## _cp(value))                        \
2956      ANYOF_BITMAP_SET(data->start_class, value);    \
2957  }                                                              \
2958  break;                                                         \
2959 case N ## nAmE:                                                    \
2960  if (flags & SCF_DO_STCLASS_AND) {                              \
2961    for (value = 0; value < 256; value++)                   \
2962     if (is_ ## nAmE ## _cp(value))                         \
2963      ANYOF_BITMAP_CLEAR(data->start_class, value);   \
2964  }                                                               \
2965  else {                                                          \
2966    for (value = 0; value < 256; value++)                   \
2967     if (!is_ ## nAmE ## _cp(value))                        \
2968      ANYOF_BITMAP_SET(data->start_class, value);     \
2969  }                                                               \
2970  break
2971
2972
2973
2974 STATIC I32
2975 S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp,
2976       I32 *minlenp, I32 *deltap,
2977       regnode *last,
2978       scan_data_t *data,
2979       I32 stopparen,
2980       U8* recursed,
2981       struct regnode_charclass_class *and_withp,
2982       U32 flags, U32 depth)
2983       /* scanp: Start here (read-write). */
2984       /* deltap: Write maxlen-minlen here. */
2985       /* last: Stop before this one. */
2986       /* data: string data about the pattern */
2987       /* stopparen: treat close N as END */
2988       /* recursed: which subroutines have we recursed into */
2989       /* and_withp: Valid if flags & SCF_DO_STCLASS_OR */
2990 {
2991  dVAR;
2992  I32 min = 0, pars = 0, code;
2993  regnode *scan = *scanp, *next;
2994  I32 delta = 0;
2995  int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
2996  int is_inf_internal = 0;  /* The studied chunk is infinite */
2997  I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
2998  scan_data_t data_fake;
2999  SV *re_trie_maxbuff = NULL;
3000  regnode *first_non_open = scan;
3001  I32 stopmin = I32_MAX;
3002  scan_frame *frame = NULL;
3003  GET_RE_DEBUG_FLAGS_DECL;
3004
3005  PERL_ARGS_ASSERT_STUDY_CHUNK;
3006
3007 #ifdef DEBUGGING
3008  StructCopy(&zero_scan_data, &data_fake, scan_data_t);
3009 #endif
3010
3011  if ( depth == 0 ) {
3012   while (first_non_open && OP(first_non_open) == OPEN)
3013    first_non_open=regnext(first_non_open);
3014  }
3015
3016
3017   fake_study_recurse:
3018  while ( scan && OP(scan) != END && scan < last ){
3019   UV min_subtract = 0;    /* How much to subtract from the minimum node
3020         length to get a real minimum (because the
3021         folded version may be shorter) */
3022   bool has_exactf_sharp_s = FALSE;
3023   /* Peephole optimizer: */
3024   DEBUG_STUDYDATA("Peep:", data,depth);
3025   DEBUG_PEEP("Peep",scan,depth);
3026
3027   /* Its not clear to khw or hv why this is done here, and not in the
3028   * clauses that deal with EXACT nodes.  khw's guess is that it's
3029   * because of a previous design */
3030   JOIN_EXACT(scan,&min_subtract, &has_exactf_sharp_s, 0);
3031
3032   /* Follow the next-chain of the current node and optimize
3033   away all the NOTHINGs from it.  */
3034   if (OP(scan) != CURLYX) {
3035    const int max = (reg_off_by_arg[OP(scan)]
3036      ? I32_MAX
3037      /* I32 may be smaller than U16 on CRAYs! */
3038      : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
3039    int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
3040    int noff;
3041    regnode *n = scan;
3042
3043    /* Skip NOTHING and LONGJMP. */
3044    while ((n = regnext(n))
3045     && ((PL_regkind[OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
3046      || ((OP(n) == LONGJMP) && (noff = ARG(n))))
3047     && off + noff < max)
3048     off += noff;
3049    if (reg_off_by_arg[OP(scan)])
3050     ARG(scan) = off;
3051    else
3052     NEXT_OFF(scan) = off;
3053   }
3054
3055
3056
3057   /* The principal pseudo-switch.  Cannot be a switch, since we
3058   look into several different things.  */
3059   if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
3060     || OP(scan) == IFTHEN) {
3061    next = regnext(scan);
3062    code = OP(scan);
3063    /* demq: the op(next)==code check is to see if we have "branch-branch" AFAICT */
3064
3065    if (OP(next) == code || code == IFTHEN) {
3066     /* NOTE - There is similar code to this block below for handling
3067     TRIE nodes on a re-study.  If you change stuff here check there
3068     too. */
3069     I32 max1 = 0, min1 = I32_MAX, num = 0;
3070     struct regnode_charclass_class accum;
3071     regnode * const startbranch=scan;
3072
3073     if (flags & SCF_DO_SUBSTR)
3074      SCAN_COMMIT(pRExC_state, data, minlenp); /* Cannot merge strings after this. */
3075     if (flags & SCF_DO_STCLASS)
3076      cl_init_zero(pRExC_state, &accum);
3077
3078     while (OP(scan) == code) {
3079      I32 deltanext, minnext, f = 0, fake;
3080      struct regnode_charclass_class this_class;
3081
3082      num++;
3083      data_fake.flags = 0;
3084      if (data) {
3085       data_fake.whilem_c = data->whilem_c;
3086       data_fake.last_closep = data->last_closep;
3087      }
3088      else
3089       data_fake.last_closep = &fake;
3090
3091      data_fake.pos_delta = delta;
3092      next = regnext(scan);
3093      scan = NEXTOPER(scan);
3094      if (code != BRANCH)
3095       scan = NEXTOPER(scan);
3096      if (flags & SCF_DO_STCLASS) {
3097       cl_init(pRExC_state, &this_class);
3098       data_fake.start_class = &this_class;
3099       f = SCF_DO_STCLASS_AND;
3100      }
3101      if (flags & SCF_WHILEM_VISITED_POS)
3102       f |= SCF_WHILEM_VISITED_POS;
3103
3104      /* we suppose the run is continuous, last=next...*/
3105      minnext = study_chunk(pRExC_state, &scan, minlenp, &deltanext,
3106           next, &data_fake,
3107           stopparen, recursed, NULL, f,depth+1);
3108      if (min1 > minnext)
3109       min1 = minnext;
3110      if (max1 < minnext + deltanext)
3111       max1 = minnext + deltanext;
3112      if (deltanext == I32_MAX)
3113       is_inf = is_inf_internal = 1;
3114      scan = next;
3115      if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
3116       pars++;
3117      if (data_fake.flags & SCF_SEEN_ACCEPT) {
3118       if ( stopmin > minnext)
3119        stopmin = min + min1;
3120       flags &= ~SCF_DO_SUBSTR;
3121       if (data)
3122        data->flags |= SCF_SEEN_ACCEPT;
3123      }
3124      if (data) {
3125       if (data_fake.flags & SF_HAS_EVAL)
3126        data->flags |= SF_HAS_EVAL;
3127       data->whilem_c = data_fake.whilem_c;
3128      }
3129      if (flags & SCF_DO_STCLASS)
3130       cl_or(pRExC_state, &accum, &this_class);
3131     }
3132     if (code == IFTHEN && num < 2) /* Empty ELSE branch */
3133      min1 = 0;
3134     if (flags & SCF_DO_SUBSTR) {
3135      data->pos_min += min1;
3136      data->pos_delta += max1 - min1;
3137      if (max1 != min1 || is_inf)
3138       data->longest = &(data->longest_float);
3139     }
3140     min += min1;
3141     delta += max1 - min1;
3142     if (flags & SCF_DO_STCLASS_OR) {
3143      cl_or(pRExC_state, data->start_class, &accum);
3144      if (min1) {
3145       cl_and(data->start_class, and_withp);
3146       flags &= ~SCF_DO_STCLASS;
3147      }
3148     }
3149     else if (flags & SCF_DO_STCLASS_AND) {
3150      if (min1) {
3151       cl_and(data->start_class, &accum);
3152       flags &= ~SCF_DO_STCLASS;
3153      }
3154      else {
3155       /* Switch to OR mode: cache the old value of
3156       * data->start_class */
3157       INIT_AND_WITHP;
3158       StructCopy(data->start_class, and_withp,
3159         struct regnode_charclass_class);
3160       flags &= ~SCF_DO_STCLASS_AND;
3161       StructCopy(&accum, data->start_class,
3162         struct regnode_charclass_class);
3163       flags |= SCF_DO_STCLASS_OR;
3164       data->start_class->flags |= ANYOF_EOS;
3165      }
3166     }
3167
3168     if (PERL_ENABLE_TRIE_OPTIMISATION && OP( startbranch ) == BRANCH ) {
3169     /* demq.
3170
3171     Assuming this was/is a branch we are dealing with: 'scan' now
3172     points at the item that follows the branch sequence, whatever
3173     it is. We now start at the beginning of the sequence and look
3174     for subsequences of
3175
3176     BRANCH->EXACT=>x1
3177     BRANCH->EXACT=>x2
3178     tail
3179
3180     which would be constructed from a pattern like /A|LIST|OF|WORDS/
3181
3182     If we can find such a subsequence we need to turn the first
3183     element into a trie and then add the subsequent branch exact
3184     strings to the trie.
3185
3186     We have two cases
3187
3188      1. patterns where the whole set of branches can be converted.
3189
3190      2. patterns where only a subset can be converted.
3191
3192     In case 1 we can replace the whole set with a single regop
3193     for the trie. In case 2 we need to keep the start and end
3194     branches so
3195
3196      'BRANCH EXACT; BRANCH EXACT; BRANCH X'
3197      becomes BRANCH TRIE; BRANCH X;
3198
3199     There is an additional case, that being where there is a
3200     common prefix, which gets split out into an EXACT like node
3201     preceding the TRIE node.
3202
3203     If x(1..n)==tail then we can do a simple trie, if not we make
3204     a "jump" trie, such that when we match the appropriate word
3205     we "jump" to the appropriate tail node. Essentially we turn
3206     a nested if into a case structure of sorts.
3207
3208     */
3209
3210      int made=0;
3211      if (!re_trie_maxbuff) {
3212       re_trie_maxbuff = get_sv(RE_TRIE_MAXBUF_NAME, 1);
3213       if (!SvIOK(re_trie_maxbuff))
3214        sv_setiv(re_trie_maxbuff, RE_TRIE_MAXBUF_INIT);
3215      }
3216      if ( SvIV(re_trie_maxbuff)>=0  ) {
3217       regnode *cur;
3218       regnode *first = (regnode *)NULL;
3219       regnode *last = (regnode *)NULL;
3220       regnode *tail = scan;
3221       U8 trietype = 0;
3222       U32 count=0;
3223
3224 #ifdef DEBUGGING
3225       SV * const mysv = sv_newmortal();       /* for dumping */
3226 #endif
3227       /* var tail is used because there may be a TAIL
3228       regop in the way. Ie, the exacts will point to the
3229       thing following the TAIL, but the last branch will
3230       point at the TAIL. So we advance tail. If we
3231       have nested (?:) we may have to move through several
3232       tails.
3233       */
3234
3235       while ( OP( tail ) == TAIL ) {
3236        /* this is the TAIL generated by (?:) */
3237        tail = regnext( tail );
3238       }
3239
3240
3241       DEBUG_OPTIMISE_r({
3242        regprop(RExC_rx, mysv, tail );
3243        PerlIO_printf( Perl_debug_log, "%*s%s%s\n",
3244         (int)depth * 2 + 2, "",
3245         "Looking for TRIE'able sequences. Tail node is: ",
3246         SvPV_nolen_const( mysv )
3247        );
3248       });
3249
3250       /*
3251
3252        Step through the branches
3253         cur represents each branch,
3254         noper is the first thing to be matched as part of that branch
3255         noper_next is the regnext() of that node.
3256
3257        We normally handle a case like this /FOO[xyz]|BAR[pqr]/
3258        via a "jump trie" but we also support building with NOJUMPTRIE,
3259        which restricts the trie logic to structures like /FOO|BAR/.
3260
3261        If noper is a trieable nodetype then the branch is a possible optimization
3262        target. If we are building under NOJUMPTRIE then we require that noper_next
3263        is the same as scan (our current position in the regex program).
3264
3265        Once we have two or more consecutive such branches we can create a
3266        trie of the EXACT's contents and stitch it in place into the program.
3267
3268        If the sequence represents all of the branches in the alternation we
3269        replace the entire thing with a single TRIE node.
3270
3271        Otherwise when it is a subsequence we need to stitch it in place and
3272        replace only the relevant branches. This means the first branch has
3273        to remain as it is used by the alternation logic, and its next pointer,
3274        and needs to be repointed at the item on the branch chain following
3275        the last branch we have optimized away.
3276
3277        This could be either a BRANCH, in which case the subsequence is internal,
3278        or it could be the item following the branch sequence in which case the
3279        subsequence is at the end (which does not necessarily mean the first node
3280        is the start of the alternation).
3281
3282        TRIE_TYPE(X) is a define which maps the optype to a trietype.
3283
3284         optype          |  trietype
3285         ----------------+-----------
3286         NOTHING         | NOTHING
3287         EXACT           | EXACT
3288         EXACTFU         | EXACTFU
3289         EXACTFU_SS      | EXACTFU
3290         EXACTFU_TRICKYFOLD | EXACTFU
3291         EXACTFA         | 0
3292
3293
3294       */
3295 #define TRIE_TYPE(X) ( ( NOTHING == (X) ) ? NOTHING :   \
3296      ( EXACT == (X) )   ? EXACT :        \
3297      ( EXACTFU == (X) || EXACTFU_SS == (X) || EXACTFU_TRICKYFOLD == (X) ) ? EXACTFU :        \
3298      0 )
3299
3300       /* dont use tail as the end marker for this traverse */
3301       for ( cur = startbranch ; cur != scan ; cur = regnext( cur ) ) {
3302        regnode * const noper = NEXTOPER( cur );
3303        U8 noper_type = OP( noper );
3304        U8 noper_trietype = TRIE_TYPE( noper_type );
3305 #if defined(DEBUGGING) || defined(NOJUMPTRIE)
3306        regnode * const noper_next = regnext( noper );
3307 #endif
3308
3309        DEBUG_OPTIMISE_r({
3310         regprop(RExC_rx, mysv, cur);
3311         PerlIO_printf( Perl_debug_log, "%*s- %s (%d)",
3312         (int)depth * 2 + 2,"", SvPV_nolen_const( mysv ), REG_NODE_NUM(cur) );
3313
3314         regprop(RExC_rx, mysv, noper);
3315         PerlIO_printf( Perl_debug_log, " -> %s",
3316          SvPV_nolen_const(mysv));
3317
3318         if ( noper_next ) {
3319         regprop(RExC_rx, mysv, noper_next );
3320         PerlIO_printf( Perl_debug_log,"\t=> %s\t",
3321          SvPV_nolen_const(mysv));
3322         }
3323         PerlIO_printf( Perl_debug_log, "(First==%d,Last==%d,Cur==%d)\n",
3324         REG_NODE_NUM(first), REG_NODE_NUM(last), REG_NODE_NUM(cur) );
3325        });
3326
3327        /* Is noper a trieable nodetype that can be merged with the
3328        * current trie (if there is one)? */
3329        if ( noper_trietype
3330         &&
3331         (
3332           /* XXX: Currently we cannot allow a NOTHING node to be the first element
3333           * of a TRIEABLE sequence, Otherwise we will overwrite the regop following
3334           * the NOTHING with the TRIE regop later on. This is because a NOTHING node
3335           * is only one regnode wide, and a TRIE is two regnodes. An example of a
3336           * problematic pattern is: "x" =~ /\A(?>(?:(?:)A|B|C?x))\z/
3337           * At a later point of time we can somewhat workaround this by handling
3338           * NOTHING -> EXACT sequences as generated by /(?:)A|(?:)B/ type patterns,
3339           * as we can effectively ignore the NOTHING regop in that case.
3340           * This clause, which allows NOTHING to start a sequence is left commented
3341           * out as a reference.
3342           * - Yves
3343
3344           ( noper_trietype == NOTHING)
3345           || ( trietype == NOTHING )
3346           */
3347           ( noper_trietype == NOTHING && trietype )
3348           || ( trietype == noper_trietype )
3349         )
3350 #ifdef NOJUMPTRIE
3351         && noper_next == tail
3352 #endif
3353         && count < U16_MAX)
3354        {
3355         /* Handle mergable triable node
3356         * Either we are the first node in a new trieable sequence,
3357         * in which case we do some bookkeeping, otherwise we update
3358         * the end pointer. */
3359         count++;
3360         if ( !first ) {
3361          first = cur;
3362          trietype = noper_trietype;
3363         } else {
3364          if ( trietype == NOTHING )
3365           trietype = noper_trietype;
3366          last = cur;
3367         }
3368        } /* end handle mergable triable node */
3369        else {
3370         /* handle unmergable node -
3371         * noper may either be a triable node which can not be tried
3372         * together with the current trie, or a non triable node */
3373         if ( last ) {
3374          /* If last is set and trietype is not NOTHING then we have found
3375          * at least two triable branch sequences in a row of a similar
3376          * trietype so we can turn them into a trie. If/when we
3377          * allow NOTHING to start a trie sequence this condition will be
3378          * required, and it isn't expensive so we leave it in for now. */
3379          if ( trietype != NOTHING )
3380           make_trie( pRExC_state,
3381             startbranch, first, cur, tail, count,
3382             trietype, depth+1 );
3383          last = NULL; /* note: we clear/update first, trietype etc below, so we dont do it here */
3384         }
3385         if ( noper_trietype
3386 #ifdef NOJUMPTRIE
3387          && noper_next == tail
3388 #endif
3389         ){
3390          /* noper is triable, so we can start a new trie sequence */
3391          count = 1;
3392          first = cur;
3393          trietype = noper_trietype;
3394         } else if (first) {
3395          /* if we already saw a first but the current node is not triable then we have
3396          * to reset the first information. */
3397          count = 0;
3398          first = NULL;
3399          trietype = 0;
3400         }
3401        } /* end handle unmergable node */
3402       } /* loop over branches */
3403       DEBUG_OPTIMISE_r({
3404        regprop(RExC_rx, mysv, cur);
3405        PerlIO_printf( Perl_debug_log,
3406        "%*s- %s (%d) <SCAN FINISHED>\n", (int)depth * 2 + 2,
3407        "", SvPV_nolen_const( mysv ),REG_NODE_NUM(cur));
3408
3409       });
3410       if ( last && trietype != NOTHING ) {
3411        /* the last branch of the sequence was part of a trie,
3412        * so we have to construct it here outside of the loop
3413        */
3414        made= make_trie( pRExC_state, startbranch, first, scan, tail, count, trietype, depth+1 );
3415 #ifdef TRIE_STUDY_OPT
3416        if ( ((made == MADE_EXACT_TRIE &&
3417         startbranch == first)
3418         || ( first_non_open == first )) &&
3419         depth==0 ) {
3420         flags |= SCF_TRIE_RESTUDY;
3421         if ( startbranch == first
3422          && scan == tail )
3423         {
3424          RExC_seen &=~REG_TOP_LEVEL_BRANCHES;
3425         }
3426        }
3427 #endif
3428       } /* end if ( last) */
3429      } /* TRIE_MAXBUF is non zero */
3430
3431     } /* do trie */
3432
3433    }
3434    else if ( code == BRANCHJ ) {  /* single branch is optimized. */
3435     scan = NEXTOPER(NEXTOPER(scan));
3436    } else   /* single branch is optimized. */
3437     scan = NEXTOPER(scan);
3438    continue;
3439   } else if (OP(scan) == SUSPEND || OP(scan) == GOSUB || OP(scan) == GOSTART) {
3440    scan_frame *newframe = NULL;
3441    I32 paren;
3442    regnode *start;
3443    regnode *end;
3444
3445    if (OP(scan) != SUSPEND) {
3446    /* set the pointer */
3447     if (OP(scan) == GOSUB) {
3448      paren = ARG(scan);
3449      RExC_recurse[ARG2L(scan)] = scan;
3450      start = RExC_open_parens[paren-1];
3451      end   = RExC_close_parens[paren-1];
3452     } else {
3453      paren = 0;
3454      start = RExC_rxi->program + 1;
3455      end   = RExC_opend;
3456     }
3457     if (!recursed) {
3458      Newxz(recursed, (((RExC_npar)>>3) +1), U8);
3459      SAVEFREEPV(recursed);
3460     }
3461     if (!PAREN_TEST(recursed,paren+1)) {
3462      PAREN_SET(recursed,paren+1);
3463      Newx(newframe,1,scan_frame);
3464     } else {
3465      if (flags & SCF_DO_SUBSTR) {
3466       SCAN_COMMIT(pRExC_state,data,minlenp);
3467       data->longest = &(data->longest_float);
3468      }
3469      is_inf = is_inf_internal = 1;
3470      if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
3471       cl_anything(pRExC_state, data->start_class);
3472      flags &= ~SCF_DO_STCLASS;
3473     }
3474    } else {
3475     Newx(newframe,1,scan_frame);
3476     paren = stopparen;
3477     start = scan+2;
3478     end = regnext(scan);
3479    }
3480    if (newframe) {
3481     assert(start);
3482     assert(end);
3483     SAVEFREEPV(newframe);
3484     newframe->next = regnext(scan);
3485     newframe->last = last;
3486     newframe->stop = stopparen;
3487     newframe->prev = frame;
3488
3489     frame = newframe;
3490     scan =  start;
3491     stopparen = paren;
3492     last = end;
3493
3494     continue;
3495    }
3496   }
3497   else if (OP(scan) == EXACT) {
3498    I32 l = STR_LEN(scan);
3499    UV uc;
3500    if (UTF) {
3501     const U8 * const s = (U8*)STRING(scan);
3502     uc = utf8_to_uvchr_buf(s, s + l, NULL);
3503     l = utf8_length(s, s + l);
3504    } else {
3505     uc = *((U8*)STRING(scan));
3506    }
3507    min += l;
3508    if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
3509     /* The code below prefers earlier match for fixed
3510     offset, later match for variable offset.  */
3511     if (data->last_end == -1) { /* Update the start info. */
3512      data->last_start_min = data->pos_min;
3513      data->last_start_max = is_inf
3514       ? I32_MAX : data->pos_min + data->pos_delta;
3515     }
3516     sv_catpvn(data->last_found, STRING(scan), STR_LEN(scan));
3517     if (UTF)
3518      SvUTF8_on(data->last_found);
3519     {
3520      SV * const sv = data->last_found;
3521      MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ?
3522       mg_find(sv, PERL_MAGIC_utf8) : NULL;
3523      if (mg && mg->mg_len >= 0)
3524       mg->mg_len += utf8_length((U8*)STRING(scan),
3525             (U8*)STRING(scan)+STR_LEN(scan));
3526     }
3527     data->last_end = data->pos_min + l;
3528     data->pos_min += l; /* As in the first entry. */
3529     data->flags &= ~SF_BEFORE_EOL;
3530    }
3531    if (flags & SCF_DO_STCLASS_AND) {
3532     /* Check whether it is compatible with what we know already! */
3533     int compat = 1;
3534
3535
3536     /* If compatible, we or it in below.  It is compatible if is
3537     * in the bitmp and either 1) its bit or its fold is set, or 2)
3538     * it's for a locale.  Even if there isn't unicode semantics
3539     * here, at runtime there may be because of matching against a
3540     * utf8 string, so accept a possible false positive for
3541     * latin1-range folds */
3542     if (uc >= 0x100 ||
3543      (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
3544      && !ANYOF_BITMAP_TEST(data->start_class, uc)
3545      && (!(data->start_class->flags & ANYOF_LOC_NONBITMAP_FOLD)
3546       || !ANYOF_BITMAP_TEST(data->start_class, PL_fold_latin1[uc])))
3547      )
3548     {
3549      compat = 0;
3550     }
3551     ANYOF_CLASS_ZERO(data->start_class);
3552     ANYOF_BITMAP_ZERO(data->start_class);
3553     if (compat)
3554      ANYOF_BITMAP_SET(data->start_class, uc);
3555     else if (uc >= 0x100) {
3556      int i;
3557
3558      /* Some Unicode code points fold to the Latin1 range; as
3559      * XXX temporary code, instead of figuring out if this is
3560      * one, just assume it is and set all the start class bits
3561      * that could be some such above 255 code point's fold
3562      * which will generate fals positives.  As the code
3563      * elsewhere that does compute the fold settles down, it
3564      * can be extracted out and re-used here */
3565      for (i = 0; i < 256; i++){
3566       if (_HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(i)) {
3567        ANYOF_BITMAP_SET(data->start_class, i);
3568       }
3569      }
3570     }
3571     data->start_class->flags &= ~ANYOF_EOS;
3572     if (uc < 0x100)
3573     data->start_class->flags &= ~ANYOF_UNICODE_ALL;
3574    }
3575    else if (flags & SCF_DO_STCLASS_OR) {
3576     /* false positive possible if the class is case-folded */
3577     if (uc < 0x100)
3578      ANYOF_BITMAP_SET(data->start_class, uc);
3579     else
3580      data->start_class->flags |= ANYOF_UNICODE_ALL;
3581     data->start_class->flags &= ~ANYOF_EOS;
3582     cl_and(data->start_class, and_withp);
3583    }
3584    flags &= ~SCF_DO_STCLASS;
3585   }
3586   else if (PL_regkind[OP(scan)] == EXACT) { /* But OP != EXACT! */
3587    I32 l = STR_LEN(scan);
3588    UV uc = *((U8*)STRING(scan));
3589
3590    /* Search for fixed substrings supports EXACT only. */
3591    if (flags & SCF_DO_SUBSTR) {
3592     assert(data);
3593     SCAN_COMMIT(pRExC_state, data, minlenp);
3594    }
3595    if (UTF) {
3596     const U8 * const s = (U8 *)STRING(scan);
3597     uc = utf8_to_uvchr_buf(s, s + l, NULL);
3598     l = utf8_length(s, s + l);
3599    }
3600    else if (has_exactf_sharp_s) {
3601     RExC_seen |= REG_SEEN_EXACTF_SHARP_S;
3602    }
3603    min += l - min_subtract;
3604    if (min < 0) {
3605     min = 0;
3606    }
3607    delta += min_subtract;
3608    if (flags & SCF_DO_SUBSTR) {
3609     data->pos_min += l - min_subtract;
3610     if (data->pos_min < 0) {
3611      data->pos_min = 0;
3612     }
3613     data->pos_delta += min_subtract;
3614     if (min_subtract) {
3615      data->longest = &(data->longest_float);
3616     }
3617    }
3618    if (flags & SCF_DO_STCLASS_AND) {
3619     /* Check whether it is compatible with what we know already! */
3620     int compat = 1;
3621     if (uc >= 0x100 ||
3622     (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
3623     && !ANYOF_BITMAP_TEST(data->start_class, uc)
3624     && !ANYOF_BITMAP_TEST(data->start_class, PL_fold_latin1[uc])))
3625     {
3626      compat = 0;
3627     }
3628     ANYOF_CLASS_ZERO(data->start_class);
3629     ANYOF_BITMAP_ZERO(data->start_class);
3630     if (compat) {
3631      ANYOF_BITMAP_SET(data->start_class, uc);
3632      data->start_class->flags &= ~ANYOF_EOS;
3633      data->start_class->flags |= ANYOF_LOC_NONBITMAP_FOLD;
3634      if (OP(scan) == EXACTFL) {
3635       /* XXX This set is probably no longer necessary, and
3636       * probably wrong as LOCALE now is on in the initial
3637       * state */
3638       data->start_class->flags |= ANYOF_LOCALE;
3639      }
3640      else {
3641
3642       /* Also set the other member of the fold pair.  In case
3643       * that unicode semantics is called for at runtime, use
3644       * the full latin1 fold.  (Can't do this for locale,
3645       * because not known until runtime) */
3646       ANYOF_BITMAP_SET(data->start_class, PL_fold_latin1[uc]);
3647
3648       /* All other (EXACTFL handled above) folds except under
3649       * /iaa that include s, S, and sharp_s also may include
3650       * the others */
3651       if (OP(scan) != EXACTFA) {
3652        if (uc == 's' || uc == 'S') {
3653         ANYOF_BITMAP_SET(data->start_class,
3654             LATIN_SMALL_LETTER_SHARP_S);
3655        }
3656        else if (uc == LATIN_SMALL_LETTER_SHARP_S) {
3657         ANYOF_BITMAP_SET(data->start_class, 's');
3658         ANYOF_BITMAP_SET(data->start_class, 'S');
3659        }
3660       }
3661      }
3662     }
3663     else if (uc >= 0x100) {
3664      int i;
3665      for (i = 0; i < 256; i++){
3666       if (_HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(i)) {
3667        ANYOF_BITMAP_SET(data->start_class, i);
3668       }
3669      }
3670     }
3671    }
3672    else if (flags & SCF_DO_STCLASS_OR) {
3673     if (data->start_class->flags & ANYOF_LOC_NONBITMAP_FOLD) {
3674      /* false positive possible if the class is case-folded.
3675      Assume that the locale settings are the same... */
3676      if (uc < 0x100) {
3677       ANYOF_BITMAP_SET(data->start_class, uc);
3678       if (OP(scan) != EXACTFL) {
3679
3680        /* And set the other member of the fold pair, but
3681        * can't do that in locale because not known until
3682        * run-time */
3683        ANYOF_BITMAP_SET(data->start_class,
3684            PL_fold_latin1[uc]);
3685
3686        /* All folds except under /iaa that include s, S,
3687        * and sharp_s also may include the others */
3688        if (OP(scan) != EXACTFA) {
3689         if (uc == 's' || uc == 'S') {
3690          ANYOF_BITMAP_SET(data->start_class,
3691             LATIN_SMALL_LETTER_SHARP_S);
3692         }
3693         else if (uc == LATIN_SMALL_LETTER_SHARP_S) {
3694          ANYOF_BITMAP_SET(data->start_class, 's');
3695          ANYOF_BITMAP_SET(data->start_class, 'S');
3696         }
3697        }
3698       }
3699      }
3700      data->start_class->flags &= ~ANYOF_EOS;
3701     }
3702     cl_and(data->start_class, and_withp);
3703    }
3704    flags &= ~SCF_DO_STCLASS;
3705   }
3706   else if (REGNODE_VARIES(OP(scan))) {
3707    I32 mincount, maxcount, minnext, deltanext, fl = 0;
3708    I32 f = flags, pos_before = 0;
3709    regnode * const oscan = scan;
3710    struct regnode_charclass_class this_class;
3711    struct regnode_charclass_class *oclass = NULL;
3712    I32 next_is_eval = 0;
3713
3714    switch (PL_regkind[OP(scan)]) {
3715    case WHILEM:  /* End of (?:...)* . */
3716     scan = NEXTOPER(scan);
3717     goto finish;
3718    case PLUS:
3719     if (flags & (SCF_DO_SUBSTR | SCF_DO_STCLASS)) {
3720      next = NEXTOPER(scan);
3721      if (OP(next) == EXACT || (flags & SCF_DO_STCLASS)) {
3722       mincount = 1;
3723       maxcount = REG_INFTY;
3724       next = regnext(scan);
3725       scan = NEXTOPER(scan);
3726       goto do_curly;
3727      }
3728     }
3729     if (flags & SCF_DO_SUBSTR)
3730      data->pos_min++;
3731     min++;
3732     /* Fall through. */
3733    case STAR:
3734     if (flags & SCF_DO_STCLASS) {
3735      mincount = 0;
3736      maxcount = REG_INFTY;
3737      next = regnext(scan);
3738      scan = NEXTOPER(scan);
3739      goto do_curly;
3740     }
3741     is_inf = is_inf_internal = 1;
3742     scan = regnext(scan);
3743     if (flags & SCF_DO_SUBSTR) {
3744      SCAN_COMMIT(pRExC_state, data, minlenp); /* Cannot extend fixed substrings */
3745      data->longest = &(data->longest_float);
3746     }
3747     goto optimize_curly_tail;
3748    case CURLY:
3749     if (stopparen>0 && (OP(scan)==CURLYN || OP(scan)==CURLYM)
3750      && (scan->flags == stopparen))
3751     {
3752      mincount = 1;
3753      maxcount = 1;
3754     } else {
3755      mincount = ARG1(scan);
3756      maxcount = ARG2(scan);
3757     }
3758     next = regnext(scan);
3759     if (OP(scan) == CURLYX) {
3760      I32 lp = (data ? *(data->last_closep) : 0);
3761      scan->flags = ((lp <= (I32)U8_MAX) ? (U8)lp : U8_MAX);
3762     }
3763     scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3764     next_is_eval = (OP(scan) == EVAL);
3765    do_curly:
3766     if (flags & SCF_DO_SUBSTR) {
3767      if (mincount == 0) SCAN_COMMIT(pRExC_state,data,minlenp); /* Cannot extend fixed substrings */
3768      pos_before = data->pos_min;
3769     }
3770     if (data) {
3771      fl = data->flags;
3772      data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
3773      if (is_inf)
3774       data->flags |= SF_IS_INF;
3775     }
3776     if (flags & SCF_DO_STCLASS) {
3777      cl_init(pRExC_state, &this_class);
3778      oclass = data->start_class;
3779      data->start_class = &this_class;
3780      f |= SCF_DO_STCLASS_AND;
3781      f &= ~SCF_DO_STCLASS_OR;
3782     }
3783     /* Exclude from super-linear cache processing any {n,m}
3784     regops for which the combination of input pos and regex
3785     pos is not enough information to determine if a match
3786     will be possible.
3787
3788     For example, in the regex /foo(bar\s*){4,8}baz/ with the
3789     regex pos at the \s*, the prospects for a match depend not
3790     only on the input position but also on how many (bar\s*)
3791     repeats into the {4,8} we are. */
3792    if ((mincount > 1) || (maxcount > 1 && maxcount != REG_INFTY))
3793      f &= ~SCF_WHILEM_VISITED_POS;
3794
3795     /* This will finish on WHILEM, setting scan, or on NULL: */
3796     minnext = study_chunk(pRExC_state, &scan, minlenp, &deltanext,
3797          last, data, stopparen, recursed, NULL,
3798          (mincount == 0
3799           ? (f & ~SCF_DO_SUBSTR) : f),depth+1);
3800
3801     if (flags & SCF_DO_STCLASS)
3802      data->start_class = oclass;
3803     if (mincount == 0 || minnext == 0) {
3804      if (flags & SCF_DO_STCLASS_OR) {
3805       cl_or(pRExC_state, data->start_class, &this_class);
3806      }
3807      else if (flags & SCF_DO_STCLASS_AND) {
3808       /* Switch to OR mode: cache the old value of
3809       * data->start_class */
3810       INIT_AND_WITHP;
3811       StructCopy(data->start_class, and_withp,
3812         struct regnode_charclass_class);
3813       flags &= ~SCF_DO_STCLASS_AND;
3814       StructCopy(&this_class, data->start_class,
3815         struct regnode_charclass_class);
3816       flags |= SCF_DO_STCLASS_OR;
3817       data->start_class->flags |= ANYOF_EOS;
3818      }
3819     } else {  /* Non-zero len */
3820      if (flags & SCF_DO_STCLASS_OR) {
3821       cl_or(pRExC_state, data->start_class, &this_class);
3822       cl_and(data->start_class, and_withp);
3823      }
3824      else if (flags & SCF_DO_STCLASS_AND)
3825       cl_and(data->start_class, &this_class);
3826      flags &= ~SCF_DO_STCLASS;
3827     }
3828     if (!scan)   /* It was not CURLYX, but CURLY. */
3829      scan = next;
3830     if ( /* ? quantifier ok, except for (?{ ... }) */
3831      (next_is_eval || !(mincount == 0 && maxcount == 1))
3832      && (minnext == 0) && (deltanext == 0)
3833      && data && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
3834      && maxcount <= REG_INFTY/3) /* Complement check for big count */
3835     {
3836      ckWARNreg(RExC_parse,
3837        "Quantifier unexpected on zero-length expression");
3838     }
3839
3840     min += minnext * mincount;
3841     is_inf_internal |= ((maxcount == REG_INFTY
3842          && (minnext + deltanext) > 0)
3843          || deltanext == I32_MAX);
3844     is_inf |= is_inf_internal;
3845     delta += (minnext + deltanext) * maxcount - minnext * mincount;
3846
3847     /* Try powerful optimization CURLYX => CURLYN. */
3848     if (  OP(oscan) == CURLYX && data
3849      && data->flags & SF_IN_PAR
3850      && !(data->flags & SF_HAS_EVAL)
3851      && !deltanext && minnext == 1 ) {
3852      /* Try to optimize to CURLYN.  */
3853      regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
3854      regnode * const nxt1 = nxt;
3855 #ifdef DEBUGGING
3856      regnode *nxt2;
3857 #endif
3858
3859      /* Skip open. */
3860      nxt = regnext(nxt);
3861      if (!REGNODE_SIMPLE(OP(nxt))
3862       && !(PL_regkind[OP(nxt)] == EXACT
3863        && STR_LEN(nxt) == 1))
3864       goto nogo;
3865 #ifdef DEBUGGING
3866      nxt2 = nxt;
3867 #endif
3868      nxt = regnext(nxt);
3869      if (OP(nxt) != CLOSE)
3870       goto nogo;
3871      if (RExC_open_parens) {
3872       RExC_open_parens[ARG(nxt1)-1]=oscan; /*open->CURLYM*/
3873       RExC_close_parens[ARG(nxt1)-1]=nxt+2; /*close->while*/
3874      }
3875      /* Now we know that nxt2 is the only contents: */
3876      oscan->flags = (U8)ARG(nxt);
3877      OP(oscan) = CURLYN;
3878      OP(nxt1) = NOTHING; /* was OPEN. */
3879
3880 #ifdef DEBUGGING
3881      OP(nxt1 + 1) = OPTIMIZED; /* was count. */
3882      NEXT_OFF(nxt1+ 1) = 0; /* just for consistency. */
3883      NEXT_OFF(nxt2) = 0; /* just for consistency with CURLY. */
3884      OP(nxt) = OPTIMIZED; /* was CLOSE. */
3885      OP(nxt + 1) = OPTIMIZED; /* was count. */
3886      NEXT_OFF(nxt+ 1) = 0; /* just for consistency. */
3887 #endif
3888     }
3889    nogo:
3890
3891     /* Try optimization CURLYX => CURLYM. */
3892     if (  OP(oscan) == CURLYX && data
3893      && !(data->flags & SF_HAS_PAR)
3894      && !(data->flags & SF_HAS_EVAL)
3895      && !deltanext /* atom is fixed width */
3896      && minnext != 0 /* CURLYM can't handle zero width */
3897     ) {
3898      /* XXXX How to optimize if data == 0? */
3899      /* Optimize to a simpler form.  */
3900      regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
3901      regnode *nxt2;
3902
3903      OP(oscan) = CURLYM;
3904      while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
3905        && (OP(nxt2) != WHILEM))
3906       nxt = nxt2;
3907      OP(nxt2)  = SUCCEED; /* Whas WHILEM */
3908      /* Need to optimize away parenths. */
3909      if ((data->flags & SF_IN_PAR) && OP(nxt) == CLOSE) {
3910       /* Set the parenth number.  */
3911       regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
3912
3913       oscan->flags = (U8)ARG(nxt);
3914       if (RExC_open_parens) {
3915        RExC_open_parens[ARG(nxt1)-1]=oscan; /*open->CURLYM*/
3916        RExC_close_parens[ARG(nxt1)-1]=nxt2+1; /*close->NOTHING*/
3917       }
3918       OP(nxt1) = OPTIMIZED; /* was OPEN. */
3919       OP(nxt) = OPTIMIZED; /* was CLOSE. */
3920
3921 #ifdef DEBUGGING
3922       OP(nxt1 + 1) = OPTIMIZED; /* was count. */
3923       OP(nxt + 1) = OPTIMIZED; /* was count. */
3924       NEXT_OFF(nxt1 + 1) = 0; /* just for consistency. */
3925       NEXT_OFF(nxt + 1) = 0; /* just for consistency. */
3926 #endif
3927 #if 0
3928       while ( nxt1 && (OP(nxt1) != WHILEM)) {
3929        regnode *nnxt = regnext(nxt1);
3930        if (nnxt == nxt) {
3931         if (reg_off_by_arg[OP(nxt1)])
3932          ARG_SET(nxt1, nxt2 - nxt1);
3933         else if (nxt2 - nxt1 < U16_MAX)
3934          NEXT_OFF(nxt1) = nxt2 - nxt1;
3935         else
3936          OP(nxt) = NOTHING; /* Cannot beautify */
3937        }
3938        nxt1 = nnxt;
3939       }
3940 #endif
3941       /* Optimize again: */
3942       study_chunk(pRExC_state, &nxt1, minlenp, &deltanext, nxt,
3943          NULL, stopparen, recursed, NULL, 0,depth+1);
3944      }
3945      else
3946       oscan->flags = 0;
3947     }
3948     else if ((OP(oscan) == CURLYX)
3949       && (flags & SCF_WHILEM_VISITED_POS)
3950       /* See the comment on a similar expression above.
3951        However, this time it's not a subexpression
3952        we care about, but the expression itself. */
3953       && (maxcount == REG_INFTY)
3954       && data && ++data->whilem_c < 16) {
3955      /* This stays as CURLYX, we can put the count/of pair. */
3956      /* Find WHILEM (as in regexec.c) */
3957      regnode *nxt = oscan + NEXT_OFF(oscan);
3958
3959      if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
3960       nxt += ARG(nxt);
3961      PREVOPER(nxt)->flags = (U8)(data->whilem_c
3962       | (RExC_whilem_seen << 4)); /* On WHILEM */
3963     }
3964     if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
3965      pars++;
3966     if (flags & SCF_DO_SUBSTR) {
3967      SV *last_str = NULL;
3968      int counted = mincount != 0;
3969
3970      if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
3971 #if defined(SPARC64_GCC_WORKAROUND)
3972       I32 b = 0;
3973       STRLEN l = 0;
3974       const char *s = NULL;
3975       I32 old = 0;
3976
3977       if (pos_before >= data->last_start_min)
3978        b = pos_before;
3979       else
3980        b = data->last_start_min;
3981
3982       l = 0;
3983       s = SvPV_const(data->last_found, l);
3984       old = b - data->last_start_min;
3985
3986 #else
3987       I32 b = pos_before >= data->last_start_min
3988        ? pos_before : data->last_start_min;
3989       STRLEN l;
3990       const char * const s = SvPV_const(data->last_found, l);
3991       I32 old = b - data->last_start_min;
3992 #endif
3993
3994       if (UTF)
3995        old = utf8_hop((U8*)s, old) - (U8*)s;
3996       l -= old;
3997       /* Get the added string: */
3998       last_str = newSVpvn_utf8(s  + old, l, UTF);
3999       if (deltanext == 0 && pos_before == b) {
4000        /* What was added is a constant string */
4001        if (mincount > 1) {
4002         SvGROW(last_str, (mincount * l) + 1);
4003         repeatcpy(SvPVX(last_str) + l,
4004           SvPVX_const(last_str), l, mincount - 1);
4005         SvCUR_set(last_str, SvCUR(last_str) * mincount);
4006         /* Add additional parts. */
4007         SvCUR_set(data->last_found,
4008           SvCUR(data->last_found) - l);
4009         sv_catsv(data->last_found, last_str);
4010         {
4011          SV * sv = data->last_found;
4012          MAGIC *mg =
4013           SvUTF8(sv) && SvMAGICAL(sv) ?
4014           mg_find(sv, PERL_MAGIC_utf8) : NULL;
4015          if (mg && mg->mg_len >= 0)
4016           mg->mg_len += CHR_SVLEN(last_str) - l;
4017         }
4018         data->last_end += l * (mincount - 1);
4019        }
4020       } else {
4021        /* start offset must point into the last copy */
4022        data->last_start_min += minnext * (mincount - 1);
4023        data->last_start_max += is_inf ? I32_MAX
4024         : (maxcount - 1) * (minnext + data->pos_delta);
4025       }
4026      }
4027      /* It is counted once already... */
4028      data->pos_min += minnext * (mincount - counted);
4029      data->pos_delta += - counted * deltanext +
4030       (minnext + deltanext) * maxcount - minnext * mincount;
4031      if (mincount != maxcount) {
4032       /* Cannot extend fixed substrings found inside
4033        the group.  */
4034       SCAN_COMMIT(pRExC_state,data,minlenp);
4035       if (mincount && last_str) {
4036        SV * const sv = data->last_found;
4037        MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ?
4038         mg_find(sv, PERL_MAGIC_utf8) : NULL;
4039
4040        if (mg)
4041         mg->mg_len = -1;
4042        sv_setsv(sv, last_str);
4043        data->last_end = data->pos_min;
4044        data->last_start_min =
4045         data->pos_min - CHR_SVLEN(last_str);
4046        data->last_start_max = is_inf
4047         ? I32_MAX
4048         : data->pos_min + data->pos_delta
4049         - CHR_SVLEN(last_str);
4050       }
4051       data->longest = &(data->longest_float);
4052      }
4053      SvREFCNT_dec(last_str);
4054     }
4055     if (data && (fl & SF_HAS_EVAL))
4056      data->flags |= SF_HAS_EVAL;
4057    optimize_curly_tail:
4058     if (OP(oscan) != CURLYX) {
4059      while (PL_regkind[OP(next = regnext(oscan))] == NOTHING
4060       && NEXT_OFF(next))
4061       NEXT_OFF(oscan) += NEXT_OFF(next);
4062     }
4063     continue;
4064    default:   /* REF, ANYOFV, and CLUMP only? */
4065     if (flags & SCF_DO_SUBSTR) {
4066      SCAN_COMMIT(pRExC_state,data,minlenp); /* Cannot expect anything... */
4067      data->longest = &(data->longest_float);
4068     }
4069     is_inf = is_inf_internal = 1;
4070     if (flags & SCF_DO_STCLASS_OR)
4071      cl_anything(pRExC_state, data->start_class);
4072     flags &= ~SCF_DO_STCLASS;
4073     break;
4074    }
4075   }
4076   else if (OP(scan) == LNBREAK) {
4077    if (flags & SCF_DO_STCLASS) {
4078     int value = 0;
4079     data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */
4080      if (flags & SCF_DO_STCLASS_AND) {
4081      for (value = 0; value < 256; value++)
4082       if (!is_VERTWS_cp(value))
4083        ANYOF_BITMAP_CLEAR(data->start_class, value);
4084     }
4085     else {
4086      for (value = 0; value < 256; value++)
4087       if (is_VERTWS_cp(value))
4088        ANYOF_BITMAP_SET(data->start_class, value);
4089     }
4090     if (flags & SCF_DO_STCLASS_OR)
4091      cl_and(data->start_class, and_withp);
4092     flags &= ~SCF_DO_STCLASS;
4093    }
4094    min += 1;
4095    delta += 1;
4096    if (flags & SCF_DO_SUBSTR) {
4097      SCAN_COMMIT(pRExC_state,data,minlenp); /* Cannot expect anything... */
4098      data->pos_min += 1;
4099     data->pos_delta += 1;
4100     data->longest = &(data->longest_float);
4101     }
4102   }
4103   else if (REGNODE_SIMPLE(OP(scan))) {
4104    int value = 0;
4105
4106    if (flags & SCF_DO_SUBSTR) {
4107     SCAN_COMMIT(pRExC_state,data,minlenp);
4108     data->pos_min++;
4109    }
4110    min++;
4111    if (flags & SCF_DO_STCLASS) {
4112     data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */
4113
4114     /* Some of the logic below assumes that switching
4115     locale on will only add false positives. */
4116     switch (PL_regkind[OP(scan)]) {
4117     case SANY:
4118     default:
4119     do_default:
4120      /* Perl_croak(aTHX_ "panic: unexpected simple REx opcode %d", OP(scan)); */
4121      if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
4122       cl_anything(pRExC_state, data->start_class);
4123      break;
4124     case REG_ANY:
4125      if (OP(scan) == SANY)
4126       goto do_default;
4127      if (flags & SCF_DO_STCLASS_OR) { /* Everything but \n */
4128       value = (ANYOF_BITMAP_TEST(data->start_class,'\n')
4129         || ANYOF_CLASS_TEST_ANY_SET(data->start_class));
4130       cl_anything(pRExC_state, data->start_class);
4131      }
4132      if (flags & SCF_DO_STCLASS_AND || !value)
4133       ANYOF_BITMAP_CLEAR(data->start_class,'\n');
4134      break;
4135     case ANYOF:
4136      if (flags & SCF_DO_STCLASS_AND)
4137       cl_and(data->start_class,
4138        (struct regnode_charclass_class*)scan);
4139      else
4140       cl_or(pRExC_state, data->start_class,
4141        (struct regnode_charclass_class*)scan);
4142      break;
4143     case ALNUM:
4144      if (flags & SCF_DO_STCLASS_AND) {
4145       if (!(data->start_class->flags & ANYOF_LOCALE)) {
4146        ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
4147        if (OP(scan) == ALNUMU) {
4148         for (value = 0; value < 256; value++) {
4149          if (!isWORDCHAR_L1(value)) {
4150           ANYOF_BITMAP_CLEAR(data->start_class, value);
4151          }
4152         }
4153        } else {
4154         for (value = 0; value < 256; value++) {
4155          if (!isALNUM(value)) {
4156           ANYOF_BITMAP_CLEAR(data->start_class, value);
4157          }
4158         }
4159        }
4160       }
4161      }
4162      else {
4163       if (data->start_class->flags & ANYOF_LOCALE)
4164        ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
4165
4166       /* Even if under locale, set the bits for non-locale
4167       * in case it isn't a true locale-node.  This will
4168       * create false positives if it truly is locale */
4169       if (OP(scan) == ALNUMU) {
4170        for (value = 0; value < 256; value++) {
4171         if (isWORDCHAR_L1(value)) {
4172          ANYOF_BITMAP_SET(data->start_class, value);
4173         }
4174        }
4175       } else {
4176        for (value = 0; value < 256; value++) {
4177         if (isALNUM(value)) {
4178          ANYOF_BITMAP_SET(data->start_class, value);
4179         }
4180        }
4181       }
4182      }
4183      break;
4184     case NALNUM:
4185      if (flags & SCF_DO_STCLASS_AND) {
4186       if (!(data->start_class->flags & ANYOF_LOCALE)) {
4187        ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
4188        if (OP(scan) == NALNUMU) {
4189         for (value = 0; value < 256; value++) {
4190          if (isWORDCHAR_L1(value)) {
4191           ANYOF_BITMAP_CLEAR(data->start_class, value);
4192          }
4193         }
4194        } else {
4195         for (value = 0; value < 256; value++) {
4196          if (isALNUM(value)) {
4197           ANYOF_BITMAP_CLEAR(data->start_class, value);
4198          }
4199         }
4200        }
4201       }
4202      }
4203      else {
4204       if (data->start_class->flags & ANYOF_LOCALE)
4205        ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
4206
4207       /* Even if under locale, set the bits for non-locale in
4208       * case it isn't a true locale-node.  This will create
4209       * false positives if it truly is locale */
4210       if (OP(scan) == NALNUMU) {
4211        for (value = 0; value < 256; value++) {
4212         if (! isWORDCHAR_L1(value)) {
4213          ANYOF_BITMAP_SET(data->start_class, value);
4214         }
4215        }
4216       } else {
4217        for (value = 0; value < 256; value++) {
4218         if (! isALNUM(value)) {
4219          ANYOF_BITMAP_SET(data->start_class, value);
4220         }
4221        }
4222       }
4223      }
4224      break;
4225     case SPACE:
4226      if (flags & SCF_DO_STCLASS_AND) {
4227       if (!(data->start_class->flags & ANYOF_LOCALE)) {
4228        ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
4229        if (OP(scan) == SPACEU) {
4230         for (value = 0; value < 256; value++) {
4231          if (!isSPACE_L1(value)) {
4232           ANYOF_BITMAP_CLEAR(data->start_class, value);
4233          }
4234         }
4235        } else {
4236         for (value = 0; value < 256; value++) {
4237          if (!isSPACE(value)) {
4238           ANYOF_BITMAP_CLEAR(data->start_class, value);
4239          }
4240         }
4241        }
4242       }
4243      }
4244      else {
4245       if (data->start_class->flags & ANYOF_LOCALE) {
4246        ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
4247       }
4248       if (OP(scan) == SPACEU) {
4249        for (value = 0; value < 256; value++) {
4250         if (isSPACE_L1(value)) {
4251          ANYOF_BITMAP_SET(data->start_class, value);
4252         }
4253        }
4254       } else {
4255        for (value = 0; value < 256; value++) {
4256         if (isSPACE(value)) {
4257          ANYOF_BITMAP_SET(data->start_class, value);
4258         }
4259        }
4260       }
4261      }
4262      break;
4263     case NSPACE:
4264      if (flags & SCF_DO_STCLASS_AND) {
4265       if (!(data->start_class->flags & ANYOF_LOCALE)) {
4266        ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
4267        if (OP(scan) == NSPACEU) {
4268         for (value = 0; value < 256; value++) {
4269          if (isSPACE_L1(value)) {
4270           ANYOF_BITMAP_CLEAR(data->start_class, value);
4271          }
4272         }
4273        } else {
4274         for (value = 0; value < 256; value++) {
4275          if (isSPACE(value)) {
4276           ANYOF_BITMAP_CLEAR(data->start_class, value);
4277          }
4278         }
4279        }
4280       }
4281      }
4282      else {
4283       if (data->start_class->flags & ANYOF_LOCALE)
4284        ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
4285       if (OP(scan) == NSPACEU) {
4286        for (value = 0; value < 256; value++) {
4287         if (!isSPACE_L1(value)) {
4288          ANYOF_BITMAP_SET(data->start_class, value);
4289         }
4290        }
4291       }
4292       else {
4293        for (value = 0; value < 256; value++) {
4294         if (!isSPACE(value)) {
4295          ANYOF_BITMAP_SET(data->start_class, value);
4296         }
4297        }
4298       }
4299      }
4300      break;
4301     case DIGIT:
4302      if (flags & SCF_DO_STCLASS_AND) {
4303       if (!(data->start_class->flags & ANYOF_LOCALE)) {
4304        ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NDIGIT);
4305        for (value = 0; value < 256; value++)
4306         if (!isDIGIT(value))
4307          ANYOF_BITMAP_CLEAR(data->start_class, value);
4308       }
4309      }
4310      else {
4311       if (data->start_class->flags & ANYOF_LOCALE)
4312        ANYOF_CLASS_SET(data->start_class,ANYOF_DIGIT);
4313       for (value = 0; value < 256; value++)
4314        if (isDIGIT(value))
4315         ANYOF_BITMAP_SET(data->start_class, value);
4316      }
4317      break;
4318     case NDIGIT:
4319      if (flags & SCF_DO_STCLASS_AND) {
4320       if (!(data->start_class->flags & ANYOF_LOCALE))
4321        ANYOF_CLASS_CLEAR(data->start_class,ANYOF_DIGIT);
4322       for (value = 0; value < 256; value++)
4323        if (isDIGIT(value))
4324         ANYOF_BITMAP_CLEAR(data->start_class, value);
4325      }
4326      else {
4327       if (data->start_class->flags & ANYOF_LOCALE)
4328        ANYOF_CLASS_SET(data->start_class,ANYOF_NDIGIT);
4329       for (value = 0; value < 256; value++)
4330        if (!isDIGIT(value))
4331         ANYOF_BITMAP_SET(data->start_class, value);
4332      }
4333      break;
4334     CASE_SYNST_FNC(VERTWS);
4335     CASE_SYNST_FNC(HORIZWS);
4336
4337     }
4338     if (flags & SCF_DO_STCLASS_OR)
4339      cl_and(data->start_class, and_withp);
4340     flags &= ~SCF_DO_STCLASS;
4341    }
4342   }
4343   else if (PL_regkind[OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
4344    data->flags |= (OP(scan) == MEOL
4345        ? SF_BEFORE_MEOL
4346        : SF_BEFORE_SEOL);
4347   }
4348   else if (  PL_regkind[OP(scan)] == BRANCHJ
4349     /* Lookbehind, or need to calculate parens/evals/stclass: */
4350     && (scan->flags || data || (flags & SCF_DO_STCLASS))
4351     && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
4352    if ( !PERL_ENABLE_POSITIVE_ASSERTION_STUDY
4353     || OP(scan) == UNLESSM )
4354    {
4355     /* Negative Lookahead/lookbehind
4356     In this case we can't do fixed string optimisation.
4357     */
4358
4359     I32 deltanext, minnext, fake = 0;
4360     regnode *nscan;
4361     struct regnode_charclass_class intrnl;
4362     int f = 0;
4363
4364     data_fake.flags = 0;
4365     if (data) {
4366      data_fake.whilem_c = data->whilem_c;
4367      data_fake.last_closep = data->last_closep;
4368     }
4369     else
4370      data_fake.last_closep = &fake;
4371     data_fake.pos_delta = delta;
4372     if ( flags & SCF_DO_STCLASS && !scan->flags
4373      && OP(scan) == IFMATCH ) { /* Lookahead */
4374      cl_init(pRExC_state, &intrnl);
4375      data_fake.start_class = &intrnl;
4376      f |= SCF_DO_STCLASS_AND;
4377     }
4378     if (flags & SCF_WHILEM_VISITED_POS)
4379      f |= SCF_WHILEM_VISITED_POS;
4380     next = regnext(scan);
4381     nscan = NEXTOPER(NEXTOPER(scan));
4382     minnext = study_chunk(pRExC_state, &nscan, minlenp, &deltanext,
4383      last, &data_fake, stopparen, recursed, NULL, f, depth+1);
4384     if (scan->flags) {
4385      if (deltanext) {
4386       FAIL("Variable length lookbehind not implemented");
4387      }
4388      else if (minnext > (I32)U8_MAX) {
4389       FAIL2("Lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
4390      }
4391      scan->flags = (U8)minnext;
4392     }
4393     if (data) {
4394      if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
4395       pars++;
4396      if (data_fake.flags & SF_HAS_EVAL)
4397       data->flags |= SF_HAS_EVAL;
4398      data->whilem_c = data_fake.whilem_c;
4399     }
4400     if (f & SCF_DO_STCLASS_AND) {
4401      if (flags & SCF_DO_STCLASS_OR) {
4402       /* OR before, AND after: ideally we would recurse with
4403       * data_fake to get the AND applied by study of the
4404       * remainder of the pattern, and then derecurse;
4405       * *** HACK *** for now just treat as "no information".
4406       * See [perl #56690].
4407       */
4408       cl_init(pRExC_state, data->start_class);
4409      }  else {
4410       /* AND before and after: combine and continue */
4411       const int was = (data->start_class->flags & ANYOF_EOS);
4412
4413       cl_and(data->start_class, &intrnl);
4414       if (was)
4415        data->start_class->flags |= ANYOF_EOS;
4416      }
4417     }
4418    }
4419 #if PERL_ENABLE_POSITIVE_ASSERTION_STUDY
4420    else {
4421     /* Positive Lookahead/lookbehind
4422     In this case we can do fixed string optimisation,
4423     but we must be careful about it. Note in the case of
4424     lookbehind the positions will be offset by the minimum
4425     length of the pattern, something we won't know about
4426     until after the recurse.
4427     */
4428     I32 deltanext, fake = 0;
4429     regnode *nscan;
4430     struct regnode_charclass_class intrnl;
4431     int f = 0;
4432     /* We use SAVEFREEPV so that when the full compile
4433      is finished perl will clean up the allocated
4434      minlens when it's all done. This way we don't
4435      have to worry about freeing them when we know
4436      they wont be used, which would be a pain.
4437     */
4438     I32 *minnextp;
4439     Newx( minnextp, 1, I32 );
4440     SAVEFREEPV(minnextp);
4441
4442     if (data) {
4443      StructCopy(data, &data_fake, scan_data_t);
4444      if ((flags & SCF_DO_SUBSTR) && data->last_found) {
4445       f |= SCF_DO_SUBSTR;
4446       if (scan->flags)
4447        SCAN_COMMIT(pRExC_state, &data_fake,minlenp);
4448       data_fake.last_found=newSVsv(data->last_found);
4449      }
4450     }
4451     else
4452      data_fake.last_closep = &fake;
4453     data_fake.flags = 0;
4454     data_fake.pos_delta = delta;
4455     if (is_inf)
4456      data_fake.flags |= SF_IS_INF;
4457     if ( flags & SCF_DO_STCLASS && !scan->flags
4458      && OP(scan) == IFMATCH ) { /* Lookahead */
4459      cl_init(pRExC_state, &intrnl);
4460      data_fake.start_class = &intrnl;
4461      f |= SCF_DO_STCLASS_AND;
4462     }
4463     if (flags & SCF_WHILEM_VISITED_POS)
4464      f |= SCF_WHILEM_VISITED_POS;
4465     next = regnext(scan);
4466     nscan = NEXTOPER(NEXTOPER(scan));
4467
4468     *minnextp = study_chunk(pRExC_state, &nscan, minnextp, &deltanext,
4469      last, &data_fake, stopparen, recursed, NULL, f,depth+1);
4470     if (scan->flags) {
4471      if (deltanext) {
4472       FAIL("Variable length lookbehind not implemented");
4473      }
4474      else if (*minnextp > (I32)U8_MAX) {
4475       FAIL2("Lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
4476      }
4477      scan->flags = (U8)*minnextp;
4478     }
4479
4480     *minnextp += min;
4481
4482     if (f & SCF_DO_STCLASS_AND) {
4483      const int was = (data->start_class->flags & ANYOF_EOS);
4484
4485      cl_and(data->start_class, &intrnl);
4486      if (was)
4487       data->start_class->flags |= ANYOF_EOS;
4488     }
4489     if (data) {
4490      if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
4491       pars++;
4492      if (data_fake.flags & SF_HAS_EVAL)
4493       data->flags |= SF_HAS_EVAL;
4494      data->whilem_c = data_fake.whilem_c;
4495      if ((flags & SCF_DO_SUBSTR) && data_fake.last_found) {
4496       if (RExC_rx->minlen<*minnextp)
4497        RExC_rx->minlen=*minnextp;
4498       SCAN_COMMIT(pRExC_state, &data_fake, minnextp);
4499       SvREFCNT_dec(data_fake.last_found);
4500
4501       if ( data_fake.minlen_fixed != minlenp )
4502       {
4503        data->offset_fixed= data_fake.offset_fixed;
4504        data->minlen_fixed= data_fake.minlen_fixed;
4505        data->lookbehind_fixed+= scan->flags;
4506       }
4507       if ( data_fake.minlen_float != minlenp )
4508       {
4509        data->minlen_float= data_fake.minlen_float;
4510        data->offset_float_min=data_fake.offset_float_min;
4511        data->offset_float_max=data_fake.offset_float_max;
4512        data->lookbehind_float+= scan->flags;
4513       }
4514      }
4515     }
4516
4517
4518    }
4519 #endif
4520   }
4521   else if (OP(scan) == OPEN) {
4522    if (stopparen != (I32)ARG(scan))
4523     pars++;
4524   }
4525   else if (OP(scan) == CLOSE) {
4526    if (stopparen == (I32)ARG(scan)) {
4527     break;
4528    }
4529    if ((I32)ARG(scan) == is_par) {
4530     next = regnext(scan);
4531
4532     if ( next && (OP(next) != WHILEM) && next < last)
4533      is_par = 0;  /* Disable optimization */
4534    }
4535    if (data)
4536     *(data->last_closep) = ARG(scan);
4537   }
4538   else if (OP(scan) == EVAL) {
4539     if (data)
4540      data->flags |= SF_HAS_EVAL;
4541   }
4542   else if ( PL_regkind[OP(scan)] == ENDLIKE ) {
4543    if (flags & SCF_DO_SUBSTR) {
4544     SCAN_COMMIT(pRExC_state,data,minlenp);
4545     flags &= ~SCF_DO_SUBSTR;
4546    }
4547    if (data && OP(scan)==ACCEPT) {
4548     data->flags |= SCF_SEEN_ACCEPT;
4549     if (stopmin > min)
4550      stopmin = min;
4551    }
4552   }
4553   else if (OP(scan) == LOGICAL && scan->flags == 2) /* Embedded follows */
4554   {
4555     if (flags & SCF_DO_SUBSTR) {
4556      SCAN_COMMIT(pRExC_state,data,minlenp);
4557      data->longest = &(data->longest_float);
4558     }
4559     is_inf = is_inf_internal = 1;
4560     if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
4561      cl_anything(pRExC_state, data->start_class);
4562     flags &= ~SCF_DO_STCLASS;
4563   }
4564   else if (OP(scan) == GPOS) {
4565    if (!(RExC_rx->extflags & RXf_GPOS_FLOAT) &&
4566     !(delta || is_inf || (data && data->pos_delta)))
4567    {
4568     if (!(RExC_rx->extflags & RXf_ANCH) && (flags & SCF_DO_SUBSTR))
4569      RExC_rx->extflags |= RXf_ANCH_GPOS;
4570     if (RExC_rx->gofs < (U32)min)
4571      RExC_rx->gofs = min;
4572    } else {
4573     RExC_rx->extflags |= RXf_GPOS_FLOAT;
4574     RExC_rx->gofs = 0;
4575    }
4576   }
4577 #ifdef TRIE_STUDY_OPT
4578 #ifdef FULL_TRIE_STUDY
4579   else if (PL_regkind[OP(scan)] == TRIE) {
4580    /* NOTE - There is similar code to this block above for handling
4581    BRANCH nodes on the initial study.  If you change stuff here
4582    check there too. */
4583    regnode *trie_node= scan;
4584    regnode *tail= regnext(scan);
4585    reg_trie_data *trie = (reg_trie_data*)RExC_rxi->data->data[ ARG(scan) ];
4586    I32 max1 = 0, min1 = I32_MAX;
4587    struct regnode_charclass_class accum;
4588
4589    if (flags & SCF_DO_SUBSTR) /* XXXX Add !SUSPEND? */
4590     SCAN_COMMIT(pRExC_state, data,minlenp); /* Cannot merge strings after this. */
4591    if (flags & SCF_DO_STCLASS)
4592     cl_init_zero(pRExC_state, &accum);
4593
4594    if (!trie->jump) {
4595     min1= trie->minlen;
4596     max1= trie->maxlen;
4597    } else {
4598     const regnode *nextbranch= NULL;
4599     U32 word;
4600
4601     for ( word=1 ; word <= trie->wordcount ; word++)
4602     {
4603      I32 deltanext=0, minnext=0, f = 0, fake;
4604      struct regnode_charclass_class this_class;
4605
4606      data_fake.flags = 0;
4607      if (data) {
4608       data_fake.whilem_c = data->whilem_c;
4609       data_fake.last_closep = data->last_closep;
4610      }
4611      else
4612       data_fake.last_closep = &fake;
4613      data_fake.pos_delta = delta;
4614      if (flags & SCF_DO_STCLASS) {
4615       cl_init(pRExC_state, &this_class);
4616       data_fake.start_class = &this_class;
4617       f = SCF_DO_STCLASS_AND;
4618      }
4619      if (flags & SCF_WHILEM_VISITED_POS)
4620       f |= SCF_WHILEM_VISITED_POS;
4621
4622      if (trie->jump[word]) {
4623       if (!nextbranch)
4624        nextbranch = trie_node + trie->jump[0];
4625       scan= trie_node + trie->jump[word];
4626       /* We go from the jump point to the branch that follows
4627       it. Note this means we need the vestigal unused branches
4628       even though they arent otherwise used.
4629       */
4630       minnext = study_chunk(pRExC_state, &scan, minlenp,
4631        &deltanext, (regnode *)nextbranch, &data_fake,
4632        stopparen, recursed, NULL, f,depth+1);
4633      }
4634      if (nextbranch && PL_regkind[OP(nextbranch)]==BRANCH)
4635       nextbranch= regnext((regnode*)nextbranch);
4636
4637      if (min1 > (I32)(minnext + trie->minlen))
4638       min1 = minnext + trie->minlen;
4639      if (max1 < (I32)(minnext + deltanext + trie->maxlen))
4640       max1 = minnext + deltanext + trie->maxlen;
4641      if (deltanext == I32_MAX)
4642       is_inf = is_inf_internal = 1;
4643
4644      if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
4645       pars++;
4646      if (data_fake.flags & SCF_SEEN_ACCEPT) {
4647       if ( stopmin > min + min1)
4648        stopmin = min + min1;
4649       flags &= ~SCF_DO_SUBSTR;
4650       if (data)
4651        data->flags |= SCF_SEEN_ACCEPT;
4652      }
4653      if (data) {
4654       if (data_fake.flags & SF_HAS_EVAL)
4655        data->flags |= SF_HAS_EVAL;
4656       data->whilem_c = data_fake.whilem_c;
4657      }
4658      if (flags & SCF_DO_STCLASS)
4659       cl_or(pRExC_state, &accum, &this_class);
4660     }
4661    }
4662    if (flags & SCF_DO_SUBSTR) {
4663     data->pos_min += min1;
4664     data->pos_delta += max1 - min1;
4665     if (max1 != min1 || is_inf)
4666      data->longest = &(data->longest_float);
4667    }
4668    min += min1;
4669    delta += max1 - min1;
4670    if (flags & SCF_DO_STCLASS_OR) {
4671     cl_or(pRExC_state, data->start_class, &accum);
4672     if (min1) {
4673      cl_and(data->start_class, and_withp);
4674      flags &= ~SCF_DO_STCLASS;
4675     }
4676    }
4677    else if (flags & SCF_DO_STCLASS_AND) {
4678     if (min1) {
4679      cl_and(data->start_class, &accum);
4680      flags &= ~SCF_DO_STCLASS;
4681     }
4682     else {
4683      /* Switch to OR mode: cache the old value of
4684      * data->start_class */
4685      INIT_AND_WITHP;
4686      StructCopy(data->start_class, and_withp,
4687        struct regnode_charclass_class);
4688      flags &= ~SCF_DO_STCLASS_AND;
4689      StructCopy(&accum, data->start_class,
4690        struct regnode_charclass_class);
4691      flags |= SCF_DO_STCLASS_OR;
4692      data->start_class->flags |= ANYOF_EOS;
4693     }
4694    }
4695    scan= tail;
4696    continue;
4697   }
4698 #else
4699   else if (PL_regkind[OP(scan)] == TRIE) {
4700    reg_trie_data *trie = (reg_trie_data*)RExC_rxi->data->data[ ARG(scan) ];
4701    U8*bang=NULL;
4702
4703    min += trie->minlen;
4704    delta += (trie->maxlen - trie->minlen);
4705    flags &= ~SCF_DO_STCLASS; /* xxx */
4706    if (flags & SCF_DO_SUBSTR) {
4707      SCAN_COMMIT(pRExC_state,data,minlenp); /* Cannot expect anything... */
4708      data->pos_min += trie->minlen;
4709      data->pos_delta += (trie->maxlen - trie->minlen);
4710     if (trie->maxlen != trie->minlen)
4711      data->longest = &(data->longest_float);
4712     }
4713     if (trie->jump) /* no more substrings -- for now /grr*/
4714      flags &= ~SCF_DO_SUBSTR;
4715   }
4716 #endif /* old or new */
4717 #endif /* TRIE_STUDY_OPT */
4718
4719   /* Else: zero-length, ignore. */
4720   scan = regnext(scan);
4721  }
4722  if (frame) {
4723   last = frame->last;
4724   scan = frame->next;
4725   stopparen = frame->stop;
4726   frame = frame->prev;
4727   goto fake_study_recurse;
4728  }
4729
4730   finish:
4731  assert(!frame);
4732  DEBUG_STUDYDATA("pre-fin:",data,depth);
4733
4734  *scanp = scan;
4735  *deltap = is_inf_internal ? I32_MAX : delta;
4736  if (flags & SCF_DO_SUBSTR && is_inf)
4737   data->pos_delta = I32_MAX - data->pos_min;
4738  if (is_par > (I32)U8_MAX)
4739   is_par = 0;
4740  if (is_par && pars==1 && data) {
4741   data->flags |= SF_IN_PAR;
4742   data->flags &= ~SF_HAS_PAR;
4743  }
4744  else if (pars && data) {
4745   data->flags |= SF_HAS_PAR;
4746   data->flags &= ~SF_IN_PAR;
4747  }
4748  if (flags & SCF_DO_STCLASS_OR)
4749   cl_and(data->start_class, and_withp);
4750  if (flags & SCF_TRIE_RESTUDY)
4751   data->flags |=  SCF_TRIE_RESTUDY;
4752
4753  DEBUG_STUDYDATA("post-fin:",data,depth);
4754
4755  return min < stopmin ? min : stopmin;
4756 }
4757
4758 STATIC U32
4759 S_add_data(RExC_state_t *pRExC_state, U32 n, const char *s)
4760 {
4761  U32 count = RExC_rxi->data ? RExC_rxi->data->count : 0;
4762
4763  PERL_ARGS_ASSERT_ADD_DATA;
4764
4765  Renewc(RExC_rxi->data,
4766   sizeof(*RExC_rxi->data) + sizeof(void*) * (count + n - 1),
4767   char, struct reg_data);
4768  if(count)
4769   Renew(RExC_rxi->data->what, count + n, U8);
4770  else
4771   Newx(RExC_rxi->data->what, n, U8);
4772  RExC_rxi->data->count = count + n;
4773  Copy(s, RExC_rxi->data->what + count, n, U8);
4774  return count;
4775 }
4776
4777 /*XXX: todo make this not included in a non debugging perl */
4778 #ifndef PERL_IN_XSUB_RE
4779 void
4780 Perl_reginitcolors(pTHX)
4781 {
4782  dVAR;
4783  const char * const s = PerlEnv_getenv("PERL_RE_COLORS");
4784  if (s) {
4785   char *t = savepv(s);
4786   int i = 0;
4787   PL_colors[0] = t;
4788   while (++i < 6) {
4789    t = strchr(t, '\t');
4790    if (t) {
4791     *t = '\0';
4792     PL_colors[i] = ++t;
4793    }
4794    else
4795     PL_colors[i] = t = (char *)"";
4796   }
4797  } else {
4798   int i = 0;
4799   while (i < 6)
4800    PL_colors[i++] = (char *)"";
4801  }
4802  PL_colorset = 1;
4803 }
4804 #endif
4805
4806
4807 #ifdef TRIE_STUDY_OPT
4808 #define CHECK_RESTUDY_GOTO                                  \
4809   if (                                                \
4810    (data.flags & SCF_TRIE_RESTUDY)               \
4811    && ! restudied++                              \
4812   )     goto reStudy
4813 #else
4814 #define CHECK_RESTUDY_GOTO
4815 #endif
4816
4817 /*
4818  - pregcomp - compile a regular expression into internal code
4819  *
4820  * We can't allocate space until we know how big the compiled form will be,
4821  * but we can't compile it (and thus know how big it is) until we've got a
4822  * place to put the code.  So we cheat:  we compile it twice, once with code
4823  * generation turned off and size counting turned on, and once "for real".
4824  * This also means that we don't allocate space until we are sure that the
4825  * thing really will compile successfully, and we never have to move the
4826  * code and thus invalidate pointers into it.  (Note that it has to be in
4827  * one piece because free() must be able to free it all.) [NB: not true in perl]
4828  *
4829  * Beware that the optimization-preparation code in here knows about some
4830  * of the structure of the compiled regexp.  [I'll say.]
4831  */
4832
4833
4834
4835 #ifndef PERL_IN_XSUB_RE
4836 #define RE_ENGINE_PTR &reh_regexp_engine
4837 #else
4838 extern const struct regexp_engine my_reg_engine;
4839 #define RE_ENGINE_PTR &my_reg_engine
4840 #endif
4841
4842 #ifndef PERL_IN_XSUB_RE
4843 REGEXP *
4844 Perl_pregcomp(pTHX_ SV * const pattern, const U32 flags)
4845 {
4846  dVAR;
4847  HV * const table = GvHV(PL_hintgv);
4848
4849  PERL_ARGS_ASSERT_PREGCOMP;
4850
4851  /* Dispatch a request to compile a regexp to correct
4852  regexp engine. */
4853  if (table) {
4854   SV **ptr= hv_fetchs(table, "regcomp", FALSE);
4855   GET_RE_DEBUG_FLAGS_DECL;
4856   if (ptr && SvIOK(*ptr) && SvIV(*ptr)) {
4857    const regexp_engine *eng=INT2PTR(regexp_engine*,SvIV(*ptr));
4858    DEBUG_COMPILE_r({
4859     PerlIO_printf(Perl_debug_log, "Using engine %"UVxf"\n",
4860      SvIV(*ptr));
4861    });
4862    return CALLREGCOMP_ENG(eng, pattern, flags);
4863   }
4864  }
4865  return Perl_re_compile(aTHX_ pattern, flags);
4866 }
4867 #endif
4868
4869 REGEXP *
4870 Perl_re_compile(pTHX_ SV * const pattern, U32 orig_pm_flags)
4871 {
4872  dVAR;
4873  REGEXP *rx;
4874  struct regexp *r;
4875  register regexp_internal *ri;
4876  STRLEN plen;
4877  char* VOL exp;
4878  char* xend;
4879  regnode *scan;
4880  I32 flags;
4881  I32 minlen = 0;
4882  U32 pm_flags;
4883
4884  /* these are all flags - maybe they should be turned
4885  * into a single int with different bit masks */
4886  I32 sawlookahead = 0;
4887  I32 sawplus = 0;
4888  I32 sawopen = 0;
4889  bool used_setjump = FALSE;
4890  regex_charset initial_charset = get_regex_charset(orig_pm_flags);
4891
4892  U8 jump_ret = 0;
4893  dJMPENV;
4894  scan_data_t data;
4895  RExC_state_t RExC_state;
4896  RExC_state_t * const pRExC_state = &RExC_state;
4897 #ifdef TRIE_STUDY_OPT
4898  int restudied;
4899  RExC_state_t copyRExC_state;
4900 #endif
4901  GET_RE_DEBUG_FLAGS_DECL;
4902
4903  PERL_ARGS_ASSERT_RE_COMPILE;
4904
4905  DEBUG_r(if (!PL_colorset) reginitcolors());
4906
4907 #ifndef PERL_IN_XSUB_RE
4908  /* Initialize these here instead of as-needed, as is quick and avoids
4909  * having to test them each time otherwise */
4910  if (! PL_AboveLatin1) {
4911   PL_AboveLatin1 = _new_invlist_C_array(AboveLatin1_invlist);
4912   PL_ASCII = _new_invlist_C_array(ASCII_invlist);
4913   PL_Latin1 = _new_invlist_C_array(Latin1_invlist);
4914
4915   PL_L1PosixAlnum = _new_invlist_C_array(L1PosixAlnum_invlist);
4916   PL_PosixAlnum = _new_invlist_C_array(PosixAlnum_invlist);
4917
4918   PL_L1PosixAlpha = _new_invlist_C_array(L1PosixAlpha_invlist);
4919   PL_PosixAlpha = _new_invlist_C_array(PosixAlpha_invlist);
4920
4921   PL_PosixBlank = _new_invlist_C_array(PosixBlank_invlist);
4922   PL_XPosixBlank = _new_invlist_C_array(XPosixBlank_invlist);
4923
4924   PL_L1Cased = _new_invlist_C_array(L1Cased_invlist);
4925
4926   PL_PosixCntrl = _new_invlist_C_array(PosixCntrl_invlist);
4927   PL_XPosixCntrl = _new_invlist_C_array(XPosixCntrl_invlist);
4928
4929   PL_PosixDigit = _new_invlist_C_array(PosixDigit_invlist);
4930
4931   PL_L1PosixGraph = _new_invlist_C_array(L1PosixGraph_invlist);
4932   PL_PosixGraph = _new_invlist_C_array(PosixGraph_invlist);
4933
4934   PL_L1PosixAlnum = _new_invlist_C_array(L1PosixAlnum_invlist);
4935   PL_PosixAlnum = _new_invlist_C_array(PosixAlnum_invlist);
4936
4937   PL_L1PosixLower = _new_invlist_C_array(L1PosixLower_invlist);
4938   PL_PosixLower = _new_invlist_C_array(PosixLower_invlist);
4939
4940   PL_L1PosixPrint = _new_invlist_C_array(L1PosixPrint_invlist);
4941   PL_PosixPrint = _new_invlist_C_array(PosixPrint_invlist);
4942
4943   PL_L1PosixPunct = _new_invlist_C_array(L1PosixPunct_invlist);
4944   PL_PosixPunct = _new_invlist_C_array(PosixPunct_invlist);
4945
4946   PL_PerlSpace = _new_invlist_C_array(PerlSpace_invlist);
4947   PL_XPerlSpace = _new_invlist_C_array(XPerlSpace_invlist);
4948
4949   PL_PosixSpace = _new_invlist_C_array(PosixSpace_invlist);
4950   PL_XPosixSpace = _new_invlist_C_array(XPosixSpace_invlist);
4951
4952   PL_L1PosixUpper = _new_invlist_C_array(L1PosixUpper_invlist);
4953   PL_PosixUpper = _new_invlist_C_array(PosixUpper_invlist);
4954
4955   PL_VertSpace = _new_invlist_C_array(VertSpace_invlist);
4956
4957   PL_PosixWord = _new_invlist_C_array(PosixWord_invlist);
4958   PL_L1PosixWord = _new_invlist_C_array(L1PosixWord_invlist);
4959
4960   PL_PosixXDigit = _new_invlist_C_array(PosixXDigit_invlist);
4961   PL_XPosixXDigit = _new_invlist_C_array(XPosixXDigit_invlist);
4962  }
4963 #endif
4964
4965  exp = SvPV(pattern, plen);
4966
4967  if (plen == 0) { /* ignore the utf8ness if the pattern is 0 length */
4968   RExC_utf8 = RExC_orig_utf8 = 0;
4969  }
4970  else {
4971   RExC_utf8 = RExC_orig_utf8 = SvUTF8(pattern);
4972  }
4973  RExC_uni_semantics = 0;
4974  RExC_contains_locale = 0;
4975
4976  /****************** LONG JUMP TARGET HERE***********************/
4977  /* Longjmp back to here if have to switch in midstream to utf8 */
4978  if (! RExC_orig_utf8) {
4979   JMPENV_PUSH(jump_ret);
4980   used_setjump = TRUE;
4981  }
4982
4983  if (jump_ret == 0) {    /* First time through */
4984   xend = exp + plen;
4985
4986   DEBUG_COMPILE_r({
4987    SV *dsv= sv_newmortal();
4988    RE_PV_QUOTED_DECL(s, RExC_utf8,
4989     dsv, exp, plen, 60);
4990    PerlIO_printf(Perl_debug_log, "%sCompiling REx%s %s\n",
4991       PL_colors[4],PL_colors[5],s);
4992   });
4993  }
4994  else {  /* longjumped back */
4995   STRLEN len = plen;
4996
4997   /* If the cause for the longjmp was other than changing to utf8, pop
4998   * our own setjmp, and longjmp to the correct handler */
4999   if (jump_ret != UTF8_LONGJMP) {
5000    JMPENV_POP;
5001    JMPENV_JUMP(jump_ret);
5002   }
5003
5004   GET_RE_DEBUG_FLAGS;
5005
5006   /* It's possible to write a regexp in ascii that represents Unicode
5007   codepoints outside of the byte range, such as via \x{100}. If we
5008   detect such a sequence we have to convert the entire pattern to utf8
5009   and then recompile, as our sizing calculation will have been based
5010   on 1 byte == 1 character, but we will need to use utf8 to encode
5011   at least some part of the pattern, and therefore must convert the whole
5012   thing.
5013   -- dmq */
5014   DEBUG_PARSE_r(PerlIO_printf(Perl_debug_log,
5015    "UTF8 mismatch! Converting to utf8 for resizing and compile\n"));
5016   exp = (char*)Perl_bytes_to_utf8(aTHX_
5017           (U8*)SvPV_nomg(pattern, plen),
5018           &len);
5019   xend = exp + len;
5020   RExC_orig_utf8 = RExC_utf8 = 1;
5021   SAVEFREEPV(exp);
5022  }
5023
5024 #ifdef TRIE_STUDY_OPT
5025  restudied = 0;
5026 #endif
5027
5028  pm_flags = orig_pm_flags;
5029
5030  if (initial_charset == REGEX_LOCALE_CHARSET) {
5031   RExC_contains_locale = 1;
5032  }
5033  else if (RExC_utf8 && initial_charset == REGEX_DEPENDS_CHARSET) {
5034
5035   /* Set to use unicode semantics if the pattern is in utf8 and has the
5036   * 'depends' charset specified, as it means unicode when utf8  */
5037   set_regex_charset(&pm_flags, REGEX_UNICODE_CHARSET);
5038  }
5039
5040  RExC_precomp = exp;
5041  RExC_flags = pm_flags;
5042  RExC_sawback = 0;
5043
5044  RExC_seen = 0;
5045  RExC_in_lookbehind = 0;
5046  RExC_seen_zerolen = *exp == '^' ? -1 : 0;
5047  RExC_seen_evals = 0;
5048  RExC_extralen = 0;
5049  RExC_override_recoding = 0;
5050
5051  /* First pass: determine size, legality. */
5052  RExC_parse = exp;
5053  RExC_start = exp;
5054  RExC_end = xend;
5055  RExC_naughty = 0;
5056  RExC_npar = 1;
5057  RExC_nestroot = 0;
5058  RExC_size = 0L;
5059  RExC_emit = &PL_regdummy;
5060  RExC_whilem_seen = 0;
5061  RExC_open_parens = NULL;
5062  RExC_close_parens = NULL;
5063  RExC_opend = NULL;
5064  RExC_paren_names = NULL;
5065 #ifdef DEBUGGING
5066  RExC_paren_name_list = NULL;
5067 #endif
5068  RExC_recurse = NULL;
5069  RExC_recurse_count = 0;
5070
5071 #if 0 /* REGC() is (currently) a NOP at the first pass.
5072  * Clever compilers notice this and complain. --jhi */
5073  REGC((U8)REG_MAGIC, (char*)RExC_emit);
5074 #endif
5075  DEBUG_PARSE_r(
5076   PerlIO_printf(Perl_debug_log, "Starting first pass (sizing)\n");
5077   RExC_lastnum=0;
5078   RExC_lastparse=NULL;
5079  );
5080  if (reg(pRExC_state, 0, &flags,1) == NULL) {
5081   RExC_precomp = NULL;
5082   return(NULL);
5083  }
5084
5085  /* Here, finished first pass.  Get rid of any added setjmp */
5086  if (used_setjump) {
5087   JMPENV_POP;
5088  }
5089
5090  DEBUG_PARSE_r({
5091   PerlIO_printf(Perl_debug_log,
5092    "Required size %"IVdf" nodes\n"
5093    "Starting second pass (creation)\n",
5094    (IV)RExC_size);
5095   RExC_lastnum=0;
5096   RExC_lastparse=NULL;
5097  });
5098
5099  /* The first pass could have found things that force Unicode semantics */
5100  if ((RExC_utf8 || RExC_uni_semantics)
5101   && get_regex_charset(pm_flags) == REGEX_DEPENDS_CHARSET)
5102  {
5103   set_regex_charset(&pm_flags, REGEX_UNICODE_CHARSET);
5104  }
5105
5106  /* Small enough for pointer-storage convention?
5107  If extralen==0, this means that we will not need long jumps. */
5108  if (RExC_size >= 0x10000L && RExC_extralen)
5109   RExC_size += RExC_extralen;
5110  else
5111   RExC_extralen = 0;
5112  if (RExC_whilem_seen > 15)
5113   RExC_whilem_seen = 15;
5114
5115  /* Allocate space and zero-initialize. Note, the two step process
5116  of zeroing when in debug mode, thus anything assigned has to
5117  happen after that */
5118  rx = (REGEXP*) newSV_type(SVt_REGEXP);
5119  r = (struct regexp*)SvANY(rx);
5120  Newxc(ri, sizeof(regexp_internal) + (unsigned)RExC_size * sizeof(regnode),
5121   char, regexp_internal);
5122  if ( r == NULL || ri == NULL )
5123   FAIL("Regexp out of space");
5124 #ifdef DEBUGGING
5125  /* avoid reading uninitialized memory in DEBUGGING code in study_chunk() */
5126  Zero(ri, sizeof(regexp_internal) + (unsigned)RExC_size * sizeof(regnode), char);
5127 #else
5128  /* bulk initialize base fields with 0. */
5129  Zero(ri, sizeof(regexp_internal), char);
5130 #endif
5131
5132  /* non-zero initialization begins here */
5133  RXi_SET( r, ri );
5134  r->engine= RE_ENGINE_PTR;
5135  r->extflags = pm_flags;
5136  {
5137   bool has_p     = ((r->extflags & RXf_PMf_KEEPCOPY) == RXf_PMf_KEEPCOPY);
5138   bool has_charset = (get_regex_charset(r->extflags) != REGEX_DEPENDS_CHARSET);
5139
5140   /* The caret is output if there are any defaults: if not all the STD
5141   * flags are set, or if no character set specifier is needed */
5142   bool has_default =
5143      (((r->extflags & RXf_PMf_STD_PMMOD) != RXf_PMf_STD_PMMOD)
5144      || ! has_charset);
5145   bool has_runon = ((RExC_seen & REG_SEEN_RUN_ON_COMMENT)==REG_SEEN_RUN_ON_COMMENT);
5146   U16 reganch = (U16)((r->extflags & RXf_PMf_STD_PMMOD)
5147        >> RXf_PMf_STD_PMMOD_SHIFT);
5148   const char *fptr = STD_PAT_MODS;        /*"msix"*/
5149   char *p;
5150   /* Allocate for the worst case, which is all the std flags are turned
5151   * on.  If more precision is desired, we could do a population count of
5152   * the flags set.  This could be done with a small lookup table, or by
5153   * shifting, masking and adding, or even, when available, assembly
5154   * language for a machine-language population count.
5155   * We never output a minus, as all those are defaults, so are
5156   * covered by the caret */
5157   const STRLEN wraplen = plen + has_p + has_runon
5158    + has_default       /* If needs a caret */
5159
5160     /* If needs a character set specifier */
5161    + ((has_charset) ? MAX_CHARSET_NAME_LENGTH : 0)
5162    + (sizeof(STD_PAT_MODS) - 1)
5163    + (sizeof("(?:)") - 1);
5164
5165   p = sv_grow(MUTABLE_SV(rx), wraplen + 1); /* +1 for the ending NUL */
5166   SvPOK_on(rx);
5167   SvFLAGS(rx) |= SvUTF8(pattern);
5168   *p++='('; *p++='?';
5169
5170   /* If a default, cover it using the caret */
5171   if (has_default) {
5172    *p++= DEFAULT_PAT_MOD;
5173   }
5174   if (has_charset) {
5175    STRLEN len;
5176    const char* const name = get_regex_charset_name(r->extflags, &len);
5177    Copy(name, p, len, char);
5178    p += len;
5179   }
5180   if (has_p)
5181    *p++ = KEEPCOPY_PAT_MOD; /*'p'*/
5182   {
5183    char ch;
5184    while((ch = *fptr++)) {
5185     if(reganch & 1)
5186      *p++ = ch;
5187     reganch >>= 1;
5188    }
5189   }
5190
5191   *p++ = ':';
5192   Copy(RExC_precomp, p, plen, char);
5193   assert ((RX_WRAPPED(rx) - p) < 16);
5194   r->pre_prefix = p - RX_WRAPPED(rx);
5195   p += plen;
5196   if (has_runon)
5197    *p++ = '\n';
5198   *p++ = ')';
5199   *p = 0;
5200   SvCUR_set(rx, p - SvPVX_const(rx));
5201  }
5202
5203  r->intflags = 0;
5204  r->nparens = RExC_npar - 1; /* set early to validate backrefs */
5205
5206  if (RExC_seen & REG_SEEN_RECURSE) {
5207   Newxz(RExC_open_parens, RExC_npar,regnode *);
5208   SAVEFREEPV(RExC_open_parens);
5209   Newxz(RExC_close_parens,RExC_npar,regnode *);
5210   SAVEFREEPV(RExC_close_parens);
5211  }
5212
5213  /* Useful during FAIL. */
5214 #ifdef RE_TRACK_PATTERN_OFFSETS
5215  Newxz(ri->u.offsets, 2*RExC_size+1, U32); /* MJD 20001228 */
5216  DEBUG_OFFSETS_r(PerlIO_printf(Perl_debug_log,
5217       "%s %"UVuf" bytes for offset annotations.\n",
5218       ri->u.offsets ? "Got" : "Couldn't get",
5219       (UV)((2*RExC_size+1) * sizeof(U32))));
5220 #endif
5221  SetProgLen(ri,RExC_size);
5222  RExC_rx_sv = rx;
5223  RExC_rx = r;
5224  RExC_rxi = ri;
5225  REH_CALL_COMP_BEGIN_HOOK(pRExC_state->rx);
5226
5227  /* Second pass: emit code. */
5228  RExC_flags = pm_flags; /* don't let top level (?i) bleed */
5229  RExC_parse = exp;
5230  RExC_end = xend;
5231  RExC_naughty = 0;
5232  RExC_npar = 1;
5233  RExC_emit_start = ri->program;
5234  RExC_emit = ri->program;
5235  RExC_emit_bound = ri->program + RExC_size + 1;
5236
5237  /* Store the count of eval-groups for security checks: */
5238  RExC_rx->seen_evals = RExC_seen_evals;
5239  REGC((U8)REG_MAGIC, (char*) RExC_emit++);
5240  if (reg(pRExC_state, 0, &flags,1) == NULL) {
5241   ReREFCNT_dec(rx);
5242   return(NULL);
5243  }
5244  /* XXXX To minimize changes to RE engine we always allocate
5245  3-units-long substrs field. */
5246  Newx(r->substrs, 1, struct reg_substr_data);
5247  if (RExC_recurse_count) {
5248   Newxz(RExC_recurse,RExC_recurse_count,regnode *);
5249   SAVEFREEPV(RExC_recurse);
5250  }
5251
5252 reStudy:
5253  r->minlen = minlen = sawlookahead = sawplus = sawopen = 0;
5254  Zero(r->substrs, 1, struct reg_substr_data);
5255
5256 #ifdef TRIE_STUDY_OPT
5257  if (!restudied) {
5258   StructCopy(&zero_scan_data, &data, scan_data_t);
5259   copyRExC_state = RExC_state;
5260  } else {
5261   U32 seen=RExC_seen;
5262   DEBUG_OPTIMISE_r(PerlIO_printf(Perl_debug_log,"Restudying\n"));
5263
5264   RExC_state = copyRExC_state;
5265   if (seen & REG_TOP_LEVEL_BRANCHES)
5266    RExC_seen |= REG_TOP_LEVEL_BRANCHES;
5267   else
5268    RExC_seen &= ~REG_TOP_LEVEL_BRANCHES;
5269   if (data.last_found) {
5270    SvREFCNT_dec(data.longest_fixed);
5271    SvREFCNT_dec(data.longest_float);
5272    SvREFCNT_dec(data.last_found);
5273   }
5274   StructCopy(&zero_scan_data, &data, scan_data_t);
5275  }
5276 #else
5277  StructCopy(&zero_scan_data, &data, scan_data_t);
5278 #endif
5279
5280  /* Dig out information for optimizations. */
5281  r->extflags = RExC_flags; /* was pm_op */
5282  /*dmq: removed as part of de-PMOP: pm->op_pmflags = RExC_flags; */
5283
5284  if (UTF)
5285   SvUTF8_on(rx); /* Unicode in it? */
5286  ri->regstclass = NULL;
5287  if (RExC_naughty >= 10) /* Probably an expensive pattern. */
5288   r->intflags |= PREGf_NAUGHTY;
5289  scan = ri->program + 1;  /* First BRANCH. */
5290
5291  /* testing for BRANCH here tells us whether there is "must appear"
5292  data in the pattern. If there is then we can use it for optimisations */
5293  if (!(RExC_seen & REG_TOP_LEVEL_BRANCHES)) { /*  Only one top-level choice. */
5294   I32 fake;
5295   STRLEN longest_float_length, longest_fixed_length;
5296   struct regnode_charclass_class ch_class; /* pointed to by data */
5297   int stclass_flag;
5298   I32 last_close = 0; /* pointed to by data */
5299   regnode *first= scan;
5300   regnode *first_next= regnext(first);
5301   /*
5302   * Skip introductions and multiplicators >= 1
5303   * so that we can extract the 'meat' of the pattern that must
5304   * match in the large if() sequence following.
5305   * NOTE that EXACT is NOT covered here, as it is normally
5306   * picked up by the optimiser separately.
5307   *
5308   * This is unfortunate as the optimiser isnt handling lookahead
5309   * properly currently.
5310   *
5311   */
5312   while ((OP(first) == OPEN && (sawopen = 1)) ||
5313    /* An OR of *one* alternative - should not happen now. */
5314    (OP(first) == BRANCH && OP(first_next) != BRANCH) ||
5315    /* for now we can't handle lookbehind IFMATCH*/
5316    (OP(first) == IFMATCH && !first->flags && (sawlookahead = 1)) ||
5317    (OP(first) == PLUS) ||
5318    (OP(first) == MINMOD) ||
5319    /* An {n,m} with n>0 */
5320    (PL_regkind[OP(first)] == CURLY && ARG1(first) > 0) ||
5321    (OP(first) == NOTHING && PL_regkind[OP(first_next)] != END ))
5322   {
5323     /*
5324     * the only op that could be a regnode is PLUS, all the rest
5325     * will be regnode_1 or regnode_2.
5326     *
5327     */
5328     if (OP(first) == PLUS)
5329      sawplus = 1;
5330     else
5331      first += regarglen[OP(first)];
5332
5333     first = NEXTOPER(first);
5334     first_next= regnext(first);
5335   }
5336
5337   /* Starting-point info. */
5338  again:
5339   DEBUG_PEEP("first:",first,0);
5340   /* Ignore EXACT as we deal with it later. */
5341   if (PL_regkind[OP(first)] == EXACT) {
5342    if (OP(first) == EXACT)
5343     NOOP; /* Empty, get anchored substr later. */
5344    else
5345     ri->regstclass = first;
5346   }
5347 #ifdef TRIE_STCLASS
5348   else if (PL_regkind[OP(first)] == TRIE &&
5349     ((reg_trie_data *)ri->data->data[ ARG(first) ])->minlen>0)
5350   {
5351    regnode *trie_op;
5352    /* this can happen only on restudy */
5353    if ( OP(first) == TRIE ) {
5354     struct regnode_1 *trieop = (struct regnode_1 *)
5355      PerlMemShared_calloc(1, sizeof(struct regnode_1));
5356     StructCopy(first,trieop,struct regnode_1);
5357     trie_op=(regnode *)trieop;
5358    } else {
5359     struct regnode_charclass *trieop = (struct regnode_charclass *)
5360      PerlMemShared_calloc(1, sizeof(struct regnode_charclass));
5361     StructCopy(first,trieop,struct regnode_charclass);
5362     trie_op=(regnode *)trieop;
5363    }
5364    OP(trie_op)+=2;
5365    make_trie_failtable(pRExC_state, (regnode *)first, trie_op, 0);
5366    ri->regstclass = trie_op;
5367   }
5368 #endif
5369   else if (REGNODE_SIMPLE(OP(first)))
5370    ri->regstclass = first;
5371   else if (PL_regkind[OP(first)] == BOUND ||
5372     PL_regkind[OP(first)] == NBOUND)
5373    ri->regstclass = first;
5374   else if (PL_regkind[OP(first)] == BOL) {
5375    r->extflags |= (OP(first) == MBOL
5376       ? RXf_ANCH_MBOL
5377       : (OP(first) == SBOL
5378        ? RXf_ANCH_SBOL
5379        : RXf_ANCH_BOL));
5380    first = NEXTOPER(first);
5381    goto again;
5382   }
5383   else if (OP(first) == GPOS) {
5384    r->extflags |= RXf_ANCH_GPOS;
5385    first = NEXTOPER(first);
5386    goto again;
5387   }
5388   else if ((!sawopen || !RExC_sawback) &&
5389    (OP(first) == STAR &&
5390    PL_regkind[OP(NEXTOPER(first))] == REG_ANY) &&
5391    !(r->extflags & RXf_ANCH) && !(RExC_seen & REG_SEEN_EVAL))
5392   {
5393    /* turn .* into ^.* with an implied $*=1 */
5394    const int type =
5395     (OP(NEXTOPER(first)) == REG_ANY)
5396      ? RXf_ANCH_MBOL
5397      : RXf_ANCH_SBOL;
5398    r->extflags |= type;
5399    r->intflags |= PREGf_IMPLICIT;
5400    first = NEXTOPER(first);
5401    goto again;
5402   }
5403   if (sawplus && !sawlookahead && (!sawopen || !RExC_sawback)
5404    && !(RExC_seen & REG_SEEN_EVAL)) /* May examine pos and $& */
5405    /* x+ must match at the 1st pos of run of x's */
5406    r->intflags |= PREGf_SKIP;
5407
5408   /* Scan is after the zeroth branch, first is atomic matcher. */
5409 #ifdef TRIE_STUDY_OPT
5410   DEBUG_PARSE_r(
5411    if (!restudied)
5412     PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n",
5413        (IV)(first - scan + 1))
5414   );
5415 #else
5416   DEBUG_PARSE_r(
5417    PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n",
5418     (IV)(first - scan + 1))
5419   );
5420 #endif
5421
5422
5423   /*
5424   * If there's something expensive in the r.e., find the
5425   * longest literal string that must appear and make it the
5426   * regmust.  Resolve ties in favor of later strings, since
5427   * the regstart check works with the beginning of the r.e.
5428   * and avoiding duplication strengthens checking.  Not a
5429   * strong reason, but sufficient in the absence of others.
5430   * [Now we resolve ties in favor of the earlier string if
5431   * it happens that c_offset_min has been invalidated, since the
5432   * earlier string may buy us something the later one won't.]
5433   */
5434
5435   data.longest_fixed = newSVpvs("");
5436   data.longest_float = newSVpvs("");
5437   data.last_found = newSVpvs("");
5438   data.longest = &(data.longest_fixed);
5439   first = scan;
5440   if (!ri->regstclass) {
5441    cl_init(pRExC_state, &ch_class);
5442    data.start_class = &ch_class;
5443    stclass_flag = SCF_DO_STCLASS_AND;
5444   } else    /* XXXX Check for BOUND? */
5445    stclass_flag = 0;
5446   data.last_closep = &last_close;
5447
5448   minlen = study_chunk(pRExC_state, &first, &minlen, &fake, scan + RExC_size, /* Up to end */
5449    &data, -1, NULL, NULL,
5450    SCF_DO_SUBSTR | SCF_WHILEM_VISITED_POS | stclass_flag,0);
5451
5452
5453   CHECK_RESTUDY_GOTO;
5454
5455
5456   if ( RExC_npar == 1 && data.longest == &(data.longest_fixed)
5457    && data.last_start_min == 0 && data.last_end > 0
5458    && !RExC_seen_zerolen
5459    && !(RExC_seen & REG_SEEN_VERBARG)
5460    && (!(RExC_seen & REG_SEEN_GPOS) || (r->extflags & RXf_ANCH_GPOS)))
5461    r->extflags |= RXf_CHECK_ALL;
5462   scan_commit(pRExC_state, &data,&minlen,0);
5463   SvREFCNT_dec(data.last_found);
5464
5465   /* Note that code very similar to this but for anchored string
5466   follows immediately below, changes may need to be made to both.
5467   Be careful.
5468   */
5469   longest_float_length = CHR_SVLEN(data.longest_float);
5470   if (longest_float_length
5471    || (data.flags & SF_FL_BEFORE_EOL
5472     && (!(data.flags & SF_FL_BEFORE_MEOL)
5473      || (RExC_flags & RXf_PMf_MULTILINE))))
5474   {
5475    I32 t,ml;
5476
5477    /* See comments for join_exact for why REG_SEEN_EXACTF_SHARP_S */
5478    if ((RExC_seen & REG_SEEN_EXACTF_SHARP_S)
5479     || (SvCUR(data.longest_fixed)  /* ok to leave SvCUR */
5480      && data.offset_fixed == data.offset_float_min
5481      && SvCUR(data.longest_fixed) == SvCUR(data.longest_float)))
5482      goto remove_float;  /* As in (a)+. */
5483
5484    /* copy the information about the longest float from the reg_scan_data
5485    over to the program. */
5486    if (SvUTF8(data.longest_float)) {
5487     r->float_utf8 = data.longest_float;
5488     r->float_substr = NULL;
5489    } else {
5490     r->float_substr = data.longest_float;
5491     r->float_utf8 = NULL;
5492    }
5493    /* float_end_shift is how many chars that must be matched that
5494    follow this item. We calculate it ahead of time as once the
5495    lookbehind offset is added in we lose the ability to correctly
5496    calculate it.*/
5497    ml = data.minlen_float ? *(data.minlen_float)
5498         : (I32)longest_float_length;
5499    r->float_end_shift = ml - data.offset_float_min
5500     - longest_float_length + (SvTAIL(data.longest_float) != 0)
5501     + data.lookbehind_float;
5502    r->float_min_offset = data.offset_float_min - data.lookbehind_float;
5503    r->float_max_offset = data.offset_float_max;
5504    if (data.offset_float_max < I32_MAX) /* Don't offset infinity */
5505     r->float_max_offset -= data.lookbehind_float;
5506
5507    t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
5508      && (!(data.flags & SF_FL_BEFORE_MEOL)
5509       || (RExC_flags & RXf_PMf_MULTILINE)));
5510    fbm_compile(data.longest_float, t ? FBMcf_TAIL : 0);
5511   }
5512   else {
5513   remove_float:
5514    r->float_substr = r->float_utf8 = NULL;
5515    SvREFCNT_dec(data.longest_float);
5516    longest_float_length = 0;
5517   }
5518
5519   /* Note that code very similar to this but for floating string
5520   is immediately above, changes may need to be made to both.
5521   Be careful.
5522   */
5523   longest_fixed_length = CHR_SVLEN(data.longest_fixed);
5524
5525   /* See comments for join_exact for why REG_SEEN_EXACTF_SHARP_S */
5526   if (! (RExC_seen & REG_SEEN_EXACTF_SHARP_S)
5527    && (longest_fixed_length
5528     || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
5529      && (!(data.flags & SF_FIX_BEFORE_MEOL)
5530       || (RExC_flags & RXf_PMf_MULTILINE)))) )
5531   {
5532    I32 t,ml;
5533
5534    /* copy the information about the longest fixed
5535    from the reg_scan_data over to the program. */
5536    if (SvUTF8(data.longest_fixed)) {
5537     r->anchored_utf8 = data.longest_fixed;
5538     r->anchored_substr = NULL;
5539    } else {
5540     r->anchored_substr = data.longest_fixed;
5541     r->anchored_utf8 = NULL;
5542    }
5543    /* fixed_end_shift is how many chars that must be matched that
5544    follow this item. We calculate it ahead of time as once the
5545    lookbehind offset is added in we lose the ability to correctly
5546    calculate it.*/
5547    ml = data.minlen_fixed ? *(data.minlen_fixed)
5548         : (I32)longest_fixed_length;
5549    r->anchored_end_shift = ml - data.offset_fixed
5550     - longest_fixed_length + (SvTAIL(data.longest_fixed) != 0)
5551     + data.lookbehind_fixed;
5552    r->anchored_offset = data.offset_fixed - data.lookbehind_fixed;
5553
5554    t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
5555     && (!(data.flags & SF_FIX_BEFORE_MEOL)
5556      || (RExC_flags & RXf_PMf_MULTILINE)));
5557    fbm_compile(data.longest_fixed, t ? FBMcf_TAIL : 0);
5558   }
5559   else {
5560    r->anchored_substr = r->anchored_utf8 = NULL;
5561    SvREFCNT_dec(data.longest_fixed);
5562    longest_fixed_length = 0;
5563   }
5564   if (ri->regstclass
5565    && (OP(ri->regstclass) == REG_ANY || OP(ri->regstclass) == SANY))
5566    ri->regstclass = NULL;
5567
5568   if ((!(r->anchored_substr || r->anchored_utf8) || r->anchored_offset)
5569    && stclass_flag
5570    && !(data.start_class->flags & ANYOF_EOS)
5571    && !cl_is_anything(data.start_class))
5572   {
5573    const U32 n = add_data(pRExC_state, 1, "f");
5574    data.start_class->flags |= ANYOF_IS_SYNTHETIC;
5575
5576    Newx(RExC_rxi->data->data[n], 1,
5577     struct regnode_charclass_class);
5578    StructCopy(data.start_class,
5579      (struct regnode_charclass_class*)RExC_rxi->data->data[n],
5580      struct regnode_charclass_class);
5581    ri->regstclass = (regnode*)RExC_rxi->data->data[n];
5582    r->intflags &= ~PREGf_SKIP; /* Used in find_byclass(). */
5583    DEBUG_COMPILE_r({ SV *sv = sv_newmortal();
5584      regprop(r, sv, (regnode*)data.start_class);
5585      PerlIO_printf(Perl_debug_log,
5586          "synthetic stclass \"%s\".\n",
5587          SvPVX_const(sv));});
5588   }
5589
5590   /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
5591   if (longest_fixed_length > longest_float_length) {
5592    r->check_end_shift = r->anchored_end_shift;
5593    r->check_substr = r->anchored_substr;
5594    r->check_utf8 = r->anchored_utf8;
5595    r->check_offset_min = r->check_offset_max = r->anchored_offset;
5596    if (r->extflags & RXf_ANCH_SINGLE)
5597     r->extflags |= RXf_NOSCAN;
5598   }
5599   else {
5600    r->check_end_shift = r->float_end_shift;
5601    r->check_substr = r->float_substr;
5602    r->check_utf8 = r->float_utf8;
5603    r->check_offset_min = r->float_min_offset;
5604    r->check_offset_max = r->float_max_offset;
5605   }
5606   /* XXXX Currently intuiting is not compatible with ANCH_GPOS.
5607   This should be changed ASAP!  */
5608   if ((r->check_substr || r->check_utf8) && !(r->extflags & RXf_ANCH_GPOS)) {
5609    r->extflags |= RXf_USE_INTUIT;
5610    if (SvTAIL(r->check_substr ? r->check_substr : r->check_utf8))
5611     r->extflags |= RXf_INTUIT_TAIL;
5612   }
5613   /* XXX Unneeded? dmq (shouldn't as this is handled elsewhere)
5614   if ( (STRLEN)minlen < longest_float_length )
5615    minlen= longest_float_length;
5616   if ( (STRLEN)minlen < longest_fixed_length )
5617    minlen= longest_fixed_length;
5618   */
5619  }
5620  else {
5621   /* Several toplevels. Best we can is to set minlen. */
5622   I32 fake;
5623   struct regnode_charclass_class ch_class;
5624   I32 last_close = 0;
5625
5626   DEBUG_PARSE_r(PerlIO_printf(Perl_debug_log, "\nMulti Top Level\n"));
5627
5628   scan = ri->program + 1;
5629   cl_init(pRExC_state, &ch_class);
5630   data.start_class = &ch_class;
5631   data.last_closep = &last_close;
5632
5633
5634   minlen = study_chunk(pRExC_state, &scan, &minlen, &fake, scan + RExC_size,
5635    &data, -1, NULL, NULL, SCF_DO_STCLASS_AND|SCF_WHILEM_VISITED_POS,0);
5636
5637   CHECK_RESTUDY_GOTO;
5638
5639   r->check_substr = r->check_utf8 = r->anchored_substr = r->anchored_utf8
5640     = r->float_substr = r->float_utf8 = NULL;
5641
5642   if (!(data.start_class->flags & ANYOF_EOS)
5643    && !cl_is_anything(data.start_class))
5644   {
5645    const U32 n = add_data(pRExC_state, 1, "f");
5646    data.start_class->flags |= ANYOF_IS_SYNTHETIC;
5647
5648    Newx(RExC_rxi->data->data[n], 1,
5649     struct regnode_charclass_class);
5650    StructCopy(data.start_class,
5651      (struct regnode_charclass_class*)RExC_rxi->data->data[n],
5652      struct regnode_charclass_class);
5653    ri->regstclass = (regnode*)RExC_rxi->data->data[n];
5654    r->intflags &= ~PREGf_SKIP; /* Used in find_byclass(). */
5655    DEBUG_COMPILE_r({ SV* sv = sv_newmortal();
5656      regprop(r, sv, (regnode*)data.start_class);
5657      PerlIO_printf(Perl_debug_log,
5658          "synthetic stclass \"%s\".\n",
5659          SvPVX_const(sv));});
5660   }
5661  }
5662
5663  /* Guard against an embedded (?=) or (?<=) with a longer minlen than
5664  the "real" pattern. */
5665  DEBUG_OPTIMISE_r({
5666   PerlIO_printf(Perl_debug_log,"minlen: %"IVdf" r->minlen:%"IVdf"\n",
5667      (IV)minlen, (IV)r->minlen);
5668  });
5669  r->minlenret = minlen;
5670  if (r->minlen < minlen)
5671   r->minlen = minlen;
5672
5673  if (RExC_seen & REG_SEEN_GPOS)
5674   r->extflags |= RXf_GPOS_SEEN;
5675  if (RExC_seen & REG_SEEN_LOOKBEHIND)
5676   r->extflags |= RXf_LOOKBEHIND_SEEN;
5677  if (RExC_seen & REG_SEEN_EVAL)
5678   r->extflags |= RXf_EVAL_SEEN;
5679  if (RExC_seen & REG_SEEN_CANY)
5680   r->extflags |= RXf_CANY_SEEN;
5681  if (RExC_seen & REG_SEEN_VERBARG)
5682   r->intflags |= PREGf_VERBARG_SEEN;
5683  if (RExC_seen & REG_SEEN_CUTGROUP)
5684   r->intflags |= PREGf_CUTGROUP_SEEN;
5685  if (RExC_paren_names)
5686   RXp_PAREN_NAMES(r) = MUTABLE_HV(SvREFCNT_inc(RExC_paren_names));
5687  else
5688   RXp_PAREN_NAMES(r) = NULL;
5689
5690 #ifdef STUPID_PATTERN_CHECKS
5691  if (RX_PRELEN(rx) == 0)
5692   r->extflags |= RXf_NULL;
5693  if (r->extflags & RXf_SPLIT && RX_PRELEN(rx) == 1 && RX_PRECOMP(rx)[0] == ' ')
5694   /* XXX: this should happen BEFORE we compile */
5695   r->extflags |= (RXf_SKIPWHITE|RXf_WHITE);
5696  else if (RX_PRELEN(rx) == 3 && memEQ("\\s+", RX_PRECOMP(rx), 3))
5697   r->extflags |= RXf_WHITE;
5698  else if (RX_PRELEN(rx) == 1 && RXp_PRECOMP(rx)[0] == '^')
5699   r->extflags |= RXf_START_ONLY;
5700 #else
5701  if (r->extflags & RXf_SPLIT && RX_PRELEN(rx) == 1 && RX_PRECOMP(rx)[0] == ' ')
5702    /* XXX: this should happen BEFORE we compile */
5703    r->extflags |= (RXf_SKIPWHITE|RXf_WHITE);
5704  else {
5705   regnode *first = ri->program + 1;
5706   U8 fop = OP(first);
5707
5708   if (PL_regkind[fop] == NOTHING && OP(NEXTOPER(first)) == END)
5709    r->extflags |= RXf_NULL;
5710   else if (PL_regkind[fop] == BOL && OP(NEXTOPER(first)) == END)
5711    r->extflags |= RXf_START_ONLY;
5712   else if (fop == PLUS && OP(NEXTOPER(first)) == SPACE
5713        && OP(regnext(first)) == END)
5714    r->extflags |= RXf_WHITE;
5715  }
5716 #endif
5717 #ifdef DEBUGGING
5718  if (RExC_paren_names) {
5719   ri->name_list_idx = add_data( pRExC_state, 1, "a" );
5720   ri->data->data[ri->name_list_idx] = (void*)SvREFCNT_inc(RExC_paren_name_list);
5721  } else
5722 #endif
5723   ri->name_list_idx = 0;
5724
5725  if (RExC_recurse_count) {
5726   for ( ; RExC_recurse_count ; RExC_recurse_count-- ) {
5727    const regnode *scan = RExC_recurse[RExC_recurse_count-1];
5728    ARG2L_SET( scan, RExC_open_parens[ARG(scan)-1] - scan );
5729   }
5730  }
5731  Newxz(r->offs, RExC_npar, regexp_paren_pair);
5732  /* assume we don't need to swap parens around before we match */
5733
5734  DEBUG_DUMP_r({
5735   PerlIO_printf(Perl_debug_log,"Final program:\n");
5736   regdump(r);
5737  });
5738 #ifdef RE_TRACK_PATTERN_OFFSETS
5739  DEBUG_OFFSETS_r(if (ri->u.offsets) {
5740   const U32 len = ri->u.offsets[0];
5741   U32 i;
5742   GET_RE_DEBUG_FLAGS_DECL;
5743   PerlIO_printf(Perl_debug_log, "Offsets: [%"UVuf"]\n\t", (UV)ri->u.offsets[0]);
5744   for (i = 1; i <= len; i++) {
5745    if (ri->u.offsets[i*2-1] || ri->u.offsets[i*2])
5746     PerlIO_printf(Perl_debug_log, "%"UVuf":%"UVuf"[%"UVuf"] ",
5747     (UV)i, (UV)ri->u.offsets[i*2-1], (UV)ri->u.offsets[i*2]);
5748    }
5749   PerlIO_printf(Perl_debug_log, "\n");
5750  });
5751 #endif
5752  return rx;
5753 }
5754
5755 #undef RE_ENGINE_PTR
5756
5757
5758 SV*
5759 Perl_reg_named_buff(pTHX_ REGEXP * const rx, SV * const key, SV * const value,
5760      const U32 flags)
5761 {
5762  PERL_ARGS_ASSERT_REG_NAMED_BUFF;
5763
5764  PERL_UNUSED_ARG(value);
5765
5766  if (flags & RXapif_FETCH) {
5767   return reg_named_buff_fetch(rx, key, flags);
5768  } else if (flags & (RXapif_STORE | RXapif_DELETE | RXapif_CLEAR)) {
5769   Perl_croak_no_modify(aTHX);
5770   return NULL;
5771  } else if (flags & RXapif_EXISTS) {
5772   return reg_named_buff_exists(rx, key, flags)
5773    ? &PL_sv_yes
5774    : &PL_sv_no;
5775  } else if (flags & RXapif_REGNAMES) {
5776   return reg_named_buff_all(rx, flags);
5777  } else if (flags & (RXapif_SCALAR | RXapif_REGNAMES_COUNT)) {
5778   return reg_named_buff_scalar(rx, flags);
5779  } else {
5780   Perl_croak(aTHX_ "panic: Unknown flags %d in named_buff", (int)flags);
5781   return NULL;
5782  }
5783 }
5784
5785 SV*
5786 Perl_reg_named_buff_iter(pTHX_ REGEXP * const rx, const SV * const lastkey,
5787       const U32 flags)
5788 {
5789  PERL_ARGS_ASSERT_REG_NAMED_BUFF_ITER;
5790  PERL_UNUSED_ARG(lastkey);
5791
5792  if (flags & RXapif_FIRSTKEY)
5793   return reg_named_buff_firstkey(rx, flags);
5794  else if (flags & RXapif_NEXTKEY)
5795   return reg_named_buff_nextkey(rx, flags);
5796  else {
5797   Perl_croak(aTHX_ "panic: Unknown flags %d in named_buff_iter", (int)flags);
5798   return NULL;
5799  }
5800 }
5801
5802 SV*
5803 Perl_reg_named_buff_fetch(pTHX_ REGEXP * const r, SV * const namesv,
5804       const U32 flags)
5805 {
5806  AV *retarray = NULL;
5807  SV *ret;
5808  struct regexp *const rx = (struct regexp *)SvANY(r);
5809
5810  PERL_ARGS_ASSERT_REG_NAMED_BUFF_FETCH;
5811
5812  if (flags & RXapif_ALL)
5813   retarray=newAV();
5814
5815  if (rx && RXp_PAREN_NAMES(rx)) {
5816   HE *he_str = hv_fetch_ent( RXp_PAREN_NAMES(rx), namesv, 0, 0 );
5817   if (he_str) {
5818    IV i;
5819    SV* sv_dat=HeVAL(he_str);
5820    I32 *nums=(I32*)SvPVX(sv_dat);
5821    for ( i=0; i<SvIVX(sv_dat); i++ ) {
5822     if ((I32)(rx->nparens) >= nums[i]
5823      && rx->offs[nums[i]].start != -1
5824      && rx->offs[nums[i]].end != -1)
5825     {
5826      ret = newSVpvs("");
5827      CALLREG_NUMBUF_FETCH(r,nums[i],ret);
5828      if (!retarray)
5829       return ret;
5830     } else {
5831      if (retarray)
5832       ret = newSVsv(&PL_sv_undef);
5833     }
5834     if (retarray)
5835      av_push(retarray, ret);
5836    }
5837    if (retarray)
5838     return newRV_noinc(MUTABLE_SV(retarray));
5839   }
5840  }
5841  return NULL;
5842 }
5843
5844 bool
5845 Perl_reg_named_buff_exists(pTHX_ REGEXP * const r, SV * const key,
5846       const U32 flags)
5847 {
5848  struct regexp *const rx = (struct regexp *)SvANY(r);
5849
5850  PERL_ARGS_ASSERT_REG_NAMED_BUFF_EXISTS;
5851
5852  if (rx && RXp_PAREN_NAMES(rx)) {
5853   if (flags & RXapif_ALL) {
5854    return hv_exists_ent(RXp_PAREN_NAMES(rx), key, 0);
5855   } else {
5856    SV *sv = CALLREG_NAMED_BUFF_FETCH(r, key, flags);
5857    if (sv) {
5858     SvREFCNT_dec(sv);
5859     return TRUE;
5860    } else {
5861     return FALSE;
5862    }
5863   }
5864  } else {
5865   return FALSE;
5866  }
5867 }
5868
5869 SV*
5870 Perl_reg_named_buff_firstkey(pTHX_ REGEXP * const r, const U32 flags)
5871 {
5872  struct regexp *const rx = (struct regexp *)SvANY(r);
5873
5874  PERL_ARGS_ASSERT_REG_NAMED_BUFF_FIRSTKEY;
5875
5876  if ( rx && RXp_PAREN_NAMES(rx) ) {
5877   (void)hv_iterinit(RXp_PAREN_NAMES(rx));
5878
5879   return CALLREG_NAMED_BUFF_NEXTKEY(r, NULL, flags & ~RXapif_FIRSTKEY);
5880  } else {
5881   return FALSE;
5882  }
5883 }
5884
5885 SV*
5886 Perl_reg_named_buff_nextkey(pTHX_ REGEXP * const r, const U32 flags)
5887 {
5888  struct regexp *const rx = (struct regexp *)SvANY(r);
5889  GET_RE_DEBUG_FLAGS_DECL;
5890
5891  PERL_ARGS_ASSERT_REG_NAMED_BUFF_NEXTKEY;
5892
5893  if (rx && RXp_PAREN_NAMES(rx)) {
5894   HV *hv = RXp_PAREN_NAMES(rx);
5895   HE *temphe;
5896   while ( (temphe = hv_iternext_flags(hv,0)) ) {
5897    IV i;
5898    IV parno = 0;
5899    SV* sv_dat = HeVAL(temphe);
5900    I32 *nums = (I32*)SvPVX(sv_dat);
5901    for ( i = 0; i < SvIVX(sv_dat); i++ ) {
5902     if ((I32)(rx->lastparen) >= nums[i] &&
5903      rx->offs[nums[i]].start != -1 &&
5904      rx->offs[nums[i]].end != -1)
5905     {
5906      parno = nums[i];
5907      break;
5908     }
5909    }
5910    if (parno || flags & RXapif_ALL) {
5911     return newSVhek(HeKEY_hek(temphe));
5912    }
5913   }
5914  }
5915  return NULL;
5916 }
5917
5918 SV*
5919 Perl_reg_named_buff_scalar(pTHX_ REGEXP * const r, const U32 flags)
5920 {
5921  SV *ret;
5922  AV *av;
5923  I32 length;
5924  struct regexp *const rx = (struct regexp *)SvANY(r);
5925
5926  PERL_ARGS_ASSERT_REG_NAMED_BUFF_SCALAR;
5927
5928  if (rx && RXp_PAREN_NAMES(rx)) {
5929   if (flags & (RXapif_ALL | RXapif_REGNAMES_COUNT)) {
5930    return newSViv(HvTOTALKEYS(RXp_PAREN_NAMES(rx)));
5931   } else if (flags & RXapif_ONE) {
5932    ret = CALLREG_NAMED_BUFF_ALL(r, (flags | RXapif_REGNAMES));
5933    av = MUTABLE_AV(SvRV(ret));
5934    length = av_len(av);
5935    SvREFCNT_dec(ret);
5936    return newSViv(length + 1);
5937   } else {
5938    Perl_croak(aTHX_ "panic: Unknown flags %d in named_buff_scalar", (int)flags);
5939    return NULL;
5940   }
5941  }
5942  return &PL_sv_undef;
5943 }
5944
5945 SV*
5946 Perl_reg_named_buff_all(pTHX_ REGEXP * const r, const U32 flags)
5947 {
5948  struct regexp *const rx = (struct regexp *)SvANY(r);
5949  AV *av = newAV();
5950
5951  PERL_ARGS_ASSERT_REG_NAMED_BUFF_ALL;
5952
5953  if (rx && RXp_PAREN_NAMES(rx)) {
5954   HV *hv= RXp_PAREN_NAMES(rx);
5955   HE *temphe;
5956   (void)hv_iterinit(hv);
5957   while ( (temphe = hv_iternext_flags(hv,0)) ) {
5958    IV i;
5959    IV parno = 0;
5960    SV* sv_dat = HeVAL(temphe);
5961    I32 *nums = (I32*)SvPVX(sv_dat);
5962    for ( i = 0; i < SvIVX(sv_dat); i++ ) {
5963     if ((I32)(rx->lastparen) >= nums[i] &&
5964      rx->offs[nums[i]].start != -1 &&
5965      rx->offs[nums[i]].end != -1)
5966     {
5967      parno = nums[i];
5968      break;
5969     }
5970    }
5971    if (parno || flags & RXapif_ALL) {
5972     av_push(av, newSVhek(HeKEY_hek(temphe)));
5973    }
5974   }
5975  }
5976
5977  return newRV_noinc(MUTABLE_SV(av));
5978 }
5979
5980 void
5981 Perl_reg_numbered_buff_fetch(pTHX_ REGEXP * const r, const I32 paren,
5982        SV * const sv)
5983 {
5984  struct regexp *const rx = (struct regexp *)SvANY(r);
5985  char *s = NULL;
5986  I32 i = 0;
5987  I32 s1, t1;
5988
5989  PERL_ARGS_ASSERT_REG_NUMBERED_BUFF_FETCH;
5990
5991  if (!rx->subbeg) {
5992   sv_setsv(sv,&PL_sv_undef);
5993   return;
5994  }
5995  else
5996  if (paren == RX_BUFF_IDX_PREMATCH && rx->offs[0].start != -1) {
5997   /* $` */
5998   i = rx->offs[0].start;
5999   s = rx->subbeg;
6000  }
6001  else
6002  if (paren == RX_BUFF_IDX_POSTMATCH && rx->offs[0].end != -1) {
6003   /* $' */
6004   s = rx->subbeg + rx->offs[0].end;
6005   i = rx->sublen - rx->offs[0].end;
6006  }
6007  else
6008  if ( 0 <= paren && paren <= (I32)rx->nparens &&
6009   (s1 = rx->offs[paren].start) != -1 &&
6010   (t1 = rx->offs[paren].end) != -1)
6011  {
6012   /* $& $1 ... */
6013   i = t1 - s1;
6014   s = rx->subbeg + s1;
6015  } else {
6016   sv_setsv(sv,&PL_sv_undef);
6017   return;
6018  }
6019  assert(rx->sublen >= (s - rx->subbeg) + i );
6020  if (i >= 0) {
6021   const int oldtainted = PL_tainted;
6022   TAINT_NOT;
6023   sv_setpvn(sv, s, i);
6024   PL_tainted = oldtainted;
6025   if ( (rx->extflags & RXf_CANY_SEEN)
6026    ? (RXp_MATCH_UTF8(rx)
6027       && (!i || is_utf8_string((U8*)s, i)))
6028    : (RXp_MATCH_UTF8(rx)) )
6029   {
6030    SvUTF8_on(sv);
6031   }
6032   else
6033    SvUTF8_off(sv);
6034   if (PL_tainting) {
6035    if (RXp_MATCH_TAINTED(rx)) {
6036     if (SvTYPE(sv) >= SVt_PVMG) {
6037      MAGIC* const mg = SvMAGIC(sv);
6038      MAGIC* mgt;
6039      PL_tainted = 1;
6040      SvMAGIC_set(sv, mg->mg_moremagic);
6041      SvTAINT(sv);
6042      if ((mgt = SvMAGIC(sv))) {
6043       mg->mg_moremagic = mgt;
6044       SvMAGIC_set(sv, mg);
6045      }
6046     } else {
6047      PL_tainted = 1;
6048      SvTAINT(sv);
6049     }
6050    } else
6051     SvTAINTED_off(sv);
6052   }
6053  } else {
6054   sv_setsv(sv,&PL_sv_undef);
6055   return;
6056  }
6057 }
6058
6059 void
6060 Perl_reg_numbered_buff_store(pTHX_ REGEXP * const rx, const I32 paren,
6061               SV const * const value)
6062 {
6063  PERL_ARGS_ASSERT_REG_NUMBERED_BUFF_STORE;
6064
6065  PERL_UNUSED_ARG(rx);
6066  PERL_UNUSED_ARG(paren);
6067  PERL_UNUSED_ARG(value);
6068
6069  if (!PL_localizing)
6070   Perl_croak_no_modify(aTHX);
6071 }
6072
6073 I32
6074 Perl_reg_numbered_buff_length(pTHX_ REGEXP * const r, const SV * const sv,
6075        const I32 paren)
6076 {
6077  struct regexp *const rx = (struct regexp *)SvANY(r);
6078  I32 i;
6079  I32 s1, t1;
6080
6081  PERL_ARGS_ASSERT_REG_NUMBERED_BUFF_LENGTH;
6082
6083  /* Some of this code was originally in C<Perl_magic_len> in F<mg.c> */
6084   switch (paren) {
6085  /* $` / ${^PREMATCH} */
6086  case RX_BUFF_IDX_PREMATCH:
6087   if (rx->offs[0].start != -1) {
6088       i = rx->offs[0].start;
6089       if (i > 0) {
6090         s1 = 0;
6091         t1 = i;
6092         goto getlen;
6093       }
6094    }
6095   return 0;
6096  /* $' / ${^POSTMATCH} */
6097  case RX_BUFF_IDX_POSTMATCH:
6098    if (rx->offs[0].end != -1) {
6099       i = rx->sublen - rx->offs[0].end;
6100       if (i > 0) {
6101         s1 = rx->offs[0].end;
6102         t1 = rx->sublen;
6103         goto getlen;
6104       }
6105    }
6106   return 0;
6107  /* $& / ${^MATCH}, $1, $2, ... */
6108  default:
6109    if (paren <= (I32)rx->nparens &&
6110    (s1 = rx->offs[paren].start) != -1 &&
6111    (t1 = rx->offs[paren].end) != -1)
6112    {
6113    i = t1 - s1;
6114    goto getlen;
6115   } else {
6116    if (ckWARN(WARN_UNINITIALIZED))
6117     report_uninit((const SV *)sv);
6118    return 0;
6119   }
6120  }
6121   getlen:
6122  if (i > 0 && RXp_MATCH_UTF8(rx)) {
6123   const char * const s = rx->subbeg + s1;
6124   const U8 *ep;
6125   STRLEN el;
6126
6127   i = t1 - s1;
6128   if (is_utf8_string_loclen((U8*)s, i, &ep, &el))
6129       i = el;
6130  }
6131  return i;
6132 }
6133
6134 SV*
6135 Perl_reg_qr_package(pTHX_ REGEXP * const rx)
6136 {
6137  PERL_ARGS_ASSERT_REG_QR_PACKAGE;
6138   PERL_UNUSED_ARG(rx);
6139   if (0)
6140    return NULL;
6141   else
6142    return newSVpvs("Regexp");
6143 }
6144
6145 /* Scans the name of a named buffer from the pattern.
6146  * If flags is REG_RSN_RETURN_NULL returns null.
6147  * If flags is REG_RSN_RETURN_NAME returns an SV* containing the name
6148  * If flags is REG_RSN_RETURN_DATA returns the data SV* corresponding
6149  * to the parsed name as looked up in the RExC_paren_names hash.
6150  * If there is an error throws a vFAIL().. type exception.
6151  */
6152
6153 #define REG_RSN_RETURN_NULL    0
6154 #define REG_RSN_RETURN_NAME    1
6155 #define REG_RSN_RETURN_DATA    2
6156
6157 STATIC SV*
6158 S_reg_scan_name(pTHX_ RExC_state_t *pRExC_state, U32 flags)
6159 {
6160  char *name_start = RExC_parse;
6161
6162  PERL_ARGS_ASSERT_REG_SCAN_NAME;
6163
6164  if (isIDFIRST_lazy_if(RExC_parse, UTF)) {
6165   /* skip IDFIRST by using do...while */
6166   if (UTF)
6167    do {
6168     RExC_parse += UTF8SKIP(RExC_parse);
6169    } while (isALNUM_utf8((U8*)RExC_parse));
6170   else
6171    do {
6172     RExC_parse++;
6173    } while (isALNUM(*RExC_parse));
6174  }
6175
6176  if ( flags ) {
6177   SV* sv_name
6178    = newSVpvn_flags(name_start, (int)(RExC_parse - name_start),
6179        SVs_TEMP | (UTF ? SVf_UTF8 : 0));
6180   if ( flags == REG_RSN_RETURN_NAME)
6181    return sv_name;
6182   else if (flags==REG_RSN_RETURN_DATA) {
6183    HE *he_str = NULL;
6184    SV *sv_dat = NULL;
6185    if ( ! sv_name )      /* should not happen*/
6186     Perl_croak(aTHX_ "panic: no svname in reg_scan_name");
6187    if (RExC_paren_names)
6188     he_str = hv_fetch_ent( RExC_paren_names, sv_name, 0, 0 );
6189    if ( he_str )
6190     sv_dat = HeVAL(he_str);
6191    if ( ! sv_dat )
6192     vFAIL("Reference to nonexistent named group");
6193    return sv_dat;
6194   }
6195   else {
6196    Perl_croak(aTHX_ "panic: bad flag %lx in reg_scan_name",
6197      (unsigned long) flags);
6198   }
6199   /* NOT REACHED */
6200  }
6201  return NULL;
6202 }
6203
6204 #define DEBUG_PARSE_MSG(funcname)     DEBUG_PARSE_r({           \
6205  int rem=(int)(RExC_end - RExC_parse);                       \
6206  int cut;                                                    \
6207  int num;                                                    \
6208  int iscut=0;                                                \
6209  if (rem>10) {                                               \
6210   rem=10;                                                 \
6211   iscut=1;                                                \
6212  }                                                           \
6213  cut=10-rem;                                                 \
6214  if (RExC_lastparse!=RExC_parse)                             \
6215   PerlIO_printf(Perl_debug_log," >%.*s%-*s",              \
6216    rem, RExC_parse,                                    \
6217    cut + 4,                                            \
6218    iscut ? "..." : "<"                                 \
6219   );                                                      \
6220  else                                                        \
6221   PerlIO_printf(Perl_debug_log,"%16s","");                \
6222                 \
6223  if (SIZE_ONLY)                                              \
6224  num = RExC_size + 1;                                     \
6225  else                                                        \
6226  num=REG_NODE_NUM(RExC_emit);                             \
6227  if (RExC_lastnum!=num)                                      \
6228  PerlIO_printf(Perl_debug_log,"|%4d",num);                \
6229  else                                                        \
6230  PerlIO_printf(Perl_debug_log,"|%4s","");                 \
6231  PerlIO_printf(Perl_debug_log,"|%*s%-4s",                    \
6232   (int)((depth*2)), "",                                   \
6233   (funcname)                                              \
6234  );                                                          \
6235  RExC_lastnum=num;                                           \
6236  RExC_lastparse=RExC_parse;                                  \
6237 })
6238
6239
6240
6241 #define DEBUG_PARSE(funcname)     DEBUG_PARSE_r({           \
6242  DEBUG_PARSE_MSG((funcname));                            \
6243  PerlIO_printf(Perl_debug_log,"%4s","\n");               \
6244 })
6245 #define DEBUG_PARSE_FMT(funcname,fmt,args)     DEBUG_PARSE_r({           \
6246  DEBUG_PARSE_MSG((funcname));                            \
6247  PerlIO_printf(Perl_debug_log,fmt "\n",args);               \
6248 })
6249
6250 /* This section of code defines the inversion list object and its methods.  The
6251  * interfaces are highly subject to change, so as much as possible is static to
6252  * this file.  An inversion list is here implemented as a malloc'd C UV array
6253  * with some added info that is placed as UVs at the beginning in a header
6254  * portion.  An inversion list for Unicode is an array of code points, sorted
6255  * by ordinal number.  The zeroth element is the first code point in the list.
6256  * The 1th element is the first element beyond that not in the list.  In other
6257  * words, the first range is
6258  *  invlist[0]..(invlist[1]-1)
6259  * The other ranges follow.  Thus every element whose index is divisible by two
6260  * marks the beginning of a range that is in the list, and every element not
6261  * divisible by two marks the beginning of a range not in the list.  A single
6262  * element inversion list that contains the single code point N generally
6263  * consists of two elements
6264  *  invlist[0] == N
6265  *  invlist[1] == N+1
6266  * (The exception is when N is the highest representable value on the
6267  * machine, in which case the list containing just it would be a single
6268  * element, itself.  By extension, if the last range in the list extends to
6269  * infinity, then the first element of that range will be in the inversion list
6270  * at a position that is divisible by two, and is the final element in the
6271  * list.)
6272  * Taking the complement (inverting) an inversion list is quite simple, if the
6273  * first element is 0, remove it; otherwise add a 0 element at the beginning.
6274  * This implementation reserves an element at the beginning of each inversion list
6275  * to contain 0 when the list contains 0, and contains 1 otherwise.  The actual
6276  * beginning of the list is either that element if 0, or the next one if 1.
6277  *
6278  * More about inversion lists can be found in "Unicode Demystified"
6279  * Chapter 13 by Richard Gillam, published by Addison-Wesley.
6280  * More will be coming when functionality is added later.
6281  *
6282  * The inversion list data structure is currently implemented as an SV pointing
6283  * to an array of UVs that the SV thinks are bytes.  This allows us to have an
6284  * array of UV whose memory management is automatically handled by the existing
6285  * facilities for SV's.
6286  *
6287  * Some of the methods should always be private to the implementation, and some
6288  * should eventually be made public */
6289
6290 #define INVLIST_LEN_OFFSET 0 /* Number of elements in the inversion list */
6291 #define INVLIST_ITER_OFFSET 1 /* Current iteration position */
6292
6293 /* This is a combination of a version and data structure type, so that one
6294  * being passed in can be validated to be an inversion list of the correct
6295  * vintage.  When the structure of the header is changed, a new random number
6296  * in the range 2**31-1 should be generated and the new() method changed to
6297  * insert that at this location.  Then, if an auxiliary program doesn't change
6298  * correspondingly, it will be discovered immediately */
6299 #define INVLIST_VERSION_ID_OFFSET 2
6300 #define INVLIST_VERSION_ID 1064334010
6301
6302 /* For safety, when adding new elements, remember to #undef them at the end of
6303  * the inversion list code section */
6304
6305 #define INVLIST_ZERO_OFFSET 3 /* 0 or 1; must be last element in header */
6306 /* The UV at position ZERO contains either 0 or 1.  If 0, the inversion list
6307  * contains the code point U+00000, and begins here.  If 1, the inversion list
6308  * doesn't contain U+0000, and it begins at the next UV in the array.
6309  * Inverting an inversion list consists of adding or removing the 0 at the
6310  * beginning of it.  By reserving a space for that 0, inversion can be made
6311  * very fast */
6312
6313 #define HEADER_LENGTH (INVLIST_ZERO_OFFSET + 1)
6314
6315 /* Internally things are UVs */
6316 #define TO_INTERNAL_SIZE(x) ((x + HEADER_LENGTH) * sizeof(UV))
6317 #define FROM_INTERNAL_SIZE(x) ((x / sizeof(UV)) - HEADER_LENGTH)
6318
6319 #define INVLIST_INITIAL_LEN 10
6320
6321 PERL_STATIC_INLINE UV*
6322 S__invlist_array_init(pTHX_ SV* const invlist, const bool will_have_0)
6323 {
6324  /* Returns a pointer to the first element in the inversion list's array.
6325  * This is called upon initialization of an inversion list.  Where the
6326  * array begins depends on whether the list has the code point U+0000
6327  * in it or not.  The other parameter tells it whether the code that
6328  * follows this call is about to put a 0 in the inversion list or not.
6329  * The first element is either the element with 0, if 0, or the next one,
6330  * if 1 */
6331
6332  UV* zero = get_invlist_zero_addr(invlist);
6333
6334  PERL_ARGS_ASSERT__INVLIST_ARRAY_INIT;
6335
6336  /* Must be empty */
6337  assert(! *get_invlist_len_addr(invlist));
6338
6339  /* 1^1 = 0; 1^0 = 1 */
6340  *zero = 1 ^ will_have_0;
6341  return zero + *zero;
6342 }
6343
6344 PERL_STATIC_INLINE UV*
6345 S_invlist_array(pTHX_ SV* const invlist)
6346 {
6347  /* Returns the pointer to the inversion list's array.  Every time the
6348  * length changes, this needs to be called in case malloc or realloc moved
6349  * it */
6350
6351  PERL_ARGS_ASSERT_INVLIST_ARRAY;
6352
6353  /* Must not be empty.  If these fail, you probably didn't check for <len>
6354  * being non-zero before trying to get the array */
6355  assert(*get_invlist_len_addr(invlist));
6356  assert(*get_invlist_zero_addr(invlist) == 0
6357   || *get_invlist_zero_addr(invlist) == 1);
6358
6359  /* The array begins either at the element reserved for zero if the
6360  * list contains 0 (that element will be set to 0), or otherwise the next
6361  * element (in which case the reserved element will be set to 1). */
6362  return (UV *) (get_invlist_zero_addr(invlist)
6363     + *get_invlist_zero_addr(invlist));
6364 }
6365
6366 PERL_STATIC_INLINE UV*
6367 S_get_invlist_len_addr(pTHX_ SV* invlist)
6368 {
6369  /* Return the address of the UV that contains the current number
6370  * of used elements in the inversion list */
6371
6372  PERL_ARGS_ASSERT_GET_INVLIST_LEN_ADDR;
6373
6374  return (UV *) (SvPVX(invlist) + (INVLIST_LEN_OFFSET * sizeof (UV)));
6375 }
6376
6377 PERL_STATIC_INLINE UV
6378 S_invlist_len(pTHX_ SV* const invlist)
6379 {
6380  /* Returns the current number of elements stored in the inversion list's
6381  * array */
6382
6383  PERL_ARGS_ASSERT_INVLIST_LEN;
6384
6385  return *get_invlist_len_addr(invlist);
6386 }
6387
6388 PERL_STATIC_INLINE void
6389 S_invlist_set_len(pTHX_ SV* const invlist, const UV len)
6390 {
6391  /* Sets the current number of elements stored in the inversion list */
6392
6393  PERL_ARGS_ASSERT_INVLIST_SET_LEN;
6394
6395  *get_invlist_len_addr(invlist) = len;
6396
6397  assert(len <= SvLEN(invlist));
6398
6399  SvCUR_set(invlist, TO_INTERNAL_SIZE(len));
6400  /* If the list contains U+0000, that element is part of the header,
6401  * and should not be counted as part of the array.  It will contain
6402  * 0 in that case, and 1 otherwise.  So we could flop 0=>1, 1=>0 and
6403  * subtract:
6404  * SvCUR_set(invlist,
6405  *    TO_INTERNAL_SIZE(len
6406  *       - (*get_invlist_zero_addr(inv_list) ^ 1)));
6407  * But, this is only valid if len is not 0.  The consequences of not doing
6408  * this is that the memory allocation code may think that 1 more UV is
6409  * being used than actually is, and so might do an unnecessary grow.  That
6410  * seems worth not bothering to make this the precise amount.
6411  *
6412  * Note that when inverting, SvCUR shouldn't change */
6413 }
6414
6415 PERL_STATIC_INLINE UV
6416 S_invlist_max(pTHX_ SV* const invlist)
6417 {
6418  /* Returns the maximum number of elements storable in the inversion list's
6419  * array, without having to realloc() */
6420
6421  PERL_ARGS_ASSERT_INVLIST_MAX;
6422
6423  return FROM_INTERNAL_SIZE(SvLEN(invlist));
6424 }
6425
6426 PERL_STATIC_INLINE UV*
6427 S_get_invlist_zero_addr(pTHX_ SV* invlist)
6428 {
6429  /* Return the address of the UV that is reserved to hold 0 if the inversion
6430  * list contains 0.  This has to be the last element of the heading, as the
6431  * list proper starts with either it if 0, or the next element if not.
6432  * (But we force it to contain either 0 or 1) */
6433
6434  PERL_ARGS_ASSERT_GET_INVLIST_ZERO_ADDR;
6435
6436  return (UV *) (SvPVX(invlist) + (INVLIST_ZERO_OFFSET * sizeof (UV)));
6437 }
6438
6439 #ifndef PERL_IN_XSUB_RE
6440 SV*
6441 Perl__new_invlist(pTHX_ IV initial_size)
6442 {
6443
6444  /* Return a pointer to a newly constructed inversion list, with enough
6445  * space to store 'initial_size' elements.  If that number is negative, a
6446  * system default is used instead */
6447
6448  SV* new_list;
6449
6450  if (initial_size < 0) {
6451   initial_size = INVLIST_INITIAL_LEN;
6452  }
6453
6454  /* Allocate the initial space */
6455  new_list = newSV(TO_INTERNAL_SIZE(initial_size));
6456  invlist_set_len(new_list, 0);
6457
6458  /* Force iterinit() to be used to get iteration to work */
6459  *get_invlist_iter_addr(new_list) = UV_MAX;
6460
6461  /* This should force a segfault if a method doesn't initialize this
6462  * properly */
6463  *get_invlist_zero_addr(new_list) = UV_MAX;
6464
6465  *get_invlist_version_id_addr(new_list) = INVLIST_VERSION_ID;
6466 #if HEADER_LENGTH != 4
6467 #   error Need to regenerate VERSION_ID by running perl -E 'say int(rand 2**31-1)', and then changing the #if to the new length
6468 #endif
6469
6470  return new_list;
6471 }
6472 #endif
6473
6474 STATIC SV*
6475 S__new_invlist_C_array(pTHX_ UV* list)
6476 {
6477  /* Return a pointer to a newly constructed inversion list, initialized to
6478  * point to <list>, which has to be in the exact correct inversion list
6479  * form, including internal fields.  Thus this is a dangerous routine that
6480  * should not be used in the wrong hands */
6481
6482  SV* invlist = newSV_type(SVt_PV);
6483
6484  PERL_ARGS_ASSERT__NEW_INVLIST_C_ARRAY;
6485
6486  SvPV_set(invlist, (char *) list);
6487  SvLEN_set(invlist, 0);  /* Means we own the contents, and the system
6488        shouldn't touch it */
6489  SvCUR_set(invlist, TO_INTERNAL_SIZE(invlist_len(invlist)));
6490
6491  if (*get_invlist_version_id_addr(invlist) != INVLIST_VERSION_ID) {
6492   Perl_croak(aTHX_ "panic: Incorrect version for previously generated inversion list");
6493  }
6494
6495  return invlist;
6496 }
6497
6498 STATIC void
6499 S_invlist_extend(pTHX_ SV* const invlist, const UV new_max)
6500 {
6501  /* Grow the maximum size of an inversion list */
6502
6503  PERL_ARGS_ASSERT_INVLIST_EXTEND;
6504
6505  SvGROW((SV *)invlist, TO_INTERNAL_SIZE(new_max));
6506 }
6507
6508 PERL_STATIC_INLINE void
6509 S_invlist_trim(pTHX_ SV* const invlist)
6510 {
6511  PERL_ARGS_ASSERT_INVLIST_TRIM;
6512
6513  /* Change the length of the inversion list to how many entries it currently
6514  * has */
6515
6516  SvPV_shrink_to_cur((SV *) invlist);
6517 }
6518
6519 /* An element is in an inversion list iff its index is even numbered: 0, 2, 4,
6520  * etc */
6521 #define ELEMENT_RANGE_MATCHES_INVLIST(i) (! ((i) & 1))
6522 #define PREV_RANGE_MATCHES_INVLIST(i) (! ELEMENT_RANGE_MATCHES_INVLIST(i))
6523
6524 #define _invlist_union_complement_2nd(a, b, output) _invlist_union_maybe_complement_2nd(a, b, TRUE, output)
6525
6526 STATIC void
6527 S__append_range_to_invlist(pTHX_ SV* const invlist, const UV start, const UV end)
6528 {
6529    /* Subject to change or removal.  Append the range from 'start' to 'end' at
6530  * the end of the inversion list.  The range must be above any existing
6531  * ones. */
6532
6533  UV* array;
6534  UV max = invlist_max(invlist);
6535  UV len = invlist_len(invlist);
6536
6537  PERL_ARGS_ASSERT__APPEND_RANGE_TO_INVLIST;
6538
6539  if (len == 0) { /* Empty lists must be initialized */
6540   array = _invlist_array_init(invlist, start == 0);
6541  }
6542  else {
6543   /* Here, the existing list is non-empty. The current max entry in the
6544   * list is generally the first value not in the set, except when the
6545   * set extends to the end of permissible values, in which case it is
6546   * the first entry in that final set, and so this call is an attempt to
6547   * append out-of-order */
6548
6549   UV final_element = len - 1;
6550   array = invlist_array(invlist);
6551   if (array[final_element] > start
6552    || ELEMENT_RANGE_MATCHES_INVLIST(final_element))
6553   {
6554    Perl_croak(aTHX_ "panic: attempting to append to an inversion list, but wasn't at the end of the list, final=%"UVuf", start=%"UVuf", match=%c",
6555      array[final_element], start,
6556      ELEMENT_RANGE_MATCHES_INVLIST(final_element) ? 't' : 'f');
6557   }
6558
6559   /* Here, it is a legal append.  If the new range begins with the first
6560   * value not in the set, it is extending the set, so the new first
6561   * value not in the set is one greater than the newly extended range.
6562   * */
6563   if (array[final_element] == start) {
6564    if (end != UV_MAX) {
6565     array[final_element] = end + 1;
6566    }
6567    else {
6568     /* But if the end is the maximum representable on the machine,
6569     * just let the range that this would extend to have no end */
6570     invlist_set_len(invlist, len - 1);
6571    }
6572    return;
6573   }
6574  }
6575
6576  /* Here the new range doesn't extend any existing set.  Add it */
6577
6578  len += 2; /* Includes an element each for the start and end of range */
6579
6580  /* If overflows the existing space, extend, which may cause the array to be
6581  * moved */
6582  if (max < len) {
6583   invlist_extend(invlist, len);
6584   invlist_set_len(invlist, len); /* Have to set len here to avoid assert
6585           failure in invlist_array() */
6586   array = invlist_array(invlist);
6587  }
6588  else {
6589   invlist_set_len(invlist, len);
6590  }
6591
6592  /* The next item on the list starts the range, the one after that is
6593  * one past the new range.  */
6594  array[len - 2] = start;
6595  if (end != UV_MAX) {
6596   array[len - 1] = end + 1;
6597  }
6598  else {
6599   /* But if the end is the maximum representable on the machine, just let
6600   * the range have no end */
6601   invlist_set_len(invlist, len - 1);
6602  }
6603 }
6604
6605 #ifndef PERL_IN_XSUB_RE
6606
6607 STATIC IV
6608 S_invlist_search(pTHX_ SV* const invlist, const UV cp)
6609 {
6610  /* Searches the inversion list for the entry that contains the input code
6611  * point <cp>.  If <cp> is not in the list, -1 is returned.  Otherwise, the
6612  * return value is the index into the list's array of the range that
6613  * contains <cp> */
6614
6615  IV low = 0;
6616  IV high = invlist_len(invlist);
6617  const UV * const array = invlist_array(invlist);
6618
6619  PERL_ARGS_ASSERT_INVLIST_SEARCH;
6620
6621  /* If list is empty or the code point is before the first element, return
6622  * failure. */
6623  if (high == 0 || cp < array[0]) {
6624   return -1;
6625  }
6626
6627  /* Binary search.  What we are looking for is <i> such that
6628  * array[i] <= cp < array[i+1]
6629  * The loop below converges on the i+1. */
6630  while (low < high) {
6631   IV mid = (low + high) / 2;
6632   if (array[mid] <= cp) {
6633    low = mid + 1;
6634
6635    /* We could do this extra test to exit the loop early.
6636    if (cp < array[low]) {
6637     return mid;
6638    }
6639    */
6640   }
6641   else { /* cp < array[mid] */
6642    high = mid;
6643   }
6644  }
6645
6646  return high - 1;
6647 }
6648
6649 void
6650 Perl__invlist_populate_swatch(pTHX_ SV* const invlist, const UV start, const UV end, U8* swatch)
6651 {
6652  /* populates a swatch of a swash the same way swatch_get() does in utf8.c,
6653  * but is used when the swash has an inversion list.  This makes this much
6654  * faster, as it uses a binary search instead of a linear one.  This is
6655  * intimately tied to that function, and perhaps should be in utf8.c,
6656  * except it is intimately tied to inversion lists as well.  It assumes
6657  * that <swatch> is all 0's on input */
6658
6659  UV current = start;
6660  const IV len = invlist_len(invlist);
6661  IV i;
6662  const UV * array;
6663
6664  PERL_ARGS_ASSERT__INVLIST_POPULATE_SWATCH;
6665
6666  if (len == 0) { /* Empty inversion list */
6667   return;
6668  }
6669
6670  array = invlist_array(invlist);
6671
6672  /* Find which element it is */
6673  i = invlist_search(invlist, start);
6674
6675  /* We populate from <start> to <end> */
6676  while (current < end) {
6677   UV upper;
6678
6679   /* The inversion list gives the results for every possible code point
6680   * after the first one in the list.  Only those ranges whose index is
6681   * even are ones that the inversion list matches.  For the odd ones,
6682   * and if the initial code point is not in the list, we have to skip
6683   * forward to the next element */
6684   if (i == -1 || ! ELEMENT_RANGE_MATCHES_INVLIST(i)) {
6685    i++;
6686    if (i >= len) { /* Finished if beyond the end of the array */
6687     return;
6688    }
6689    current = array[i];
6690    if (current >= end) {   /* Finished if beyond the end of what we
6691          are populating */
6692     return;
6693    }
6694   }
6695   assert(current >= start);
6696
6697   /* The current range ends one below the next one, except don't go past
6698   * <end> */
6699   i++;
6700   upper = (i < len && array[i] < end) ? array[i] : end;
6701
6702   /* Here we are in a range that matches.  Populate a bit in the 3-bit U8
6703   * for each code point in it */
6704   for (; current < upper; current++) {
6705    const STRLEN offset = (STRLEN)(current - start);
6706    swatch[offset >> 3] |= 1 << (offset & 7);
6707   }
6708
6709   /* Quit if at the end of the list */
6710   if (i >= len) {
6711
6712    /* But first, have to deal with the highest possible code point on
6713    * the platform.  The previous code assumes that <end> is one
6714    * beyond where we want to populate, but that is impossible at the
6715    * platform's infinity, so have to handle it specially */
6716    if (UNLIKELY(end == UV_MAX && ELEMENT_RANGE_MATCHES_INVLIST(len-1)))
6717    {
6718     const STRLEN offset = (STRLEN)(end - start);
6719     swatch[offset >> 3] |= 1 << (offset & 7);
6720    }
6721    return;
6722   }
6723
6724   /* Advance to the next range, which will be for code points not in the
6725   * inversion list */
6726   current = array[i];
6727  }
6728
6729  return;
6730 }
6731
6732
6733 void
6734 Perl__invlist_union_maybe_complement_2nd(pTHX_ SV* const a, SV* const b, bool complement_b, SV** output)
6735 {
6736  /* Take the union of two inversion lists and point <output> to it.  *output
6737  * should be defined upon input, and if it points to one of the two lists,
6738  * the reference count to that list will be decremented.  The first list,
6739  * <a>, may be NULL, in which case a copy of the second list is returned.
6740  * If <complement_b> is TRUE, the union is taken of the complement
6741  * (inversion) of <b> instead of b itself.
6742  *
6743  * The basis for this comes from "Unicode Demystified" Chapter 13 by
6744  * Richard Gillam, published by Addison-Wesley, and explained at some
6745  * length there.  The preface says to incorporate its examples into your
6746  * code at your own risk.
6747  *
6748  * The algorithm is like a merge sort.
6749  *
6750  * XXX A potential performance improvement is to keep track as we go along
6751  * if only one of the inputs contributes to the result, meaning the other
6752  * is a subset of that one.  In that case, we can skip the final copy and
6753  * return the larger of the input lists, but then outside code might need
6754  * to keep track of whether to free the input list or not */
6755
6756  UV* array_a;    /* a's array */
6757  UV* array_b;
6758  UV len_a;     /* length of a's array */
6759  UV len_b;
6760
6761  SV* u;   /* the resulting union */
6762  UV* array_u;
6763  UV len_u;
6764
6765  UV i_a = 0;      /* current index into a's array */
6766  UV i_b = 0;
6767  UV i_u = 0;
6768
6769  /* running count, as explained in the algorithm source book; items are
6770  * stopped accumulating and are output when the count changes to/from 0.
6771  * The count is incremented when we start a range that's in the set, and
6772  * decremented when we start a range that's not in the set.  So its range
6773  * is 0 to 2.  Only when the count is zero is something not in the set.
6774  */
6775  UV count = 0;
6776
6777  PERL_ARGS_ASSERT__INVLIST_UNION_MAYBE_COMPLEMENT_2ND;
6778  assert(a != b);
6779
6780  /* If either one is empty, the union is the other one */
6781  if (a == NULL || ((len_a = invlist_len(a)) == 0)) {
6782   if (*output == a) {
6783    if (a != NULL) {
6784     SvREFCNT_dec(a);
6785    }
6786   }
6787   if (*output != b) {
6788    *output = invlist_clone(b);
6789    if (complement_b) {
6790     _invlist_invert(*output);
6791    }
6792   } /* else *output already = b; */
6793   return;
6794  }
6795  else if ((len_b = invlist_len(b)) == 0) {
6796   if (*output == b) {
6797    SvREFCNT_dec(b);
6798   }
6799
6800   /* The complement of an empty list is a list that has everything in it,
6801   * so the union with <a> includes everything too */
6802   if (complement_b) {
6803    if (a == *output) {
6804     SvREFCNT_dec(a);
6805    }
6806    *output = _new_invlist(1);
6807    _append_range_to_invlist(*output, 0, UV_MAX);
6808   }
6809   else if (*output != a) {
6810    *output = invlist_clone(a);
6811   }
6812   /* else *output already = a; */
6813   return;
6814  }
6815
6816  /* Here both lists exist and are non-empty */
6817  array_a = invlist_array(a);
6818  array_b = invlist_array(b);
6819
6820  /* If are to take the union of 'a' with the complement of b, set it
6821  * up so are looking at b's complement. */
6822  if (complement_b) {
6823
6824   /* To complement, we invert: if the first element is 0, remove it.  To
6825   * do this, we just pretend the array starts one later, and clear the
6826   * flag as we don't have to do anything else later */
6827   if (array_b[0] == 0) {
6828    array_b++;
6829    len_b--;
6830    complement_b = FALSE;
6831   }
6832   else {
6833
6834    /* But if the first element is not zero, we unshift a 0 before the
6835    * array.  The data structure reserves a space for that 0 (which
6836    * should be a '1' right now), so physical shifting is unneeded,
6837    * but temporarily change that element to 0.  Before exiting the
6838    * routine, we must restore the element to '1' */
6839    array_b--;
6840    len_b++;
6841    array_b[0] = 0;
6842   }
6843  }
6844
6845  /* Size the union for the worst case: that the sets are completely
6846  * disjoint */
6847  u = _new_invlist(len_a + len_b);
6848
6849  /* Will contain U+0000 if either component does */
6850  array_u = _invlist_array_init(u, (len_a > 0 && array_a[0] == 0)
6851          || (len_b > 0 && array_b[0] == 0));
6852
6853  /* Go through each list item by item, stopping when exhausted one of
6854  * them */
6855  while (i_a < len_a && i_b < len_b) {
6856   UV cp;     /* The element to potentially add to the union's array */
6857   bool cp_in_set;   /* is it in the the input list's set or not */
6858
6859   /* We need to take one or the other of the two inputs for the union.
6860   * Since we are merging two sorted lists, we take the smaller of the
6861   * next items.  In case of a tie, we take the one that is in its set
6862   * first.  If we took one not in the set first, it would decrement the
6863   * count, possibly to 0 which would cause it to be output as ending the
6864   * range, and the next time through we would take the same number, and
6865   * output it again as beginning the next range.  By doing it the
6866   * opposite way, there is no possibility that the count will be
6867   * momentarily decremented to 0, and thus the two adjoining ranges will
6868   * be seamlessly merged.  (In a tie and both are in the set or both not
6869   * in the set, it doesn't matter which we take first.) */
6870   if (array_a[i_a] < array_b[i_b]
6871    || (array_a[i_a] == array_b[i_b]
6872     && ELEMENT_RANGE_MATCHES_INVLIST(i_a)))
6873   {
6874    cp_in_set = ELEMENT_RANGE_MATCHES_INVLIST(i_a);
6875    cp= array_a[i_a++];
6876   }
6877   else {
6878    cp_in_set = ELEMENT_RANGE_MATCHES_INVLIST(i_b);
6879    cp= array_b[i_b++];
6880   }
6881
6882   /* Here, have chosen which of the two inputs to look at.  Only output
6883   * if the running count changes to/from 0, which marks the
6884   * beginning/end of a range in that's in the set */
6885   if (cp_in_set) {
6886    if (count == 0) {
6887     array_u[i_u++] = cp;
6888    }
6889    count++;
6890   }
6891   else {
6892    count--;
6893    if (count == 0) {
6894     array_u[i_u++] = cp;
6895    }
6896   }
6897  }
6898
6899  /* Here, we are finished going through at least one of the lists, which
6900  * means there is something remaining in at most one.  We check if the list
6901  * that hasn't been exhausted is positioned such that we are in the middle
6902  * of a range in its set or not.  (i_a and i_b point to the element beyond
6903  * the one we care about.) If in the set, we decrement 'count'; if 0, there
6904  * is potentially more to output.
6905  * There are four cases:
6906  * 1) Both weren't in their sets, count is 0, and remains 0.  What's left
6907  *    in the union is entirely from the non-exhausted set.
6908  * 2) Both were in their sets, count is 2.  Nothing further should
6909  *    be output, as everything that remains will be in the exhausted
6910  *    list's set, hence in the union; decrementing to 1 but not 0 insures
6911  *    that
6912  * 3) the exhausted was in its set, non-exhausted isn't, count is 1.
6913  *    Nothing further should be output because the union includes
6914  *    everything from the exhausted set.  Not decrementing ensures that.
6915  * 4) the exhausted wasn't in its set, non-exhausted is, count is 1;
6916  *    decrementing to 0 insures that we look at the remainder of the
6917  *    non-exhausted set */
6918  if ((i_a != len_a && PREV_RANGE_MATCHES_INVLIST(i_a))
6919   || (i_b != len_b && PREV_RANGE_MATCHES_INVLIST(i_b)))
6920  {
6921   count--;
6922  }
6923
6924  /* The final length is what we've output so far, plus what else is about to
6925  * be output.  (If 'count' is non-zero, then the input list we exhausted
6926  * has everything remaining up to the machine's limit in its set, and hence
6927  * in the union, so there will be no further output. */
6928  len_u = i_u;
6929  if (count == 0) {
6930   /* At most one of the subexpressions will be non-zero */
6931   len_u += (len_a - i_a) + (len_b - i_b);
6932  }
6933
6934  /* Set result to final length, which can change the pointer to array_u, so
6935  * re-find it */
6936  if (len_u != invlist_len(u)) {
6937   invlist_set_len(u, len_u);
6938   invlist_trim(u);
6939   array_u = invlist_array(u);
6940  }
6941
6942  /* When 'count' is 0, the list that was exhausted (if one was shorter than
6943  * the other) ended with everything above it not in its set.  That means
6944  * that the remaining part of the union is precisely the same as the
6945  * non-exhausted list, so can just copy it unchanged.  (If both list were
6946  * exhausted at the same time, then the operations below will be both 0.)
6947  */
6948  if (count == 0) {
6949   IV copy_count; /* At most one will have a non-zero copy count */
6950   if ((copy_count = len_a - i_a) > 0) {
6951    Copy(array_a + i_a, array_u + i_u, copy_count, UV);
6952   }
6953   else if ((copy_count = len_b - i_b) > 0) {
6954    Copy(array_b + i_b, array_u + i_u, copy_count, UV);
6955   }
6956  }
6957
6958  /*  We may be removing a reference to one of the inputs */
6959  if (a == *output || b == *output) {
6960   SvREFCNT_dec(*output);
6961  }
6962
6963  /* If we've changed b, restore it */
6964  if (complement_b) {
6965   array_b[0] = 1;
6966  }
6967
6968  *output = u;
6969  return;
6970 }
6971
6972 void
6973 Perl__invlist_intersection_maybe_complement_2nd(pTHX_ SV* const a, SV* const b, bool complement_b, SV** i)
6974 {
6975  /* Take the intersection of two inversion lists and point <i> to it.  *i
6976  * should be defined upon input, and if it points to one of the two lists,
6977  * the reference count to that list will be decremented.
6978  * If <complement_b> is TRUE, the result will be the intersection of <a>
6979  * and the complement (or inversion) of <b> instead of <b> directly.
6980  *
6981  * The basis for this comes from "Unicode Demystified" Chapter 13 by
6982  * Richard Gillam, published by Addison-Wesley, and explained at some
6983  * length there.  The preface says to incorporate its examples into your
6984  * code at your own risk.  In fact, it had bugs
6985  *
6986  * The algorithm is like a merge sort, and is essentially the same as the
6987  * union above
6988  */
6989
6990  UV* array_a;  /* a's array */
6991  UV* array_b;
6992  UV len_a; /* length of a's array */
6993  UV len_b;
6994
6995  SV* r;       /* the resulting intersection */
6996  UV* array_r;
6997  UV len_r;
6998
6999  UV i_a = 0;      /* current index into a's array */
7000  UV i_b = 0;
7001  UV i_r = 0;
7002
7003  /* running count, as explained in the algorithm source book; items are
7004  * stopped accumulating and are output when the count changes to/from 2.
7005  * The count is incremented when we start a range that's in the set, and
7006  * decremented when we start a range that's not in the set.  So its range
7007  * is 0 to 2.  Only when the count is 2 is something in the intersection.
7008  */
7009  UV count = 0;
7010
7011  PERL_ARGS_ASSERT__INVLIST_INTERSECTION_MAYBE_COMPLEMENT_2ND;
7012  assert(a != b);
7013
7014  /* Special case if either one is empty */
7015  len_a = invlist_len(a);
7016  if ((len_a == 0) || ((len_b = invlist_len(b)) == 0)) {
7017
7018   if (len_a != 0 && complement_b) {
7019
7020    /* Here, 'a' is not empty, therefore from the above 'if', 'b' must
7021    * be empty.  Here, also we are using 'b's complement, which hence
7022    * must be every possible code point.  Thus the intersection is
7023    * simply 'a'. */
7024    if (*i != a) {
7025     *i = invlist_clone(a);
7026
7027     if (*i == b) {
7028      SvREFCNT_dec(b);
7029     }
7030    }
7031    /* else *i is already 'a' */
7032    return;
7033   }
7034
7035   /* Here, 'a' or 'b' is empty and not using the complement of 'b'.  The
7036   * intersection must be empty */
7037   if (*i == a) {
7038    SvREFCNT_dec(a);
7039   }
7040   else if (*i == b) {
7041    SvREFCNT_dec(b);
7042   }
7043   *i = _new_invlist(0);
7044   return;
7045  }
7046
7047  /* Here both lists exist and are non-empty */
7048  array_a = invlist_array(a);
7049  array_b = invlist_array(b);
7050
7051  /* If are to take the intersection of 'a' with the complement of b, set it
7052  * up so are looking at b's complement. */
7053  if (complement_b) {
7054
7055   /* To complement, we invert: if the first element is 0, remove it.  To
7056   * do this, we just pretend the array starts one later, and clear the
7057   * flag as we don't have to do anything else later */
7058   if (array_b[0] == 0) {
7059    array_b++;
7060    len_b--;
7061    complement_b = FALSE;
7062   }
7063   else {
7064
7065    /* But if the first element is not zero, we unshift a 0 before the
7066    * array.  The data structure reserves a space for that 0 (which
7067    * should be a '1' right now), so physical shifting is unneeded,
7068    * but temporarily change that element to 0.  Before exiting the
7069    * routine, we must restore the element to '1' */
7070    array_b--;
7071    len_b++;
7072    array_b[0] = 0;
7073   }
7074  }
7075
7076  /* Size the intersection for the worst case: that the intersection ends up
7077  * fragmenting everything to be completely disjoint */
7078  r= _new_invlist(len_a + len_b);
7079
7080  /* Will contain U+0000 iff both components do */
7081  array_r = _invlist_array_init(r, len_a > 0 && array_a[0] == 0
7082          && len_b > 0 && array_b[0] == 0);
7083
7084  /* Go through each list item by item, stopping when exhausted one of
7085  * them */
7086  while (i_a < len_a && i_b < len_b) {
7087   UV cp;     /* The element to potentially add to the intersection's
7088      array */
7089   bool cp_in_set; /* Is it in the input list's set or not */
7090
7091   /* We need to take one or the other of the two inputs for the
7092   * intersection.  Since we are merging two sorted lists, we take the
7093   * smaller of the next items.  In case of a tie, we take the one that
7094   * is not in its set first (a difference from the union algorithm).  If
7095   * we took one in the set first, it would increment the count, possibly
7096   * to 2 which would cause it to be output as starting a range in the
7097   * intersection, and the next time through we would take that same
7098   * number, and output it again as ending the set.  By doing it the
7099   * opposite of this, there is no possibility that the count will be
7100   * momentarily incremented to 2.  (In a tie and both are in the set or
7101   * both not in the set, it doesn't matter which we take first.) */
7102   if (array_a[i_a] < array_b[i_b]
7103    || (array_a[i_a] == array_b[i_b]
7104     && ! ELEMENT_RANGE_MATCHES_INVLIST(i_a)))
7105   {
7106    cp_in_set = ELEMENT_RANGE_MATCHES_INVLIST(i_a);
7107    cp= array_a[i_a++];
7108   }
7109   else {
7110    cp_in_set = ELEMENT_RANGE_MATCHES_INVLIST(i_b);
7111    cp= array_b[i_b++];
7112   }
7113
7114   /* Here, have chosen which of the two inputs to look at.  Only output
7115   * if the running count changes to/from 2, which marks the
7116   * beginning/end of a range that's in the intersection */
7117   if (cp_in_set) {
7118    count++;
7119    if (count == 2) {
7120     array_r[i_r++] = cp;
7121    }
7122   }
7123   else {
7124    if (count == 2) {
7125     array_r[i_r++] = cp;
7126    }
7127    count--;
7128   }
7129  }
7130
7131  /* Here, we are finished going through at least one of the lists, which
7132  * means there is something remaining in at most one.  We check if the list
7133  * that has been exhausted is positioned such that we are in the middle
7134  * of a range in its set or not.  (i_a and i_b point to elements 1 beyond
7135  * the ones we care about.)  There are four cases:
7136  * 1) Both weren't in their sets, count is 0, and remains 0.  There's
7137  *    nothing left in the intersection.
7138  * 2) Both were in their sets, count is 2 and perhaps is incremented to
7139  *    above 2.  What should be output is exactly that which is in the
7140  *    non-exhausted set, as everything it has is also in the intersection
7141  *    set, and everything it doesn't have can't be in the intersection
7142  * 3) The exhausted was in its set, non-exhausted isn't, count is 1, and
7143  *    gets incremented to 2.  Like the previous case, the intersection is
7144  *    everything that remains in the non-exhausted set.
7145  * 4) the exhausted wasn't in its set, non-exhausted is, count is 1, and
7146  *    remains 1.  And the intersection has nothing more. */
7147  if ((i_a == len_a && PREV_RANGE_MATCHES_INVLIST(i_a))
7148   || (i_b == len_b && PREV_RANGE_MATCHES_INVLIST(i_b)))
7149  {
7150   count++;
7151  }
7152
7153  /* The final length is what we've output so far plus what else is in the
7154  * intersection.  At most one of the subexpressions below will be non-zero */
7155  len_r = i_r;
7156  if (count >= 2) {
7157   len_r += (len_a - i_a) + (len_b - i_b);
7158  }
7159
7160  /* Set result to final length, which can change the pointer to array_r, so
7161  * re-find it */
7162  if (len_r != invlist_len(r)) {
7163   invlist_set_len(r, len_r);
7164   invlist_trim(r);
7165   array_r = invlist_array(r);
7166  }
7167
7168  /* Finish outputting any remaining */
7169  if (count >= 2) { /* At most one will have a non-zero copy count */
7170   IV copy_count;
7171   if ((copy_count = len_a - i_a) > 0) {
7172    Copy(array_a + i_a, array_r + i_r, copy_count, UV);
7173   }
7174   else if ((copy_count = len_b - i_b) > 0) {
7175    Copy(array_b + i_b, array_r + i_r, copy_count, UV);
7176   }
7177  }
7178
7179  /*  We may be removing a reference to one of the inputs */
7180  if (a == *i || b == *i) {
7181   SvREFCNT_dec(*i);
7182  }
7183
7184  /* If we've changed b, restore it */
7185  if (complement_b) {
7186   array_b[0] = 1;
7187  }
7188
7189  *i = r;
7190  return;
7191 }
7192
7193 SV*
7194 Perl__add_range_to_invlist(pTHX_ SV* invlist, const UV start, const UV end)
7195 {
7196  /* Add the range from 'start' to 'end' inclusive to the inversion list's
7197  * set.  A pointer to the inversion list is returned.  This may actually be
7198  * a new list, in which case the passed in one has been destroyed.  The
7199  * passed in inversion list can be NULL, in which case a new one is created
7200  * with just the one range in it */
7201
7202  SV* range_invlist;
7203  UV len;
7204
7205  if (invlist == NULL) {
7206   invlist = _new_invlist(2);
7207   len = 0;
7208  }
7209  else {
7210   len = invlist_len(invlist);
7211  }
7212
7213  /* If comes after the final entry, can just append it to the end */
7214  if (len == 0
7215   || start >= invlist_array(invlist)
7216          [invlist_len(invlist) - 1])
7217  {
7218   _append_range_to_invlist(invlist, start, end);
7219   return invlist;
7220  }
7221
7222  /* Here, can't just append things, create and return a new inversion list
7223  * which is the union of this range and the existing inversion list */
7224  range_invlist = _new_invlist(2);
7225  _append_range_to_invlist(range_invlist, start, end);
7226
7227  _invlist_union(invlist, range_invlist, &invlist);
7228
7229  /* The temporary can be freed */
7230  SvREFCNT_dec(range_invlist);
7231
7232  return invlist;
7233 }
7234
7235 #endif
7236
7237 PERL_STATIC_INLINE SV*
7238 S_add_cp_to_invlist(pTHX_ SV* invlist, const UV cp) {
7239  return _add_range_to_invlist(invlist, cp, cp);
7240 }
7241
7242 #ifndef PERL_IN_XSUB_RE
7243 void
7244 Perl__invlist_invert(pTHX_ SV* const invlist)
7245 {
7246  /* Complement the input inversion list.  This adds a 0 if the list didn't
7247  * have a zero; removes it otherwise.  As described above, the data
7248  * structure is set up so that this is very efficient */
7249
7250  UV* len_pos = get_invlist_len_addr(invlist);
7251
7252  PERL_ARGS_ASSERT__INVLIST_INVERT;
7253
7254  /* The inverse of matching nothing is matching everything */
7255  if (*len_pos == 0) {
7256   _append_range_to_invlist(invlist, 0, UV_MAX);
7257   return;
7258  }
7259
7260  /* The exclusive or complents 0 to 1; and 1 to 0.  If the result is 1, the
7261  * zero element was a 0, so it is being removed, so the length decrements
7262  * by 1; and vice-versa.  SvCUR is unaffected */
7263  if (*get_invlist_zero_addr(invlist) ^= 1) {
7264   (*len_pos)--;
7265  }
7266  else {
7267   (*len_pos)++;
7268  }
7269 }
7270
7271 void
7272 Perl__invlist_invert_prop(pTHX_ SV* const invlist)
7273 {
7274  /* Complement the input inversion list (which must be a Unicode property,
7275  * all of which don't match above the Unicode maximum code point.)  And
7276  * Perl has chosen to not have the inversion match above that either.  This
7277  * adds a 0x110000 if the list didn't end with it, and removes it if it did
7278  */
7279
7280  UV len;
7281  UV* array;
7282
7283  PERL_ARGS_ASSERT__INVLIST_INVERT_PROP;
7284
7285  _invlist_invert(invlist);
7286
7287  len = invlist_len(invlist);
7288
7289  if (len != 0) { /* If empty do nothing */
7290   array = invlist_array(invlist);
7291   if (array[len - 1] != PERL_UNICODE_MAX + 1) {
7292    /* Add 0x110000.  First, grow if necessary */
7293    len++;
7294    if (invlist_max(invlist) < len) {
7295     invlist_extend(invlist, len);
7296     array = invlist_array(invlist);
7297    }
7298    invlist_set_len(invlist, len);
7299    array[len - 1] = PERL_UNICODE_MAX + 1;
7300   }
7301   else {  /* Remove the 0x110000 */
7302    invlist_set_len(invlist, len - 1);
7303   }
7304  }
7305
7306  return;
7307 }
7308 #endif
7309
7310 PERL_STATIC_INLINE SV*
7311 S_invlist_clone(pTHX_ SV* const invlist)
7312 {
7313
7314  /* Return a new inversion list that is a copy of the input one, which is
7315  * unchanged */
7316
7317  /* Need to allocate extra space to accommodate Perl's addition of a
7318  * trailing NUL to SvPV's, since it thinks they are always strings */
7319  SV* new_invlist = _new_invlist(invlist_len(invlist) + 1);
7320  STRLEN length = SvCUR(invlist);
7321
7322  PERL_ARGS_ASSERT_INVLIST_CLONE;
7323
7324  SvCUR_set(new_invlist, length); /* This isn't done automatically */
7325  Copy(SvPVX(invlist), SvPVX(new_invlist), length, char);
7326
7327  return new_invlist;
7328 }
7329
7330 PERL_STATIC_INLINE UV*
7331 S_get_invlist_iter_addr(pTHX_ SV* invlist)
7332 {
7333  /* Return the address of the UV that contains the current iteration
7334  * position */
7335
7336  PERL_ARGS_ASSERT_GET_INVLIST_ITER_ADDR;
7337
7338  return (UV *) (SvPVX(invlist) + (INVLIST_ITER_OFFSET * sizeof (UV)));
7339 }
7340
7341 PERL_STATIC_INLINE UV*
7342 S_get_invlist_version_id_addr(pTHX_ SV* invlist)
7343 {
7344  /* Return the address of the UV that contains the version id. */
7345
7346  PERL_ARGS_ASSERT_GET_INVLIST_VERSION_ID_ADDR;
7347
7348  return (UV *) (SvPVX(invlist) + (INVLIST_VERSION_ID_OFFSET * sizeof (UV)));
7349 }
7350
7351 PERL_STATIC_INLINE void
7352 S_invlist_iterinit(pTHX_ SV* invlist) /* Initialize iterator for invlist */
7353 {
7354  PERL_ARGS_ASSERT_INVLIST_ITERINIT;
7355
7356  *get_invlist_iter_addr(invlist) = 0;
7357 }
7358
7359 STATIC bool
7360 S_invlist_iternext(pTHX_ SV* invlist, UV* start, UV* end)
7361 {
7362  /* An C<invlist_iterinit> call on <invlist> must be used to set this up.
7363  * This call sets in <*start> and <*end>, the next range in <invlist>.
7364  * Returns <TRUE> if successful and the next call will return the next
7365  * range; <FALSE> if was already at the end of the list.  If the latter,
7366  * <*start> and <*end> are unchanged, and the next call to this function
7367  * will start over at the beginning of the list */
7368
7369  UV* pos = get_invlist_iter_addr(invlist);
7370  UV len = invlist_len(invlist);
7371  UV *array;
7372
7373  PERL_ARGS_ASSERT_INVLIST_ITERNEXT;
7374
7375  if (*pos >= len) {
7376   *pos = UV_MAX; /* Force iternit() to be required next time */
7377   return FALSE;
7378  }
7379
7380  array = invlist_array(invlist);
7381
7382  *start = array[(*pos)++];
7383
7384  if (*pos >= len) {
7385   *end = UV_MAX;
7386  }
7387  else {
7388   *end = array[(*pos)++] - 1;
7389  }
7390
7391  return TRUE;
7392 }
7393
7394 #ifndef PERL_IN_XSUB_RE
7395 SV *
7396 Perl__invlist_contents(pTHX_ SV* const invlist)
7397 {
7398  /* Get the contents of an inversion list into a string SV so that they can
7399  * be printed out.  It uses the format traditionally done for debug tracing
7400  */
7401
7402  UV start, end;
7403  SV* output = newSVpvs("\n");
7404
7405  PERL_ARGS_ASSERT__INVLIST_CONTENTS;
7406
7407  invlist_iterinit(invlist);
7408  while (invlist_iternext(invlist, &start, &end)) {
7409   if (end == UV_MAX) {
7410    Perl_sv_catpvf(aTHX_ output, "%04"UVXf"\tINFINITY\n", start);
7411   }
7412   else if (end != start) {
7413    Perl_sv_catpvf(aTHX_ output, "%04"UVXf"\t%04"UVXf"\n",
7414      start,       end);
7415   }
7416   else {
7417    Perl_sv_catpvf(aTHX_ output, "%04"UVXf"\n", start);
7418   }
7419  }
7420
7421  return output;
7422 }
7423 #endif
7424
7425 #if 0
7426 void
7427 S_invlist_dump(pTHX_ SV* const invlist, const char * const header)
7428 {
7429  /* Dumps out the ranges in an inversion list.  The string 'header'
7430  * if present is output on a line before the first range */
7431
7432  UV start, end;
7433
7434  if (header && strlen(header)) {
7435   PerlIO_printf(Perl_debug_log, "%s\n", header);
7436  }
7437  invlist_iterinit(invlist);
7438  while (invlist_iternext(invlist, &start, &end)) {
7439   if (end == UV_MAX) {
7440    PerlIO_printf(Perl_debug_log, "0x%04"UVXf" .. INFINITY\n", start);
7441   }
7442   else {
7443    PerlIO_printf(Perl_debug_log, "0x%04"UVXf" .. 0x%04"UVXf"\n", start, end);
7444   }
7445  }
7446 }
7447 #endif
7448
7449 #undef HEADER_LENGTH
7450 #undef INVLIST_INITIAL_LENGTH
7451 #undef TO_INTERNAL_SIZE
7452 #undef FROM_INTERNAL_SIZE
7453 #undef INVLIST_LEN_OFFSET
7454 #undef INVLIST_ZERO_OFFSET
7455 #undef INVLIST_ITER_OFFSET
7456 #undef INVLIST_VERSION_ID
7457
7458 /* End of inversion list object */
7459
7460 /*
7461  - reg - regular expression, i.e. main body or parenthesized thing
7462  *
7463  * Caller must absorb opening parenthesis.
7464  *
7465  * Combining parenthesis handling with the base level of regular expression
7466  * is a trifle forced, but the need to tie the tails of the branches to what
7467  * follows makes it hard to avoid.
7468  */
7469 #define REGTAIL(x,y,z) regtail((x),(y),(z),depth+1)
7470 #ifdef DEBUGGING
7471 #define REGTAIL_STUDY(x,y,z) regtail_study((x),(y),(z),depth+1)
7472 #else
7473 #define REGTAIL_STUDY(x,y,z) regtail((x),(y),(z),depth+1)
7474 #endif
7475
7476 STATIC regnode *
7477 S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp,U32 depth)
7478  /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
7479 {
7480  dVAR;
7481  register regnode *ret;  /* Will be the head of the group. */
7482  register regnode *br;
7483  register regnode *lastbr;
7484  register regnode *ender = NULL;
7485  register I32 parno = 0;
7486  I32 flags;
7487  U32 oregflags = RExC_flags;
7488  bool have_branch = 0;
7489  bool is_open = 0;
7490  I32 freeze_paren = 0;
7491  I32 after_freeze = 0;
7492
7493  /* for (?g), (?gc), and (?o) warnings; warning
7494  about (?c) will warn about (?g) -- japhy    */
7495
7496 #define WASTED_O  0x01
7497 #define WASTED_G  0x02
7498 #define WASTED_C  0x04
7499 #define WASTED_GC (0x02|0x04)
7500  I32 wastedflags = 0x00;
7501
7502  char * parse_start = RExC_parse; /* MJD */
7503  char * const oregcomp_parse = RExC_parse;
7504
7505  GET_RE_DEBUG_FLAGS_DECL;
7506
7507  PERL_ARGS_ASSERT_REG;
7508  DEBUG_PARSE("reg ");
7509
7510  *flagp = 0;    /* Tentatively. */
7511
7512
7513  /* Make an OPEN node, if parenthesized. */
7514  if (paren) {
7515   if ( *RExC_parse == '*') { /* (*VERB:ARG) */
7516    char *start_verb = RExC_parse;
7517    STRLEN verb_len = 0;
7518    char *start_arg = NULL;
7519    unsigned char op = 0;
7520    int argok = 1;
7521    int internal_argval = 0; /* internal_argval is only useful if !argok */
7522    while ( *RExC_parse && *RExC_parse != ')' ) {
7523     if ( *RExC_parse == ':' ) {
7524      start_arg = RExC_parse + 1;
7525      break;
7526     }
7527     RExC_parse++;
7528    }
7529    ++start_verb;
7530    verb_len = RExC_parse - start_verb;
7531    if ( start_arg ) {
7532     RExC_parse++;
7533     while ( *RExC_parse && *RExC_parse != ')' )
7534      RExC_parse++;
7535     if ( *RExC_parse != ')' )
7536      vFAIL("Unterminated verb pattern argument");
7537     if ( RExC_parse == start_arg )
7538      start_arg = NULL;
7539    } else {
7540     if ( *RExC_parse != ')' )
7541      vFAIL("Unterminated verb pattern");
7542    }
7543
7544    switch ( *start_verb ) {
7545    case 'A':  /* (*ACCEPT) */
7546     if ( memEQs(start_verb,verb_len,"ACCEPT") ) {
7547      op = ACCEPT;
7548      internal_argval = RExC_nestroot;
7549     }
7550     break;
7551    case 'C':  /* (*COMMIT) */
7552     if ( memEQs(start_verb,verb_len,"COMMIT") )
7553      op = COMMIT;
7554     break;
7555    case 'F':  /* (*FAIL) */
7556     if ( verb_len==1 || memEQs(start_verb,verb_len,"FAIL") ) {
7557      op = OPFAIL;
7558      argok = 0;
7559     }
7560     break;
7561    case ':':  /* (*:NAME) */
7562    case 'M':  /* (*MARK:NAME) */
7563     if ( verb_len==0 || memEQs(start_verb,verb_len,"MARK") ) {
7564      op = MARKPOINT;
7565      argok = -1;
7566     }
7567     break;
7568    case 'P':  /* (*PRUNE) */
7569     if ( memEQs(start_verb,verb_len,"PRUNE") )
7570      op = PRUNE;
7571     break;
7572    case 'S':   /* (*SKIP) */
7573     if ( memEQs(start_verb,verb_len,"SKIP") )
7574      op = SKIP;
7575     break;
7576    case 'T':  /* (*THEN) */
7577     /* [19:06] <TimToady> :: is then */
7578     if ( memEQs(start_verb,verb_len,"THEN") ) {
7579      op = CUTGROUP;
7580      RExC_seen |= REG_SEEN_CUTGROUP;
7581     }
7582     break;
7583    }
7584    if ( ! op ) {
7585     RExC_parse++;
7586     vFAIL3("Unknown verb pattern '%.*s'",
7587      verb_len, start_verb);
7588    }
7589    if ( argok ) {
7590     if ( start_arg && internal_argval ) {
7591      vFAIL3("Verb pattern '%.*s' may not have an argument",
7592       verb_len, start_verb);
7593     } else if ( argok < 0 && !start_arg ) {
7594      vFAIL3("Verb pattern '%.*s' has a mandatory argument",
7595       verb_len, start_verb);
7596     } else {
7597      ret = reganode(pRExC_state, op, internal_argval);
7598      if ( ! internal_argval && ! SIZE_ONLY ) {
7599       if (start_arg) {
7600        SV *sv = newSVpvn( start_arg, RExC_parse - start_arg);
7601        ARG(ret) = add_data( pRExC_state, 1, "S" );
7602        RExC_rxi->data->data[ARG(ret)]=(void*)sv;
7603        ret->flags = 0;
7604       } else {
7605        ret->flags = 1;
7606       }
7607      }
7608     }
7609     if (!internal_argval)
7610      RExC_seen |= REG_SEEN_VERBARG;
7611    } else if ( start_arg ) {
7612     vFAIL3("Verb pattern '%.*s' may not have an argument",
7613       verb_len, start_verb);
7614    } else {
7615     ret = reg_node(pRExC_state, op);
7616    }
7617    nextchar(pRExC_state);
7618    return ret;
7619   } else
7620   if (*RExC_parse == '?') { /* (?...) */
7621    bool is_logical = 0;
7622    const char * const seqstart = RExC_parse;
7623    bool has_use_defaults = FALSE;
7624
7625    RExC_parse++;
7626    paren = *RExC_parse++;
7627    ret = NULL;   /* For look-ahead/behind. */
7628    switch (paren) {
7629
7630    case 'P': /* (?P...) variants for those used to PCRE/Python */
7631     paren = *RExC_parse++;
7632     if ( paren == '<')         /* (?P<...>) named capture */
7633      goto named_capture;
7634     else if (paren == '>') {   /* (?P>name) named recursion */
7635      goto named_recursion;
7636     }
7637     else if (paren == '=') {   /* (?P=...)  named backref */
7638      /* this pretty much dupes the code for \k<NAME> in regatom(), if
7639      you change this make sure you change that */
7640      char* name_start = RExC_parse;
7641      U32 num = 0;
7642      SV *sv_dat = reg_scan_name(pRExC_state,
7643       SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
7644      if (RExC_parse == name_start || *RExC_parse != ')')
7645       vFAIL2("Sequence %.3s... not terminated",parse_start);
7646
7647      if (!SIZE_ONLY) {
7648       num = add_data( pRExC_state, 1, "S" );
7649       RExC_rxi->data->data[num]=(void*)sv_dat;
7650       SvREFCNT_inc_simple_void(sv_dat);
7651      }
7652      RExC_sawback = 1;
7653      ret = reganode(pRExC_state,
7654         ((! FOLD)
7655          ? NREF
7656          : (MORE_ASCII_RESTRICTED)
7657          ? NREFFA
7658          : (AT_LEAST_UNI_SEMANTICS)
7659           ? NREFFU
7660           : (LOC)
7661           ? NREFFL
7662           : NREFF),
7663          num);
7664      *flagp |= HASWIDTH;
7665
7666      Set_Node_Offset(ret, parse_start+1);
7667      Set_Node_Cur_Length(ret); /* MJD */
7668
7669      nextchar(pRExC_state);
7670      return ret;
7671     }
7672     RExC_parse++;
7673     vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
7674     /*NOTREACHED*/
7675    case '<':           /* (?<...) */
7676     if (*RExC_parse == '!')
7677      paren = ',';
7678     else if (*RExC_parse != '=')
7679    named_capture:
7680     {               /* (?<...>) */
7681      char *name_start;
7682      SV *svname;
7683      paren= '>';
7684    case '\'':          /* (?'...') */
7685       name_start= RExC_parse;
7686       svname = reg_scan_name(pRExC_state,
7687        SIZE_ONLY ?  /* reverse test from the others */
7688        REG_RSN_RETURN_NAME :
7689        REG_RSN_RETURN_NULL);
7690      if (RExC_parse == name_start) {
7691       RExC_parse++;
7692       vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
7693       /*NOTREACHED*/
7694      }
7695      if (*RExC_parse != paren)
7696       vFAIL2("Sequence (?%c... not terminated",
7697        paren=='>' ? '<' : paren);
7698      if (SIZE_ONLY) {
7699       HE *he_str;
7700       SV *sv_dat = NULL;
7701       if (!svname) /* shouldn't happen */
7702        Perl_croak(aTHX_
7703         "panic: reg_scan_name returned NULL");
7704       if (!RExC_paren_names) {
7705        RExC_paren_names= newHV();
7706        sv_2mortal(MUTABLE_SV(RExC_paren_names));
7707 #ifdef DEBUGGING
7708        RExC_paren_name_list= newAV();
7709        sv_2mortal(MUTABLE_SV(RExC_paren_name_list));
7710 #endif
7711       }
7712       he_str = hv_fetch_ent( RExC_paren_names, svname, 1, 0 );
7713       if ( he_str )
7714        sv_dat = HeVAL(he_str);
7715       if ( ! sv_dat ) {
7716        /* croak baby croak */
7717        Perl_croak(aTHX_
7718         "panic: paren_name hash element allocation failed");
7719       } else if ( SvPOK(sv_dat) ) {
7720        /* (?|...) can mean we have dupes so scan to check
7721        its already been stored. Maybe a flag indicating
7722        we are inside such a construct would be useful,
7723        but the arrays are likely to be quite small, so
7724        for now we punt -- dmq */
7725        IV count = SvIV(sv_dat);
7726        I32 *pv = (I32*)SvPVX(sv_dat);
7727        IV i;
7728        for ( i = 0 ; i < count ; i++ ) {
7729         if ( pv[i] == RExC_npar ) {
7730          count = 0;
7731          break;
7732         }
7733        }
7734        if ( count ) {
7735         pv = (I32*)SvGROW(sv_dat, SvCUR(sv_dat) + sizeof(I32)+1);
7736         SvCUR_set(sv_dat, SvCUR(sv_dat) + sizeof(I32));
7737         pv[count] = RExC_npar;
7738         SvIV_set(sv_dat, SvIVX(sv_dat) + 1);
7739        }
7740       } else {
7741        (void)SvUPGRADE(sv_dat,SVt_PVNV);
7742        sv_setpvn(sv_dat, (char *)&(RExC_npar), sizeof(I32));
7743        SvIOK_on(sv_dat);
7744        SvIV_set(sv_dat, 1);
7745       }
7746 #ifdef DEBUGGING
7747       /* Yes this does cause a memory leak in debugging Perls */
7748       if (!av_store(RExC_paren_name_list, RExC_npar, SvREFCNT_inc(svname)))
7749        SvREFCNT_dec(svname);
7750 #endif
7751
7752       /*sv_dump(sv_dat);*/
7753      }
7754      nextchar(pRExC_state);
7755      paren = 1;
7756      goto capturing_parens;
7757     }
7758     RExC_seen |= REG_SEEN_LOOKBEHIND;
7759     RExC_in_lookbehind++;
7760     RExC_parse++;
7761    case '=':           /* (?=...) */
7762     RExC_seen_zerolen++;
7763     break;
7764    case '!':           /* (?!...) */
7765     RExC_seen_zerolen++;
7766     if (*RExC_parse == ')') {
7767      ret=reg_node(pRExC_state, OPFAIL);
7768      nextchar(pRExC_state);
7769      return ret;
7770     }
7771     break;
7772    case '|':           /* (?|...) */
7773     /* branch reset, behave like a (?:...) except that
7774     buffers in alternations share the same numbers */
7775     paren = ':';
7776     after_freeze = freeze_paren = RExC_npar;
7777     break;
7778    case ':':           /* (?:...) */
7779    case '>':           /* (?>...) */
7780     break;
7781    case '$':           /* (?$...) */
7782    case '@':           /* (?@...) */
7783     vFAIL2("Sequence (?%c...) not implemented", (int)paren);
7784     break;
7785    case '#':           /* (?#...) */
7786     while (*RExC_parse && *RExC_parse != ')')
7787      RExC_parse++;
7788     if (*RExC_parse != ')')
7789      FAIL("Sequence (?#... not terminated");
7790     nextchar(pRExC_state);
7791     *flagp = TRYAGAIN;
7792     return NULL;
7793    case '0' :           /* (?0) */
7794    case 'R' :           /* (?R) */
7795     if (*RExC_parse != ')')
7796      FAIL("Sequence (?R) not terminated");
7797     ret = reg_node(pRExC_state, GOSTART);
7798     *flagp |= POSTPONED;
7799     nextchar(pRExC_state);
7800     return ret;
7801     /*notreached*/
7802    { /* named and numeric backreferences */
7803     I32 num;
7804    case '&':            /* (?&NAME) */
7805     parse_start = RExC_parse - 1;
7806    named_recursion:
7807     {
7808       SV *sv_dat = reg_scan_name(pRExC_state,
7809        SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
7810       num = sv_dat ? *((I32 *)SvPVX(sv_dat)) : 0;
7811     }
7812     goto gen_recurse_regop;
7813     /* NOT REACHED */
7814    case '+':
7815     if (!(RExC_parse[0] >= '1' && RExC_parse[0] <= '9')) {
7816      RExC_parse++;
7817      vFAIL("Illegal pattern");
7818     }
7819     goto parse_recursion;
7820     /* NOT REACHED*/
7821    case '-': /* (?-1) */
7822     if (!(RExC_parse[0] >= '1' && RExC_parse[0] <= '9')) {
7823      RExC_parse--; /* rewind to let it be handled later */
7824      goto parse_flags;
7825     }
7826     /*FALLTHROUGH */
7827    case '1': case '2': case '3': case '4': /* (?1) */
7828    case '5': case '6': case '7': case '8': case '9':
7829     RExC_parse--;
7830    parse_recursion:
7831     num = atoi(RExC_parse);
7832     parse_start = RExC_parse - 1; /* MJD */
7833     if (*RExC_parse == '-')
7834      RExC_parse++;
7835     while (isDIGIT(*RExC_parse))
7836       RExC_parse++;
7837     if (*RExC_parse!=')')
7838      vFAIL("Expecting close bracket");
7839
7840    gen_recurse_regop:
7841     if ( paren == '-' ) {
7842      /*
7843      Diagram of capture buffer numbering.
7844      Top line is the normal capture buffer numbers
7845      Bottom line is the negative indexing as from
7846      the X (the (?-2))
7847
7848      +   1 2    3 4 5 X          6 7
7849      /(a(x)y)(a(b(c(?-2)d)e)f)(g(h))/
7850      -   5 4    3 2 1 X          x x
7851
7852      */
7853      num = RExC_npar + num;
7854      if (num < 1)  {
7855       RExC_parse++;
7856       vFAIL("Reference to nonexistent group");
7857      }
7858     } else if ( paren == '+' ) {
7859      num = RExC_npar + num - 1;
7860     }
7861
7862     ret = reganode(pRExC_state, GOSUB, num);
7863     if (!SIZE_ONLY) {
7864      if (num > (I32)RExC_rx->nparens) {
7865       RExC_parse++;
7866       vFAIL("Reference to nonexistent group");
7867      }
7868      ARG2L_SET( ret, RExC_recurse_count++);
7869      RExC_emit++;
7870      DEBUG_OPTIMISE_MORE_r(PerlIO_printf(Perl_debug_log,
7871       "Recurse #%"UVuf" to %"IVdf"\n", (UV)ARG(ret), (IV)ARG2L(ret)));
7872     } else {
7873      RExC_size++;
7874      }
7875      RExC_seen |= REG_SEEN_RECURSE;
7876     Set_Node_Length(ret, 1 + regarglen[OP(ret)]); /* MJD */
7877     Set_Node_Offset(ret, parse_start); /* MJD */
7878
7879     *flagp |= POSTPONED;
7880     nextchar(pRExC_state);
7881     return ret;
7882    } /* named and numeric backreferences */
7883    /* NOT REACHED */
7884
7885    case '?':           /* (??...) */
7886     is_logical = 1;
7887     if (*RExC_parse != '{') {
7888      RExC_parse++;
7889      vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
7890      /*NOTREACHED*/
7891     }
7892     *flagp |= POSTPONED;
7893     paren = *RExC_parse++;
7894     /* FALL THROUGH */
7895    case '{':           /* (?{...}) */
7896    {
7897     I32 count = 1;
7898     U32 n = 0;
7899     char c;
7900     char *s = RExC_parse;
7901
7902     RExC_seen_zerolen++;
7903     RExC_seen |= REG_SEEN_EVAL;
7904     while (count && (c = *RExC_parse)) {
7905      if (c == '\\') {
7906       if (RExC_parse[1])
7907        RExC_parse++;
7908      }
7909      else if (c == '{')
7910       count++;
7911      else if (c == '}')
7912       count--;
7913      RExC_parse++;
7914     }
7915     if (*RExC_parse != ')') {
7916      RExC_parse = s;
7917      vFAIL("Sequence (?{...}) not terminated or not {}-balanced");
7918     }
7919     if (!SIZE_ONLY) {
7920      PAD *pad;
7921      OP_4tree *sop, *rop;
7922      SV * const sv = newSVpvn(s, RExC_parse - 1 - s);
7923
7924      ENTER;
7925      Perl_save_re_context(aTHX);
7926      rop = Perl_sv_compile_2op_is_broken(aTHX_ sv, &sop, "re", &pad);
7927      sop->op_private |= OPpREFCOUNTED;
7928      /* re_dup will OpREFCNT_inc */
7929      OpREFCNT_set(sop, 1);
7930      LEAVE;
7931
7932      n = add_data(pRExC_state, 3, "nop");
7933      RExC_rxi->data->data[n] = (void*)rop;
7934      RExC_rxi->data->data[n+1] = (void*)sop;
7935      RExC_rxi->data->data[n+2] = (void*)pad;
7936      SvREFCNT_dec(sv);
7937     }
7938     else {      /* First pass */
7939      if (PL_reginterp_cnt < ++RExC_seen_evals
7940       && IN_PERL_RUNTIME)
7941       /* No compiled RE interpolated, has runtime
7942       components ===> unsafe.  */
7943       FAIL("Eval-group not allowed at runtime, use re 'eval'");
7944      if (PL_tainting && PL_tainted)
7945       FAIL("Eval-group in insecure regular expression");
7946 #if PERL_VERSION > 8
7947      if (IN_PERL_COMPILETIME)
7948       PL_cv_has_eval = 1;
7949 #endif
7950     }
7951
7952     nextchar(pRExC_state);
7953     if (is_logical) {
7954      ret = reg_node(pRExC_state, LOGICAL);
7955      if (!SIZE_ONLY)
7956       ret->flags = 2;
7957      REGTAIL(pRExC_state, ret, reganode(pRExC_state, EVAL, n));
7958      /* deal with the length of this later - MJD */
7959      return ret;
7960     }
7961     ret = reganode(pRExC_state, EVAL, n);
7962     Set_Node_Length(ret, RExC_parse - parse_start + 1);
7963     Set_Node_Offset(ret, parse_start);
7964     return ret;
7965    }
7966    case '(':           /* (?(?{...})...) and (?(?=...)...) */
7967    {
7968     int is_define= 0;
7969     if (RExC_parse[0] == '?') {        /* (?(?...)) */
7970      if (RExC_parse[1] == '=' || RExC_parse[1] == '!'
7971       || RExC_parse[1] == '<'
7972       || RExC_parse[1] == '{') { /* Lookahead or eval. */
7973       I32 flag;
7974
7975       ret = reg_node(pRExC_state, LOGICAL);
7976       if (!SIZE_ONLY)
7977        ret->flags = 1;
7978       REGTAIL(pRExC_state, ret, reg(pRExC_state, 1, &flag,depth+1));
7979       goto insert_if;
7980      }
7981     }
7982     else if ( RExC_parse[0] == '<'     /* (?(<NAME>)...) */
7983       || RExC_parse[0] == '\'' ) /* (?('NAME')...) */
7984     {
7985      char ch = RExC_parse[0] == '<' ? '>' : '\'';
7986      char *name_start= RExC_parse++;
7987      U32 num = 0;
7988      SV *sv_dat=reg_scan_name(pRExC_state,
7989       SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
7990      if (RExC_parse == name_start || *RExC_parse != ch)
7991       vFAIL2("Sequence (?(%c... not terminated",
7992        (ch == '>' ? '<' : ch));
7993      RExC_parse++;
7994      if (!SIZE_ONLY) {
7995       num = add_data( pRExC_state, 1, "S" );
7996       RExC_rxi->data->data[num]=(void*)sv_dat;
7997       SvREFCNT_inc_simple_void(sv_dat);
7998      }
7999      ret = reganode(pRExC_state,NGROUPP,num);
8000      goto insert_if_check_paren;
8001     }
8002     else if (RExC_parse[0] == 'D' &&
8003       RExC_parse[1] == 'E' &&
8004       RExC_parse[2] == 'F' &&
8005       RExC_parse[3] == 'I' &&
8006       RExC_parse[4] == 'N' &&
8007       RExC_parse[5] == 'E')
8008     {
8009      ret = reganode(pRExC_state,DEFINEP,0);
8010      RExC_parse +=6 ;
8011      is_define = 1;
8012      goto insert_if_check_paren;
8013     }
8014     else if (RExC_parse[0] == 'R') {
8015      RExC_parse++;
8016      parno = 0;
8017      if (RExC_parse[0] >= '1' && RExC_parse[0] <= '9' ) {
8018       parno = atoi(RExC_parse++);
8019       while (isDIGIT(*RExC_parse))
8020        RExC_parse++;
8021      } else if (RExC_parse[0] == '&') {
8022       SV *sv_dat;
8023       RExC_parse++;
8024       sv_dat = reg_scan_name(pRExC_state,
8025         SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
8026        parno = sv_dat ? *((I32 *)SvPVX(sv_dat)) : 0;
8027      }
8028      ret = reganode(pRExC_state,INSUBP,parno);
8029      goto insert_if_check_paren;
8030     }
8031     else if (RExC_parse[0] >= '1' && RExC_parse[0] <= '9' ) {
8032      /* (?(1)...) */
8033      char c;
8034      parno = atoi(RExC_parse++);
8035
8036      while (isDIGIT(*RExC_parse))
8037       RExC_parse++;
8038      ret = reganode(pRExC_state, GROUPP, parno);
8039
8040     insert_if_check_paren:
8041      if ((c = *nextchar(pRExC_state)) != ')')
8042       vFAIL("Switch condition not recognized");
8043     insert_if:
8044      REGTAIL(pRExC_state, ret, reganode(pRExC_state, IFTHEN, 0));
8045      br = regbranch(pRExC_state, &flags, 1,depth+1);
8046      if (br == NULL)
8047       br = reganode(pRExC_state, LONGJMP, 0);
8048      else
8049       REGTAIL(pRExC_state, br, reganode(pRExC_state, LONGJMP, 0));
8050      c = *nextchar(pRExC_state);
8051      if (flags&HASWIDTH)
8052       *flagp |= HASWIDTH;
8053      if (c == '|') {
8054       if (is_define)
8055        vFAIL("(?(DEFINE)....) does not allow branches");
8056       lastbr = reganode(pRExC_state, IFTHEN, 0); /* Fake one for optimizer. */
8057       regbranch(pRExC_state, &flags, 1,depth+1);
8058       REGTAIL(pRExC_state, ret, lastbr);
8059       if (flags&HASWIDTH)
8060        *flagp |= HASWIDTH;
8061       c = *nextchar(pRExC_state);
8062      }
8063      else
8064       lastbr = NULL;
8065      if (c != ')')
8066       vFAIL("Switch (?(condition)... contains too many branches");
8067      ender = reg_node(pRExC_state, TAIL);
8068      REGTAIL(pRExC_state, br, ender);
8069      if (lastbr) {
8070       REGTAIL(pRExC_state, lastbr, ender);
8071       REGTAIL(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender);
8072      }
8073      else
8074       REGTAIL(pRExC_state, ret, ender);
8075      RExC_size++; /* XXX WHY do we need this?!!
8076          For large programs it seems to be required
8077          but I can't figure out why. -- dmq*/
8078      return ret;
8079     }
8080     else {
8081      vFAIL2("Unknown switch condition (?(%.2s", RExC_parse);
8082     }
8083    }
8084    case 0:
8085     RExC_parse--; /* for vFAIL to print correctly */
8086     vFAIL("Sequence (? incomplete");
8087     break;
8088    case DEFAULT_PAT_MOD:   /* Use default flags with the exceptions
8089          that follow */
8090     has_use_defaults = TRUE;
8091     STD_PMMOD_FLAGS_CLEAR(&RExC_flags);
8092     set_regex_charset(&RExC_flags, (RExC_utf8 || RExC_uni_semantics)
8093             ? REGEX_UNICODE_CHARSET
8094             : REGEX_DEPENDS_CHARSET);
8095     goto parse_flags;
8096    default:
8097     --RExC_parse;
8098     parse_flags:      /* (?i) */
8099    {
8100     U32 posflags = 0, negflags = 0;
8101     U32 *flagsp = &posflags;
8102     char has_charset_modifier = '\0';
8103     regex_charset cs = get_regex_charset(RExC_flags);
8104     if (cs == REGEX_DEPENDS_CHARSET
8105      && (RExC_utf8 || RExC_uni_semantics))
8106     {
8107      cs = REGEX_UNICODE_CHARSET;
8108     }
8109
8110     while (*RExC_parse) {
8111      /* && strchr("iogcmsx", *RExC_parse) */
8112      /* (?g), (?gc) and (?o) are useless here
8113      and must be globally applied -- japhy */
8114      switch (*RExC_parse) {
8115      CASE_STD_PMMOD_FLAGS_PARSE_SET(flagsp);
8116      case LOCALE_PAT_MOD:
8117       if (has_charset_modifier) {
8118        goto excess_modifier;
8119       }
8120       else if (flagsp == &negflags) {
8121        goto neg_modifier;
8122       }
8123       cs = REGEX_LOCALE_CHARSET;
8124       has_charset_modifier = LOCALE_PAT_MOD;
8125       RExC_contains_locale = 1;
8126       break;
8127      case UNICODE_PAT_MOD:
8128       if (has_charset_modifier) {
8129        goto excess_modifier;
8130       }
8131       else if (flagsp == &negflags) {
8132        goto neg_modifier;
8133       }
8134       cs = REGEX_UNICODE_CHARSET;
8135       has_charset_modifier = UNICODE_PAT_MOD;
8136       break;
8137      case ASCII_RESTRICT_PAT_MOD:
8138       if (flagsp == &negflags) {
8139        goto neg_modifier;
8140       }
8141       if (has_charset_modifier) {
8142        if (cs != REGEX_ASCII_RESTRICTED_CHARSET) {
8143         goto excess_modifier;
8144        }
8145        /* Doubled modifier implies more restricted */
8146        cs = REGEX_ASCII_MORE_RESTRICTED_CHARSET;
8147       }
8148       else {
8149        cs = REGEX_ASCII_RESTRICTED_CHARSET;
8150       }
8151       has_charset_modifier = ASCII_RESTRICT_PAT_MOD;
8152       break;
8153      case DEPENDS_PAT_MOD:
8154       if (has_use_defaults) {
8155        goto fail_modifiers;
8156       }
8157       else if (flagsp == &negflags) {
8158        goto neg_modifier;
8159       }
8160       else if (has_charset_modifier) {
8161        goto excess_modifier;
8162       }
8163
8164       /* The dual charset means unicode semantics if the
8165       * pattern (or target, not known until runtime) are
8166       * utf8, or something in the pattern indicates unicode
8167       * semantics */
8168       cs = (RExC_utf8 || RExC_uni_semantics)
8169        ? REGEX_UNICODE_CHARSET
8170        : REGEX_DEPENDS_CHARSET;
8171       has_charset_modifier = DEPENDS_PAT_MOD;
8172       break;
8173      excess_modifier:
8174       RExC_parse++;
8175       if (has_charset_modifier == ASCII_RESTRICT_PAT_MOD) {
8176        vFAIL2("Regexp modifier \"%c\" may appear a maximum of twice", ASCII_RESTRICT_PAT_MOD);
8177       }
8178       else if (has_charset_modifier == *(RExC_parse - 1)) {
8179        vFAIL2("Regexp modifier \"%c\" may not appear twice", *(RExC_parse - 1));
8180       }
8181       else {
8182        vFAIL3("Regexp modifiers \"%c\" and \"%c\" are mutually exclusive", has_charset_modifier, *(RExC_parse - 1));
8183       }
8184       /*NOTREACHED*/
8185      neg_modifier:
8186       RExC_parse++;
8187       vFAIL2("Regexp modifier \"%c\" may not appear after the \"-\"", *(RExC_parse - 1));
8188       /*NOTREACHED*/
8189      case ONCE_PAT_MOD: /* 'o' */
8190      case GLOBAL_PAT_MOD: /* 'g' */
8191       if (SIZE_ONLY && ckWARN(WARN_REGEXP)) {
8192        const I32 wflagbit = *RExC_parse == 'o' ? WASTED_O : WASTED_G;
8193        if (! (wastedflags & wflagbit) ) {
8194         wastedflags |= wflagbit;
8195         vWARN5(
8196          RExC_parse + 1,
8197          "Useless (%s%c) - %suse /%c modifier",
8198          flagsp == &negflags ? "?-" : "?",
8199          *RExC_parse,
8200          flagsp == &negflags ? "don't " : "",
8201          *RExC_parse
8202         );
8203        }
8204       }
8205       break;
8206
8207      case CONTINUE_PAT_MOD: /* 'c' */
8208       if (SIZE_ONLY && ckWARN(WARN_REGEXP)) {
8209        if (! (wastedflags & WASTED_C) ) {
8210         wastedflags |= WASTED_GC;
8211         vWARN3(
8212          RExC_parse + 1,
8213          "Useless (%sc) - %suse /gc modifier",
8214          flagsp == &negflags ? "?-" : "?",
8215          flagsp == &negflags ? "don't " : ""
8216         );
8217        }
8218       }
8219       break;
8220      case KEEPCOPY_PAT_MOD: /* 'p' */
8221       if (flagsp == &negflags) {
8222        if (SIZE_ONLY)
8223         ckWARNreg(RExC_parse + 1,"Useless use of (?-p)");
8224       } else {
8225        *flagsp |= RXf_PMf_KEEPCOPY;
8226       }
8227       break;
8228      case '-':
8229       /* A flag is a default iff it is following a minus, so
8230       * if there is a minus, it means will be trying to
8231       * re-specify a default which is an error */
8232       if (has_use_defaults || flagsp == &negflags) {
8233    fail_modifiers:
8234        RExC_parse++;
8235        vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
8236        /*NOTREACHED*/
8237       }
8238       flagsp = &negflags;
8239       wastedflags = 0;  /* reset so (?g-c) warns twice */
8240       break;
8241      case ':':
8242       paren = ':';
8243       /*FALLTHROUGH*/
8244      case ')':
8245       RExC_flags |= posflags;
8246       RExC_flags &= ~negflags;
8247       set_regex_charset(&RExC_flags, cs);
8248       if (paren != ':') {
8249        oregflags |= posflags;
8250        oregflags &= ~negflags;
8251        set_regex_charset(&oregflags, cs);
8252       }
8253       nextchar(pRExC_state);
8254       if (paren != ':') {
8255        *flagp = TRYAGAIN;
8256        return NULL;
8257       } else {
8258        ret = NULL;
8259        goto parse_rest;
8260       }
8261       /*NOTREACHED*/
8262      default:
8263       RExC_parse++;
8264       vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
8265       /*NOTREACHED*/
8266      }
8267      ++RExC_parse;
8268     }
8269    }} /* one for the default block, one for the switch */
8270   }
8271   else {                  /* (...) */
8272   capturing_parens:
8273    parno = RExC_npar;
8274    RExC_npar++;
8275
8276    ret = reganode(pRExC_state, OPEN, parno);
8277    if (!SIZE_ONLY ){
8278     if (!RExC_nestroot)
8279      RExC_nestroot = parno;
8280     if (RExC_seen & REG_SEEN_RECURSE
8281      && !RExC_open_parens[parno-1])
8282     {
8283      DEBUG_OPTIMISE_MORE_r(PerlIO_printf(Perl_debug_log,
8284       "Setting open paren #%"IVdf" to %d\n",
8285       (IV)parno, REG_NODE_NUM(ret)));
8286      RExC_open_parens[parno-1]= ret;
8287     }
8288    }
8289    Set_Node_Length(ret, 1); /* MJD */
8290    Set_Node_Offset(ret, RExC_parse); /* MJD */
8291    is_open = 1;
8292   }
8293  }
8294  else                        /* ! paren */
8295   ret = NULL;
8296
8297    parse_rest:
8298  /* Pick up the branches, linking them together. */
8299  parse_start = RExC_parse;   /* MJD */
8300  br = regbranch(pRExC_state, &flags, 1,depth+1);
8301
8302  /*     branch_len = (paren != 0); */
8303
8304  if (br == NULL)
8305   return(NULL);
8306  if (*RExC_parse == '|') {
8307   if (!SIZE_ONLY && RExC_extralen) {
8308    reginsert(pRExC_state, BRANCHJ, br, depth+1);
8309   }
8310   else {                  /* MJD */
8311    reginsert(pRExC_state, BRANCH, br, depth+1);
8312    Set_Node_Length(br, paren != 0);
8313    Set_Node_Offset_To_R(br-RExC_emit_start, parse_start-RExC_start);
8314   }
8315   have_branch = 1;
8316   if (SIZE_ONLY)
8317    RExC_extralen += 1;  /* For BRANCHJ-BRANCH. */
8318  }
8319  else if (paren == ':') {
8320   *flagp |= flags&SIMPLE;
8321  }
8322  if (is_open) {    /* Starts with OPEN. */
8323   REGTAIL(pRExC_state, ret, br);          /* OPEN -> first. */
8324  }
8325  else if (paren != '?')  /* Not Conditional */
8326   ret = br;
8327  *flagp |= flags & (SPSTART | HASWIDTH | POSTPONED);
8328  lastbr = br;
8329  while (*RExC_parse == '|') {
8330   if (!SIZE_ONLY && RExC_extralen) {
8331    ender = reganode(pRExC_state, LONGJMP,0);
8332    REGTAIL(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
8333   }
8334   if (SIZE_ONLY)
8335    RExC_extralen += 2;  /* Account for LONGJMP. */
8336   nextchar(pRExC_state);
8337   if (freeze_paren) {
8338    if (RExC_npar > after_freeze)
8339     after_freeze = RExC_npar;
8340    RExC_npar = freeze_paren;
8341   }
8342   br = regbranch(pRExC_state, &flags, 0, depth+1);
8343
8344   if (br == NULL)
8345    return(NULL);
8346   REGTAIL(pRExC_state, lastbr, br);               /* BRANCH -> BRANCH. */
8347   lastbr = br;
8348   *flagp |= flags & (SPSTART | HASWIDTH | POSTPONED);
8349  }
8350
8351  if (have_branch || paren != ':') {
8352   /* Make a closing node, and hook it on the end. */
8353   switch (paren) {
8354   case ':':
8355    ender = reg_node(pRExC_state, TAIL);
8356    break;
8357   case 1:
8358    ender = reganode(pRExC_state, CLOSE, parno);
8359    if (!SIZE_ONLY && RExC_seen & REG_SEEN_RECURSE) {
8360     DEBUG_OPTIMISE_MORE_r(PerlIO_printf(Perl_debug_log,
8361       "Setting close paren #%"IVdf" to %d\n",
8362       (IV)parno, REG_NODE_NUM(ender)));
8363     RExC_close_parens[parno-1]= ender;
8364     if (RExC_nestroot == parno)
8365      RExC_nestroot = 0;
8366    }
8367    Set_Node_Offset(ender,RExC_parse+1); /* MJD */
8368    Set_Node_Length(ender,1); /* MJD */
8369    break;
8370   case '<':
8371   case ',':
8372   case '=':
8373   case '!':
8374    *flagp &= ~HASWIDTH;
8375    /* FALL THROUGH */
8376   case '>':
8377    ender = reg_node(pRExC_state, SUCCEED);
8378    break;
8379   case 0:
8380    ender = reg_node(pRExC_state, END);
8381    if (!SIZE_ONLY) {
8382     assert(!RExC_opend); /* there can only be one! */
8383     RExC_opend = ender;
8384    }
8385    break;
8386   }
8387   REGTAIL(pRExC_state, lastbr, ender);
8388
8389   if (have_branch && !SIZE_ONLY) {
8390    if (depth==1)
8391     RExC_seen |= REG_TOP_LEVEL_BRANCHES;
8392
8393    /* Hook the tails of the branches to the closing node. */
8394    for (br = ret; br; br = regnext(br)) {
8395     const U8 op = PL_regkind[OP(br)];
8396     if (op == BRANCH) {
8397      REGTAIL_STUDY(pRExC_state, NEXTOPER(br), ender);
8398     }
8399     else if (op == BRANCHJ) {
8400      REGTAIL_STUDY(pRExC_state, NEXTOPER(NEXTOPER(br)), ender);
8401     }
8402    }
8403   }
8404  }
8405
8406  {
8407   const char *p;
8408   static const char parens[] = "=!<,>";
8409
8410   if (paren && (p = strchr(parens, paren))) {
8411    U8 node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
8412    int flag = (p - parens) > 1;
8413
8414    if (paren == '>')
8415     node = SUSPEND, flag = 0;
8416    reginsert(pRExC_state, node,ret, depth+1);
8417    Set_Node_Cur_Length(ret);
8418    Set_Node_Offset(ret, parse_start + 1);
8419    ret->flags = flag;
8420    REGTAIL_STUDY(pRExC_state, ret, reg_node(pRExC_state, TAIL));
8421   }
8422  }
8423
8424  /* Check for proper termination. */
8425  if (paren) {
8426   RExC_flags = oregflags;
8427   if (RExC_parse >= RExC_end || *nextchar(pRExC_state) != ')') {
8428    RExC_parse = oregcomp_parse;
8429    vFAIL("Unmatched (");
8430   }
8431  }
8432  else if (!paren && RExC_parse < RExC_end) {
8433   if (*RExC_parse == ')') {
8434    RExC_parse++;
8435    vFAIL("Unmatched )");
8436   }
8437   else
8438    FAIL("Junk on end of regexp"); /* "Can't happen". */
8439   /* NOTREACHED */
8440  }
8441
8442  if (RExC_in_lookbehind) {
8443   RExC_in_lookbehind--;
8444  }
8445  if (after_freeze > RExC_npar)
8446   RExC_npar = after_freeze;
8447  return(ret);
8448 }
8449
8450 /*
8451  - regbranch - one alternative of an | operator
8452  *
8453  * Implements the concatenation operator.
8454  */
8455 STATIC regnode *
8456 S_regbranch(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, I32 first, U32 depth)
8457 {
8458  dVAR;
8459  register regnode *ret;
8460  register regnode *chain = NULL;
8461  register regnode *latest;
8462  I32 flags = 0, c = 0;
8463  GET_RE_DEBUG_FLAGS_DECL;
8464
8465  PERL_ARGS_ASSERT_REGBRANCH;
8466
8467  DEBUG_PARSE("brnc");
8468
8469  if (first)
8470   ret = NULL;
8471  else {
8472   if (!SIZE_ONLY && RExC_extralen)
8473    ret = reganode(pRExC_state, BRANCHJ,0);
8474   else {
8475    ret = reg_node(pRExC_state, BRANCH);
8476    Set_Node_Length(ret, 1);
8477   }
8478  }
8479
8480  if (!first && SIZE_ONLY)
8481   RExC_extralen += 1;   /* BRANCHJ */
8482
8483  *flagp = WORST;   /* Tentatively. */
8484
8485  RExC_parse--;
8486  nextchar(pRExC_state);
8487  while (RExC_parse < RExC_end && *RExC_parse != '|' && *RExC_parse != ')') {
8488   flags &= ~TRYAGAIN;
8489   latest = regpiece(pRExC_state, &flags,depth+1);
8490   if (latest == NULL) {
8491    if (flags & TRYAGAIN)
8492     continue;
8493    return(NULL);
8494   }
8495   else if (ret == NULL)
8496    ret = latest;
8497   *flagp |= flags&(HASWIDTH|POSTPONED);
8498   if (chain == NULL)  /* First piece. */
8499    *flagp |= flags&SPSTART;
8500   else {
8501    RExC_naughty++;
8502    REGTAIL(pRExC_state, chain, latest);
8503   }
8504   chain = latest;
8505   c++;
8506  }
8507  if (chain == NULL) { /* Loop ran zero times. */
8508   chain = reg_node(pRExC_state, NOTHING);
8509   if (ret == NULL)
8510    ret = chain;
8511  }
8512  if (c == 1) {
8513   *flagp |= flags&SIMPLE;
8514  }
8515
8516  return ret;
8517 }
8518
8519 /*
8520  - regpiece - something followed by possible [*+?]
8521  *
8522  * Note that the branching code sequences used for ? and the general cases
8523  * of * and + are somewhat optimized:  they use the same NOTHING node as
8524  * both the endmarker for their branch list and the body of the last branch.
8525  * It might seem that this node could be dispensed with entirely, but the
8526  * endmarker role is not redundant.
8527  */
8528 STATIC regnode *
8529 S_regpiece(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, U32 depth)
8530 {
8531  dVAR;
8532  register regnode *ret;
8533  register char op;
8534  register char *next;
8535  I32 flags;
8536  const char * const origparse = RExC_parse;
8537  I32 min;
8538  I32 max = REG_INFTY;
8539 #ifdef RE_TRACK_PATTERN_OFFSETS
8540  char *parse_start;
8541 #endif
8542  const char *maxpos = NULL;
8543  GET_RE_DEBUG_FLAGS_DECL;
8544
8545  PERL_ARGS_ASSERT_REGPIECE;
8546
8547  DEBUG_PARSE("piec");
8548
8549  ret = regatom(pRExC_state, &flags,depth+1);
8550  if (ret == NULL) {
8551   if (flags & TRYAGAIN)
8552    *flagp |= TRYAGAIN;
8553   return(NULL);
8554  }
8555
8556  op = *RExC_parse;
8557
8558  if (op == '{' && regcurly(RExC_parse)) {
8559   maxpos = NULL;
8560 #ifdef RE_TRACK_PATTERN_OFFSETS
8561   parse_start = RExC_parse; /* MJD */
8562 #endif
8563   next = RExC_parse + 1;
8564   while (isDIGIT(*next) || *next == ',') {
8565    if (*next == ',') {
8566     if (maxpos)
8567      break;
8568     else
8569      maxpos = next;
8570    }
8571    next++;
8572   }
8573   if (*next == '}') {  /* got one */
8574    if (!maxpos)
8575     maxpos = next;
8576    RExC_parse++;
8577    min = atoi(RExC_parse);
8578    if (*maxpos == ',')
8579     maxpos++;
8580    else
8581     maxpos = RExC_parse;
8582    max = atoi(maxpos);
8583    if (!max && *maxpos != '0')
8584     max = REG_INFTY;  /* meaning "infinity" */
8585    else if (max >= REG_INFTY)
8586     vFAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
8587    RExC_parse = next;
8588    nextchar(pRExC_state);
8589
8590   do_curly:
8591    if ((flags&SIMPLE)) {
8592     RExC_naughty += 2 + RExC_naughty / 2;
8593     reginsert(pRExC_state, CURLY, ret, depth+1);
8594     Set_Node_Offset(ret, parse_start+1); /* MJD */
8595     Set_Node_Cur_Length(ret);
8596    }
8597    else {
8598     regnode * const w = reg_node(pRExC_state, WHILEM);
8599
8600     w->flags = 0;
8601     REGTAIL(pRExC_state, ret, w);
8602     if (!SIZE_ONLY && RExC_extralen) {
8603      reginsert(pRExC_state, LONGJMP,ret, depth+1);
8604      reginsert(pRExC_state, NOTHING,ret, depth+1);
8605      NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
8606     }
8607     reginsert(pRExC_state, CURLYX,ret, depth+1);
8608         /* MJD hk */
8609     Set_Node_Offset(ret, parse_start+1);
8610     Set_Node_Length(ret,
8611         op == '{' ? (RExC_parse - parse_start) : 1);
8612
8613     if (!SIZE_ONLY && RExC_extralen)
8614      NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
8615     REGTAIL(pRExC_state, ret, reg_node(pRExC_state, NOTHING));
8616     if (SIZE_ONLY)
8617      RExC_whilem_seen++, RExC_extralen += 3;
8618     RExC_naughty += 4 + RExC_naughty; /* compound interest */
8619    }
8620    ret->flags = 0;
8621
8622    if (min > 0)
8623     *flagp = WORST;
8624    if (max > 0)
8625     *flagp |= HASWIDTH;
8626    if (max < min)
8627     vFAIL("Can't do {n,m} with n > m");
8628    if (!SIZE_ONLY) {
8629     ARG1_SET(ret, (U16)min);
8630     ARG2_SET(ret, (U16)max);
8631    }
8632
8633    goto nest_check;
8634   }
8635  }
8636
8637  if (!ISMULT1(op)) {
8638   *flagp = flags;
8639   return(ret);
8640  }
8641
8642 #if 0    /* Now runtime fix should be reliable. */
8643
8644  /* if this is reinstated, don't forget to put this back into perldiag:
8645
8646    =item Regexp *+ operand could be empty at {#} in regex m/%s/
8647
8648   (F) The part of the regexp subject to either the * or + quantifier
8649   could match an empty string. The {#} shows in the regular
8650   expression about where the problem was discovered.
8651
8652  */
8653
8654  if (!(flags&HASWIDTH) && op != '?')
8655  vFAIL("Regexp *+ operand could be empty");
8656 #endif
8657
8658 #ifdef RE_TRACK_PATTERN_OFFSETS
8659  parse_start = RExC_parse;
8660 #endif
8661  nextchar(pRExC_state);
8662
8663  *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
8664
8665  if (op == '*' && (flags&SIMPLE)) {
8666   reginsert(pRExC_state, STAR, ret, depth+1);
8667   ret->flags = 0;
8668   RExC_naughty += 4;
8669  }
8670  else if (op == '*') {
8671   min = 0;
8672   goto do_curly;
8673  }
8674  else if (op == '+' && (flags&SIMPLE)) {
8675   reginsert(pRExC_state, PLUS, ret, depth+1);
8676   ret->flags = 0;
8677   RExC_naughty += 3;
8678  }
8679  else if (op == '+') {
8680   min = 1;
8681   goto do_curly;
8682  }
8683  else if (op == '?') {
8684   min = 0; max = 1;
8685   goto do_curly;
8686  }
8687   nest_check:
8688  if (!SIZE_ONLY && !(flags&(HASWIDTH|POSTPONED)) && max > REG_INFTY/3) {
8689   ckWARN3reg(RExC_parse,
8690     "%.*s matches null string many times",
8691     (int)(RExC_parse >= origparse ? RExC_parse - origparse : 0),
8692     origparse);
8693  }
8694
8695  if (RExC_parse < RExC_end && *RExC_parse == '?') {
8696   nextchar(pRExC_state);
8697   reginsert(pRExC_state, MINMOD, ret, depth+1);
8698   REGTAIL(pRExC_state, ret, ret + NODE_STEP_REGNODE);
8699  }
8700 #ifndef REG_ALLOW_MINMOD_SUSPEND
8701  else
8702 #endif
8703  if (RExC_parse < RExC_end && *RExC_parse == '+') {
8704   regnode *ender;
8705   nextchar(pRExC_state);
8706   ender = reg_node(pRExC_state, SUCCEED);
8707   REGTAIL(pRExC_state, ret, ender);
8708   reginsert(pRExC_state, SUSPEND, ret, depth+1);
8709   ret->flags = 0;
8710   ender = reg_node(pRExC_state, TAIL);
8711   REGTAIL(pRExC_state, ret, ender);
8712   /*ret= ender;*/
8713  }
8714
8715  if (RExC_parse < RExC_end && ISMULT2(RExC_parse)) {
8716   RExC_parse++;
8717   vFAIL("Nested quantifiers");
8718  }
8719
8720  return(ret);
8721 }
8722
8723
8724 /* reg_namedseq(pRExC_state,UVp, UV depth)
8725
8726    This is expected to be called by a parser routine that has
8727    recognized '\N' and needs to handle the rest. RExC_parse is
8728    expected to point at the first char following the N at the time
8729    of the call.
8730
8731    The \N may be inside (indicated by valuep not being NULL) or outside a
8732    character class.
8733
8734    \N may begin either a named sequence, or if outside a character class, mean
8735    to match a non-newline.  For non single-quoted regexes, the tokenizer has
8736    attempted to decide which, and in the case of a named sequence converted it
8737    into one of the forms: \N{} (if the sequence is null), or \N{U+c1.c2...},
8738    where c1... are the characters in the sequence.  For single-quoted regexes,
8739    the tokenizer passes the \N sequence through unchanged; this code will not
8740    attempt to determine this nor expand those.  The net effect is that if the
8741    beginning of the passed-in pattern isn't '{U+' or there is no '}', it
8742    signals that this \N occurrence means to match a non-newline.
8743
8744    Only the \N{U+...} form should occur in a character class, for the same
8745    reason that '.' inside a character class means to just match a period: it
8746    just doesn't make sense.
8747
8748    If valuep is non-null then it is assumed that we are parsing inside
8749    of a charclass definition and the first codepoint in the resolved
8750    string is returned via *valuep and the routine will return NULL.
8751    In this mode if a multichar string is returned from the charnames
8752    handler, a warning will be issued, and only the first char in the
8753    sequence will be examined. If the string returned is zero length
8754    then the value of *valuep is undefined and NON-NULL will
8755    be returned to indicate failure. (This will NOT be a valid pointer
8756    to a regnode.)
8757
8758    If valuep is null then it is assumed that we are parsing normal text and a
8759    new EXACT node is inserted into the program containing the resolved string,
8760    and a pointer to the new node is returned.  But if the string is zero length
8761    a NOTHING node is emitted instead.
8762
8763    On success RExC_parse is set to the char following the endbrace.
8764    Parsing failures will generate a fatal error via vFAIL(...)
8765  */
8766 STATIC regnode *
8767 S_reg_namedseq(pTHX_ RExC_state_t *pRExC_state, UV *valuep, I32 *flagp, U32 depth)
8768 {
8769  char * endbrace;    /* '}' following the name */
8770  regnode *ret = NULL;
8771  char* p;
8772
8773  GET_RE_DEBUG_FLAGS_DECL;
8774
8775  PERL_ARGS_ASSERT_REG_NAMEDSEQ;
8776
8777  GET_RE_DEBUG_FLAGS;
8778
8779  /* The [^\n] meaning of \N ignores spaces and comments under the /x
8780  * modifier.  The other meaning does not */
8781  p = (RExC_flags & RXf_PMf_EXTENDED)
8782   ? regwhite( pRExC_state, RExC_parse )
8783   : RExC_parse;
8784
8785  /* Disambiguate between \N meaning a named character versus \N meaning
8786  * [^\n].  The former is assumed when it can't be the latter. */
8787  if (*p != '{' || regcurly(p)) {
8788   RExC_parse = p;
8789   if (valuep) {
8790    /* no bare \N in a charclass */
8791    vFAIL("\\N in a character class must be a named character: \\N{...}");
8792   }
8793   nextchar(pRExC_state);
8794   ret = reg_node(pRExC_state, REG_ANY);
8795   *flagp |= HASWIDTH|SIMPLE;
8796   RExC_naughty++;
8797   RExC_parse--;
8798   Set_Node_Length(ret, 1); /* MJD */
8799   return ret;
8800  }
8801
8802  /* Here, we have decided it should be a named sequence */
8803
8804  /* The test above made sure that the next real character is a '{', but
8805  * under the /x modifier, it could be separated by space (or a comment and
8806  * \n) and this is not allowed (for consistency with \x{...} and the
8807  * tokenizer handling of \N{NAME}). */
8808  if (*RExC_parse != '{') {
8809   vFAIL("Missing braces on \\N{}");
8810  }
8811
8812  RExC_parse++; /* Skip past the '{' */
8813
8814  if (! (endbrace = strchr(RExC_parse, '}')) /* no trailing brace */
8815   || ! (endbrace == RExC_parse  /* nothing between the {} */
8816    || (endbrace - RExC_parse >= 2 /* U+ (bad hex is checked below */
8817     && strnEQ(RExC_parse, "U+", 2)))) /* for a better error msg) */
8818  {
8819   if (endbrace) RExC_parse = endbrace; /* position msg's '<--HERE' */
8820   vFAIL("\\N{NAME} must be resolved by the lexer");
8821  }
8822
8823  if (endbrace == RExC_parse) {   /* empty: \N{} */
8824   if (! valuep) {
8825    RExC_parse = endbrace + 1;
8826    return reg_node(pRExC_state,NOTHING);
8827   }
8828
8829   if (SIZE_ONLY) {
8830    ckWARNreg(RExC_parse,
8831      "Ignoring zero length \\N{} in character class"
8832    );
8833    RExC_parse = endbrace + 1;
8834   }
8835   *valuep = 0;
8836   return (regnode *) &RExC_parse; /* Invalid regnode pointer */
8837  }
8838
8839  REQUIRE_UTF8; /* named sequences imply Unicode semantics */
8840  RExC_parse += 2; /* Skip past the 'U+' */
8841
8842  if (valuep) {   /* In a bracketed char class */
8843   /* We only pay attention to the first char of
8844   multichar strings being returned. I kinda wonder
8845   if this makes sense as it does change the behaviour
8846   from earlier versions, OTOH that behaviour was broken
8847   as well. XXX Solution is to recharacterize as
8848   [rest-of-class]|multi1|multi2... */
8849
8850   STRLEN length_of_hex;
8851   I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
8852    | PERL_SCAN_DISALLOW_PREFIX
8853    | (SIZE_ONLY ? PERL_SCAN_SILENT_ILLDIGIT : 0);
8854
8855   char * endchar = RExC_parse + strcspn(RExC_parse, ".}");
8856   if (endchar < endbrace) {
8857    ckWARNreg(endchar, "Using just the first character returned by \\N{} in character class");
8858   }
8859
8860   length_of_hex = (STRLEN)(endchar - RExC_parse);
8861   *valuep = grok_hex(RExC_parse, &length_of_hex, &flags, NULL);
8862
8863   /* The tokenizer should have guaranteed validity, but it's possible to
8864   * bypass it by using single quoting, so check */
8865   if (length_of_hex == 0
8866    || length_of_hex != (STRLEN)(endchar - RExC_parse) )
8867   {
8868    RExC_parse += length_of_hex; /* Includes all the valid */
8869    RExC_parse += (RExC_orig_utf8) /* point to after 1st invalid */
8870        ? UTF8SKIP(RExC_parse)
8871        : 1;
8872    /* Guard against malformed utf8 */
8873    if (RExC_parse >= endchar) RExC_parse = endchar;
8874    vFAIL("Invalid hexadecimal number in \\N{U+...}");
8875   }
8876
8877   RExC_parse = endbrace + 1;
8878   if (endchar == endbrace) return NULL;
8879
8880   ret = (regnode *) &RExC_parse; /* Invalid regnode pointer */
8881  }
8882  else { /* Not a char class */
8883
8884   /* What is done here is to convert this to a sub-pattern of the form
8885   * (?:\x{char1}\x{char2}...)
8886   * and then call reg recursively.  That way, it retains its atomicness,
8887   * while not having to worry about special handling that some code
8888   * points may have.  toke.c has converted the original Unicode values
8889   * to native, so that we can just pass on the hex values unchanged.  We
8890   * do have to set a flag to keep recoding from happening in the
8891   * recursion */
8892
8893   SV * substitute_parse = newSVpvn_flags("?:", 2, SVf_UTF8|SVs_TEMP);
8894   STRLEN len;
8895   char *endchar;     /* Points to '.' or '}' ending cur char in the input
8896        stream */
8897   char *orig_end = RExC_end;
8898
8899   while (RExC_parse < endbrace) {
8900
8901    /* Code points are separated by dots.  If none, there is only one
8902    * code point, and is terminated by the brace */
8903    endchar = RExC_parse + strcspn(RExC_parse, ".}");
8904
8905    /* Convert to notation the rest of the code understands */
8906    sv_catpv(substitute_parse, "\\x{");
8907    sv_catpvn(substitute_parse, RExC_parse, endchar - RExC_parse);
8908    sv_catpv(substitute_parse, "}");
8909
8910    /* Point to the beginning of the next character in the sequence. */
8911    RExC_parse = endchar + 1;
8912   }
8913   sv_catpv(substitute_parse, ")");
8914
8915   RExC_parse = SvPV(substitute_parse, len);
8916
8917   /* Don't allow empty number */
8918   if (len < 8) {
8919    vFAIL("Invalid hexadecimal number in \\N{U+...}");
8920   }
8921   RExC_end = RExC_parse + len;
8922
8923   /* The values are Unicode, and therefore not subject to recoding */
8924   RExC_override_recoding = 1;
8925
8926   ret = reg(pRExC_state, 1, flagp, depth+1);
8927
8928   RExC_parse = endbrace;
8929   RExC_end = orig_end;
8930   RExC_override_recoding = 0;
8931
8932   nextchar(pRExC_state);
8933  }
8934
8935  return ret;
8936 }
8937
8938
8939 /*
8940  * reg_recode
8941  *
8942  * It returns the code point in utf8 for the value in *encp.
8943  *    value: a code value in the source encoding
8944  *    encp:  a pointer to an Encode object
8945  *
8946  * If the result from Encode is not a single character,
8947  * it returns U+FFFD (Replacement character) and sets *encp to NULL.
8948  */
8949 STATIC UV
8950 S_reg_recode(pTHX_ const char value, SV **encp)
8951 {
8952  STRLEN numlen = 1;
8953  SV * const sv = newSVpvn_flags(&value, numlen, SVs_TEMP);
8954  const char * const s = *encp ? sv_recode_to_utf8(sv, *encp) : SvPVX(sv);
8955  const STRLEN newlen = SvCUR(sv);
8956  UV uv = UNICODE_REPLACEMENT;
8957
8958  PERL_ARGS_ASSERT_REG_RECODE;
8959
8960  if (newlen)
8961   uv = SvUTF8(sv)
8962    ? utf8n_to_uvchr((U8*)s, newlen, &numlen, UTF8_ALLOW_DEFAULT)
8963    : *(U8*)s;
8964
8965  if (!newlen || numlen != newlen) {
8966   uv = UNICODE_REPLACEMENT;
8967   *encp = NULL;
8968  }
8969  return uv;
8970 }
8971
8972
8973 /*
8974  - regatom - the lowest level
8975
8976    Try to identify anything special at the start of the pattern. If there
8977    is, then handle it as required. This may involve generating a single regop,
8978    such as for an assertion; or it may involve recursing, such as to
8979    handle a () structure.
8980
8981    If the string doesn't start with something special then we gobble up
8982    as much literal text as we can.
8983
8984    Once we have been able to handle whatever type of thing started the
8985    sequence, we return.
8986
8987    Note: we have to be careful with escapes, as they can be both literal
8988    and special, and in the case of \10 and friends can either, depending
8989    on context. Specifically there are two separate switches for handling
8990    escape sequences, with the one for handling literal escapes requiring
8991    a dummy entry for all of the special escapes that are actually handled
8992    by the other.
8993 */
8994
8995 STATIC regnode *
8996 S_regatom(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, U32 depth)
8997 {
8998  dVAR;
8999  register regnode *ret = NULL;
9000  I32 flags;
9001  char *parse_start = RExC_parse;
9002  U8 op;
9003  GET_RE_DEBUG_FLAGS_DECL;
9004  DEBUG_PARSE("atom");
9005  *flagp = WORST;  /* Tentatively. */
9006
9007  PERL_ARGS_ASSERT_REGATOM;
9008
9009 tryagain:
9010  switch ((U8)*RExC_parse) {
9011  case '^':
9012   RExC_seen_zerolen++;
9013   nextchar(pRExC_state);
9014   if (RExC_flags & RXf_PMf_MULTILINE)
9015    ret = reg_node(pRExC_state, MBOL);
9016   else if (RExC_flags & RXf_PMf_SINGLELINE)
9017    ret = reg_node(pRExC_state, SBOL);
9018   else
9019    ret = reg_node(pRExC_state, BOL);
9020   Set_Node_Length(ret, 1); /* MJD */
9021   break;
9022  case '$':
9023   nextchar(pRExC_state);
9024   if (*RExC_parse)
9025    RExC_seen_zerolen++;
9026   if (RExC_flags & RXf_PMf_MULTILINE)
9027    ret = reg_node(pRExC_state, MEOL);
9028   else if (RExC_flags & RXf_PMf_SINGLELINE)
9029    ret = reg_node(pRExC_state, SEOL);
9030   else
9031    ret = reg_node(pRExC_state, EOL);
9032   Set_Node_Length(ret, 1); /* MJD */
9033   break;
9034  case '.':
9035   nextchar(pRExC_state);
9036   if (RExC_flags & RXf_PMf_SINGLELINE)
9037    ret = reg_node(pRExC_state, SANY);
9038   else
9039    ret = reg_node(pRExC_state, REG_ANY);
9040   *flagp |= HASWIDTH|SIMPLE;
9041   RExC_naughty++;
9042   Set_Node_Length(ret, 1); /* MJD */
9043   break;
9044  case '[':
9045  {
9046   char * const oregcomp_parse = ++RExC_parse;
9047   ret = regclass(pRExC_state,depth+1);
9048   if (*RExC_parse != ']') {
9049    RExC_parse = oregcomp_parse;
9050    vFAIL("Unmatched [");
9051   }
9052   nextchar(pRExC_state);
9053   *flagp |= HASWIDTH|SIMPLE;
9054   Set_Node_Length(ret, RExC_parse - oregcomp_parse + 1); /* MJD */
9055   break;
9056  }
9057  case '(':
9058   nextchar(pRExC_state);
9059   ret = reg(pRExC_state, 1, &flags,depth+1);
9060   if (ret == NULL) {
9061     if (flags & TRYAGAIN) {
9062      if (RExC_parse == RExC_end) {
9063       /* Make parent create an empty node if needed. */
9064       *flagp |= TRYAGAIN;
9065       return(NULL);
9066      }
9067      goto tryagain;
9068     }
9069     return(NULL);
9070   }
9071   *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE|POSTPONED);
9072   break;
9073  case '|':
9074  case ')':
9075   if (flags & TRYAGAIN) {
9076    *flagp |= TRYAGAIN;
9077    return NULL;
9078   }
9079   vFAIL("Internal urp");
9080         /* Supposed to be caught earlier. */
9081   break;
9082  case '{':
9083   if (!regcurly(RExC_parse)) {
9084    RExC_parse++;
9085    goto defchar;
9086   }
9087   /* FALL THROUGH */
9088  case '?':
9089  case '+':
9090  case '*':
9091   RExC_parse++;
9092   vFAIL("Quantifier follows nothing");
9093   break;
9094  case '\\':
9095   /* Special Escapes
9096
9097   This switch handles escape sequences that resolve to some kind
9098   of special regop and not to literal text. Escape sequnces that
9099   resolve to literal text are handled below in the switch marked
9100   "Literal Escapes".
9101
9102   Every entry in this switch *must* have a corresponding entry
9103   in the literal escape switch. However, the opposite is not
9104   required, as the default for this switch is to jump to the
9105   literal text handling code.
9106   */
9107   switch ((U8)*++RExC_parse) {
9108   /* Special Escapes */
9109   case 'A':
9110    RExC_seen_zerolen++;
9111    ret = reg_node(pRExC_state, SBOL);
9112    *flagp |= SIMPLE;
9113    goto finish_meta_pat;
9114   case 'G':
9115    ret = reg_node(pRExC_state, GPOS);
9116    RExC_seen |= REG_SEEN_GPOS;
9117    *flagp |= SIMPLE;
9118    goto finish_meta_pat;
9119   case 'K':
9120    RExC_seen_zerolen++;
9121    ret = reg_node(pRExC_state, KEEPS);
9122    *flagp |= SIMPLE;
9123    /* XXX:dmq : disabling in-place substitution seems to
9124    * be necessary here to avoid cases of memory corruption, as
9125    * with: C<$_="x" x 80; s/x\K/y/> -- rgs
9126    */
9127    RExC_seen |= REG_SEEN_LOOKBEHIND;
9128    goto finish_meta_pat;
9129   case 'Z':
9130    ret = reg_node(pRExC_state, SEOL);
9131    *flagp |= SIMPLE;
9132    RExC_seen_zerolen++;  /* Do not optimize RE away */
9133    goto finish_meta_pat;
9134   case 'z':
9135    ret = reg_node(pRExC_state, EOS);
9136    *flagp |= SIMPLE;
9137    RExC_seen_zerolen++;  /* Do not optimize RE away */
9138    goto finish_meta_pat;
9139   case 'C':
9140    ret = reg_node(pRExC_state, CANY);
9141    RExC_seen |= REG_SEEN_CANY;
9142    *flagp |= HASWIDTH|SIMPLE;
9143    goto finish_meta_pat;
9144   case 'X':
9145    ret = reg_node(pRExC_state, CLUMP);
9146    *flagp |= HASWIDTH;
9147    goto finish_meta_pat;
9148   case 'w':
9149    switch (get_regex_charset(RExC_flags)) {
9150     case REGEX_LOCALE_CHARSET:
9151      op = ALNUML;
9152      break;
9153     case REGEX_UNICODE_CHARSET:
9154      op = ALNUMU;
9155      break;
9156     case REGEX_ASCII_RESTRICTED_CHARSET:
9157     case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
9158      op = ALNUMA;
9159      break;
9160     case REGEX_DEPENDS_CHARSET:
9161      op = ALNUM;
9162      break;
9163     default:
9164      goto bad_charset;
9165    }
9166    ret = reg_node(pRExC_state, op);
9167    *flagp |= HASWIDTH|SIMPLE;
9168    goto finish_meta_pat;
9169   case 'W':
9170    switch (get_regex_charset(RExC_flags)) {
9171     case REGEX_LOCALE_CHARSET:
9172      op = NALNUML;
9173      break;
9174     case REGEX_UNICODE_CHARSET:
9175      op = NALNUMU;
9176      break;
9177     case REGEX_ASCII_RESTRICTED_CHARSET:
9178     case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
9179      op = NALNUMA;
9180      break;
9181     case REGEX_DEPENDS_CHARSET:
9182      op = NALNUM;
9183      break;
9184     default:
9185      goto bad_charset;
9186    }
9187    ret = reg_node(pRExC_state, op);
9188    *flagp |= HASWIDTH|SIMPLE;
9189    goto finish_meta_pat;
9190   case 'b':
9191    RExC_seen_zerolen++;
9192    RExC_seen |= REG_SEEN_LOOKBEHIND;
9193    switch (get_regex_charset(RExC_flags)) {
9194     case REGEX_LOCALE_CHARSET:
9195      op = BOUNDL;
9196      break;
9197     case REGEX_UNICODE_CHARSET:
9198      op = BOUNDU;
9199      break;
9200     case REGEX_ASCII_RESTRICTED_CHARSET:
9201     case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
9202      op = BOUNDA;
9203      break;
9204     case REGEX_DEPENDS_CHARSET:
9205      op = BOUND;
9206      break;
9207     default:
9208      goto bad_charset;
9209    }
9210    ret = reg_node(pRExC_state, op);
9211    FLAGS(ret) = get_regex_charset(RExC_flags);
9212    *flagp |= SIMPLE;
9213    if (! SIZE_ONLY && (U8) *(RExC_parse + 1) == '{') {
9214     ckWARNregdep(RExC_parse, "\"\\b{\" is deprecated; use \"\\b\\{\" instead");
9215    }
9216    goto finish_meta_pat;
9217   case 'B':
9218    RExC_seen_zerolen++;
9219    RExC_seen |= REG_SEEN_LOOKBEHIND;
9220    switch (get_regex_charset(RExC_flags)) {
9221     case REGEX_LOCALE_CHARSET:
9222      op = NBOUNDL;
9223      break;
9224     case REGEX_UNICODE_CHARSET:
9225      op = NBOUNDU;
9226      break;
9227     case REGEX_ASCII_RESTRICTED_CHARSET:
9228     case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
9229      op = NBOUNDA;
9230      break;
9231     case REGEX_DEPENDS_CHARSET:
9232      op = NBOUND;
9233      break;
9234     default:
9235      goto bad_charset;
9236    }
9237    ret = reg_node(pRExC_state, op);
9238    FLAGS(ret) = get_regex_charset(RExC_flags);
9239    *flagp |= SIMPLE;
9240    if (! SIZE_ONLY && (U8) *(RExC_parse + 1) == '{') {
9241     ckWARNregdep(RExC_parse, "\"\\B{\" is deprecated; use \"\\B\\{\" instead");
9242    }
9243    goto finish_meta_pat;
9244   case 's':
9245    switch (get_regex_charset(RExC_flags)) {
9246     case REGEX_LOCALE_CHARSET:
9247      op = SPACEL;
9248      break;
9249     case REGEX_UNICODE_CHARSET:
9250      op = SPACEU;
9251      break;
9252     case REGEX_ASCII_RESTRICTED_CHARSET:
9253     case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
9254      op = SPACEA;
9255      break;
9256     case REGEX_DEPENDS_CHARSET:
9257      op = SPACE;
9258      break;
9259     default:
9260      goto bad_charset;
9261    }
9262    ret = reg_node(pRExC_state, op);
9263    *flagp |= HASWIDTH|SIMPLE;
9264    goto finish_meta_pat;
9265   case 'S':
9266    switch (get_regex_charset(RExC_flags)) {
9267     case REGEX_LOCALE_CHARSET:
9268      op = NSPACEL;
9269      break;
9270     case REGEX_UNICODE_CHARSET:
9271      op = NSPACEU;
9272      break;
9273     case REGEX_ASCII_RESTRICTED_CHARSET:
9274     case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
9275      op = NSPACEA;
9276      break;
9277     case REGEX_DEPENDS_CHARSET:
9278      op = NSPACE;
9279      break;
9280     default:
9281      goto bad_charset;
9282    }
9283    ret = reg_node(pRExC_state, op);
9284    *flagp |= HASWIDTH|SIMPLE;
9285    goto finish_meta_pat;
9286   case 'd':
9287    switch (get_regex_charset(RExC_flags)) {
9288     case REGEX_LOCALE_CHARSET:
9289      op = DIGITL;
9290      break;
9291     case REGEX_ASCII_RESTRICTED_CHARSET:
9292     case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
9293      op = DIGITA;
9294      break;
9295     case REGEX_DEPENDS_CHARSET: /* No difference between these */
9296     case REGEX_UNICODE_CHARSET:
9297      op = DIGIT;
9298      break;
9299     default:
9300      goto bad_charset;
9301    }
9302    ret = reg_node(pRExC_state, op);
9303    *flagp |= HASWIDTH|SIMPLE;
9304    goto finish_meta_pat;
9305   case 'D':
9306    switch (get_regex_charset(RExC_flags)) {
9307     case REGEX_LOCALE_CHARSET:
9308      op = NDIGITL;
9309      break;
9310     case REGEX_ASCII_RESTRICTED_CHARSET:
9311     case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
9312      op = NDIGITA;
9313      break;
9314     case REGEX_DEPENDS_CHARSET: /* No difference between these */
9315     case REGEX_UNICODE_CHARSET:
9316      op = NDIGIT;
9317      break;
9318     default:
9319      goto bad_charset;
9320    }
9321    ret = reg_node(pRExC_state, op);
9322    *flagp |= HASWIDTH|SIMPLE;
9323    goto finish_meta_pat;
9324   case 'R':
9325    ret = reg_node(pRExC_state, LNBREAK);
9326    *flagp |= HASWIDTH|SIMPLE;
9327    goto finish_meta_pat;
9328   case 'h':
9329    ret = reg_node(pRExC_state, HORIZWS);
9330    *flagp |= HASWIDTH|SIMPLE;
9331    goto finish_meta_pat;
9332   case 'H':
9333    ret = reg_node(pRExC_state, NHORIZWS);
9334    *flagp |= HASWIDTH|SIMPLE;
9335    goto finish_meta_pat;
9336   case 'v':
9337    ret = reg_node(pRExC_state, VERTWS);
9338    *flagp |= HASWIDTH|SIMPLE;
9339    goto finish_meta_pat;
9340   case 'V':
9341    ret = reg_node(pRExC_state, NVERTWS);
9342    *flagp |= HASWIDTH|SIMPLE;
9343   finish_meta_pat:
9344    nextchar(pRExC_state);
9345    Set_Node_Length(ret, 2); /* MJD */
9346    break;
9347   case 'p':
9348   case 'P':
9349    {
9350     char* const oldregxend = RExC_end;
9351 #ifdef DEBUGGING
9352     char* parse_start = RExC_parse - 2;
9353 #endif
9354
9355     if (RExC_parse[1] == '{') {
9356     /* a lovely hack--pretend we saw [\pX] instead */
9357      RExC_end = strchr(RExC_parse, '}');
9358      if (!RExC_end) {
9359       const U8 c = (U8)*RExC_parse;
9360       RExC_parse += 2;
9361       RExC_end = oldregxend;
9362       vFAIL2("Missing right brace on \\%c{}", c);
9363      }
9364      RExC_end++;
9365     }
9366     else {
9367      RExC_end = RExC_parse + 2;
9368      if (RExC_end > oldregxend)
9369       RExC_end = oldregxend;
9370     }
9371     RExC_parse--;
9372
9373     ret = regclass(pRExC_state,depth+1);
9374
9375     RExC_end = oldregxend;
9376     RExC_parse--;
9377
9378     Set_Node_Offset(ret, parse_start + 2);
9379     Set_Node_Cur_Length(ret);
9380     nextchar(pRExC_state);
9381     *flagp |= HASWIDTH|SIMPLE;
9382    }
9383    break;
9384   case 'N':
9385    /* Handle \N and \N{NAME} here and not below because it can be
9386    multicharacter. join_exact() will join them up later on.
9387    Also this makes sure that things like /\N{BLAH}+/ and
9388    \N{BLAH} being multi char Just Happen. dmq*/
9389    ++RExC_parse;
9390    ret= reg_namedseq(pRExC_state, NULL, flagp, depth);
9391    break;
9392   case 'k':    /* Handle \k<NAME> and \k'NAME' */
9393   parse_named_seq:
9394   {
9395    char ch= RExC_parse[1];
9396    if (ch != '<' && ch != '\'' && ch != '{') {
9397     RExC_parse++;
9398     vFAIL2("Sequence %.2s... not terminated",parse_start);
9399    } else {
9400     /* this pretty much dupes the code for (?P=...) in reg(), if
9401     you change this make sure you change that */
9402     char* name_start = (RExC_parse += 2);
9403     U32 num = 0;
9404     SV *sv_dat = reg_scan_name(pRExC_state,
9405      SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
9406     ch= (ch == '<') ? '>' : (ch == '{') ? '}' : '\'';
9407     if (RExC_parse == name_start || *RExC_parse != ch)
9408      vFAIL2("Sequence %.3s... not terminated",parse_start);
9409
9410     if (!SIZE_ONLY) {
9411      num = add_data( pRExC_state, 1, "S" );
9412      RExC_rxi->data->data[num]=(void*)sv_dat;
9413      SvREFCNT_inc_simple_void(sv_dat);
9414     }
9415
9416     RExC_sawback = 1;
9417     ret = reganode(pRExC_state,
9418        ((! FOLD)
9419         ? NREF
9420         : (MORE_ASCII_RESTRICTED)
9421         ? NREFFA
9422         : (AT_LEAST_UNI_SEMANTICS)
9423          ? NREFFU
9424          : (LOC)
9425          ? NREFFL
9426          : NREFF),
9427         num);
9428     *flagp |= HASWIDTH;
9429
9430     /* override incorrect value set in reganode MJD */
9431     Set_Node_Offset(ret, parse_start+1);
9432     Set_Node_Cur_Length(ret); /* MJD */
9433     nextchar(pRExC_state);
9434
9435    }
9436    break;
9437   }
9438   case 'g':
9439   case '1': case '2': case '3': case '4':
9440   case '5': case '6': case '7': case '8': case '9':
9441    {
9442     I32 num;
9443     bool isg = *RExC_parse == 'g';
9444     bool isrel = 0;
9445     bool hasbrace = 0;
9446     if (isg) {
9447      RExC_parse++;
9448      if (*RExC_parse == '{') {
9449       RExC_parse++;
9450       hasbrace = 1;
9451      }
9452      if (*RExC_parse == '-') {
9453       RExC_parse++;
9454       isrel = 1;
9455      }
9456      if (hasbrace && !isDIGIT(*RExC_parse)) {
9457       if (isrel) RExC_parse--;
9458       RExC_parse -= 2;
9459       goto parse_named_seq;
9460     }   }
9461     num = atoi(RExC_parse);
9462     if (isg && num == 0)
9463      vFAIL("Reference to invalid group 0");
9464     if (isrel) {
9465      num = RExC_npar - num;
9466      if (num < 1)
9467       vFAIL("Reference to nonexistent or unclosed group");
9468     }
9469     if (!isg && num > 9 && num >= RExC_npar)
9470      goto defchar;
9471     else {
9472      char * const parse_start = RExC_parse - 1; /* MJD */
9473      while (isDIGIT(*RExC_parse))
9474       RExC_parse++;
9475      if (parse_start == RExC_parse - 1)
9476       vFAIL("Unterminated \\g... pattern");
9477      if (hasbrace) {
9478       if (*RExC_parse != '}')
9479        vFAIL("Unterminated \\g{...} pattern");
9480       RExC_parse++;
9481      }
9482      if (!SIZE_ONLY) {
9483       if (num > (I32)RExC_rx->nparens)
9484        vFAIL("Reference to nonexistent group");
9485      }
9486      RExC_sawback = 1;
9487      ret = reganode(pRExC_state,
9488         ((! FOLD)
9489          ? REF
9490          : (MORE_ASCII_RESTRICTED)
9491          ? REFFA
9492          : (AT_LEAST_UNI_SEMANTICS)
9493           ? REFFU
9494           : (LOC)
9495           ? REFFL
9496           : REFF),
9497          num);
9498      *flagp |= HASWIDTH;
9499
9500      /* override incorrect value set in reganode MJD */
9501      Set_Node_Offset(ret, parse_start+1);
9502      Set_Node_Cur_Length(ret); /* MJD */
9503      RExC_parse--;
9504      nextchar(pRExC_state);
9505     }
9506    }
9507    break;
9508   case '\0':
9509    if (RExC_parse >= RExC_end)
9510     FAIL("Trailing \\");
9511    /* FALL THROUGH */
9512   default:
9513    /* Do not generate "unrecognized" warnings here, we fall
9514    back into the quick-grab loop below */
9515    parse_start--;
9516    goto defchar;
9517   }
9518   break;
9519
9520  case '#':
9521   if (RExC_flags & RXf_PMf_EXTENDED) {
9522    if ( reg_skipcomment( pRExC_state ) )
9523     goto tryagain;
9524   }
9525   /* FALL THROUGH */
9526
9527  default:
9528
9529    parse_start = RExC_parse - 1;
9530
9531    RExC_parse++;
9532
9533   defchar: {
9534    register STRLEN len;
9535    register UV ender;
9536    register char *p;
9537    char *s;
9538    STRLEN foldlen;
9539    U8 tmpbuf[UTF8_MAXBYTES_CASE+1], *foldbuf;
9540    U8 node_type;
9541
9542    /* Is this a LATIN LOWER CASE SHARP S in an EXACTFU node?  If so,
9543    * it is folded to 'ss' even if not utf8 */
9544    bool is_exactfu_sharp_s;
9545
9546    ender = 0;
9547    node_type = ((! FOLD) ? EXACT
9548       : (LOC)
9549       ? EXACTFL
9550       : (MORE_ASCII_RESTRICTED)
9551        ? EXACTFA
9552        : (AT_LEAST_UNI_SEMANTICS)
9553        ? EXACTFU
9554        : EXACTF);
9555    ret = reg_node(pRExC_state, node_type);
9556    s = STRING(ret);
9557
9558    /* XXX The node can hold up to 255 bytes, yet this only goes to
9559    * 127.  I (khw) do not know why.  Keeping it somewhat less than
9560    * 255 allows us to not have to worry about overflow due to
9561    * converting to utf8 and fold expansion, but that value is
9562    * 255-UTF8_MAXBYTES_CASE.  join_exact() may join adjacent nodes
9563    * split up by this limit into a single one using the real max of
9564    * 255.  Even at 127, this breaks under rare circumstances.  If
9565    * folding, we do not want to split a node at a character that is a
9566    * non-final in a multi-char fold, as an input string could just
9567    * happen to want to match across the node boundary.  The join
9568    * would solve that problem if the join actually happens.  But a
9569    * series of more than two nodes in a row each of 127 would cause
9570    * the first join to succeed to get to 254, but then there wouldn't
9571    * be room for the next one, which could at be one of those split
9572    * multi-char folds.  I don't know of any fool-proof solution.  One
9573    * could back off to end with only a code point that isn't such a
9574    * non-final, but it is possible for there not to be any in the
9575    * entire node. */
9576    for (len = 0, p = RExC_parse - 1;
9577     len < 127 && p < RExC_end;
9578     len++)
9579    {
9580     char * const oldp = p;
9581
9582     if (RExC_flags & RXf_PMf_EXTENDED)
9583      p = regwhite( pRExC_state, p );
9584     switch ((U8)*p) {
9585     case '^':
9586     case '$':
9587     case '.':
9588     case '[':
9589     case '(':
9590     case ')':
9591     case '|':
9592      goto loopdone;
9593     case '\\':
9594      /* Literal Escapes Switch
9595
9596      This switch is meant to handle escape sequences that
9597      resolve to a literal character.
9598
9599      Every escape sequence that represents something
9600      else, like an assertion or a char class, is handled
9601      in the switch marked 'Special Escapes' above in this
9602      routine, but also has an entry here as anything that
9603      isn't explicitly mentioned here will be treated as
9604      an unescaped equivalent literal.
9605      */
9606
9607      switch ((U8)*++p) {
9608      /* These are all the special escapes. */
9609      case 'A':             /* Start assertion */
9610      case 'b': case 'B':   /* Word-boundary assertion*/
9611      case 'C':             /* Single char !DANGEROUS! */
9612      case 'd': case 'D':   /* digit class */
9613      case 'g': case 'G':   /* generic-backref, pos assertion */
9614      case 'h': case 'H':   /* HORIZWS */
9615      case 'k': case 'K':   /* named backref, keep marker */
9616      case 'N':             /* named char sequence */
9617      case 'p': case 'P':   /* Unicode property */
9618        case 'R':   /* LNBREAK */
9619      case 's': case 'S':   /* space class */
9620      case 'v': case 'V':   /* VERTWS */
9621      case 'w': case 'W':   /* word class */
9622      case 'X':             /* eXtended Unicode "combining character sequence" */
9623      case 'z': case 'Z':   /* End of line/string assertion */
9624       --p;
9625       goto loopdone;
9626
9627      /* Anything after here is an escape that resolves to a
9628      literal. (Except digits, which may or may not)
9629      */
9630      case 'n':
9631       ender = '\n';
9632       p++;
9633       break;
9634      case 'r':
9635       ender = '\r';
9636       p++;
9637       break;
9638      case 't':
9639       ender = '\t';
9640       p++;
9641       break;
9642      case 'f':
9643       ender = '\f';
9644       p++;
9645       break;
9646      case 'e':
9647       ender = ASCII_TO_NATIVE('\033');
9648       p++;
9649       break;
9650      case 'a':
9651       ender = ASCII_TO_NATIVE('\007');
9652       p++;
9653       break;
9654      case 'o':
9655       {
9656        STRLEN brace_len = len;
9657        UV result;
9658        const char* error_msg;
9659
9660        bool valid = grok_bslash_o(p,
9661              &result,
9662              &brace_len,
9663              &error_msg,
9664              1);
9665        p += brace_len;
9666        if (! valid) {
9667         RExC_parse = p; /* going to die anyway; point
9668             to exact spot of failure */
9669         vFAIL(error_msg);
9670        }
9671        else
9672        {
9673         ender = result;
9674        }
9675        if (PL_encoding && ender < 0x100) {
9676         goto recode_encoding;
9677        }
9678        if (ender > 0xff) {
9679         REQUIRE_UTF8;
9680        }
9681        break;
9682       }
9683      case 'x':
9684       if (*++p == '{') {
9685        char* const e = strchr(p, '}');
9686
9687        if (!e) {
9688         RExC_parse = p + 1;
9689         vFAIL("Missing right brace on \\x{}");
9690        }
9691        else {
9692         I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
9693          | PERL_SCAN_DISALLOW_PREFIX;
9694         STRLEN numlen = e - p - 1;
9695         ender = grok_hex(p + 1, &numlen, &flags, NULL);
9696         if (ender > 0xff)
9697          REQUIRE_UTF8;
9698         p = e + 1;
9699        }
9700       }
9701       else {
9702        I32 flags = PERL_SCAN_DISALLOW_PREFIX;
9703        STRLEN numlen = 2;
9704        ender = grok_hex(p, &numlen, &flags, NULL);
9705        p += numlen;
9706       }
9707       if (PL_encoding && ender < 0x100)
9708        goto recode_encoding;
9709       break;
9710      case 'c':
9711       p++;
9712       ender = grok_bslash_c(*p++, UTF, SIZE_ONLY);
9713       break;
9714      case '0': case '1': case '2': case '3':case '4':
9715      case '5': case '6': case '7': case '8':case '9':
9716       if (*p == '0' ||
9717        (isDIGIT(p[1]) && atoi(p) >= RExC_npar))
9718       {
9719        I32 flags = PERL_SCAN_SILENT_ILLDIGIT;
9720        STRLEN numlen = 3;
9721        ender = grok_oct(p, &numlen, &flags, NULL);
9722        if (ender > 0xff) {
9723         REQUIRE_UTF8;
9724        }
9725        p += numlen;
9726       }
9727       else {
9728        --p;
9729        goto loopdone;
9730       }
9731       if (PL_encoding && ender < 0x100)
9732        goto recode_encoding;
9733       break;
9734      recode_encoding:
9735       if (! RExC_override_recoding) {
9736        SV* enc = PL_encoding;
9737        ender = reg_recode((const char)(U8)ender, &enc);
9738        if (!enc && SIZE_ONLY)
9739         ckWARNreg(p, "Invalid escape in the specified encoding");
9740        REQUIRE_UTF8;
9741       }
9742       break;
9743      case '\0':
9744       if (p >= RExC_end)
9745        FAIL("Trailing \\");
9746       /* FALL THROUGH */
9747      default:
9748       if (!SIZE_ONLY&& isALPHA(*p)) {
9749        /* Include any { following the alpha to emphasize
9750        * that it could be part of an escape at some point
9751        * in the future */
9752        int len = (*(p + 1) == '{') ? 2 : 1;
9753        ckWARN3reg(p + len, "Unrecognized escape \\%.*s passed through", len, p);
9754       }
9755       goto normal_default;
9756      }
9757      break;
9758     default:
9759     normal_default:
9760      if (UTF8_IS_START(*p) && UTF) {
9761       STRLEN numlen;
9762       ender = utf8n_to_uvchr((U8*)p, RExC_end - p,
9763            &numlen, UTF8_ALLOW_DEFAULT);
9764       p += numlen;
9765      }
9766      else
9767       ender = (U8) *p++;
9768      break;
9769     } /* End of switch on the literal */
9770
9771     is_exactfu_sharp_s = (node_type == EXACTFU
9772          && ender == LATIN_SMALL_LETTER_SHARP_S);
9773     if ( RExC_flags & RXf_PMf_EXTENDED)
9774      p = regwhite( pRExC_state, p );
9775     if ((UTF && FOLD) || is_exactfu_sharp_s) {
9776      /* Prime the casefolded buffer.  Locale rules, which apply
9777      * only to code points < 256, aren't known until execution,
9778      * so for them, just output the original character using
9779      * utf8.  If we start to fold non-UTF patterns, be sure to
9780      * update join_exact() */
9781      if (LOC && ender < 256) {
9782       if (UNI_IS_INVARIANT(ender)) {
9783        *tmpbuf = (U8) ender;
9784        foldlen = 1;
9785       } else {
9786        *tmpbuf = UTF8_TWO_BYTE_HI(ender);
9787        *(tmpbuf + 1) = UTF8_TWO_BYTE_LO(ender);
9788        foldlen = 2;
9789       }
9790      }
9791      else if (isASCII(ender)) { /* Note: Here can't also be LOC
9792             */
9793       ender = toLOWER(ender);
9794       *tmpbuf = (U8) ender;
9795       foldlen = 1;
9796      }
9797      else if (! MORE_ASCII_RESTRICTED && ! LOC) {
9798
9799       /* Locale and /aa require more selectivity about the
9800       * fold, so are handled below.  Otherwise, here, just
9801       * use the fold */
9802       ender = toFOLD_uni(ender, tmpbuf, &foldlen);
9803      }
9804      else {
9805       /* Under locale rules or /aa we are not to mix,
9806       * respectively, ords < 256 or ASCII with non-.  So
9807       * reject folds that mix them, using only the
9808       * non-folded code point.  So do the fold to a
9809       * temporary, and inspect each character in it. */
9810       U8 trialbuf[UTF8_MAXBYTES_CASE+1];
9811       U8* s = trialbuf;
9812       UV tmpender = toFOLD_uni(ender, trialbuf, &foldlen);
9813       U8* e = s + foldlen;
9814       bool fold_ok = TRUE;
9815
9816       while (s < e) {
9817        if (isASCII(*s)
9818         || (LOC && (UTF8_IS_INVARIANT(*s)
9819           || UTF8_IS_DOWNGRADEABLE_START(*s))))
9820        {
9821         fold_ok = FALSE;
9822         break;
9823        }
9824        s += UTF8SKIP(s);
9825       }
9826       if (fold_ok) {
9827        Copy(trialbuf, tmpbuf, foldlen, U8);
9828        ender = tmpender;
9829       }
9830       else {
9831        uvuni_to_utf8(tmpbuf, ender);
9832        foldlen = UNISKIP(ender);
9833       }
9834      }
9835     }
9836     if (p < RExC_end && ISMULT2(p)) { /* Back off on ?+*. */
9837      if (len)
9838       p = oldp;
9839      else if (UTF || is_exactfu_sharp_s) {
9840       if (FOLD) {
9841        /* Emit all the Unicode characters. */
9842        STRLEN numlen;
9843        for (foldbuf = tmpbuf;
9844         foldlen;
9845         foldlen -= numlen) {
9846
9847         /* tmpbuf has been constructed by us, so we
9848          * know it is valid utf8 */
9849         ender = valid_utf8_to_uvchr(foldbuf, &numlen);
9850         if (numlen > 0) {
9851           const STRLEN unilen = reguni(pRExC_state, ender, s);
9852           s       += unilen;
9853           len     += unilen;
9854           /* In EBCDIC the numlen
9855           * and unilen can differ. */
9856           foldbuf += numlen;
9857           if (numlen >= foldlen)
9858            break;
9859         }
9860         else
9861           break; /* "Can't happen." */
9862        }
9863       }
9864       else {
9865        const STRLEN unilen = reguni(pRExC_state, ender, s);
9866        if (unilen > 0) {
9867         s   += unilen;
9868         len += unilen;
9869        }
9870       }
9871      }
9872      else {
9873       len++;
9874       REGC((char)ender, s++);
9875      }
9876      break;
9877     }
9878     if (UTF || is_exactfu_sharp_s) {
9879      if (FOLD) {
9880       /* Emit all the Unicode characters. */
9881       STRLEN numlen;
9882       for (foldbuf = tmpbuf;
9883        foldlen;
9884        foldlen -= numlen) {
9885        ender = valid_utf8_to_uvchr(foldbuf, &numlen);
9886        if (numlen > 0) {
9887          const STRLEN unilen = reguni(pRExC_state, ender, s);
9888          len     += unilen;
9889          s       += unilen;
9890          /* In EBCDIC the numlen
9891          * and unilen can differ. */
9892          foldbuf += numlen;
9893          if (numlen >= foldlen)
9894           break;
9895        }
9896        else
9897          break;
9898       }
9899      }
9900      else {
9901       const STRLEN unilen = reguni(pRExC_state, ender, s);
9902       if (unilen > 0) {
9903        s   += unilen;
9904        len += unilen;
9905       }
9906      }
9907      len--;
9908     }
9909     else {
9910      REGC((char)ender, s++);
9911     }
9912    }
9913   loopdone:   /* Jumped to when encounters something that shouldn't be in
9914      the node */
9915    RExC_parse = p - 1;
9916    Set_Node_Cur_Length(ret); /* MJD */
9917    nextchar(pRExC_state);
9918    {
9919     /* len is STRLEN which is unsigned, need to copy to signed */
9920     IV iv = len;
9921     if (iv < 0)
9922      vFAIL("Internal disaster");
9923    }
9924    if (len > 0)
9925     *flagp |= HASWIDTH;
9926    if (len == 1 && UNI_IS_INVARIANT(ender))
9927     *flagp |= SIMPLE;
9928
9929    if (SIZE_ONLY)
9930     RExC_size += STR_SZ(len);
9931    else {
9932     STR_LEN(ret) = len;
9933     RExC_emit += STR_SZ(len);
9934    }
9935   }
9936   break;
9937  }
9938
9939  return(ret);
9940
9941 /* Jumped to when an unrecognized character set is encountered */
9942 bad_charset:
9943  Perl_croak(aTHX_ "panic: Unknown regex character set encoding: %u", get_regex_charset(RExC_flags));
9944  return(NULL);
9945 }
9946
9947 STATIC char *
9948 S_regwhite( RExC_state_t *pRExC_state, char *p )
9949 {
9950  const char *e = RExC_end;
9951
9952  PERL_ARGS_ASSERT_REGWHITE;
9953
9954  while (p < e) {
9955   if (isSPACE(*p))
9956    ++p;
9957   else if (*p == '#') {
9958    bool ended = 0;
9959    do {
9960     if (*p++ == '\n') {
9961      ended = 1;
9962      break;
9963     }
9964    } while (p < e);
9965    if (!ended)
9966     RExC_seen |= REG_SEEN_RUN_ON_COMMENT;
9967   }
9968   else
9969    break;
9970  }
9971  return p;
9972 }
9973
9974 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
9975    Character classes ([:foo:]) can also be negated ([:^foo:]).
9976    Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
9977    Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
9978    but trigger failures because they are currently unimplemented. */
9979
9980 #define POSIXCC_DONE(c)   ((c) == ':')
9981 #define POSIXCC_NOTYET(c) ((c) == '=' || (c) == '.')
9982 #define POSIXCC(c) (POSIXCC_DONE(c) || POSIXCC_NOTYET(c))
9983
9984 STATIC I32
9985 S_regpposixcc(pTHX_ RExC_state_t *pRExC_state, I32 value)
9986 {
9987  dVAR;
9988  I32 namedclass = OOB_NAMEDCLASS;
9989
9990  PERL_ARGS_ASSERT_REGPPOSIXCC;
9991
9992  if (value == '[' && RExC_parse + 1 < RExC_end &&
9993   /* I smell either [: or [= or [. -- POSIX has been here, right? */
9994   POSIXCC(UCHARAT(RExC_parse))) {
9995   const char c = UCHARAT(RExC_parse);
9996   char* const s = RExC_parse++;
9997
9998   while (RExC_parse < RExC_end && UCHARAT(RExC_parse) != c)
9999    RExC_parse++;
10000   if (RExC_parse == RExC_end)
10001    /* Grandfather lone [:, [=, [. */
10002    RExC_parse = s;
10003   else {
10004    const char* const t = RExC_parse++; /* skip over the c */
10005    assert(*t == c);
10006
10007    if (UCHARAT(RExC_parse) == ']') {
10008     const char *posixcc = s + 1;
10009     RExC_parse++; /* skip over the ending ] */
10010
10011     if (*s == ':') {
10012      const I32 complement = *posixcc == '^' ? *posixcc++ : 0;
10013      const I32 skip = t - posixcc;
10014
10015      /* Initially switch on the length of the name.  */
10016      switch (skip) {
10017      case 4:
10018       if (memEQ(posixcc, "word", 4)) /* this is not POSIX, this is the Perl \w */
10019        namedclass = complement ? ANYOF_NALNUM : ANYOF_ALNUM;
10020       break;
10021      case 5:
10022       /* Names all of length 5.  */
10023       /* alnum alpha ascii blank cntrl digit graph lower
10024       print punct space upper  */
10025       /* Offset 4 gives the best switch position.  */
10026       switch (posixcc[4]) {
10027       case 'a':
10028        if (memEQ(posixcc, "alph", 4)) /* alpha */
10029         namedclass = complement ? ANYOF_NALPHA : ANYOF_ALPHA;
10030        break;
10031       case 'e':
10032        if (memEQ(posixcc, "spac", 4)) /* space */
10033         namedclass = complement ? ANYOF_NPSXSPC : ANYOF_PSXSPC;
10034        break;
10035       case 'h':
10036        if (memEQ(posixcc, "grap", 4)) /* graph */
10037         namedclass = complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
10038        break;
10039       case 'i':
10040        if (memEQ(posixcc, "asci", 4)) /* ascii */
10041         namedclass = complement ? ANYOF_NASCII : ANYOF_ASCII;
10042        break;
10043       case 'k':
10044        if (memEQ(posixcc, "blan", 4)) /* blank */
10045         namedclass = complement ? ANYOF_NBLANK : ANYOF_BLANK;
10046        break;
10047       case 'l':
10048        if (memEQ(posixcc, "cntr", 4)) /* cntrl */
10049         namedclass = complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
10050        break;
10051       case 'm':
10052        if (memEQ(posixcc, "alnu", 4)) /* alnum */
10053         namedclass = complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
10054        break;
10055       case 'r':
10056        if (memEQ(posixcc, "lowe", 4)) /* lower */
10057         namedclass = complement ? ANYOF_NLOWER : ANYOF_LOWER;
10058        else if (memEQ(posixcc, "uppe", 4)) /* upper */
10059         namedclass = complement ? ANYOF_NUPPER : ANYOF_UPPER;
10060        break;
10061       case 't':
10062        if (memEQ(posixcc, "digi", 4)) /* digit */
10063         namedclass = complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
10064        else if (memEQ(posixcc, "prin", 4)) /* print */
10065         namedclass = complement ? ANYOF_NPRINT : ANYOF_PRINT;
10066        else if (memEQ(posixcc, "punc", 4)) /* punct */
10067         namedclass = complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
10068        break;
10069       }
10070       break;
10071      case 6:
10072       if (memEQ(posixcc, "xdigit", 6))
10073        namedclass = complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
10074       break;
10075      }
10076
10077      if (namedclass == OOB_NAMEDCLASS)
10078       Simple_vFAIL3("POSIX class [:%.*s:] unknown",
10079          t - s - 1, s + 1);
10080      assert (posixcc[skip] == ':');
10081      assert (posixcc[skip+1] == ']');
10082     } else if (!SIZE_ONLY) {
10083      /* [[=foo=]] and [[.foo.]] are still future. */
10084
10085      /* adjust RExC_parse so the warning shows after
10086      the class closes */
10087      while (UCHARAT(RExC_parse) && UCHARAT(RExC_parse) != ']')
10088       RExC_parse++;
10089      Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
10090     }
10091    } else {
10092     /* Maternal grandfather:
10093     * "[:" ending in ":" but not in ":]" */
10094     RExC_parse = s;
10095    }
10096   }
10097  }
10098
10099  return namedclass;
10100 }
10101
10102 STATIC void
10103 S_checkposixcc(pTHX_ RExC_state_t *pRExC_state)
10104 {
10105  dVAR;
10106
10107  PERL_ARGS_ASSERT_CHECKPOSIXCC;
10108
10109  if (POSIXCC(UCHARAT(RExC_parse))) {
10110   const char *s = RExC_parse;
10111   const char  c = *s++;
10112
10113   while (isALNUM(*s))
10114    s++;
10115   if (*s && c == *s && s[1] == ']') {
10116    ckWARN3reg(s+2,
10117      "POSIX syntax [%c %c] belongs inside character classes",
10118      c, c);
10119
10120    /* [[=foo=]] and [[.foo.]] are still future. */
10121    if (POSIXCC_NOTYET(c)) {
10122     /* adjust RExC_parse so the error shows after
10123     the class closes */
10124     while (UCHARAT(RExC_parse) && UCHARAT(RExC_parse++) != ']')
10125      NOOP;
10126     Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
10127    }
10128   }
10129  }
10130 }
10131
10132 /* Generate the code to add a full posix character <class> to the bracketed
10133  * character class given by <node>.  (<node> is needed only under locale rules)
10134  * destlist     is the inversion list for non-locale rules that this class is
10135  *              to be added to
10136  * sourcelist   is the ASCII-range inversion list to add under /a rules
10137  * Xsourcelist  is the full Unicode range list to use otherwise. */
10138 #define DO_POSIX(node, class, destlist, sourcelist, Xsourcelist)           \
10139  if (LOC) {                                                             \
10140   SV* scratch_list = NULL;                                           \
10141                   \
10142   /* Set this class in the node for runtime matching */              \
10143   ANYOF_CLASS_SET(node, class);                                      \
10144                   \
10145   /* For above Latin1 code points, we use the full Unicode range */  \
10146   _invlist_intersection(PL_AboveLatin1,                              \
10147        Xsourcelist,                                 \
10148        &scratch_list);                              \
10149   /* And set the output to it, adding instead if there already is an \
10150   * output.  Checking if <destlist> is NULL first saves an extra    \
10151   * clone.  Its reference count will be decremented at the next     \
10152   * union, etc, or if this is the only instance, at the end of the  \
10153   * routine */                                                      \
10154   if (! destlist) {                                                  \
10155    destlist = scratch_list;                                       \
10156   }                                                                  \
10157   else {                                                             \
10158    _invlist_union(destlist, scratch_list, &destlist);             \
10159    SvREFCNT_dec(scratch_list);                                    \
10160   }                                                                  \
10161  }                                                                      \
10162  else {                                                                 \
10163   /* For non-locale, just add it to any existing list */             \
10164   _invlist_union(destlist,                                           \
10165      (AT_LEAST_ASCII_RESTRICTED)                         \
10166       ? sourcelist                                    \
10167       : Xsourcelist,                                  \
10168      &destlist);                                         \
10169  }
10170
10171 /* Like DO_POSIX, but matches the complement of <sourcelist> and <Xsourcelist>.
10172  */
10173 #define DO_N_POSIX(node, class, destlist, sourcelist, Xsourcelist)         \
10174  if (LOC) {                                                             \
10175   SV* scratch_list = NULL;                                           \
10176   ANYOF_CLASS_SET(node, class);        \
10177   _invlist_subtract(PL_AboveLatin1, Xsourcelist, &scratch_list);    \
10178   if (! destlist) {                \
10179    destlist = scratch_list;        \
10180   }                                                                  \
10181   else {                                                             \
10182    _invlist_union(destlist, scratch_list, &destlist);             \
10183    SvREFCNT_dec(scratch_list);                                    \
10184   }                                                                  \
10185  }                                                                      \
10186  else {                                                                 \
10187   _invlist_union_complement_2nd(destlist,                            \
10188          (AT_LEAST_ASCII_RESTRICTED)            \
10189           ? sourcelist                       \
10190           : Xsourcelist,                     \
10191          &destlist);                            \
10192   /* Under /d, everything in the upper half of the Latin1 range      \
10193   * matches this complement */                                      \
10194   if (DEPENDS_SEMANTICS) {                                           \
10195    ANYOF_FLAGS(node) |= ANYOF_NON_UTF8_LATIN1_ALL;                \
10196   }                                                                  \
10197  }
10198
10199 /* Generate the code to add a posix character <class> to the bracketed
10200  * character class given by <node>.  (<node> is needed only under locale rules)
10201  * destlist       is the inversion list for non-locale rules that this class is
10202  *                to be added to
10203  * sourcelist     is the ASCII-range inversion list to add under /a rules
10204  * l1_sourcelist  is the Latin1 range list to use otherwise.
10205  * Xpropertyname  is the name to add to <run_time_list> of the property to
10206  *                specify the code points above Latin1 that will have to be
10207  *                determined at run-time
10208  * run_time_list  is a SV* that contains text names of properties that are to
10209  *                be computed at run time.  This concatenates <Xpropertyname>
10210  *                to it, apppropriately
10211  * This is essentially DO_POSIX, but we know only the Latin1 values at compile
10212  * time */
10213 #define DO_POSIX_LATIN1_ONLY_KNOWN(node, class, destlist, sourcelist,      \
10214        l1_sourcelist, Xpropertyname, run_time_list) \
10215  /* If not /a matching, there are going to be code points we will have  \
10216  * to defer to runtime to look-up */                                   \
10217  if (! AT_LEAST_ASCII_RESTRICTED) {                                     \
10218   Perl_sv_catpvf(aTHX_ run_time_list, "+utf8::%s\n", Xpropertyname); \
10219  }                                                                      \
10220  if (LOC) {                                                             \
10221   ANYOF_CLASS_SET(node, class);                                      \
10222  }                                                                      \
10223  else {                                                                 \
10224   _invlist_union(destlist,                                           \
10225      (AT_LEAST_ASCII_RESTRICTED)                         \
10226       ? sourcelist                                    \
10227       : l1_sourcelist,                                \
10228      &destlist);                                         \
10229  }
10230
10231 /* Like DO_POSIX_LATIN1_ONLY_KNOWN, but for the complement.  A combination of
10232  * this and DO_N_POSIX */
10233 #define DO_N_POSIX_LATIN1_ONLY_KNOWN(node, class, destlist, sourcelist,    \
10234        l1_sourcelist, Xpropertyname, run_time_list) \
10235  if (AT_LEAST_ASCII_RESTRICTED) {                                       \
10236   _invlist_union_complement_2nd(destlist, sourcelist, &destlist);    \
10237  }                                                                      \
10238  else {                                                                 \
10239   Perl_sv_catpvf(aTHX_ run_time_list, "!utf8::%s\n", Xpropertyname); \
10240   if (LOC) {                                                         \
10241    ANYOF_CLASS_SET(node, namedclass);       \
10242   }                                                                  \
10243   else {                                                             \
10244    SV* scratch_list = NULL;                                       \
10245    _invlist_subtract(PL_Latin1, l1_sourcelist, &scratch_list);    \
10246    if (! destlist) {                                              \
10247     destlist = scratch_list;                                   \
10248    }                                                              \
10249    else {                                                         \
10250     _invlist_union(destlist, scratch_list, &destlist);         \
10251     SvREFCNT_dec(scratch_list);                                \
10252    }                                                              \
10253    if (DEPENDS_SEMANTICS) {                                       \
10254     ANYOF_FLAGS(node) |= ANYOF_NON_UTF8_LATIN1_ALL;            \
10255    }                                                              \
10256   }                                                                  \
10257  }
10258
10259 STATIC U8
10260 S_set_regclass_bit_fold(pTHX_ RExC_state_t *pRExC_state, regnode* node, const U8 value, SV** invlist_ptr, AV** alternate_ptr)
10261 {
10262
10263  /* Handle the setting of folds in the bitmap for non-locale ANYOF nodes.
10264  * Locale folding is done at run-time, so this function should not be
10265  * called for nodes that are for locales.
10266  *
10267  * This function sets the bit corresponding to the fold of the input
10268  * 'value', if not already set.  The fold of 'f' is 'F', and the fold of
10269  * 'F' is 'f'.
10270  *
10271  * It also knows about the characters that are in the bitmap that have
10272  * folds that are matchable only outside it, and sets the appropriate lists
10273  * and flags.
10274  *
10275  * It returns the number of bits that actually changed from 0 to 1 */
10276
10277  U8 stored = 0;
10278  U8 fold;
10279
10280  PERL_ARGS_ASSERT_SET_REGCLASS_BIT_FOLD;
10281
10282  fold = (AT_LEAST_UNI_SEMANTICS) ? PL_fold_latin1[value]
10283          : PL_fold[value];
10284
10285  /* It assumes the bit for 'value' has already been set */
10286  if (fold != value && ! ANYOF_BITMAP_TEST(node, fold)) {
10287   ANYOF_BITMAP_SET(node, fold);
10288   stored++;
10289  }
10290  if (_HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(value) && (! isASCII(value) || ! MORE_ASCII_RESTRICTED)) {
10291   /* Certain Latin1 characters have matches outside the bitmap.  To get
10292   * here, 'value' is one of those characters.   None of these matches is
10293   * valid for ASCII characters under /aa, which have been excluded by
10294   * the 'if' above.  The matches fall into three categories:
10295   * 1) They are singly folded-to or -from an above 255 character, as
10296   *    LATIN SMALL LETTER Y WITH DIAERESIS and LATIN CAPITAL LETTER Y
10297   *    WITH DIAERESIS;
10298   * 2) They are part of a multi-char fold with another character in the
10299   *    bitmap, only LATIN SMALL LETTER SHARP S => "ss" fits that bill;
10300   * 3) They are part of a multi-char fold with a character not in the
10301   *    bitmap, such as various ligatures.
10302   * We aren't dealing fully with multi-char folds, except we do deal
10303   * with the pattern containing a character that has a multi-char fold
10304   * (not so much the inverse).
10305   * For types 1) and 3), the matches only happen when the target string
10306   * is utf8; that's not true for 2), and we set a flag for it.
10307   *
10308   * The code below adds to the passed in inversion list the single fold
10309   * closures for 'value'.  The values are hard-coded here so that an
10310   * innocent-looking character class, like /[ks]/i won't have to go out
10311   * to disk to find the possible matches.  XXX It would be better to
10312   * generate these via regen, in case a new version of the Unicode
10313   * standard adds new mappings, though that is not really likely. */
10314   switch (value) {
10315    case 'k':
10316    case 'K':
10317     /* KELVIN SIGN */
10318     *invlist_ptr = add_cp_to_invlist(*invlist_ptr, 0x212A);
10319     break;
10320    case 's':
10321    case 'S':
10322     /* LATIN SMALL LETTER LONG S */
10323     *invlist_ptr = add_cp_to_invlist(*invlist_ptr, 0x017F);
10324     break;
10325    case MICRO_SIGN:
10326     *invlist_ptr = add_cp_to_invlist(*invlist_ptr,
10327             GREEK_SMALL_LETTER_MU);
10328     *invlist_ptr = add_cp_to_invlist(*invlist_ptr,
10329             GREEK_CAPITAL_LETTER_MU);
10330     break;
10331    case LATIN_CAPITAL_LETTER_A_WITH_RING_ABOVE:
10332    case LATIN_SMALL_LETTER_A_WITH_RING_ABOVE:
10333     /* ANGSTROM SIGN */
10334     *invlist_ptr = add_cp_to_invlist(*invlist_ptr, 0x212B);
10335     if (DEPENDS_SEMANTICS) {    /* See DEPENDS comment below */
10336      *invlist_ptr = add_cp_to_invlist(*invlist_ptr,
10337              PL_fold_latin1[value]);
10338     }
10339     break;
10340    case LATIN_SMALL_LETTER_Y_WITH_DIAERESIS:
10341     *invlist_ptr = add_cp_to_invlist(*invlist_ptr,
10342           LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS);
10343     break;
10344    case LATIN_SMALL_LETTER_SHARP_S:
10345     *invlist_ptr = add_cp_to_invlist(*invlist_ptr,
10346           LATIN_CAPITAL_LETTER_SHARP_S);
10347
10348     /* Under /a, /d, and /u, this can match the two chars "ss" */
10349     if (! MORE_ASCII_RESTRICTED) {
10350      add_alternate(alternate_ptr, (U8 *) "ss", 2);
10351
10352      /* And under /u or /a, it can match even if the target is
10353      * not utf8 */
10354      if (AT_LEAST_UNI_SEMANTICS) {
10355       ANYOF_FLAGS(node) |= ANYOF_NONBITMAP_NON_UTF8;
10356      }
10357     }
10358     break;
10359    case 'F': case 'f':
10360    case 'I': case 'i':
10361    case 'L': case 'l':
10362    case 'T': case 't':
10363    case 'A': case 'a':
10364    case 'H': case 'h':
10365    case 'J': case 'j':
10366    case 'N': case 'n':
10367    case 'W': case 'w':
10368    case 'Y': case 'y':
10369     /* These all are targets of multi-character folds from code
10370     * points that require UTF8 to express, so they can't match
10371     * unless the target string is in UTF-8, so no action here is
10372     * necessary, as regexec.c properly handles the general case
10373     * for UTF-8 matching */
10374     break;
10375    default:
10376     /* Use deprecated warning to increase the chances of this
10377     * being output */
10378     ckWARN2regdep(RExC_parse, "Perl folding rules are not up-to-date for 0x%x; please use the perlbug utility to report;", value);
10379     break;
10380   }
10381  }
10382  else if (DEPENDS_SEMANTICS
10383    && ! isASCII(value)
10384    && PL_fold_latin1[value] != value)
10385  {
10386   /* Under DEPENDS rules, non-ASCII Latin1 characters match their
10387    * folds only when the target string is in UTF-8.  We add the fold
10388    * here to the list of things to match outside the bitmap, which
10389    * won't be looked at unless it is UTF8 (or else if something else
10390    * says to look even if not utf8, but those things better not happen
10391    * under DEPENDS semantics. */
10392   *invlist_ptr = add_cp_to_invlist(*invlist_ptr, PL_fold_latin1[value]);
10393  }
10394
10395  return stored;
10396 }
10397
10398
10399 PERL_STATIC_INLINE U8
10400 S_set_regclass_bit(pTHX_ RExC_state_t *pRExC_state, regnode* node, const U8 value, SV** invlist_ptr, AV** alternate_ptr)
10401 {
10402  /* This inline function sets a bit in the bitmap if not already set, and if
10403  * appropriate, its fold, returning the number of bits that actually
10404  * changed from 0 to 1 */
10405
10406  U8 stored;
10407
10408  PERL_ARGS_ASSERT_SET_REGCLASS_BIT;
10409
10410  if (ANYOF_BITMAP_TEST(node, value)) {   /* Already set */
10411   return 0;
10412  }
10413
10414  ANYOF_BITMAP_SET(node, value);
10415  stored = 1;
10416
10417  if (FOLD && ! LOC) { /* Locale folds aren't known until runtime */
10418   stored += set_regclass_bit_fold(pRExC_state, node, value, invlist_ptr, alternate_ptr);
10419  }
10420
10421  return stored;
10422 }
10423
10424 STATIC void
10425 S_add_alternate(pTHX_ AV** alternate_ptr, U8* string, STRLEN len)
10426 {
10427  /* Adds input 'string' with length 'len' to the ANYOF node's unicode
10428  * alternate list, pointed to by 'alternate_ptr'.  This is an array of
10429  * the multi-character folds of characters in the node */
10430  SV *sv;
10431
10432  PERL_ARGS_ASSERT_ADD_ALTERNATE;
10433
10434  if (! *alternate_ptr) {
10435   *alternate_ptr = newAV();
10436  }
10437  sv = newSVpvn_utf8((char*)string, len, TRUE);
10438  av_push(*alternate_ptr, sv);
10439  return;
10440 }
10441
10442 /*
10443    parse a class specification and produce either an ANYOF node that
10444    matches the pattern or perhaps will be optimized into an EXACTish node
10445    instead. The node contains a bit map for the first 256 characters, with the
10446    corresponding bit set if that character is in the list.  For characters
10447    above 255, a range list is used */
10448
10449 STATIC regnode *
10450 S_regclass(pTHX_ RExC_state_t *pRExC_state, U32 depth)
10451 {
10452  dVAR;
10453  register UV nextvalue;
10454  register IV prevvalue = OOB_UNICODE;
10455  register IV range = 0;
10456  UV value = 0; /* XXX:dmq: needs to be referenceable (unfortunately) */
10457  register regnode *ret;
10458  STRLEN numlen;
10459  IV namedclass;
10460  char *rangebegin = NULL;
10461  bool need_class = 0;
10462  bool allow_full_fold = TRUE;   /* Assume wants multi-char folding */
10463  SV *listsv = NULL;
10464  STRLEN initial_listsv_len = 0; /* Kind of a kludge to see if it is more
10465          than just initialized.  */
10466  SV* properties = NULL;    /* Code points that match \p{} \P{} */
10467  UV element_count = 0;   /* Number of distinct elements in the class.
10468        Optimizations may be possible if this is tiny */
10469  UV n;
10470
10471  /* Unicode properties are stored in a swash; this holds the current one
10472  * being parsed.  If this swash is the only above-latin1 component of the
10473  * character class, an optimization is to pass it directly on to the
10474  * execution engine.  Otherwise, it is set to NULL to indicate that there
10475  * are other things in the class that have to be dealt with at execution
10476  * time */
10477  SV* swash = NULL;  /* Code points that match \p{} \P{} */
10478
10479  /* Set if a component of this character class is user-defined; just passed
10480  * on to the engine */
10481  UV has_user_defined_property = 0;
10482
10483  /* code points this node matches that can't be stored in the bitmap */
10484  SV* nonbitmap = NULL;
10485
10486  /* The items that are to match that aren't stored in the bitmap, but are a
10487  * result of things that are stored there.  This is the fold closure of
10488  * such a character, either because it has DEPENDS semantics and shouldn't
10489  * be matched unless the target string is utf8, or is a code point that is
10490  * too large for the bit map, as for example, the fold of the MICRO SIGN is
10491  * above 255.  This all is solely for performance reasons.  By having this
10492  * code know the outside-the-bitmap folds that the bitmapped characters are
10493  * involved with, we don't have to go out to disk to find the list of
10494  * matches, unless the character class includes code points that aren't
10495  * storable in the bit map.  That means that a character class with an 's'
10496  * in it, for example, doesn't need to go out to disk to find everything
10497  * that matches.  A 2nd list is used so that the 'nonbitmap' list is kept
10498  * empty unless there is something whose fold we don't know about, and will
10499  * have to go out to the disk to find. */
10500  SV* l1_fold_invlist = NULL;
10501
10502  /* List of multi-character folds that are matched by this node */
10503  AV* unicode_alternate  = NULL;
10504 #ifdef EBCDIC
10505  UV literal_endpoint = 0;
10506 #endif
10507  UV stored = 0;  /* how many chars stored in the bitmap */
10508
10509  regnode * const orig_emit = RExC_emit; /* Save the original RExC_emit in
10510   case we need to change the emitted regop to an EXACT. */
10511  const char * orig_parse = RExC_parse;
10512  GET_RE_DEBUG_FLAGS_DECL;
10513
10514  PERL_ARGS_ASSERT_REGCLASS;
10515 #ifndef DEBUGGING
10516  PERL_UNUSED_ARG(depth);
10517 #endif
10518
10519  DEBUG_PARSE("clas");
10520
10521  /* Assume we are going to generate an ANYOF node. */
10522  ret = reganode(pRExC_state, ANYOF, 0);
10523
10524
10525  if (!SIZE_ONLY) {
10526   ANYOF_FLAGS(ret) = 0;
10527  }
10528
10529  if (UCHARAT(RExC_parse) == '^') { /* Complement of range. */
10530   RExC_naughty++;
10531   RExC_parse++;
10532   if (!SIZE_ONLY)
10533    ANYOF_FLAGS(ret) |= ANYOF_INVERT;
10534
10535   /* We have decided to not allow multi-char folds in inverted character
10536   * classes, due to the confusion that can happen, especially with
10537   * classes that are designed for a non-Unicode world:  You have the
10538   * peculiar case that:
10539    "s s" =~ /^[^\xDF]+$/i => Y
10540    "ss"  =~ /^[^\xDF]+$/i => N
10541   *
10542   * See [perl #89750] */
10543   allow_full_fold = FALSE;
10544  }
10545
10546  if (SIZE_ONLY) {
10547   RExC_size += ANYOF_SKIP;
10548   listsv = &PL_sv_undef; /* For code scanners: listsv always non-NULL. */
10549  }
10550  else {
10551   RExC_emit += ANYOF_SKIP;
10552   if (LOC) {
10553    ANYOF_FLAGS(ret) |= ANYOF_LOCALE;
10554   }
10555   ANYOF_BITMAP_ZERO(ret);
10556   listsv = newSVpvs("# comment\n");
10557   initial_listsv_len = SvCUR(listsv);
10558  }
10559
10560  nextvalue = RExC_parse < RExC_end ? UCHARAT(RExC_parse) : 0;
10561
10562  if (!SIZE_ONLY && POSIXCC(nextvalue))
10563   checkposixcc(pRExC_state);
10564
10565  /* allow 1st char to be ] (allowing it to be - is dealt with later) */
10566  if (UCHARAT(RExC_parse) == ']')
10567   goto charclassloop;
10568
10569 parseit:
10570  while (RExC_parse < RExC_end && UCHARAT(RExC_parse) != ']') {
10571
10572  charclassloop:
10573
10574   namedclass = OOB_NAMEDCLASS; /* initialize as illegal */
10575
10576   if (!range) {
10577    rangebegin = RExC_parse;
10578    element_count++;
10579   }
10580   if (UTF) {
10581    value = utf8n_to_uvchr((U8*)RExC_parse,
10582         RExC_end - RExC_parse,
10583         &numlen, UTF8_ALLOW_DEFAULT);
10584    RExC_parse += numlen;
10585   }
10586   else
10587    value = UCHARAT(RExC_parse++);
10588
10589   nextvalue = RExC_parse < RExC_end ? UCHARAT(RExC_parse) : 0;
10590   if (value == '[' && POSIXCC(nextvalue))
10591    namedclass = regpposixcc(pRExC_state, value);
10592   else if (value == '\\') {
10593    if (UTF) {
10594     value = utf8n_to_uvchr((U8*)RExC_parse,
10595         RExC_end - RExC_parse,
10596         &numlen, UTF8_ALLOW_DEFAULT);
10597     RExC_parse += numlen;
10598    }
10599    else
10600     value = UCHARAT(RExC_parse++);
10601    /* Some compilers cannot handle switching on 64-bit integer
10602    * values, therefore value cannot be an UV.  Yes, this will
10603    * be a problem later if we want switch on Unicode.
10604    * A similar issue a little bit later when switching on
10605    * namedclass. --jhi */
10606    switch ((I32)value) {
10607    case 'w': namedclass = ANYOF_ALNUM; break;
10608    case 'W': namedclass = ANYOF_NALNUM; break;
10609    case 's': namedclass = ANYOF_SPACE; break;
10610    case 'S': namedclass = ANYOF_NSPACE; break;
10611    case 'd': namedclass = ANYOF_DIGIT; break;
10612    case 'D': namedclass = ANYOF_NDIGIT; break;
10613    case 'v': namedclass = ANYOF_VERTWS; break;
10614    case 'V': namedclass = ANYOF_NVERTWS; break;
10615    case 'h': namedclass = ANYOF_HORIZWS; break;
10616    case 'H': namedclass = ANYOF_NHORIZWS; break;
10617    case 'N':  /* Handle \N{NAME} in class */
10618     {
10619      /* We only pay attention to the first char of
10620      multichar strings being returned. I kinda wonder
10621      if this makes sense as it does change the behaviour
10622      from earlier versions, OTOH that behaviour was broken
10623      as well. */
10624      UV v; /* value is register so we cant & it /grrr */
10625      if (reg_namedseq(pRExC_state, &v, NULL, depth)) {
10626       goto parseit;
10627      }
10628      value= v;
10629     }
10630     break;
10631    case 'p':
10632    case 'P':
10633     {
10634     char *e;
10635     if (RExC_parse >= RExC_end)
10636      vFAIL2("Empty \\%c{}", (U8)value);
10637     if (*RExC_parse == '{') {
10638      const U8 c = (U8)value;
10639      e = strchr(RExC_parse++, '}');
10640      if (!e)
10641       vFAIL2("Missing right brace on \\%c{}", c);
10642      while (isSPACE(UCHARAT(RExC_parse)))
10643       RExC_parse++;
10644      if (e == RExC_parse)
10645       vFAIL2("Empty \\%c{}", c);
10646      n = e - RExC_parse;
10647      while (isSPACE(UCHARAT(RExC_parse + n - 1)))
10648       n--;
10649     }
10650     else {
10651      e = RExC_parse;
10652      n = 1;
10653     }
10654     if (!SIZE_ONLY) {
10655      SV** invlistsvp;
10656      SV* invlist;
10657      char* name;
10658      if (UCHARAT(RExC_parse) == '^') {
10659       RExC_parse++;
10660       n--;
10661       value = value == 'p' ? 'P' : 'p'; /* toggle */
10662       while (isSPACE(UCHARAT(RExC_parse))) {
10663        RExC_parse++;
10664        n--;
10665       }
10666      }
10667      /* Try to get the definition of the property into
10668      * <invlist>.  If /i is in effect, the effective property
10669      * will have its name be <__NAME_i>.  The design is
10670      * discussed in commit
10671      * 2f833f5208e26b208886e51e09e2c072b5eabb46 */
10672      Newx(name, n + sizeof("_i__\n"), char);
10673
10674      sprintf(name, "%s%.*s%s\n",
10675          (FOLD) ? "__" : "",
10676          (int)n,
10677          RExC_parse,
10678          (FOLD) ? "_i" : ""
10679      );
10680
10681      /* Look up the property name, and get its swash and
10682      * inversion list, if the property is found  */
10683      if (swash) {
10684       SvREFCNT_dec(swash);
10685      }
10686      swash = _core_swash_init("utf8", name, &PL_sv_undef,
10687            1, /* binary */
10688            0, /* not tr/// */
10689            TRUE, /* this routine will handle
10690              undefined properties */
10691            NULL, FALSE /* No inversion list */
10692            );
10693      if (   ! swash
10694       || ! SvROK(swash)
10695       || ! SvTYPE(SvRV(swash)) == SVt_PVHV
10696       || ! (invlistsvp =
10697         hv_fetchs(MUTABLE_HV(SvRV(swash)),
10698         "INVLIST", FALSE))
10699       || ! (invlist = *invlistsvp))
10700      {
10701       if (swash) {
10702        SvREFCNT_dec(swash);
10703        swash = NULL;
10704       }
10705
10706       /* Here didn't find it.  It could be a user-defined
10707       * property that will be available at run-time.  Add it
10708       * to the list to look up then */
10709       Perl_sv_catpvf(aTHX_ listsv, "%cutf8::%s\n",
10710           (value == 'p' ? '+' : '!'),
10711           name);
10712       has_user_defined_property = 1;
10713
10714       /* We don't know yet, so have to assume that the
10715       * property could match something in the Latin1 range,
10716       * hence something that isn't utf8 */
10717       ANYOF_FLAGS(ret) |= ANYOF_NONBITMAP_NON_UTF8;
10718      }
10719      else {
10720
10721       /* Here, did get the swash and its inversion list.  If
10722       * the swash is from a user-defined property, then this
10723       * whole character class should be regarded as such */
10724       SV** user_defined_svp =
10725            hv_fetchs(MUTABLE_HV(SvRV(swash)),
10726               "USER_DEFINED", FALSE);
10727       if (user_defined_svp) {
10728        has_user_defined_property
10729              |= SvUV(*user_defined_svp);
10730       }
10731
10732       /* Invert if asking for the complement */
10733       if (value == 'P') {
10734        _invlist_union_complement_2nd(properties, invlist, &properties);
10735
10736        /* The swash can't be used as-is, because we've
10737        * inverted things; delay removing it to here after
10738        * have copied its invlist above */
10739        SvREFCNT_dec(swash);
10740        swash = NULL;
10741       }
10742       else {
10743        _invlist_union(properties, invlist, &properties);
10744       }
10745      }
10746      Safefree(name);
10747     }
10748     RExC_parse = e + 1;
10749     namedclass = ANYOF_MAX;  /* no official name, but it's named */
10750
10751     /* \p means they want Unicode semantics */
10752     RExC_uni_semantics = 1;
10753     }
10754     break;
10755    case 'n': value = '\n';   break;
10756    case 'r': value = '\r';   break;
10757    case 't': value = '\t';   break;
10758    case 'f': value = '\f';   break;
10759    case 'b': value = '\b';   break;
10760    case 'e': value = ASCII_TO_NATIVE('\033');break;
10761    case 'a': value = ASCII_TO_NATIVE('\007');break;
10762    case 'o':
10763     RExC_parse--; /* function expects to be pointed at the 'o' */
10764     {
10765      const char* error_msg;
10766      bool valid = grok_bslash_o(RExC_parse,
10767            &value,
10768            &numlen,
10769            &error_msg,
10770            SIZE_ONLY);
10771      RExC_parse += numlen;
10772      if (! valid) {
10773       vFAIL(error_msg);
10774      }
10775     }
10776     if (PL_encoding && value < 0x100) {
10777      goto recode_encoding;
10778     }
10779     break;
10780    case 'x':
10781     if (*RExC_parse == '{') {
10782      I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
10783       | PERL_SCAN_DISALLOW_PREFIX;
10784      char * const e = strchr(RExC_parse++, '}');
10785      if (!e)
10786       vFAIL("Missing right brace on \\x{}");
10787
10788      numlen = e - RExC_parse;
10789      value = grok_hex(RExC_parse, &numlen, &flags, NULL);
10790      RExC_parse = e + 1;
10791     }
10792     else {
10793      I32 flags = PERL_SCAN_DISALLOW_PREFIX;
10794      numlen = 2;
10795      value = grok_hex(RExC_parse, &numlen, &flags, NULL);
10796      RExC_parse += numlen;
10797     }
10798     if (PL_encoding && value < 0x100)
10799      goto recode_encoding;
10800     break;
10801    case 'c':
10802     value = grok_bslash_c(*RExC_parse++, UTF, SIZE_ONLY);
10803     break;
10804    case '0': case '1': case '2': case '3': case '4':
10805    case '5': case '6': case '7':
10806     {
10807      /* Take 1-3 octal digits */
10808      I32 flags = PERL_SCAN_SILENT_ILLDIGIT;
10809      numlen = 3;
10810      value = grok_oct(--RExC_parse, &numlen, &flags, NULL);
10811      RExC_parse += numlen;
10812      if (PL_encoding && value < 0x100)
10813       goto recode_encoding;
10814      break;
10815     }
10816    recode_encoding:
10817     if (! RExC_override_recoding) {
10818      SV* enc = PL_encoding;
10819      value = reg_recode((const char)(U8)value, &enc);
10820      if (!enc && SIZE_ONLY)
10821       ckWARNreg(RExC_parse,
10822         "Invalid escape in the specified encoding");
10823      break;
10824     }
10825    default:
10826     /* Allow \_ to not give an error */
10827     if (!SIZE_ONLY && isALNUM(value) && value != '_') {
10828      ckWARN2reg(RExC_parse,
10829        "Unrecognized escape \\%c in character class passed through",
10830        (int)value);
10831     }
10832     break;
10833    }
10834   } /* end of \blah */
10835 #ifdef EBCDIC
10836   else
10837    literal_endpoint++;
10838 #endif
10839
10840   if (namedclass > OOB_NAMEDCLASS) { /* this is a named class \blah */
10841
10842    /* What matches in a locale is not known until runtime, so need to
10843    * (one time per class) allocate extra space to pass to regexec.
10844    * The space will contain a bit for each named class that is to be
10845    * matched against.  This isn't needed for \p{} and pseudo-classes,
10846    * as they are not affected by locale, and hence are dealt with
10847    * separately */
10848    if (LOC && namedclass < ANYOF_MAX && ! need_class) {
10849     need_class = 1;
10850     if (SIZE_ONLY) {
10851      RExC_size += ANYOF_CLASS_SKIP - ANYOF_SKIP;
10852     }
10853     else {
10854      RExC_emit += ANYOF_CLASS_SKIP - ANYOF_SKIP;
10855      ANYOF_CLASS_ZERO(ret);
10856     }
10857     ANYOF_FLAGS(ret) |= ANYOF_CLASS;
10858    }
10859
10860    /* a bad range like a-\d, a-[:digit:].  The '-' is taken as a
10861    * literal, as is the character that began the false range, i.e.
10862    * the 'a' in the examples */
10863    if (range) {
10864     if (!SIZE_ONLY) {
10865      const int w =
10866       RExC_parse >= rangebegin ?
10867       RExC_parse - rangebegin : 0;
10868      ckWARN4reg(RExC_parse,
10869        "False [] range \"%*.*s\"",
10870        w, w, rangebegin);
10871
10872      stored +=
10873       set_regclass_bit(pRExC_state, ret, '-', &l1_fold_invlist, &unicode_alternate);
10874      if (prevvalue < 256) {
10875       stored +=
10876       set_regclass_bit(pRExC_state, ret, (U8) prevvalue, &l1_fold_invlist, &unicode_alternate);
10877      }
10878      else {
10879       nonbitmap = add_cp_to_invlist(nonbitmap, prevvalue);
10880      }
10881     }
10882
10883     range = 0; /* this was not a true range */
10884    }
10885
10886    if (!SIZE_ONLY) {
10887
10888     /* Possible truncation here but in some 64-bit environments
10889     * the compiler gets heartburn about switch on 64-bit values.
10890     * A similar issue a little earlier when switching on value.
10891     * --jhi */
10892     switch ((I32)namedclass) {
10893      int i;  /* loop counter */
10894
10895     case ANYOF_ALNUMC: /* C's alnum, in contrast to \w */
10896      DO_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
10897       PL_PosixAlnum, PL_L1PosixAlnum, "XPosixAlnum", listsv);
10898      break;
10899     case ANYOF_NALNUMC:
10900      DO_N_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
10901       PL_PosixAlnum, PL_L1PosixAlnum, "XPosixAlnum", listsv);
10902      break;
10903     case ANYOF_ALPHA:
10904      DO_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
10905       PL_PosixAlpha, PL_L1PosixAlpha, "XPosixAlpha", listsv);
10906      break;
10907     case ANYOF_NALPHA:
10908      DO_N_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
10909       PL_PosixAlpha, PL_L1PosixAlpha, "XPosixAlpha", listsv);
10910      break;
10911     case ANYOF_ASCII:
10912      if (LOC) {
10913       ANYOF_CLASS_SET(ret, namedclass);
10914      }
10915      else {
10916       _invlist_union(properties, PL_ASCII, &properties);
10917      }
10918      break;
10919     case ANYOF_NASCII:
10920      if (LOC) {
10921       ANYOF_CLASS_SET(ret, namedclass);
10922      }
10923      else {
10924       _invlist_union_complement_2nd(properties,
10925              PL_ASCII, &properties);
10926       if (DEPENDS_SEMANTICS) {
10927        ANYOF_FLAGS(ret) |= ANYOF_NON_UTF8_LATIN1_ALL;
10928       }
10929      }
10930      break;
10931     case ANYOF_BLANK:
10932      DO_POSIX(ret, namedclass, properties,
10933            PL_PosixBlank, PL_XPosixBlank);
10934      break;
10935     case ANYOF_NBLANK:
10936      DO_N_POSIX(ret, namedclass, properties,
10937            PL_PosixBlank, PL_XPosixBlank);
10938      break;
10939     case ANYOF_CNTRL:
10940      DO_POSIX(ret, namedclass, properties,
10941            PL_PosixCntrl, PL_XPosixCntrl);
10942      break;
10943     case ANYOF_NCNTRL:
10944      DO_N_POSIX(ret, namedclass, properties,
10945            PL_PosixCntrl, PL_XPosixCntrl);
10946      break;
10947     case ANYOF_DIGIT:
10948      /* Ignore the compiler warning for this macro, planned to
10949      * be eliminated later */
10950      DO_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
10951       PL_PosixDigit, PL_PosixDigit, "XPosixDigit", listsv);
10952      break;
10953     case ANYOF_NDIGIT:
10954      DO_N_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
10955       PL_PosixDigit, PL_PosixDigit, "XPosixDigit", listsv);
10956      break;
10957     case ANYOF_GRAPH:
10958      DO_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
10959       PL_PosixGraph, PL_L1PosixGraph, "XPosixGraph", listsv);
10960      break;
10961     case ANYOF_NGRAPH:
10962      DO_N_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
10963       PL_PosixGraph, PL_L1PosixGraph, "XPosixGraph", listsv);
10964      break;
10965     case ANYOF_HORIZWS:
10966      /* NBSP matches this, and needs to be added unconditionally
10967      * to the bit map as it matches even under /d, unlike all
10968      * the rest of the Posix-like classes (\v doesn't have any
10969      * matches in the Latin1 range, so it is unaffected.) which
10970      * Otherwise, we use the nonbitmap, as /d doesn't make a
10971      * difference in what these match.  It turns out that \h is
10972      * just a synonym for XPosixBlank */
10973      _invlist_union(nonbitmap, PL_XPosixBlank, &nonbitmap);
10974      stored += set_regclass_bit(pRExC_state, ret,
10975            UNI_TO_NATIVE(0xA0),
10976            &l1_fold_invlist,
10977            &unicode_alternate);
10978
10979      break;
10980     case ANYOF_NHORIZWS:
10981      _invlist_union_complement_2nd(nonbitmap,
10982             PL_XPosixBlank, &nonbitmap);
10983      for (i = 128; i < 256; i++) {
10984       if (i == 0xA0) {
10985        continue;
10986       }
10987       stored += set_regclass_bit(pRExC_state, ret,
10988             UNI_TO_NATIVE(i),
10989             &l1_fold_invlist,
10990             &unicode_alternate);
10991      }
10992      break;
10993     case ANYOF_LOWER:
10994     case ANYOF_NLOWER:
10995     {   /* These require special handling, as they differ under
10996      folding, matching Cased there (which in the ASCII range
10997      is the same as Alpha */
10998
10999      SV* ascii_source;
11000      SV* l1_source;
11001      const char *Xname;
11002
11003      if (FOLD && ! LOC) {
11004       ascii_source = PL_PosixAlpha;
11005       l1_source = PL_L1Cased;
11006       Xname = "Cased";
11007      }
11008      else {
11009       ascii_source = PL_PosixLower;
11010       l1_source = PL_L1PosixLower;
11011       Xname = "XPosixLower";
11012      }
11013      if (namedclass == ANYOF_LOWER) {
11014       DO_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
11015          ascii_source, l1_source, Xname, listsv);
11016      }
11017      else {
11018       DO_N_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass,
11019        properties, ascii_source, l1_source, Xname, listsv);
11020      }
11021      break;
11022     }
11023     case ANYOF_PRINT:
11024      DO_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
11025       PL_PosixPrint, PL_L1PosixPrint, "XPosixPrint", listsv);
11026      break;
11027     case ANYOF_NPRINT:
11028      DO_N_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
11029       PL_PosixPrint, PL_L1PosixPrint, "XPosixPrint", listsv);
11030      break;
11031     case ANYOF_PUNCT:
11032      DO_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
11033       PL_PosixPunct, PL_L1PosixPunct, "XPosixPunct", listsv);
11034      break;
11035     case ANYOF_NPUNCT:
11036      DO_N_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
11037       PL_PosixPunct, PL_L1PosixPunct, "XPosixPunct", listsv);
11038      break;
11039     case ANYOF_PSXSPC:
11040      DO_POSIX(ret, namedclass, properties,
11041            PL_PosixSpace, PL_XPosixSpace);
11042      break;
11043     case ANYOF_NPSXSPC:
11044      DO_N_POSIX(ret, namedclass, properties,
11045            PL_PosixSpace, PL_XPosixSpace);
11046      break;
11047     case ANYOF_SPACE:
11048      DO_POSIX(ret, namedclass, properties,
11049            PL_PerlSpace, PL_XPerlSpace);
11050      break;
11051     case ANYOF_NSPACE:
11052      DO_N_POSIX(ret, namedclass, properties,
11053            PL_PerlSpace, PL_XPerlSpace);
11054      break;
11055     case ANYOF_UPPER:   /* Same as LOWER, above */
11056     case ANYOF_NUPPER:
11057     {
11058      SV* ascii_source;
11059      SV* l1_source;
11060      const char *Xname;
11061
11062      if (FOLD && ! LOC) {
11063       ascii_source = PL_PosixAlpha;
11064       l1_source = PL_L1Cased;
11065       Xname = "Cased";
11066      }
11067      else {
11068       ascii_source = PL_PosixUpper;
11069       l1_source = PL_L1PosixUpper;
11070       Xname = "XPosixUpper";
11071      }
11072      if (namedclass == ANYOF_UPPER) {
11073       DO_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
11074          ascii_source, l1_source, Xname, listsv);
11075      }
11076      else {
11077       DO_N_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass,
11078       properties, ascii_source, l1_source, Xname, listsv);
11079      }
11080      break;
11081     }
11082     case ANYOF_ALNUM:   /* Really is 'Word' */
11083      DO_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
11084        PL_PosixWord, PL_L1PosixWord, "XPosixWord", listsv);
11085      break;
11086     case ANYOF_NALNUM:
11087      DO_N_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
11088        PL_PosixWord, PL_L1PosixWord, "XPosixWord", listsv);
11089      break;
11090     case ANYOF_VERTWS:
11091      /* For these, we use the nonbitmap, as /d doesn't make a
11092      * difference in what these match.  There would be problems
11093      * if these characters had folds other than themselves, as
11094      * nonbitmap is subject to folding */
11095      _invlist_union(nonbitmap, PL_VertSpace, &nonbitmap);
11096      break;
11097     case ANYOF_NVERTWS:
11098      _invlist_union_complement_2nd(nonbitmap,
11099              PL_VertSpace, &nonbitmap);
11100      break;
11101     case ANYOF_XDIGIT:
11102      DO_POSIX(ret, namedclass, properties,
11103            PL_PosixXDigit, PL_XPosixXDigit);
11104      break;
11105     case ANYOF_NXDIGIT:
11106      DO_N_POSIX(ret, namedclass, properties,
11107            PL_PosixXDigit, PL_XPosixXDigit);
11108      break;
11109     case ANYOF_MAX:
11110      /* this is to handle \p and \P */
11111      break;
11112     default:
11113      vFAIL("Invalid [::] class");
11114      break;
11115     }
11116
11117     continue;
11118    }
11119   } /* end of namedclass \blah */
11120
11121   if (range) {
11122    if (prevvalue > (IV)value) /* b-a */ {
11123     const int w = RExC_parse - rangebegin;
11124     Simple_vFAIL4("Invalid [] range \"%*.*s\"", w, w, rangebegin);
11125     range = 0; /* not a valid range */
11126    }
11127   }
11128   else {
11129    prevvalue = value; /* save the beginning of the range */
11130    if (RExC_parse+1 < RExC_end
11131     && *RExC_parse == '-'
11132     && RExC_parse[1] != ']')
11133    {
11134     RExC_parse++;
11135
11136     /* a bad range like \w-, [:word:]- ? */
11137     if (namedclass > OOB_NAMEDCLASS) {
11138      if (ckWARN(WARN_REGEXP)) {
11139       const int w =
11140        RExC_parse >= rangebegin ?
11141        RExC_parse - rangebegin : 0;
11142       vWARN4(RExC_parse,
11143        "False [] range \"%*.*s\"",
11144        w, w, rangebegin);
11145      }
11146      if (!SIZE_ONLY)
11147       stored +=
11148        set_regclass_bit(pRExC_state, ret, '-', &l1_fold_invlist, &unicode_alternate);
11149     } else
11150      range = 1; /* yeah, it's a range! */
11151     continue; /* but do it the next time */
11152    }
11153   }
11154
11155   /* non-Latin1 code point implies unicode semantics.  Must be set in
11156   * pass1 so is there for the whole of pass 2 */
11157   if (value > 255) {
11158    RExC_uni_semantics = 1;
11159   }
11160
11161   /* now is the next time */
11162   if (!SIZE_ONLY) {
11163    if (prevvalue < 256) {
11164     const IV ceilvalue = value < 256 ? value : 255;
11165     IV i;
11166 #ifdef EBCDIC
11167     /* In EBCDIC [\x89-\x91] should include
11168     * the \x8e but [i-j] should not. */
11169     if (literal_endpoint == 2 &&
11170      ((isLOWER(prevvalue) && isLOWER(ceilvalue)) ||
11171      (isUPPER(prevvalue) && isUPPER(ceilvalue))))
11172     {
11173      if (isLOWER(prevvalue)) {
11174       for (i = prevvalue; i <= ceilvalue; i++)
11175        if (isLOWER(i) && !ANYOF_BITMAP_TEST(ret,i)) {
11176         stored +=
11177         set_regclass_bit(pRExC_state, ret, (U8) i, &l1_fold_invlist, &unicode_alternate);
11178        }
11179      } else {
11180       for (i = prevvalue; i <= ceilvalue; i++)
11181        if (isUPPER(i) && !ANYOF_BITMAP_TEST(ret,i)) {
11182         stored +=
11183         set_regclass_bit(pRExC_state, ret, (U8) i, &l1_fold_invlist, &unicode_alternate);
11184        }
11185      }
11186     }
11187     else
11188 #endif
11189      for (i = prevvalue; i <= ceilvalue; i++) {
11190       stored += set_regclass_bit(pRExC_state, ret, (U8) i, &l1_fold_invlist, &unicode_alternate);
11191      }
11192   }
11193   if (value > 255) {
11194    const UV prevnatvalue  = NATIVE_TO_UNI(prevvalue);
11195    const UV natvalue      = NATIVE_TO_UNI(value);
11196    nonbitmap = _add_range_to_invlist(nonbitmap, prevnatvalue, natvalue);
11197   }
11198 #ifdef EBCDIC
11199    literal_endpoint = 0;
11200 #endif
11201   }
11202
11203   range = 0; /* this range (if it was one) is done now */
11204  }
11205
11206
11207
11208  if (SIZE_ONLY)
11209   return ret;
11210  /****** !SIZE_ONLY AFTER HERE *********/
11211
11212  /* If folding and there are code points above 255, we calculate all
11213  * characters that could fold to or from the ones already on the list */
11214  if (FOLD && nonbitmap) {
11215   UV start, end; /* End points of code point ranges */
11216
11217   SV* fold_intersection = NULL;
11218
11219   /* This is a list of all the characters that participate in folds
11220    * (except marks, etc in multi-char folds */
11221   if (! PL_utf8_foldable) {
11222    SV* swash = swash_init("utf8", "Cased", &PL_sv_undef, 1, 0);
11223    PL_utf8_foldable = _swash_to_invlist(swash);
11224    SvREFCNT_dec(swash);
11225   }
11226
11227   /* This is a hash that for a particular fold gives all characters
11228    * that are involved in it */
11229   if (! PL_utf8_foldclosures) {
11230
11231    /* If we were unable to find any folds, then we likely won't be
11232    * able to find the closures.  So just create an empty list.
11233    * Folding will effectively be restricted to the non-Unicode rules
11234    * hard-coded into Perl.  (This case happens legitimately during
11235    * compilation of Perl itself before the Unicode tables are
11236    * generated) */
11237    if (invlist_len(PL_utf8_foldable) == 0) {
11238     PL_utf8_foldclosures = newHV();
11239    } else {
11240     /* If the folds haven't been read in, call a fold function
11241      * to force that */
11242     if (! PL_utf8_tofold) {
11243      U8 dummy[UTF8_MAXBYTES+1];
11244      STRLEN dummy_len;
11245
11246      /* This particular string is above \xff in both UTF-8 and
11247      * UTFEBCDIC */
11248      to_utf8_fold((U8*) "\xC8\x80", dummy, &dummy_len);
11249      assert(PL_utf8_tofold); /* Verify that worked */
11250     }
11251     PL_utf8_foldclosures = _swash_inversion_hash(PL_utf8_tofold);
11252    }
11253   }
11254
11255   /* Only the characters in this class that participate in folds need be
11256   * checked.  Get the intersection of this class and all the possible
11257   * characters that are foldable.  This can quickly narrow down a large
11258   * class */
11259   _invlist_intersection(PL_utf8_foldable, nonbitmap, &fold_intersection);
11260
11261   /* Now look at the foldable characters in this class individually */
11262   invlist_iterinit(fold_intersection);
11263   while (invlist_iternext(fold_intersection, &start, &end)) {
11264    UV j;
11265
11266    /* Look at every character in the range */
11267    for (j = start; j <= end; j++) {
11268
11269     /* Get its fold */
11270     U8 foldbuf[UTF8_MAXBYTES_CASE+1];
11271     STRLEN foldlen;
11272     const UV f =
11273      _to_uni_fold_flags(j, foldbuf, &foldlen, allow_full_fold);
11274
11275     if (foldlen > (STRLEN)UNISKIP(f)) {
11276
11277      /* Any multicharacter foldings (disallowed in lookbehind
11278      * patterns) require the following transform: [ABCDEF] ->
11279      * (?:[ABCabcDEFd]|pq|rst) where E folds into "pq" and F
11280      * folds into "rst", all other characters fold to single
11281      * characters.  We save away these multicharacter foldings,
11282      * to be later saved as part of the additional "s" data. */
11283      if (! RExC_in_lookbehind) {
11284       U8* loc = foldbuf;
11285       U8* e = foldbuf + foldlen;
11286
11287       /* If any of the folded characters of this are in the
11288       * Latin1 range, tell the regex engine that this can
11289       * match a non-utf8 target string.  The only multi-byte
11290       * fold whose source is in the Latin1 range (U+00DF)
11291       * applies only when the target string is utf8, or
11292       * under unicode rules */
11293       if (j > 255 || AT_LEAST_UNI_SEMANTICS) {
11294        while (loc < e) {
11295
11296         /* Can't mix ascii with non- under /aa */
11297         if (MORE_ASCII_RESTRICTED
11298          && (isASCII(*loc) != isASCII(j)))
11299         {
11300          goto end_multi_fold;
11301         }
11302         if (UTF8_IS_INVARIANT(*loc)
11303          || UTF8_IS_DOWNGRADEABLE_START(*loc))
11304         {
11305          /* Can't mix above and below 256 under LOC
11306          */
11307          if (LOC) {
11308           goto end_multi_fold;
11309          }
11310          ANYOF_FLAGS(ret)
11311            |= ANYOF_NONBITMAP_NON_UTF8;
11312          break;
11313         }
11314         loc += UTF8SKIP(loc);
11315        }
11316       }
11317
11318       add_alternate(&unicode_alternate, foldbuf, foldlen);
11319      end_multi_fold: ;
11320      }
11321
11322      /* This is special-cased, as it is the only letter which
11323      * has both a multi-fold and single-fold in Latin1.  All
11324      * the other chars that have single and multi-folds are
11325      * always in utf8, and the utf8 folding algorithm catches
11326      * them */
11327      if (! LOC && j == LATIN_CAPITAL_LETTER_SHARP_S) {
11328       stored += set_regclass_bit(pRExC_state,
11329           ret,
11330           LATIN_SMALL_LETTER_SHARP_S,
11331           &l1_fold_invlist, &unicode_alternate);
11332      }
11333     }
11334     else {
11335      /* Single character fold.  Add everything in its fold
11336      * closure to the list that this node should match */
11337      SV** listp;
11338
11339      /* The fold closures data structure is a hash with the keys
11340      * being every character that is folded to, like 'k', and
11341      * the values each an array of everything that folds to its
11342      * key.  e.g. [ 'k', 'K', KELVIN_SIGN ] */
11343      if ((listp = hv_fetch(PL_utf8_foldclosures,
11344          (char *) foldbuf, foldlen, FALSE)))
11345      {
11346       AV* list = (AV*) *listp;
11347       IV k;
11348       for (k = 0; k <= av_len(list); k++) {
11349        SV** c_p = av_fetch(list, k, FALSE);
11350        UV c;
11351        if (c_p == NULL) {
11352         Perl_croak(aTHX_ "panic: invalid PL_utf8_foldclosures structure");
11353        }
11354        c = SvUV(*c_p);
11355
11356        /* /aa doesn't allow folds between ASCII and non-;
11357        * /l doesn't allow them between above and below
11358        * 256 */
11359        if ((MORE_ASCII_RESTRICTED
11360         && (isASCII(c) != isASCII(j)))
11361          || (LOC && ((c < 256) != (j < 256))))
11362        {
11363         continue;
11364        }
11365
11366        if (c < 256 && AT_LEAST_UNI_SEMANTICS) {
11367         stored += set_regclass_bit(pRExC_state,
11368           ret,
11369           (U8) c,
11370           &l1_fold_invlist, &unicode_alternate);
11371        }
11372         /* It may be that the code point is already in
11373         * this range or already in the bitmap, in
11374         * which case we need do nothing */
11375        else if ((c < start || c > end)
11376           && (c > 255
11377            || ! ANYOF_BITMAP_TEST(ret, c)))
11378        {
11379         nonbitmap = add_cp_to_invlist(nonbitmap, c);
11380        }
11381       }
11382      }
11383     }
11384    }
11385   }
11386   SvREFCNT_dec(fold_intersection);
11387  }
11388
11389  /* Combine the two lists into one. */
11390  if (l1_fold_invlist) {
11391   if (nonbitmap) {
11392    _invlist_union(nonbitmap, l1_fold_invlist, &nonbitmap);
11393    SvREFCNT_dec(l1_fold_invlist);
11394   }
11395   else {
11396    nonbitmap = l1_fold_invlist;
11397   }
11398  }
11399
11400  /* And combine the result (if any) with any inversion list from properties.
11401  * The lists are kept separate up to now because we don't want to fold the
11402  * properties */
11403  if (properties) {
11404   if (nonbitmap) {
11405    _invlist_union(nonbitmap, properties, &nonbitmap);
11406    SvREFCNT_dec(properties);
11407   }
11408   else {
11409    nonbitmap = properties;
11410   }
11411  }
11412
11413  /* Here, <nonbitmap> contains all the code points we can determine at
11414  * compile time that we haven't put into the bitmap.  Go through it, and
11415  * for things that belong in the bitmap, put them there, and delete from
11416  * <nonbitmap> */
11417  if (nonbitmap) {
11418
11419   /* Above-ASCII code points in /d have to stay in <nonbitmap>, as they
11420   * possibly only should match when the target string is UTF-8 */
11421   UV max_cp_to_set = (DEPENDS_SEMANTICS) ? 127 : 255;
11422
11423   /* This gets set if we actually need to modify things */
11424   bool change_invlist = FALSE;
11425
11426   UV start, end;
11427
11428   /* Start looking through <nonbitmap> */
11429   invlist_iterinit(nonbitmap);
11430   while (invlist_iternext(nonbitmap, &start, &end)) {
11431    UV high;
11432    int i;
11433
11434    /* Quit if are above what we should change */
11435    if (start > max_cp_to_set) {
11436     break;
11437    }
11438
11439    change_invlist = TRUE;
11440
11441    /* Set all the bits in the range, up to the max that we are doing */
11442    high = (end < max_cp_to_set) ? end : max_cp_to_set;
11443    for (i = start; i <= (int) high; i++) {
11444     if (! ANYOF_BITMAP_TEST(ret, i)) {
11445      ANYOF_BITMAP_SET(ret, i);
11446      stored++;
11447      prevvalue = value;
11448      value = i;
11449     }
11450    }
11451   }
11452
11453   /* Done with loop; remove any code points that are in the bitmap from
11454   * <nonbitmap> */
11455   if (change_invlist) {
11456    _invlist_subtract(nonbitmap,
11457        (DEPENDS_SEMANTICS)
11458         ? PL_ASCII
11459         : PL_Latin1,
11460        &nonbitmap);
11461   }
11462
11463   /* If have completely emptied it, remove it completely */
11464   if (invlist_len(nonbitmap) == 0) {
11465    SvREFCNT_dec(nonbitmap);
11466    nonbitmap = NULL;
11467   }
11468  }
11469
11470  /* Here, we have calculated what code points should be in the character
11471  * class.  <nonbitmap> does not overlap the bitmap except possibly in the
11472  * case of DEPENDS rules.
11473  *
11474  * Now we can see about various optimizations.  Fold calculation (which we
11475  * did above) needs to take place before inversion.  Otherwise /[^k]/i
11476  * would invert to include K, which under /i would match k, which it
11477  * shouldn't. */
11478
11479  /* Optimize inverted simple patterns (e.g. [^a-z]).  Note that we haven't
11480  * set the FOLD flag yet, so this does optimize those.  It doesn't
11481  * optimize locale.  Doing so perhaps could be done as long as there is
11482  * nothing like \w in it; some thought also would have to be given to the
11483  * interaction with above 0x100 chars */
11484  if ((ANYOF_FLAGS(ret) & ANYOF_INVERT)
11485   && ! LOC
11486   && ! unicode_alternate
11487   /* In case of /d, there are some things that should match only when in
11488   * not in the bitmap, i.e., they require UTF8 to match.  These are
11489   * listed in nonbitmap, but if ANYOF_NONBITMAP_NON_UTF8 is set in this
11490   * case, they don't require UTF8, so can invert here */
11491   && (! nonbitmap
11492    || ! DEPENDS_SEMANTICS
11493    || (ANYOF_FLAGS(ret) & ANYOF_NONBITMAP_NON_UTF8))
11494   && SvCUR(listsv) == initial_listsv_len)
11495  {
11496   int i;
11497   if (! nonbitmap) {
11498    for (i = 0; i < 256; ++i) {
11499     if (ANYOF_BITMAP_TEST(ret, i)) {
11500      ANYOF_BITMAP_CLEAR(ret, i);
11501     }
11502     else {
11503      ANYOF_BITMAP_SET(ret, i);
11504      prevvalue = value;
11505      value = i;
11506     }
11507    }
11508    /* The inversion means that everything above 255 is matched */
11509    ANYOF_FLAGS(ret) |= ANYOF_UNICODE_ALL;
11510   }
11511   else {
11512    /* Here, also has things outside the bitmap that may overlap with
11513    * the bitmap.  We have to sync them up, so that they get inverted
11514    * in both places.  Earlier, we removed all overlaps except in the
11515    * case of /d rules, so no syncing is needed except for this case
11516    */
11517    SV *remove_list = NULL;
11518
11519    if (DEPENDS_SEMANTICS) {
11520     UV start, end;
11521
11522     /* Set the bits that correspond to the ones that aren't in the
11523     * bitmap.  Otherwise, when we invert, we'll miss these.
11524     * Earlier, we removed from the nonbitmap all code points
11525     * < 128, so there is no extra work here */
11526     invlist_iterinit(nonbitmap);
11527     while (invlist_iternext(nonbitmap, &start, &end)) {
11528      if (start > 255) {  /* The bit map goes to 255 */
11529       break;
11530      }
11531      if (end > 255) {
11532       end = 255;
11533      }
11534      for (i = start; i <= (int) end; ++i) {
11535       ANYOF_BITMAP_SET(ret, i);
11536       prevvalue = value;
11537       value = i;
11538      }
11539     }
11540    }
11541
11542    /* Now invert both the bitmap and the nonbitmap.  Anything in the
11543    * bitmap has to also be removed from the non-bitmap, but again,
11544    * there should not be overlap unless is /d rules. */
11545    _invlist_invert(nonbitmap);
11546
11547    /* Any swash can't be used as-is, because we've inverted things */
11548    if (swash) {
11549     SvREFCNT_dec(swash);
11550     swash = NULL;
11551    }
11552
11553    for (i = 0; i < 256; ++i) {
11554     if (ANYOF_BITMAP_TEST(ret, i)) {
11555      ANYOF_BITMAP_CLEAR(ret, i);
11556      if (DEPENDS_SEMANTICS) {
11557       if (! remove_list) {
11558        remove_list = _new_invlist(2);
11559       }
11560       remove_list = add_cp_to_invlist(remove_list, i);
11561      }
11562     }
11563     else {
11564      ANYOF_BITMAP_SET(ret, i);
11565      prevvalue = value;
11566      value = i;
11567     }
11568    }
11569
11570    /* And do the removal */
11571    if (DEPENDS_SEMANTICS) {
11572     if (remove_list) {
11573      _invlist_subtract(nonbitmap, remove_list, &nonbitmap);
11574      SvREFCNT_dec(remove_list);
11575     }
11576    }
11577    else {
11578     /* There is no overlap for non-/d, so just delete anything
11579     * below 256 */
11580     _invlist_intersection(nonbitmap, PL_AboveLatin1, &nonbitmap);
11581    }
11582   }
11583
11584   stored = 256 - stored;
11585
11586   /* Clear the invert flag since have just done it here */
11587   ANYOF_FLAGS(ret) &= ~ANYOF_INVERT;
11588  }
11589
11590  /* Folding in the bitmap is taken care of above, but not for locale (for
11591  * which we have to wait to see what folding is in effect at runtime), and
11592  * for some things not in the bitmap (only the upper latin folds in this
11593  * case, as all other single-char folding has been set above).  Set
11594  * run-time fold flag for these */
11595  if (FOLD && (LOC
11596     || (DEPENDS_SEMANTICS
11597      && nonbitmap
11598      && ! (ANYOF_FLAGS(ret) & ANYOF_NONBITMAP_NON_UTF8))
11599     || unicode_alternate))
11600  {
11601   ANYOF_FLAGS(ret) |= ANYOF_LOC_NONBITMAP_FOLD;
11602  }
11603
11604  /* A single character class can be "optimized" into an EXACTish node.
11605  * Note that since we don't currently count how many characters there are
11606  * outside the bitmap, we are XXX missing optimization possibilities for
11607  * them.  This optimization can't happen unless this is a truly single
11608  * character class, which means that it can't be an inversion into a
11609  * many-character class, and there must be no possibility of there being
11610  * things outside the bitmap.  'stored' (only) for locales doesn't include
11611  * \w, etc, so have to make a special test that they aren't present
11612  *
11613  * Similarly A 2-character class of the very special form like [bB] can be
11614  * optimized into an EXACTFish node, but only for non-locales, and for
11615  * characters which only have the two folds; so things like 'fF' and 'Ii'
11616  * wouldn't work because they are part of the fold of 'LATIN SMALL LIGATURE
11617  * FI'. */
11618  if (! nonbitmap
11619   && ! unicode_alternate
11620   && SvCUR(listsv) == initial_listsv_len
11621   && ! (ANYOF_FLAGS(ret) & (ANYOF_INVERT|ANYOF_UNICODE_ALL))
11622   && (((stored == 1 && ((! (ANYOF_FLAGS(ret) & ANYOF_LOCALE))
11623        || (! ANYOF_CLASS_TEST_ANY_SET(ret)))))
11624    || (stored == 2 && ((! (ANYOF_FLAGS(ret) & ANYOF_LOCALE))
11625         && (! _HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(value))
11626         /* If the latest code point has a fold whose
11627         * bit is set, it must be the only other one */
11628         && ((prevvalue = PL_fold_latin1[value]) != (IV)value)
11629         && ANYOF_BITMAP_TEST(ret, prevvalue)))))
11630  {
11631   /* Note that the information needed to decide to do this optimization
11632   * is not currently available until the 2nd pass, and that the actually
11633   * used EXACTish node takes less space than the calculated ANYOF node,
11634   * and hence the amount of space calculated in the first pass is larger
11635   * than actually used, so this optimization doesn't gain us any space.
11636   * But an EXACT node is faster than an ANYOF node, and can be combined
11637   * with any adjacent EXACT nodes later by the optimizer for further
11638   * gains.  The speed of executing an EXACTF is similar to an ANYOF
11639   * node, so the optimization advantage comes from the ability to join
11640   * it to adjacent EXACT nodes */
11641
11642   const char * cur_parse= RExC_parse;
11643   U8 op;
11644   RExC_emit = (regnode *)orig_emit;
11645   RExC_parse = (char *)orig_parse;
11646
11647   if (stored == 1) {
11648
11649    /* A locale node with one point can be folded; all the other cases
11650    * with folding will have two points, since we calculate them above
11651    */
11652    if (ANYOF_FLAGS(ret) & ANYOF_LOC_NONBITMAP_FOLD) {
11653     op = EXACTFL;
11654    }
11655    else {
11656     op = EXACT;
11657    }
11658   }
11659   else {   /* else 2 chars in the bit map: the folds of each other */
11660
11661    /* Use the folded value, which for the cases where we get here,
11662    * is just the lower case of the current one (which may resolve to
11663    * itself, or to the other one */
11664    value = toLOWER_LATIN1(value);
11665
11666    /* To join adjacent nodes, they must be the exact EXACTish type.
11667    * Try to use the most likely type, by using EXACTFA if possible,
11668    * then EXACTFU if the regex calls for it, or is required because
11669    * the character is non-ASCII.  (If <value> is ASCII, its fold is
11670    * also ASCII for the cases where we get here.) */
11671    if (MORE_ASCII_RESTRICTED && isASCII(value)) {
11672     op = EXACTFA;
11673    }
11674    else if (AT_LEAST_UNI_SEMANTICS || !isASCII(value)) {
11675     op = EXACTFU;
11676    }
11677    else {    /* Otherwise, more likely to be EXACTF type */
11678     op = EXACTF;
11679    }
11680   }
11681
11682   ret = reg_node(pRExC_state, op);
11683   RExC_parse = (char *)cur_parse;
11684   if (UTF && ! NATIVE_IS_INVARIANT(value)) {
11685    *STRING(ret)= UTF8_EIGHT_BIT_HI((U8) value);
11686    *(STRING(ret) + 1)= UTF8_EIGHT_BIT_LO((U8) value);
11687    STR_LEN(ret)= 2;
11688    RExC_emit += STR_SZ(2);
11689   }
11690   else {
11691    *STRING(ret)= (char)value;
11692    STR_LEN(ret)= 1;
11693    RExC_emit += STR_SZ(1);
11694   }
11695   SvREFCNT_dec(listsv);
11696   return ret;
11697  }
11698
11699  /* If there is a swash and more than one element, we can't use the swash in
11700  * the optimization below. */
11701  if (swash && element_count > 1) {
11702   SvREFCNT_dec(swash);
11703   swash = NULL;
11704  }
11705  if (! nonbitmap
11706   && SvCUR(listsv) == initial_listsv_len
11707   && ! unicode_alternate)
11708  {
11709   ARG_SET(ret, ANYOF_NONBITMAP_EMPTY);
11710   SvREFCNT_dec(listsv);
11711   SvREFCNT_dec(unicode_alternate);
11712  }
11713  else {
11714   /* av[0] stores the character class description in its textual form:
11715   *       used later (regexec.c:Perl_regclass_swash()) to initialize the
11716   *       appropriate swash, and is also useful for dumping the regnode.
11717   * av[1] if NULL, is a placeholder to later contain the swash computed
11718   *       from av[0].  But if no further computation need be done, the
11719   *       swash is stored there now.
11720   * av[2] stores the multicharacter foldings, used later in
11721   *       regexec.c:S_reginclass().
11722   * av[3] stores the nonbitmap inversion list for use in addition or
11723   *       instead of av[0]; not used if av[1] isn't NULL
11724   * av[4] is set if any component of the class is from a user-defined
11725   *       property; not used if av[1] isn't NULL */
11726   AV * const av = newAV();
11727   SV *rv;
11728
11729   av_store(av, 0, (SvCUR(listsv) == initial_listsv_len)
11730       ? &PL_sv_undef
11731       : listsv);
11732   if (swash) {
11733    av_store(av, 1, swash);
11734    SvREFCNT_dec(nonbitmap);
11735   }
11736   else {
11737    av_store(av, 1, NULL);
11738    if (nonbitmap) {
11739     av_store(av, 3, nonbitmap);
11740     av_store(av, 4, newSVuv(has_user_defined_property));
11741    }
11742   }
11743
11744   /* Store any computed multi-char folds only if we are allowing
11745   * them */
11746   if (allow_full_fold) {
11747    av_store(av, 2, MUTABLE_SV(unicode_alternate));
11748    if (unicode_alternate) { /* This node is variable length */
11749     OP(ret) = ANYOFV;
11750    }
11751   }
11752   else {
11753    av_store(av, 2, NULL);
11754   }
11755   rv = newRV_noinc(MUTABLE_SV(av));
11756   n = add_data(pRExC_state, 1, "s");
11757   RExC_rxi->data->data[n] = (void*)rv;
11758   ARG_SET(ret, n);
11759  }
11760  return ret;
11761 }
11762
11763
11764 /* reg_skipcomment()
11765
11766    Absorbs an /x style # comments from the input stream.
11767    Returns true if there is more text remaining in the stream.
11768    Will set the REG_SEEN_RUN_ON_COMMENT flag if the comment
11769    terminates the pattern without including a newline.
11770
11771    Note its the callers responsibility to ensure that we are
11772    actually in /x mode
11773
11774 */
11775
11776 STATIC bool
11777 S_reg_skipcomment(pTHX_ RExC_state_t *pRExC_state)
11778 {
11779  bool ended = 0;
11780
11781  PERL_ARGS_ASSERT_REG_SKIPCOMMENT;
11782
11783  while (RExC_parse < RExC_end)
11784   if (*RExC_parse++ == '\n') {
11785    ended = 1;
11786    break;
11787   }
11788  if (!ended) {
11789   /* we ran off the end of the pattern without ending
11790   the comment, so we have to add an \n when wrapping */
11791   RExC_seen |= REG_SEEN_RUN_ON_COMMENT;
11792   return 0;
11793  } else
11794   return 1;
11795 }
11796
11797 /* nextchar()
11798
11799    Advances the parse position, and optionally absorbs
11800    "whitespace" from the inputstream.
11801
11802    Without /x "whitespace" means (?#...) style comments only,
11803    with /x this means (?#...) and # comments and whitespace proper.
11804
11805    Returns the RExC_parse point from BEFORE the scan occurs.
11806
11807    This is the /x friendly way of saying RExC_parse++.
11808 */
11809
11810 STATIC char*
11811 S_nextchar(pTHX_ RExC_state_t *pRExC_state)
11812 {
11813  char* const retval = RExC_parse++;
11814
11815  PERL_ARGS_ASSERT_NEXTCHAR;
11816
11817  for (;;) {
11818   if (RExC_end - RExC_parse >= 3
11819    && *RExC_parse == '('
11820    && RExC_parse[1] == '?'
11821    && RExC_parse[2] == '#')
11822   {
11823    while (*RExC_parse != ')') {
11824     if (RExC_parse == RExC_end)
11825      FAIL("Sequence (?#... not terminated");
11826     RExC_parse++;
11827    }
11828    RExC_parse++;
11829    continue;
11830   }
11831   if (RExC_flags & RXf_PMf_EXTENDED) {
11832    if (isSPACE(*RExC_parse)) {
11833     RExC_parse++;
11834     continue;
11835    }
11836    else if (*RExC_parse == '#') {
11837     if ( reg_skipcomment( pRExC_state ) )
11838      continue;
11839    }
11840   }
11841   return retval;
11842  }
11843 }
11844
11845 /*
11846 - reg_node - emit a node
11847 */
11848 STATIC regnode *   /* Location. */
11849 S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op)
11850 {
11851  dVAR;
11852  register regnode *ptr;
11853  regnode * const ret = RExC_emit;
11854  GET_RE_DEBUG_FLAGS_DECL;
11855
11856  PERL_ARGS_ASSERT_REG_NODE;
11857
11858  if (SIZE_ONLY) {
11859   SIZE_ALIGN(RExC_size);
11860   RExC_size += 1;
11861   return(ret);
11862  }
11863  if (RExC_emit >= RExC_emit_bound)
11864   Perl_croak(aTHX_ "panic: reg_node overrun trying to emit %d, %p>=%p",
11865     op, RExC_emit, RExC_emit_bound);
11866
11867  NODE_ALIGN_FILL(ret);
11868  ptr = ret;
11869  FILL_ADVANCE_NODE(ptr, op);
11870  REH_CALL_COMP_NODE_HOOK(pRExC_state->rx, (ptr) - 1);
11871 #ifdef RE_TRACK_PATTERN_OFFSETS
11872  if (RExC_offsets) {         /* MJD */
11873   MJD_OFFSET_DEBUG(("%s:%d: (op %s) %s %"UVuf" (len %"UVuf") (max %"UVuf").\n",
11874    "reg_node", __LINE__,
11875    PL_reg_name[op],
11876    (UV)(RExC_emit - RExC_emit_start) > RExC_offsets[0]
11877     ? "Overwriting end of array!\n" : "OK",
11878    (UV)(RExC_emit - RExC_emit_start),
11879    (UV)(RExC_parse - RExC_start),
11880    (UV)RExC_offsets[0]));
11881   Set_Node_Offset(RExC_emit, RExC_parse + (op == END));
11882  }
11883 #endif
11884  RExC_emit = ptr;
11885  return(ret);
11886 }
11887
11888 /*
11889 - reganode - emit a node with an argument
11890 */
11891 STATIC regnode *   /* Location. */
11892 S_reganode(pTHX_ RExC_state_t *pRExC_state, U8 op, U32 arg)
11893 {
11894  dVAR;
11895  register regnode *ptr;
11896  regnode * const ret = RExC_emit;
11897  GET_RE_DEBUG_FLAGS_DECL;
11898
11899  PERL_ARGS_ASSERT_REGANODE;
11900
11901  if (SIZE_ONLY) {
11902   SIZE_ALIGN(RExC_size);
11903   RExC_size += 2;
11904   /*
11905   We can't do this:
11906
11907   assert(2==regarglen[op]+1);
11908
11909   Anything larger than this has to allocate the extra amount.
11910   If we changed this to be:
11911
11912   RExC_size += (1 + regarglen[op]);
11913
11914   then it wouldn't matter. Its not clear what side effect
11915   might come from that so its not done so far.
11916   -- dmq
11917   */
11918   return(ret);
11919  }
11920  if (RExC_emit >= RExC_emit_bound)
11921   Perl_croak(aTHX_ "panic: reg_node overrun trying to emit %d, %p>=%p",
11922     op, RExC_emit, RExC_emit_bound);
11923
11924  NODE_ALIGN_FILL(ret);
11925  ptr = ret;
11926  FILL_ADVANCE_NODE_ARG(ptr, op, arg);
11927  REH_CALL_COMP_NODE_HOOK(pRExC_state->rx, (ptr) - 2);
11928 #ifdef RE_TRACK_PATTERN_OFFSETS
11929  if (RExC_offsets) {         /* MJD */
11930   MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s %"UVuf" <- %"UVuf" (max %"UVuf").\n",
11931    "reganode",
11932    __LINE__,
11933    PL_reg_name[op],
11934    (UV)(RExC_emit - RExC_emit_start) > RExC_offsets[0] ?
11935    "Overwriting end of array!\n" : "OK",
11936    (UV)(RExC_emit - RExC_emit_start),
11937    (UV)(RExC_parse - RExC_start),
11938    (UV)RExC_offsets[0]));
11939   Set_Cur_Node_Offset;
11940  }
11941 #endif
11942  RExC_emit = ptr;
11943  return(ret);
11944 }
11945
11946 /*
11947 - reguni - emit (if appropriate) a Unicode character
11948 */
11949 STATIC STRLEN
11950 S_reguni(pTHX_ const RExC_state_t *pRExC_state, UV uv, char* s)
11951 {
11952  dVAR;
11953
11954  PERL_ARGS_ASSERT_REGUNI;
11955
11956  return SIZE_ONLY ? UNISKIP(uv) : (uvchr_to_utf8((U8*)s, uv) - (U8*)s);
11957 }
11958
11959 /*
11960 - reginsert - insert an operator in front of already-emitted operand
11961 *
11962 * Means relocating the operand.
11963 */
11964 STATIC void
11965 S_reginsert(pTHX_ RExC_state_t *pRExC_state, U8 op, regnode *opnd, U32 depth)
11966 {
11967  dVAR;
11968  register regnode *src;
11969  register regnode *dst;
11970  register regnode *place;
11971  const int offset = regarglen[(U8)op];
11972  const int size = NODE_STEP_REGNODE + offset;
11973  GET_RE_DEBUG_FLAGS_DECL;
11974
11975  PERL_ARGS_ASSERT_REGINSERT;
11976  PERL_UNUSED_ARG(depth);
11977 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
11978  DEBUG_PARSE_FMT("inst"," - %s",PL_reg_name[op]);
11979  if (SIZE_ONLY) {
11980   RExC_size += size;
11981   return;
11982  }
11983
11984  src = RExC_emit;
11985  RExC_emit += size;
11986  dst = RExC_emit;
11987  if (RExC_open_parens) {
11988   int paren;
11989   /*DEBUG_PARSE_FMT("inst"," - %"IVdf, (IV)RExC_npar);*/
11990   for ( paren=0 ; paren < RExC_npar ; paren++ ) {
11991    if ( RExC_open_parens[paren] >= opnd ) {
11992     /*DEBUG_PARSE_FMT("open"," - %d",size);*/
11993     RExC_open_parens[paren] += size;
11994    } else {
11995     /*DEBUG_PARSE_FMT("open"," - %s","ok");*/
11996    }
11997    if ( RExC_close_parens[paren] >= opnd ) {
11998     /*DEBUG_PARSE_FMT("close"," - %d",size);*/
11999     RExC_close_parens[paren] += size;
12000    } else {
12001     /*DEBUG_PARSE_FMT("close"," - %s","ok");*/
12002    }
12003   }
12004  }
12005
12006  while (src > opnd) {
12007   StructCopy(--src, --dst, regnode);
12008 #ifdef RE_TRACK_PATTERN_OFFSETS
12009   if (RExC_offsets) {     /* MJD 20010112 */
12010    MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s copy %"UVuf" -> %"UVuf" (max %"UVuf").\n",
12011     "reg_insert",
12012     __LINE__,
12013     PL_reg_name[op],
12014     (UV)(dst - RExC_emit_start) > RExC_offsets[0]
12015      ? "Overwriting end of array!\n" : "OK",
12016     (UV)(src - RExC_emit_start),
12017     (UV)(dst - RExC_emit_start),
12018     (UV)RExC_offsets[0]));
12019    Set_Node_Offset_To_R(dst-RExC_emit_start, Node_Offset(src));
12020    Set_Node_Length_To_R(dst-RExC_emit_start, Node_Length(src));
12021   }
12022 #endif
12023  }
12024
12025
12026  place = opnd;  /* Op node, where operand used to be. */
12027 #ifdef RE_TRACK_PATTERN_OFFSETS
12028  if (RExC_offsets) {         /* MJD */
12029   MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s %"UVuf" <- %"UVuf" (max %"UVuf").\n",
12030    "reginsert",
12031    __LINE__,
12032    PL_reg_name[op],
12033    (UV)(place - RExC_emit_start) > RExC_offsets[0]
12034    ? "Overwriting end of array!\n" : "OK",
12035    (UV)(place - RExC_emit_start),
12036    (UV)(RExC_parse - RExC_start),
12037    (UV)RExC_offsets[0]));
12038   Set_Node_Offset(place, RExC_parse);
12039   Set_Node_Length(place, 1);
12040  }
12041 #endif
12042  src = NEXTOPER(place);
12043  FILL_ADVANCE_NODE(place, op);
12044  REH_CALL_COMP_NODE_HOOK(pRExC_state->rx, (place) - 1);
12045  Zero(src, offset, regnode);
12046 }
12047
12048 /*
12049 - regtail - set the next-pointer at the end of a node chain of p to val.
12050 - SEE ALSO: regtail_study
12051 */
12052 /* TODO: All three parms should be const */
12053 STATIC void
12054 S_regtail(pTHX_ RExC_state_t *pRExC_state, regnode *p, const regnode *val,U32 depth)
12055 {
12056  dVAR;
12057  register regnode *scan;
12058  GET_RE_DEBUG_FLAGS_DECL;
12059
12060  PERL_ARGS_ASSERT_REGTAIL;
12061 #ifndef DEBUGGING
12062  PERL_UNUSED_ARG(depth);
12063 #endif
12064
12065  if (SIZE_ONLY)
12066   return;
12067
12068  /* Find last node. */
12069  scan = p;
12070  for (;;) {
12071   regnode * const temp = regnext(scan);
12072   DEBUG_PARSE_r({
12073    SV * const mysv=sv_newmortal();
12074    DEBUG_PARSE_MSG((scan==p ? "tail" : ""));
12075    regprop(RExC_rx, mysv, scan);
12076    PerlIO_printf(Perl_debug_log, "~ %s (%d) %s %s\n",
12077     SvPV_nolen_const(mysv), REG_NODE_NUM(scan),
12078      (temp == NULL ? "->" : ""),
12079      (temp == NULL ? PL_reg_name[OP(val)] : "")
12080    );
12081   });
12082   if (temp == NULL)
12083    break;
12084   scan = temp;
12085  }
12086
12087  if (reg_off_by_arg[OP(scan)]) {
12088   ARG_SET(scan, val - scan);
12089  }
12090  else {
12091   NEXT_OFF(scan) = val - scan;
12092  }
12093 }
12094
12095 #ifdef DEBUGGING
12096 /*
12097 - regtail_study - set the next-pointer at the end of a node chain of p to val.
12098 - Look for optimizable sequences at the same time.
12099 - currently only looks for EXACT chains.
12100
12101 This is experimental code. The idea is to use this routine to perform
12102 in place optimizations on branches and groups as they are constructed,
12103 with the long term intention of removing optimization from study_chunk so
12104 that it is purely analytical.
12105
12106 Currently only used when in DEBUG mode. The macro REGTAIL_STUDY() is used
12107 to control which is which.
12108
12109 */
12110 /* TODO: All four parms should be const */
12111
12112 STATIC U8
12113 S_regtail_study(pTHX_ RExC_state_t *pRExC_state, regnode *p, const regnode *val,U32 depth)
12114 {
12115  dVAR;
12116  register regnode *scan;
12117  U8 exact = PSEUDO;
12118 #ifdef EXPERIMENTAL_INPLACESCAN
12119  I32 min = 0;
12120 #endif
12121  GET_RE_DEBUG_FLAGS_DECL;
12122
12123  PERL_ARGS_ASSERT_REGTAIL_STUDY;
12124
12125
12126  if (SIZE_ONLY)
12127   return exact;
12128
12129  /* Find last node. */
12130
12131  scan = p;
12132  for (;;) {
12133   regnode * const temp = regnext(scan);
12134 #ifdef EXPERIMENTAL_INPLACESCAN
12135   if (PL_regkind[OP(scan)] == EXACT) {
12136    bool has_exactf_sharp_s; /* Unexamined in this routine */
12137    if (join_exact(pRExC_state,scan,&min, &has_exactf_sharp_s, 1,val,depth+1))
12138     return EXACT;
12139   }
12140 #endif
12141   if ( exact ) {
12142    switch (OP(scan)) {
12143     case EXACT:
12144     case EXACTF:
12145     case EXACTFA:
12146     case EXACTFU:
12147     case EXACTFU_SS:
12148     case EXACTFU_TRICKYFOLD:
12149     case EXACTFL:
12150       if( exact == PSEUDO )
12151        exact= OP(scan);
12152       else if ( exact != OP(scan) )
12153        exact= 0;
12154     case NOTHING:
12155      break;
12156     default:
12157      exact= 0;
12158    }
12159   }
12160   DEBUG_PARSE_r({
12161    SV * const mysv=sv_newmortal();
12162    DEBUG_PARSE_MSG((scan==p ? "tsdy" : ""));
12163    regprop(RExC_rx, mysv, scan);
12164    PerlIO_printf(Perl_debug_log, "~ %s (%d) -> %s\n",
12165     SvPV_nolen_const(mysv),
12166     REG_NODE_NUM(scan),
12167     PL_reg_name[exact]);
12168   });
12169   if (temp == NULL)
12170    break;
12171   scan = temp;
12172  }
12173  DEBUG_PARSE_r({
12174   SV * const mysv_val=sv_newmortal();
12175   DEBUG_PARSE_MSG("");
12176   regprop(RExC_rx, mysv_val, val);
12177   PerlIO_printf(Perl_debug_log, "~ attach to %s (%"IVdf") offset to %"IVdf"\n",
12178      SvPV_nolen_const(mysv_val),
12179      (IV)REG_NODE_NUM(val),
12180      (IV)(val - scan)
12181   );
12182  });
12183  if (reg_off_by_arg[OP(scan)]) {
12184   ARG_SET(scan, val - scan);
12185  }
12186  else {
12187   NEXT_OFF(scan) = val - scan;
12188  }
12189
12190  return exact;
12191 }
12192 #endif
12193
12194 /*
12195  - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
12196  */
12197 #ifdef DEBUGGING
12198 static void
12199 S_regdump_extflags(pTHX_ const char *lead, const U32 flags)
12200 {
12201  int bit;
12202  int set=0;
12203  regex_charset cs;
12204
12205  for (bit=0; bit<32; bit++) {
12206   if (flags & (1<<bit)) {
12207    if ((1<<bit) & RXf_PMf_CHARSET) { /* Output separately, below */
12208     continue;
12209    }
12210    if (!set++ && lead)
12211     PerlIO_printf(Perl_debug_log, "%s",lead);
12212    PerlIO_printf(Perl_debug_log, "%s ",PL_reg_extflags_name[bit]);
12213   }
12214  }
12215  if ((cs = get_regex_charset(flags)) != REGEX_DEPENDS_CHARSET) {
12216    if (!set++ && lead) {
12217     PerlIO_printf(Perl_debug_log, "%s",lead);
12218    }
12219    switch (cs) {
12220     case REGEX_UNICODE_CHARSET:
12221      PerlIO_printf(Perl_debug_log, "UNICODE");
12222      break;
12223     case REGEX_LOCALE_CHARSET:
12224      PerlIO_printf(Perl_debug_log, "LOCALE");
12225      break;
12226     case REGEX_ASCII_RESTRICTED_CHARSET:
12227      PerlIO_printf(Perl_debug_log, "ASCII-RESTRICTED");
12228      break;
12229     case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
12230      PerlIO_printf(Perl_debug_log, "ASCII-MORE_RESTRICTED");
12231      break;
12232     default:
12233      PerlIO_printf(Perl_debug_log, "UNKNOWN CHARACTER SET");
12234      break;
12235    }
12236  }
12237  if (lead)  {
12238   if (set)
12239    PerlIO_printf(Perl_debug_log, "\n");
12240   else
12241    PerlIO_printf(Perl_debug_log, "%s[none-set]\n",lead);
12242  }
12243 }
12244 #endif
12245
12246 void
12247 Perl_regdump(pTHX_ const regexp *r)
12248 {
12249 #ifdef DEBUGGING
12250  dVAR;
12251  SV * const sv = sv_newmortal();
12252  SV *dsv= sv_newmortal();
12253  RXi_GET_DECL(r,ri);
12254  GET_RE_DEBUG_FLAGS_DECL;
12255
12256  PERL_ARGS_ASSERT_REGDUMP;
12257
12258  (void)dumpuntil(r, ri->program, ri->program + 1, NULL, NULL, sv, 0, 0);
12259
12260  /* Header fields of interest. */
12261  if (r->anchored_substr) {
12262   RE_PV_QUOTED_DECL(s, 0, dsv, SvPVX_const(r->anchored_substr),
12263    RE_SV_DUMPLEN(r->anchored_substr), 30);
12264   PerlIO_printf(Perl_debug_log,
12265      "anchored %s%s at %"IVdf" ",
12266      s, RE_SV_TAIL(r->anchored_substr),
12267      (IV)r->anchored_offset);
12268  } else if (r->anchored_utf8) {
12269   RE_PV_QUOTED_DECL(s, 1, dsv, SvPVX_const(r->anchored_utf8),
12270    RE_SV_DUMPLEN(r->anchored_utf8), 30);
12271   PerlIO_printf(Perl_debug_log,
12272      "anchored utf8 %s%s at %"IVdf" ",
12273      s, RE_SV_TAIL(r->anchored_utf8),
12274      (IV)r->anchored_offset);
12275  }
12276  if (r->float_substr) {
12277   RE_PV_QUOTED_DECL(s, 0, dsv, SvPVX_const(r->float_substr),
12278    RE_SV_DUMPLEN(r->float_substr), 30);
12279   PerlIO_printf(Perl_debug_log,
12280      "floating %s%s at %"IVdf"..%"UVuf" ",
12281      s, RE_SV_TAIL(r->float_substr),
12282      (IV)r->float_min_offset, (UV)r->float_max_offset);
12283  } else if (r->float_utf8) {
12284   RE_PV_QUOTED_DECL(s, 1, dsv, SvPVX_const(r->float_utf8),
12285    RE_SV_DUMPLEN(r->float_utf8), 30);
12286   PerlIO_printf(Perl_debug_log,
12287      "floating utf8 %s%s at %"IVdf"..%"UVuf" ",
12288      s, RE_SV_TAIL(r->float_utf8),
12289      (IV)r->float_min_offset, (UV)r->float_max_offset);
12290  }
12291  if (r->check_substr || r->check_utf8)
12292   PerlIO_printf(Perl_debug_log,
12293      (const char *)
12294      (r->check_substr == r->float_substr
12295      && r->check_utf8 == r->float_utf8
12296      ? "(checking floating" : "(checking anchored"));
12297  if (r->extflags & RXf_NOSCAN)
12298   PerlIO_printf(Perl_debug_log, " noscan");
12299  if (r->extflags & RXf_CHECK_ALL)
12300   PerlIO_printf(Perl_debug_log, " isall");
12301  if (r->check_substr || r->check_utf8)
12302   PerlIO_printf(Perl_debug_log, ") ");
12303
12304  if (ri->regstclass) {
12305   regprop(r, sv, ri->regstclass);
12306   PerlIO_printf(Perl_debug_log, "stclass %s ", SvPVX_const(sv));
12307  }
12308  if (r->extflags & RXf_ANCH) {
12309   PerlIO_printf(Perl_debug_log, "anchored");
12310   if (r->extflags & RXf_ANCH_BOL)
12311    PerlIO_printf(Perl_debug_log, "(BOL)");
12312   if (r->extflags & RXf_ANCH_MBOL)
12313    PerlIO_printf(Perl_debug_log, "(MBOL)");
12314   if (r->extflags & RXf_ANCH_SBOL)
12315    PerlIO_printf(Perl_debug_log, "(SBOL)");
12316   if (r->extflags & RXf_ANCH_GPOS)
12317    PerlIO_printf(Perl_debug_log, "(GPOS)");
12318   PerlIO_putc(Perl_debug_log, ' ');
12319  }
12320  if (r->extflags & RXf_GPOS_SEEN)
12321   PerlIO_printf(Perl_debug_log, "GPOS:%"UVuf" ", (UV)r->gofs);
12322  if (r->intflags & PREGf_SKIP)
12323   PerlIO_printf(Perl_debug_log, "plus ");
12324  if (r->intflags & PREGf_IMPLICIT)
12325   PerlIO_printf(Perl_debug_log, "implicit ");
12326  PerlIO_printf(Perl_debug_log, "minlen %"IVdf" ", (IV)r->minlen);
12327  if (r->extflags & RXf_EVAL_SEEN)
12328   PerlIO_printf(Perl_debug_log, "with eval ");
12329  PerlIO_printf(Perl_debug_log, "\n");
12330  DEBUG_FLAGS_r(regdump_extflags("r->extflags: ",r->extflags));
12331 #else
12332  PERL_ARGS_ASSERT_REGDUMP;
12333  PERL_UNUSED_CONTEXT;
12334  PERL_UNUSED_ARG(r);
12335 #endif /* DEBUGGING */
12336 }
12337
12338 /*
12339 - regprop - printable representation of opcode
12340 */
12341 #define EMIT_ANYOF_TEST_SEPARATOR(do_sep,sv,flags) \
12342 STMT_START { \
12343   if (do_sep) {                           \
12344    Perl_sv_catpvf(aTHX_ sv,"%s][%s",PL_colors[1],PL_colors[0]); \
12345    if (flags & ANYOF_INVERT)           \
12346     /*make sure the invert info is in each */ \
12347     sv_catpvs(sv, "^");             \
12348    do_sep = 0;                         \
12349   }                                       \
12350 } STMT_END
12351
12352 void
12353 Perl_regprop(pTHX_ const regexp *prog, SV *sv, const regnode *o)
12354 {
12355 #ifdef DEBUGGING
12356  dVAR;
12357  register int k;
12358  RXi_GET_DECL(prog,progi);
12359  GET_RE_DEBUG_FLAGS_DECL;
12360
12361  PERL_ARGS_ASSERT_REGPROP;
12362
12363  sv_setpvs(sv, "");
12364
12365  if (OP(o) > REGNODE_MAX)  /* regnode.type is unsigned */
12366   /* It would be nice to FAIL() here, but this may be called from
12367   regexec.c, and it would be hard to supply pRExC_state. */
12368   Perl_croak(aTHX_ "Corrupted regexp opcode %d > %d", (int)OP(o), (int)REGNODE_MAX);
12369  sv_catpv(sv, PL_reg_name[OP(o)]); /* Take off const! */
12370
12371  k = PL_regkind[OP(o)];
12372
12373  if (k == EXACT) {
12374   sv_catpvs(sv, " ");
12375   /* Using is_utf8_string() (via PERL_PV_UNI_DETECT)
12376   * is a crude hack but it may be the best for now since
12377   * we have no flag "this EXACTish node was UTF-8"
12378   * --jhi */
12379   pv_pretty(sv, STRING(o), STR_LEN(o), 60, PL_colors[0], PL_colors[1],
12380     PERL_PV_ESCAPE_UNI_DETECT |
12381     PERL_PV_ESCAPE_NONASCII   |
12382     PERL_PV_PRETTY_ELLIPSES   |
12383     PERL_PV_PRETTY_LTGT       |
12384     PERL_PV_PRETTY_NOCLEAR
12385     );
12386  } else if (k == TRIE) {
12387   /* print the details of the trie in dumpuntil instead, as
12388   * progi->data isn't available here */
12389   const char op = OP(o);
12390   const U32 n = ARG(o);
12391   const reg_ac_data * const ac = IS_TRIE_AC(op) ?
12392    (reg_ac_data *)progi->data->data[n] :
12393    NULL;
12394   const reg_trie_data * const trie
12395    = (reg_trie_data*)progi->data->data[!IS_TRIE_AC(op) ? n : ac->trie];
12396
12397   Perl_sv_catpvf(aTHX_ sv, "-%s",PL_reg_name[o->flags]);
12398   DEBUG_TRIE_COMPILE_r(
12399    Perl_sv_catpvf(aTHX_ sv,
12400     "<S:%"UVuf"/%"IVdf" W:%"UVuf" L:%"UVuf"/%"UVuf" C:%"UVuf"/%"UVuf">",
12401     (UV)trie->startstate,
12402     (IV)trie->statecount-1, /* -1 because of the unused 0 element */
12403     (UV)trie->wordcount,
12404     (UV)trie->minlen,
12405     (UV)trie->maxlen,
12406     (UV)TRIE_CHARCOUNT(trie),
12407     (UV)trie->uniquecharcount
12408    )
12409   );
12410   if ( IS_ANYOF_TRIE(op) || trie->bitmap ) {
12411    int i;
12412    int rangestart = -1;
12413    U8* bitmap = IS_ANYOF_TRIE(op) ? (U8*)ANYOF_BITMAP(o) : (U8*)TRIE_BITMAP(trie);
12414    sv_catpvs(sv, "[");
12415    for (i = 0; i <= 256; i++) {
12416     if (i < 256 && BITMAP_TEST(bitmap,i)) {
12417      if (rangestart == -1)
12418       rangestart = i;
12419     } else if (rangestart != -1) {
12420      if (i <= rangestart + 3)
12421       for (; rangestart < i; rangestart++)
12422        put_byte(sv, rangestart);
12423      else {
12424       put_byte(sv, rangestart);
12425       sv_catpvs(sv, "-");
12426       put_byte(sv, i - 1);
12427      }
12428      rangestart = -1;
12429     }
12430    }
12431    sv_catpvs(sv, "]");
12432   }
12433
12434  } else if (k == CURLY) {
12435   if (OP(o) == CURLYM || OP(o) == CURLYN || OP(o) == CURLYX)
12436    Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
12437   Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
12438  }
12439  else if (k == WHILEM && o->flags)   /* Ordinal/of */
12440   Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
12441  else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP || OP(o)==ACCEPT) {
12442   Perl_sv_catpvf(aTHX_ sv, "%d", (int)ARG(o)); /* Parenth number */
12443   if ( RXp_PAREN_NAMES(prog) ) {
12444    if ( k != REF || (OP(o) < NREF)) {
12445     AV *list= MUTABLE_AV(progi->data->data[progi->name_list_idx]);
12446     SV **name= av_fetch(list, ARG(o), 0 );
12447     if (name)
12448      Perl_sv_catpvf(aTHX_ sv, " '%"SVf"'", SVfARG(*name));
12449    }
12450    else {
12451     AV *list= MUTABLE_AV(progi->data->data[ progi->name_list_idx ]);
12452     SV *sv_dat= MUTABLE_SV(progi->data->data[ ARG( o ) ]);
12453     I32 *nums=(I32*)SvPVX(sv_dat);
12454     SV **name= av_fetch(list, nums[0], 0 );
12455     I32 n;
12456     if (name) {
12457      for ( n=0; n<SvIVX(sv_dat); n++ ) {
12458       Perl_sv_catpvf(aTHX_ sv, "%s%"IVdf,
12459          (n ? "," : ""), (IV)nums[n]);
12460      }
12461      Perl_sv_catpvf(aTHX_ sv, " '%"SVf"'", SVfARG(*name));
12462     }
12463    }
12464   }
12465  } else if (k == GOSUB)
12466   Perl_sv_catpvf(aTHX_ sv, "%d[%+d]", (int)ARG(o),(int)ARG2L(o)); /* Paren and offset */
12467  else if (k == VERB) {
12468   if (!o->flags)
12469    Perl_sv_catpvf(aTHX_ sv, ":%"SVf,
12470       SVfARG((MUTABLE_SV(progi->data->data[ ARG( o ) ]))));
12471  } else if (k == LOGICAL)
12472   Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* 2: embedded, otherwise 1 */
12473  else if (k == ANYOF) {
12474   int i, rangestart = -1;
12475   const U8 flags = ANYOF_FLAGS(o);
12476   int do_sep = 0;
12477
12478   /* Should be synchronized with * ANYOF_ #xdefines in regcomp.h */
12479   static const char * const anyofs[] = {
12480    "\\w",
12481    "\\W",
12482    "\\s",
12483    "\\S",
12484    "\\d",
12485    "\\D",
12486    "[:alnum:]",
12487    "[:^alnum:]",
12488    "[:alpha:]",
12489    "[:^alpha:]",
12490    "[:ascii:]",
12491    "[:^ascii:]",
12492    "[:cntrl:]",
12493    "[:^cntrl:]",
12494    "[:graph:]",
12495    "[:^graph:]",
12496    "[:lower:]",
12497    "[:^lower:]",
12498    "[:print:]",
12499    "[:^print:]",
12500    "[:punct:]",
12501    "[:^punct:]",
12502    "[:upper:]",
12503    "[:^upper:]",
12504    "[:xdigit:]",
12505    "[:^xdigit:]",
12506    "[:space:]",
12507    "[:^space:]",
12508    "[:blank:]",
12509    "[:^blank:]"
12510   };
12511
12512   if (flags & ANYOF_LOCALE)
12513    sv_catpvs(sv, "{loc}");
12514   if (flags & ANYOF_LOC_NONBITMAP_FOLD)
12515    sv_catpvs(sv, "{i}");
12516   Perl_sv_catpvf(aTHX_ sv, "[%s", PL_colors[0]);
12517   if (flags & ANYOF_INVERT)
12518    sv_catpvs(sv, "^");
12519
12520   /* output what the standard cp 0-255 bitmap matches */
12521   for (i = 0; i <= 256; i++) {
12522    if (i < 256 && ANYOF_BITMAP_TEST(o,i)) {
12523     if (rangestart == -1)
12524      rangestart = i;
12525    } else if (rangestart != -1) {
12526     if (i <= rangestart + 3)
12527      for (; rangestart < i; rangestart++)
12528       put_byte(sv, rangestart);
12529     else {
12530      put_byte(sv, rangestart);
12531      sv_catpvs(sv, "-");
12532      put_byte(sv, i - 1);
12533     }
12534     do_sep = 1;
12535     rangestart = -1;
12536    }
12537   }
12538
12539   EMIT_ANYOF_TEST_SEPARATOR(do_sep,sv,flags);
12540   /* output any special charclass tests (used entirely under use locale) */
12541   if (ANYOF_CLASS_TEST_ANY_SET(o))
12542    for (i = 0; i < (int)(sizeof(anyofs)/sizeof(char*)); i++)
12543     if (ANYOF_CLASS_TEST(o,i)) {
12544      sv_catpv(sv, anyofs[i]);
12545      do_sep = 1;
12546     }
12547
12548   EMIT_ANYOF_TEST_SEPARATOR(do_sep,sv,flags);
12549
12550   if (flags & ANYOF_NON_UTF8_LATIN1_ALL) {
12551    sv_catpvs(sv, "{non-utf8-latin1-all}");
12552   }
12553
12554   /* output information about the unicode matching */
12555   if (flags & ANYOF_UNICODE_ALL)
12556    sv_catpvs(sv, "{unicode_all}");
12557   else if (ANYOF_NONBITMAP(o))
12558    sv_catpvs(sv, "{unicode}");
12559   if (flags & ANYOF_NONBITMAP_NON_UTF8)
12560    sv_catpvs(sv, "{outside bitmap}");
12561
12562   if (ANYOF_NONBITMAP(o)) {
12563    SV *lv; /* Set if there is something outside the bit map */
12564    SV * const sw = regclass_swash(prog, o, FALSE, &lv, 0);
12565    bool byte_output = FALSE;   /* If something in the bitmap has been
12566           output */
12567
12568    if (lv && lv != &PL_sv_undef) {
12569     if (sw) {
12570      U8 s[UTF8_MAXBYTES_CASE+1];
12571
12572      for (i = 0; i <= 256; i++) { /* Look at chars in bitmap */
12573       uvchr_to_utf8(s, i);
12574
12575       if (i < 256
12576        && ! ANYOF_BITMAP_TEST(o, i)    /* Don't duplicate
12577                things already
12578                output as part
12579                of the bitmap */
12580        && swash_fetch(sw, s, TRUE))
12581       {
12582        if (rangestart == -1)
12583         rangestart = i;
12584       } else if (rangestart != -1) {
12585        byte_output = TRUE;
12586        if (i <= rangestart + 3)
12587         for (; rangestart < i; rangestart++) {
12588          put_byte(sv, rangestart);
12589         }
12590        else {
12591         put_byte(sv, rangestart);
12592         sv_catpvs(sv, "-");
12593         put_byte(sv, i-1);
12594        }
12595        rangestart = -1;
12596       }
12597      }
12598     }
12599
12600     {
12601      char *s = savesvpv(lv);
12602      char * const origs = s;
12603
12604      while (*s && *s != '\n')
12605       s++;
12606
12607      if (*s == '\n') {
12608       const char * const t = ++s;
12609
12610       if (byte_output) {
12611        sv_catpvs(sv, " ");
12612       }
12613
12614       while (*s) {
12615        if (*s == '\n') {
12616
12617         /* Truncate very long output */
12618         if (s - origs > 256) {
12619          Perl_sv_catpvf(aTHX_ sv,
12620             "%.*s...",
12621             (int) (s - origs - 1),
12622             t);
12623          goto out_dump;
12624         }
12625         *s = ' ';
12626        }
12627        else if (*s == '\t') {
12628         *s = '-';
12629        }
12630        s++;
12631       }
12632       if (s[-1] == ' ')
12633        s[-1] = 0;
12634
12635       sv_catpv(sv, t);
12636      }
12637
12638     out_dump:
12639
12640      Safefree(origs);
12641     }
12642     SvREFCNT_dec(lv);
12643    }
12644   }
12645
12646   Perl_sv_catpvf(aTHX_ sv, "%s]", PL_colors[1]);
12647  }
12648  else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
12649   Perl_sv_catpvf(aTHX_ sv, "[%d]", -(o->flags));
12650 #else
12651  PERL_UNUSED_CONTEXT;
12652  PERL_UNUSED_ARG(sv);
12653  PERL_UNUSED_ARG(o);
12654  PERL_UNUSED_ARG(prog);
12655 #endif /* DEBUGGING */
12656 }
12657
12658 SV *
12659 Perl_re_intuit_string(pTHX_ REGEXP * const r)
12660 {    /* Assume that RE_INTUIT is set */
12661  dVAR;
12662  struct regexp *const prog = (struct regexp *)SvANY(r);
12663  GET_RE_DEBUG_FLAGS_DECL;
12664
12665  PERL_ARGS_ASSERT_RE_INTUIT_STRING;
12666  PERL_UNUSED_CONTEXT;
12667
12668  DEBUG_COMPILE_r(
12669   {
12670    const char * const s = SvPV_nolen_const(prog->check_substr
12671      ? prog->check_substr : prog->check_utf8);
12672
12673    if (!PL_colorset) reginitcolors();
12674    PerlIO_printf(Perl_debug_log,
12675      "%sUsing REx %ssubstr:%s \"%s%.60s%s%s\"\n",
12676      PL_colors[4],
12677      prog->check_substr ? "" : "utf8 ",
12678      PL_colors[5],PL_colors[0],
12679      s,
12680      PL_colors[1],
12681      (strlen(s) > 60 ? "..." : ""));
12682   } );
12683
12684  return prog->check_substr ? prog->check_substr : prog->check_utf8;
12685 }
12686
12687 /*
12688    pregfree()
12689
12690    handles refcounting and freeing the perl core regexp structure. When
12691    it is necessary to actually free the structure the first thing it
12692    does is call the 'free' method of the regexp_engine associated to
12693    the regexp, allowing the handling of the void *pprivate; member
12694    first. (This routine is not overridable by extensions, which is why
12695    the extensions free is called first.)
12696
12697    See regdupe and regdupe_internal if you change anything here.
12698 */
12699 #ifndef PERL_IN_XSUB_RE
12700 void
12701 Perl_pregfree(pTHX_ REGEXP *r)
12702 {
12703  SvREFCNT_dec(r);
12704 }
12705
12706 void
12707 Perl_pregfree2(pTHX_ REGEXP *rx)
12708 {
12709  dVAR;
12710  struct regexp *const r = (struct regexp *)SvANY(rx);
12711  GET_RE_DEBUG_FLAGS_DECL;
12712
12713  PERL_ARGS_ASSERT_PREGFREE2;
12714
12715  if (r->mother_re) {
12716   ReREFCNT_dec(r->mother_re);
12717  } else {
12718   CALLREGFREE_PVT(rx); /* free the private data */
12719   SvREFCNT_dec(RXp_PAREN_NAMES(r));
12720  }
12721  if (r->substrs) {
12722   SvREFCNT_dec(r->anchored_substr);
12723   SvREFCNT_dec(r->anchored_utf8);
12724   SvREFCNT_dec(r->float_substr);
12725   SvREFCNT_dec(r->float_utf8);
12726   Safefree(r->substrs);
12727  }
12728  RX_MATCH_COPY_FREE(rx);
12729 #ifdef PERL_OLD_COPY_ON_WRITE
12730  SvREFCNT_dec(r->saved_copy);
12731 #endif
12732  Safefree(r->offs);
12733 }
12734
12735 /*  reg_temp_copy()
12736
12737  This is a hacky workaround to the structural issue of match results
12738  being stored in the regexp structure which is in turn stored in
12739  PL_curpm/PL_reg_curpm. The problem is that due to qr// the pattern
12740  could be PL_curpm in multiple contexts, and could require multiple
12741  result sets being associated with the pattern simultaneously, such
12742  as when doing a recursive match with (??{$qr})
12743
12744  The solution is to make a lightweight copy of the regexp structure
12745  when a qr// is returned from the code executed by (??{$qr}) this
12746  lightweight copy doesn't actually own any of its data except for
12747  the starp/end and the actual regexp structure itself.
12748
12749 */
12750
12751
12752 REGEXP *
12753 Perl_reg_temp_copy (pTHX_ REGEXP *ret_x, REGEXP *rx)
12754 {
12755  struct regexp *ret;
12756  struct regexp *const r = (struct regexp *)SvANY(rx);
12757  register const I32 npar = r->nparens+1;
12758
12759  PERL_ARGS_ASSERT_REG_TEMP_COPY;
12760
12761  if (!ret_x)
12762   ret_x = (REGEXP*) newSV_type(SVt_REGEXP);
12763  ret = (struct regexp *)SvANY(ret_x);
12764
12765  (void)ReREFCNT_inc(rx);
12766  /* We can take advantage of the existing "copied buffer" mechanism in SVs
12767  by pointing directly at the buffer, but flagging that the allocated
12768  space in the copy is zero. As we've just done a struct copy, it's now
12769  a case of zero-ing that, rather than copying the current length.  */
12770  SvPV_set(ret_x, RX_WRAPPED(rx));
12771  SvFLAGS(ret_x) |= SvFLAGS(rx) & (SVf_POK|SVp_POK|SVf_UTF8);
12772  memcpy(&(ret->xpv_cur), &(r->xpv_cur),
12773   sizeof(regexp) - STRUCT_OFFSET(regexp, xpv_cur));
12774  SvLEN_set(ret_x, 0);
12775  SvSTASH_set(ret_x, NULL);
12776  SvMAGIC_set(ret_x, NULL);
12777  Newx(ret->offs, npar, regexp_paren_pair);
12778  Copy(r->offs, ret->offs, npar, regexp_paren_pair);
12779  if (r->substrs) {
12780   Newx(ret->substrs, 1, struct reg_substr_data);
12781   StructCopy(r->substrs, ret->substrs, struct reg_substr_data);
12782
12783   SvREFCNT_inc_void(ret->anchored_substr);
12784   SvREFCNT_inc_void(ret->anchored_utf8);
12785   SvREFCNT_inc_void(ret->float_substr);
12786   SvREFCNT_inc_void(ret->float_utf8);
12787
12788   /* check_substr and check_utf8, if non-NULL, point to either their
12789   anchored or float namesakes, and don't hold a second reference.  */
12790  }
12791  RX_MATCH_COPIED_off(ret_x);
12792 #ifdef PERL_OLD_COPY_ON_WRITE
12793  ret->saved_copy = NULL;
12794 #endif
12795  ret->mother_re = rx;
12796
12797  return ret_x;
12798 }
12799 #endif
12800
12801 /* regfree_internal()
12802
12803    Free the private data in a regexp. This is overloadable by
12804    extensions. Perl takes care of the regexp structure in pregfree(),
12805    this covers the *pprivate pointer which technically perl doesn't
12806    know about, however of course we have to handle the
12807    regexp_internal structure when no extension is in use.
12808
12809    Note this is called before freeing anything in the regexp
12810    structure.
12811  */
12812
12813 void
12814 Perl_regfree_internal(pTHX_ REGEXP * const rx)
12815 {
12816  dVAR;
12817  struct regexp *const r = (struct regexp *)SvANY(rx);
12818  RXi_GET_DECL(r,ri);
12819  GET_RE_DEBUG_FLAGS_DECL;
12820
12821  PERL_ARGS_ASSERT_REGFREE_INTERNAL;
12822
12823  DEBUG_COMPILE_r({
12824   if (!PL_colorset)
12825    reginitcolors();
12826   {
12827    SV *dsv= sv_newmortal();
12828    RE_PV_QUOTED_DECL(s, RX_UTF8(rx),
12829     dsv, RX_PRECOMP(rx), RX_PRELEN(rx), 60);
12830    PerlIO_printf(Perl_debug_log,"%sFreeing REx:%s %s\n",
12831     PL_colors[4],PL_colors[5],s);
12832   }
12833  });
12834 #ifdef RE_TRACK_PATTERN_OFFSETS
12835  if (ri->u.offsets)
12836   Safefree(ri->u.offsets);             /* 20010421 MJD */
12837 #endif
12838  if (ri->data) {
12839   int n = ri->data->count;
12840   PAD* new_comppad = NULL;
12841   PAD* old_comppad;
12842   PADOFFSET refcnt;
12843
12844   while (--n >= 0) {
12845   /* If you add a ->what type here, update the comment in regcomp.h */
12846    switch (ri->data->what[n]) {
12847    case 'a':
12848    case 's':
12849    case 'S':
12850    case 'u':
12851     SvREFCNT_dec(MUTABLE_SV(ri->data->data[n]));
12852     break;
12853    case 'f':
12854     Safefree(ri->data->data[n]);
12855     break;
12856    case 'p':
12857     new_comppad = MUTABLE_AV(ri->data->data[n]);
12858     break;
12859    case 'o':
12860     if (new_comppad == NULL)
12861      Perl_croak(aTHX_ "panic: pregfree comppad");
12862     PAD_SAVE_LOCAL(old_comppad,
12863      /* Watch out for global destruction's random ordering. */
12864      (SvTYPE(new_comppad) == SVt_PVAV) ? new_comppad : NULL
12865     );
12866     OP_REFCNT_LOCK;
12867     refcnt = OpREFCNT_dec((OP_4tree*)ri->data->data[n]);
12868     OP_REFCNT_UNLOCK;
12869     if (!refcnt)
12870      op_free((OP_4tree*)ri->data->data[n]);
12871
12872     PAD_RESTORE_LOCAL(old_comppad);
12873     SvREFCNT_dec(MUTABLE_SV(new_comppad));
12874     new_comppad = NULL;
12875     break;
12876    case 'n':
12877     break;
12878    case 'T':
12879     { /* Aho Corasick add-on structure for a trie node.
12880      Used in stclass optimization only */
12881      U32 refcount;
12882      reg_ac_data *aho=(reg_ac_data*)ri->data->data[n];
12883      OP_REFCNT_LOCK;
12884      refcount = --aho->refcount;
12885      OP_REFCNT_UNLOCK;
12886      if ( !refcount ) {
12887       PerlMemShared_free(aho->states);
12888       PerlMemShared_free(aho->fail);
12889       /* do this last!!!! */
12890       PerlMemShared_free(ri->data->data[n]);
12891       PerlMemShared_free(ri->regstclass);
12892      }
12893     }
12894     break;
12895    case 't':
12896     {
12897      /* trie structure. */
12898      U32 refcount;
12899      reg_trie_data *trie=(reg_trie_data*)ri->data->data[n];
12900      OP_REFCNT_LOCK;
12901      refcount = --trie->refcount;
12902      OP_REFCNT_UNLOCK;
12903      if ( !refcount ) {
12904       PerlMemShared_free(trie->charmap);
12905       PerlMemShared_free(trie->states);
12906       PerlMemShared_free(trie->trans);
12907       if (trie->bitmap)
12908        PerlMemShared_free(trie->bitmap);
12909       if (trie->jump)
12910        PerlMemShared_free(trie->jump);
12911       PerlMemShared_free(trie->wordinfo);
12912       /* do this last!!!! */
12913       PerlMemShared_free(ri->data->data[n]);
12914      }
12915     }
12916     break;
12917    default:
12918     Perl_croak(aTHX_ "panic: regfree data code '%c'", ri->data->what[n]);
12919    }
12920   }
12921   Safefree(ri->data->what);
12922   Safefree(ri->data);
12923  }
12924
12925  Safefree(ri);
12926 }
12927
12928 #define av_dup_inc(s,t) MUTABLE_AV(sv_dup_inc((const SV *)s,t))
12929 #define hv_dup_inc(s,t) MUTABLE_HV(sv_dup_inc((const SV *)s,t))
12930 #define SAVEPVN(p,n) ((p) ? savepvn(p,n) : NULL)
12931
12932 /*
12933    re_dup - duplicate a regexp.
12934
12935    This routine is expected to clone a given regexp structure. It is only
12936    compiled under USE_ITHREADS.
12937
12938    After all of the core data stored in struct regexp is duplicated
12939    the regexp_engine.dupe method is used to copy any private data
12940    stored in the *pprivate pointer. This allows extensions to handle
12941    any duplication it needs to do.
12942
12943    See pregfree() and regfree_internal() if you change anything here.
12944 */
12945 #if defined(USE_ITHREADS)
12946 #ifndef PERL_IN_XSUB_RE
12947 void
12948 Perl_re_dup_guts(pTHX_ const REGEXP *sstr, REGEXP *dstr, CLONE_PARAMS *param)
12949 {
12950  dVAR;
12951  I32 npar;
12952  const struct regexp *r = (const struct regexp *)SvANY(sstr);
12953  struct regexp *ret = (struct regexp *)SvANY(dstr);
12954
12955  PERL_ARGS_ASSERT_RE_DUP_GUTS;
12956
12957  npar = r->nparens+1;
12958  Newx(ret->offs, npar, regexp_paren_pair);
12959  Copy(r->offs, ret->offs, npar, regexp_paren_pair);
12960  if(ret->swap) {
12961   /* no need to copy these */
12962   Newx(ret->swap, npar, regexp_paren_pair);
12963  }
12964
12965  if (ret->substrs) {
12966   /* Do it this way to avoid reading from *r after the StructCopy().
12967   That way, if any of the sv_dup_inc()s dislodge *r from the L1
12968   cache, it doesn't matter.  */
12969   const bool anchored = r->check_substr
12970    ? r->check_substr == r->anchored_substr
12971    : r->check_utf8 == r->anchored_utf8;
12972   Newx(ret->substrs, 1, struct reg_substr_data);
12973   StructCopy(r->substrs, ret->substrs, struct reg_substr_data);
12974
12975   ret->anchored_substr = sv_dup_inc(ret->anchored_substr, param);
12976   ret->anchored_utf8 = sv_dup_inc(ret->anchored_utf8, param);
12977   ret->float_substr = sv_dup_inc(ret->float_substr, param);
12978   ret->float_utf8 = sv_dup_inc(ret->float_utf8, param);
12979
12980   /* check_substr and check_utf8, if non-NULL, point to either their
12981   anchored or float namesakes, and don't hold a second reference.  */
12982
12983   if (ret->check_substr) {
12984    if (anchored) {
12985     assert(r->check_utf8 == r->anchored_utf8);
12986     ret->check_substr = ret->anchored_substr;
12987     ret->check_utf8 = ret->anchored_utf8;
12988    } else {
12989     assert(r->check_substr == r->float_substr);
12990     assert(r->check_utf8 == r->float_utf8);
12991     ret->check_substr = ret->float_substr;
12992     ret->check_utf8 = ret->float_utf8;
12993    }
12994   } else if (ret->check_utf8) {
12995    if (anchored) {
12996     ret->check_utf8 = ret->anchored_utf8;
12997    } else {
12998     ret->check_utf8 = ret->float_utf8;
12999    }
13000   }
13001  }
13002
13003  RXp_PAREN_NAMES(ret) = hv_dup_inc(RXp_PAREN_NAMES(ret), param);
13004
13005  if (ret->pprivate)
13006   RXi_SET(ret,CALLREGDUPE_PVT(dstr,param));
13007
13008  if (RX_MATCH_COPIED(dstr))
13009   ret->subbeg  = SAVEPVN(ret->subbeg, ret->sublen);
13010  else
13011   ret->subbeg = NULL;
13012 #ifdef PERL_OLD_COPY_ON_WRITE
13013  ret->saved_copy = NULL;
13014 #endif
13015
13016  if (ret->mother_re) {
13017   if (SvPVX_const(dstr) == SvPVX_const(ret->mother_re)) {
13018    /* Our storage points directly to our mother regexp, but that's
13019    1: a buffer in a different thread
13020    2: something we no longer hold a reference on
13021    so we need to copy it locally.  */
13022    /* Note we need to use SvCUR(), rather than
13023    SvLEN(), on our mother_re, because it, in
13024    turn, may well be pointing to its own mother_re.  */
13025    SvPV_set(dstr, SAVEPVN(SvPVX_const(ret->mother_re),
13026         SvCUR(ret->mother_re)+1));
13027    SvLEN_set(dstr, SvCUR(ret->mother_re)+1);
13028   }
13029   ret->mother_re      = NULL;
13030  }
13031  ret->gofs = 0;
13032 }
13033 #endif /* PERL_IN_XSUB_RE */
13034
13035 /*
13036    regdupe_internal()
13037
13038    This is the internal complement to regdupe() which is used to copy
13039    the structure pointed to by the *pprivate pointer in the regexp.
13040    This is the core version of the extension overridable cloning hook.
13041    The regexp structure being duplicated will be copied by perl prior
13042    to this and will be provided as the regexp *r argument, however
13043    with the /old/ structures pprivate pointer value. Thus this routine
13044    may override any copying normally done by perl.
13045
13046    It returns a pointer to the new regexp_internal structure.
13047 */
13048
13049 void *
13050 Perl_regdupe_internal(pTHX_ REGEXP * const rx, CLONE_PARAMS *param)
13051 {
13052  dVAR;
13053  struct regexp *const r = (struct regexp *)SvANY(rx);
13054  regexp_internal *reti;
13055  int len;
13056  RXi_GET_DECL(r,ri);
13057
13058  PERL_ARGS_ASSERT_REGDUPE_INTERNAL;
13059
13060  len = ProgLen(ri);
13061
13062  Newxc(reti, sizeof(regexp_internal) + len*sizeof(regnode), char, regexp_internal);
13063  Copy(ri->program, reti->program, len+1, regnode);
13064
13065
13066  reti->regstclass = NULL;
13067
13068  if (ri->data) {
13069   struct reg_data *d;
13070   const int count = ri->data->count;
13071   int i;
13072
13073   Newxc(d, sizeof(struct reg_data) + count*sizeof(void *),
13074     char, struct reg_data);
13075   Newx(d->what, count, U8);
13076
13077   d->count = count;
13078   for (i = 0; i < count; i++) {
13079    d->what[i] = ri->data->what[i];
13080    switch (d->what[i]) {
13081     /* legal options are one of: sSfpontTua
13082     see also regcomp.h and pregfree() */
13083    case 'a': /* actually an AV, but the dup function is identical.  */
13084    case 's':
13085    case 'S':
13086    case 'p': /* actually an AV, but the dup function is identical.  */
13087    case 'u': /* actually an HV, but the dup function is identical.  */
13088     d->data[i] = sv_dup_inc((const SV *)ri->data->data[i], param);
13089     break;
13090    case 'f':
13091     /* This is cheating. */
13092     Newx(d->data[i], 1, struct regnode_charclass_class);
13093     StructCopy(ri->data->data[i], d->data[i],
13094        struct regnode_charclass_class);
13095     reti->regstclass = (regnode*)d->data[i];
13096     break;
13097    case 'o':
13098     /* Compiled op trees are readonly and in shared memory,
13099     and can thus be shared without duplication. */
13100     OP_REFCNT_LOCK;
13101     d->data[i] = (void*)OpREFCNT_inc((OP*)ri->data->data[i]);
13102     OP_REFCNT_UNLOCK;
13103     break;
13104    case 'T':
13105     /* Trie stclasses are readonly and can thus be shared
13106     * without duplication. We free the stclass in pregfree
13107     * when the corresponding reg_ac_data struct is freed.
13108     */
13109     reti->regstclass= ri->regstclass;
13110     /* Fall through */
13111    case 't':
13112     OP_REFCNT_LOCK;
13113     ((reg_trie_data*)ri->data->data[i])->refcount++;
13114     OP_REFCNT_UNLOCK;
13115     /* Fall through */
13116    case 'n':
13117     d->data[i] = ri->data->data[i];
13118     break;
13119    default:
13120     Perl_croak(aTHX_ "panic: re_dup unknown data code '%c'", ri->data->what[i]);
13121    }
13122   }
13123
13124   reti->data = d;
13125  }
13126  else
13127   reti->data = NULL;
13128
13129  reti->name_list_idx = ri->name_list_idx;
13130
13131 #ifdef RE_TRACK_PATTERN_OFFSETS
13132  if (ri->u.offsets) {
13133   Newx(reti->u.offsets, 2*len+1, U32);
13134   Copy(ri->u.offsets, reti->u.offsets, 2*len+1, U32);
13135  }
13136 #else
13137  SetProgLen(reti,len);
13138 #endif
13139
13140  return (void*)reti;
13141 }
13142
13143 #endif    /* USE_ITHREADS */
13144
13145 #ifndef PERL_IN_XSUB_RE
13146
13147 /*
13148  - regnext - dig the "next" pointer out of a node
13149  */
13150 regnode *
13151 Perl_regnext(pTHX_ register regnode *p)
13152 {
13153  dVAR;
13154  register I32 offset;
13155
13156  if (!p)
13157   return(NULL);
13158
13159  if (OP(p) > REGNODE_MAX) {  /* regnode.type is unsigned */
13160   Perl_croak(aTHX_ "Corrupted regexp opcode %d > %d", (int)OP(p), (int)REGNODE_MAX);
13161  }
13162
13163  offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
13164  if (offset == 0)
13165   return(NULL);
13166
13167  return(p+offset);
13168 }
13169 #endif
13170
13171 STATIC void
13172 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
13173 {
13174  va_list args;
13175  STRLEN l1 = strlen(pat1);
13176  STRLEN l2 = strlen(pat2);
13177  char buf[512];
13178  SV *msv;
13179  const char *message;
13180
13181  PERL_ARGS_ASSERT_RE_CROAK2;
13182
13183  if (l1 > 510)
13184   l1 = 510;
13185  if (l1 + l2 > 510)
13186   l2 = 510 - l1;
13187  Copy(pat1, buf, l1 , char);
13188  Copy(pat2, buf + l1, l2 , char);
13189  buf[l1 + l2] = '\n';
13190  buf[l1 + l2 + 1] = '\0';
13191 #ifdef I_STDARG
13192  /* ANSI variant takes additional second argument */
13193  va_start(args, pat2);
13194 #else
13195  va_start(args);
13196 #endif
13197  msv = vmess(buf, &args);
13198  va_end(args);
13199  message = SvPV_const(msv,l1);
13200  if (l1 > 512)
13201   l1 = 512;
13202  Copy(message, buf, l1 , char);
13203  buf[l1-1] = '\0';   /* Overwrite \n */
13204  Perl_croak(aTHX_ "%s", buf);
13205 }
13206
13207 /* XXX Here's a total kludge.  But we need to re-enter for swash routines. */
13208
13209 #ifndef PERL_IN_XSUB_RE
13210 void
13211 Perl_save_re_context(pTHX)
13212 {
13213  dVAR;
13214
13215  struct re_save_state *state;
13216
13217  SAVEVPTR(PL_curcop);
13218  SSGROW(SAVESTACK_ALLOC_FOR_RE_SAVE_STATE + 1);
13219
13220  state = (struct re_save_state *)(PL_savestack + PL_savestack_ix);
13221  PL_savestack_ix += SAVESTACK_ALLOC_FOR_RE_SAVE_STATE;
13222  SSPUSHUV(SAVEt_RE_STATE);
13223
13224  Copy(&PL_reg_state, state, 1, struct re_save_state);
13225
13226  PL_reg_start_tmp = 0;
13227  PL_reg_start_tmpl = 0;
13228  PL_reg_oldsaved = NULL;
13229  PL_reg_oldsavedlen = 0;
13230  PL_reg_maxiter = 0;
13231  PL_reg_leftiter = 0;
13232  PL_reg_poscache = NULL;
13233  PL_reg_poscache_size = 0;
13234 #ifdef PERL_OLD_COPY_ON_WRITE
13235  PL_nrs = NULL;
13236 #endif
13237
13238  /* Save $1..$n (#18107: UTF-8 s/(\w+)/uc($1)/e); AMS 20021106. */
13239  if (PL_curpm) {
13240   const REGEXP * const rx = PM_GETRE(PL_curpm);
13241   if (rx) {
13242    U32 i;
13243    for (i = 1; i <= RX_NPARENS(rx); i++) {
13244     char digits[TYPE_CHARS(long)];
13245     const STRLEN len = my_snprintf(digits, sizeof(digits), "%lu", (long)i);
13246     GV *const *const gvp
13247      = (GV**)hv_fetch(PL_defstash, digits, len, 0);
13248
13249     if (gvp) {
13250      GV * const gv = *gvp;
13251      if (SvTYPE(gv) == SVt_PVGV && GvSV(gv))
13252       save_scalar(gv);
13253     }
13254    }
13255   }
13256  }
13257 }
13258 #endif
13259
13260 static void
13261 clear_re(pTHX_ void *r)
13262 {
13263  dVAR;
13264  ReREFCNT_dec((REGEXP *)r);
13265 }
13266
13267 #ifdef DEBUGGING
13268
13269 STATIC void
13270 S_put_byte(pTHX_ SV *sv, int c)
13271 {
13272  PERL_ARGS_ASSERT_PUT_BYTE;
13273
13274  /* Our definition of isPRINT() ignores locales, so only bytes that are
13275  not part of UTF-8 are considered printable. I assume that the same
13276  holds for UTF-EBCDIC.
13277  Also, code point 255 is not printable in either (it's E0 in EBCDIC,
13278  which Wikipedia says:
13279
13280  EO, or Eight Ones, is an 8-bit EBCDIC character code represented as all
13281  ones (binary 1111 1111, hexadecimal FF). It is similar, but not
13282  identical, to the ASCII delete (DEL) or rubout control character.
13283  ) So the old condition can be simplified to !isPRINT(c)  */
13284  if (!isPRINT(c)) {
13285   if (c < 256) {
13286    Perl_sv_catpvf(aTHX_ sv, "\\x%02x", c);
13287   }
13288   else {
13289    Perl_sv_catpvf(aTHX_ sv, "\\x{%x}", c);
13290   }
13291  }
13292  else {
13293   const char string = c;
13294   if (c == '-' || c == ']' || c == '\\' || c == '^')
13295    sv_catpvs(sv, "\\");
13296   sv_catpvn(sv, &string, 1);
13297  }
13298 }
13299
13300
13301 #define CLEAR_OPTSTART \
13302  if (optstart) STMT_START { \
13303    DEBUG_OPTIMISE_r(PerlIO_printf(Perl_debug_log, " (%"IVdf" nodes)\n", (IV)(node - optstart))); \
13304    optstart=NULL; \
13305  } STMT_END
13306
13307 #define DUMPUNTIL(b,e) CLEAR_OPTSTART; node=dumpuntil(r,start,(b),(e),last,sv,indent+1,depth+1);
13308
13309 STATIC const regnode *
13310 S_dumpuntil(pTHX_ const regexp *r, const regnode *start, const regnode *node,
13311    const regnode *last, const regnode *plast,
13312    SV* sv, I32 indent, U32 depth)
13313 {
13314  dVAR;
13315  register U8 op = PSEUDO; /* Arbitrary non-END op. */
13316  register const regnode *next;
13317  const regnode *optstart= NULL;
13318
13319  RXi_GET_DECL(r,ri);
13320  GET_RE_DEBUG_FLAGS_DECL;
13321
13322  PERL_ARGS_ASSERT_DUMPUNTIL;
13323
13324 #ifdef DEBUG_DUMPUNTIL
13325  PerlIO_printf(Perl_debug_log, "--- %d : %d - %d - %d\n",indent,node-start,
13326   last ? last-start : 0,plast ? plast-start : 0);
13327 #endif
13328
13329  if (plast && plast < last)
13330   last= plast;
13331
13332  while (PL_regkind[op] != END && (!last || node < last)) {
13333   /* While that wasn't END last time... */
13334   NODE_ALIGN(node);
13335   op = OP(node);
13336   if (op == CLOSE || op == WHILEM)
13337    indent--;
13338   next = regnext((regnode *)node);
13339
13340   /* Where, what. */
13341   if (OP(node) == OPTIMIZED) {
13342    if (!optstart && RE_DEBUG_FLAG(RE_DEBUG_COMPILE_OPTIMISE))
13343     optstart = node;
13344    else
13345     goto after_print;
13346   } else
13347    CLEAR_OPTSTART;
13348
13349   regprop(r, sv, node);
13350   PerlIO_printf(Perl_debug_log, "%4"IVdf":%*s%s", (IV)(node - start),
13351      (int)(2*indent + 1), "", SvPVX_const(sv));
13352
13353   if (OP(node) != OPTIMIZED) {
13354    if (next == NULL)  /* Next ptr. */
13355     PerlIO_printf(Perl_debug_log, " (0)");
13356    else if (PL_regkind[(U8)op] == BRANCH && PL_regkind[OP(next)] != BRANCH )
13357     PerlIO_printf(Perl_debug_log, " (FAIL)");
13358    else
13359     PerlIO_printf(Perl_debug_log, " (%"IVdf")", (IV)(next - start));
13360    (void)PerlIO_putc(Perl_debug_log, '\n');
13361   }
13362
13363  after_print:
13364   if (PL_regkind[(U8)op] == BRANCHJ) {
13365    assert(next);
13366    {
13367     register const regnode *nnode = (OP(next) == LONGJMP
13368            ? regnext((regnode *)next)
13369            : next);
13370     if (last && nnode > last)
13371      nnode = last;
13372     DUMPUNTIL(NEXTOPER(NEXTOPER(node)), nnode);
13373    }
13374   }
13375   else if (PL_regkind[(U8)op] == BRANCH) {
13376    assert(next);
13377    DUMPUNTIL(NEXTOPER(node), next);
13378   }
13379   else if ( PL_regkind[(U8)op]  == TRIE ) {
13380    const regnode *this_trie = node;
13381    const char op = OP(node);
13382    const U32 n = ARG(node);
13383    const reg_ac_data * const ac = op>=AHOCORASICK ?
13384    (reg_ac_data *)ri->data->data[n] :
13385    NULL;
13386    const reg_trie_data * const trie =
13387     (reg_trie_data*)ri->data->data[op<AHOCORASICK ? n : ac->trie];
13388 #ifdef DEBUGGING
13389    AV *const trie_words = MUTABLE_AV(ri->data->data[n + TRIE_WORDS_OFFSET]);
13390 #endif
13391    const regnode *nextbranch= NULL;
13392    I32 word_idx;
13393    sv_setpvs(sv, "");
13394    for (word_idx= 0; word_idx < (I32)trie->wordcount; word_idx++) {
13395     SV ** const elem_ptr = av_fetch(trie_words,word_idx,0);
13396
13397     PerlIO_printf(Perl_debug_log, "%*s%s ",
13398     (int)(2*(indent+3)), "",
13399      elem_ptr ? pv_pretty(sv, SvPV_nolen_const(*elem_ptr), SvCUR(*elem_ptr), 60,
13400        PL_colors[0], PL_colors[1],
13401        (SvUTF8(*elem_ptr) ? PERL_PV_ESCAPE_UNI : 0) |
13402        PERL_PV_PRETTY_ELLIPSES    |
13403        PERL_PV_PRETTY_LTGT
13404        )
13405        : "???"
13406     );
13407     if (trie->jump) {
13408      U16 dist= trie->jump[word_idx+1];
13409      PerlIO_printf(Perl_debug_log, "(%"UVuf")\n",
13410         (UV)((dist ? this_trie + dist : next) - start));
13411      if (dist) {
13412       if (!nextbranch)
13413        nextbranch= this_trie + trie->jump[0];
13414       DUMPUNTIL(this_trie + dist, nextbranch);
13415      }
13416      if (nextbranch && PL_regkind[OP(nextbranch)]==BRANCH)
13417       nextbranch= regnext((regnode *)nextbranch);
13418     } else {
13419      PerlIO_printf(Perl_debug_log, "\n");
13420     }
13421    }
13422    if (last && next > last)
13423     node= last;
13424    else
13425     node= next;
13426   }
13427   else if ( op == CURLY ) {   /* "next" might be very big: optimizer */
13428    DUMPUNTIL(NEXTOPER(node) + EXTRA_STEP_2ARGS,
13429      NEXTOPER(node) + EXTRA_STEP_2ARGS + 1);
13430   }
13431   else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
13432    assert(next);
13433    DUMPUNTIL(NEXTOPER(node) + EXTRA_STEP_2ARGS, next);
13434   }
13435   else if ( op == PLUS || op == STAR) {
13436    DUMPUNTIL(NEXTOPER(node), NEXTOPER(node) + 1);
13437   }
13438   else if (PL_regkind[(U8)op] == ANYOF) {
13439    /* arglen 1 + class block */
13440    node += 1 + ((ANYOF_FLAGS(node) & ANYOF_CLASS)
13441      ? ANYOF_CLASS_SKIP : ANYOF_SKIP);
13442    node = NEXTOPER(node);
13443   }
13444   else if (PL_regkind[(U8)op] == EXACT) {
13445    /* Literal string, where present. */
13446    node += NODE_SZ_STR(node) - 1;
13447    node = NEXTOPER(node);
13448   }
13449   else {
13450    node = NEXTOPER(node);
13451    node += regarglen[(U8)op];
13452   }
13453   if (op == CURLYX || op == OPEN)
13454    indent++;
13455  }
13456  CLEAR_OPTSTART;
13457 #ifdef DEBUG_DUMPUNTIL
13458  PerlIO_printf(Perl_debug_log, "--- %d\n", (int)indent);
13459 #endif
13460  return node;
13461 }
13462
13463 #endif /* DEBUGGING */
13464
13465 /*
13466  * Local variables:
13467  * c-indentation-style: bsd
13468  * c-basic-offset: 4
13469  * indent-tabs-mode: t
13470  * End:
13471  *
13472  * ex: set ts=8 sts=4 sw=4 noet:
13473  */