Re: [ID 20010212.006] Core dump with /((?:hard|soft)cover)?/
[p5sagit/p5-mst-13.2.git] / regcomp.c
1 /*    regcomp.c
2  */
3
4 /*
5  * "A fair jaw-cracker dwarf-language must be."  --Samwise Gamgee
6  */
7
8 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
9  * confused with the original package (see point 3 below).  Thanks, Henry!
10  */
11
12 /* Additional note: this code is very heavily munged from Henry's version
13  * in places.  In some spots I've traded clarity for efficiency, so don't
14  * blame Henry for some of the lack of readability.
15  */
16
17 /* The names of the functions have been changed from regcomp and
18  * regexec to  pregcomp and pregexec in order to avoid conflicts
19  * with the POSIX routines of the same names.
20 */
21
22 #ifdef PERL_EXT_RE_BUILD
23 /* need to replace pregcomp et al, so enable that */
24 #  ifndef PERL_IN_XSUB_RE
25 #    define PERL_IN_XSUB_RE
26 #  endif
27 /* need access to debugger hooks */
28 #  if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
29 #    define DEBUGGING
30 #  endif
31 #endif
32
33 #ifdef PERL_IN_XSUB_RE
34 /* We *really* need to overwrite these symbols: */
35 #  define Perl_pregcomp my_regcomp
36 #  define Perl_regdump my_regdump
37 #  define Perl_regprop my_regprop
38 #  define Perl_pregfree my_regfree
39 #  define Perl_re_intuit_string my_re_intuit_string
40 /* *These* symbols are masked to allow static link. */
41 #  define Perl_regnext my_regnext
42 #  define Perl_save_re_context my_save_re_context
43 #  define Perl_reginitcolors my_reginitcolors
44
45 #  define PERL_NO_GET_CONTEXT
46 #endif
47
48 /*SUPPRESS 112*/
49 /*
50  * pregcomp and pregexec -- regsub and regerror are not used in perl
51  *
52  *      Copyright (c) 1986 by University of Toronto.
53  *      Written by Henry Spencer.  Not derived from licensed software.
54  *
55  *      Permission is granted to anyone to use this software for any
56  *      purpose on any computer system, and to redistribute it freely,
57  *      subject to the following restrictions:
58  *
59  *      1. The author is not responsible for the consequences of use of
60  *              this software, no matter how awful, even if they arise
61  *              from defects in it.
62  *
63  *      2. The origin of this software must not be misrepresented, either
64  *              by explicit claim or by omission.
65  *
66  *      3. Altered versions must be plainly marked as such, and must not
67  *              be misrepresented as being the original software.
68  *
69  *
70  ****    Alterations to Henry's code are...
71  ****
72  ****    Copyright (c) 1991-2001, Larry Wall
73  ****
74  ****    You may distribute under the terms of either the GNU General Public
75  ****    License or the Artistic License, as specified in the README file.
76
77  *
78  * Beware that some of this code is subtly aware of the way operator
79  * precedence is structured in regular expressions.  Serious changes in
80  * regular-expression syntax might require a total rethink.
81  */
82 #include "EXTERN.h"
83 #define PERL_IN_REGCOMP_C
84 #include "perl.h"
85
86 #ifdef PERL_IN_XSUB_RE
87 #  if defined(PERL_CAPI) || defined(PERL_OBJECT)
88 #    include "XSUB.h"
89 #  endif
90 #else
91 #  include "INTERN.h"
92 #endif
93
94 #define REG_COMP_C
95 #include "regcomp.h"
96
97 #ifdef op
98 #undef op
99 #endif /* op */
100
101 #ifdef MSDOS
102 # if defined(BUGGY_MSC6)
103  /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
104  # pragma optimize("a",off)
105  /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
106  # pragma optimize("w",on )
107 # endif /* BUGGY_MSC6 */
108 #endif /* MSDOS */
109
110 #ifndef STATIC
111 #define STATIC  static
112 #endif
113
114 typedef struct RExC_state_t {
115     U16         flags16;                /* are we folding, multilining? */
116     char        *precomp;               /* uncompiled string. */
117     regexp      *rx;
118     char        *end;                   /* End of input for compile */
119     char        *parse;                 /* Input-scan pointer. */
120     I32         whilem_seen;            /* number of WHILEM in this expr */
121     regnode     *emit;                  /* Code-emit pointer; &regdummy = don't = compiling */
122     I32         naughty;                /* How bad is this pattern? */
123     I32         sawback;                /* Did we see \1, ...? */
124     U32         seen;
125     I32         size;                   /* Code size. */
126     I32         npar;                   /* () count. */
127     I32         extralen;
128     I32         seen_zerolen;
129     I32         seen_evals;
130     I32         utf8;
131 #if ADD_TO_REGEXEC
132     char        *starttry;              /* -Dr: where regtry was called. */
133 #define RExC_starttry   (pRExC_state->starttry)
134 #endif
135 } RExC_state_t;
136
137 #define RExC_flags16    (pRExC_state->flags16)
138 #define RExC_precomp    (pRExC_state->precomp)
139 #define RExC_rx         (pRExC_state->rx)
140 #define RExC_end        (pRExC_state->end)
141 #define RExC_parse      (pRExC_state->parse)
142 #define RExC_whilem_seen        (pRExC_state->whilem_seen)
143 #define RExC_emit       (pRExC_state->emit)
144 #define RExC_naughty    (pRExC_state->naughty)
145 #define RExC_sawback    (pRExC_state->sawback)
146 #define RExC_seen       (pRExC_state->seen)
147 #define RExC_size       (pRExC_state->size)
148 #define RExC_npar       (pRExC_state->npar)
149 #define RExC_extralen   (pRExC_state->extralen)
150 #define RExC_seen_zerolen       (pRExC_state->seen_zerolen)
151 #define RExC_seen_evals (pRExC_state->seen_evals)
152 #define RExC_utf8       (pRExC_state->utf8)
153
154 #define ISMULT1(c)      ((c) == '*' || (c) == '+' || (c) == '?')
155 #define ISMULT2(s)      ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
156         ((*s) == '{' && regcurly(s)))
157
158 #ifdef SPSTART
159 #undef SPSTART          /* dratted cpp namespace... */
160 #endif
161 /*
162  * Flags to be passed up and down.
163  */
164 #define WORST           0       /* Worst case. */
165 #define HASWIDTH        0x1     /* Known to match non-null strings. */
166 #define SIMPLE          0x2     /* Simple enough to be STAR/PLUS operand. */
167 #define SPSTART         0x4     /* Starts with * or +. */
168 #define TRYAGAIN        0x8     /* Weeded out a declaration. */
169
170 /* Length of a variant. */
171
172 typedef struct scan_data_t {
173     I32 len_min;
174     I32 len_delta;
175     I32 pos_min;
176     I32 pos_delta;
177     SV *last_found;
178     I32 last_end;                       /* min value, <0 unless valid. */
179     I32 last_start_min;
180     I32 last_start_max;
181     SV **longest;                       /* Either &l_fixed, or &l_float. */
182     SV *longest_fixed;
183     I32 offset_fixed;
184     SV *longest_float;
185     I32 offset_float_min;
186     I32 offset_float_max;
187     I32 flags;
188     I32 whilem_c;
189     I32 *last_closep;
190     struct regnode_charclass_class *start_class;
191 } scan_data_t;
192
193 /*
194  * Forward declarations for pregcomp()'s friends.
195  */
196
197 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198                                       0, 0, 0, 0, 0, 0};
199
200 #define SF_BEFORE_EOL           (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
201 #define SF_BEFORE_SEOL          0x1
202 #define SF_BEFORE_MEOL          0x2
203 #define SF_FIX_BEFORE_EOL       (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
204 #define SF_FL_BEFORE_EOL        (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
205
206 #ifdef NO_UNARY_PLUS
207 #  define SF_FIX_SHIFT_EOL      (0+2)
208 #  define SF_FL_SHIFT_EOL               (0+4)
209 #else
210 #  define SF_FIX_SHIFT_EOL      (+2)
211 #  define SF_FL_SHIFT_EOL               (+4)
212 #endif
213
214 #define SF_FIX_BEFORE_SEOL      (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
215 #define SF_FIX_BEFORE_MEOL      (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
216
217 #define SF_FL_BEFORE_SEOL       (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
218 #define SF_FL_BEFORE_MEOL       (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
219 #define SF_IS_INF               0x40
220 #define SF_HAS_PAR              0x80
221 #define SF_IN_PAR               0x100
222 #define SF_HAS_EVAL             0x200
223 #define SCF_DO_SUBSTR           0x400
224 #define SCF_DO_STCLASS_AND      0x0800
225 #define SCF_DO_STCLASS_OR       0x1000
226 #define SCF_DO_STCLASS          (SCF_DO_STCLASS_AND|SCF_DO_STCLASS_OR)
227 #define SCF_WHILEM_VISITED_POS  0x2000
228
229 #define UTF RExC_utf8
230 #define LOC (RExC_flags16 & PMf_LOCALE)
231 #define FOLD (RExC_flags16 & PMf_FOLD)
232
233 #define OOB_UNICODE             12345678
234 #define OOB_NAMEDCLASS          -1
235
236 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
237 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
238
239
240 /* length of regex to show in messages that don't mark a position within */
241 #define RegexLengthToShowInErrorMessages 127
242
243 /*
244  * If MARKER[12] are adjusted, be sure to adjust the constants at the top
245  * of t/op/regmesg.t, the tests in t/op/re_tests, and those in
246  * op/pragma/warn/regcomp.
247  */
248 #define MARKER1 "HERE"      /* marker as it appears in the description */
249 #define MARKER2 " << HERE "  /* marker as it appears within the regex */
250
251 #define REPORT_LOCATION " before " MARKER1 " mark in regex m/%.*s" MARKER2 "%s/"
252
253 /*
254  * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
255  * arg. Show regex, up to a maximum length. If it's too long, chop and add
256  * "...".
257  */
258 #define FAIL(msg)                                                             \
259     STMT_START {                                                             \
260         char *ellipses = "";                                                 \
261         unsigned len = strlen(RExC_precomp);                                \
262                                                                              \
263         if (!SIZE_ONLY)                                                      \
264             SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx);                 \
265                                                                              \
266         if (len > RegexLengthToShowInErrorMessages) {                        \
267             /* chop 10 shorter than the max, to ensure meaning of "..." */   \
268             len = RegexLengthToShowInErrorMessages - 10;                     \
269             ellipses = "...";                                                \
270         }                                                                    \
271         Perl_croak(aTHX_ "%s in regex m/%.*s%s/",                            \
272                    msg, (int)len, RExC_precomp, ellipses);                  \
273     } STMT_END
274
275 /*
276  * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
277  * args. Show regex, up to a maximum length. If it's too long, chop and add
278  * "...".
279  */
280 #define FAIL2(pat,msg)                                                        \
281     STMT_START {                                                             \
282         char *ellipses = "";                                                 \
283         unsigned len = strlen(RExC_precomp);                                \
284                                                                              \
285         if (!SIZE_ONLY)                                                      \
286             SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx);                 \
287                                                                              \
288         if (len > RegexLengthToShowInErrorMessages) {                        \
289             /* chop 10 shorter than the max, to ensure meaning of "..." */   \
290             len = RegexLengthToShowInErrorMessages - 10;                     \
291             ellipses = "...";                                                \
292         }                                                                    \
293         S_re_croak2(aTHX_ pat, " in regex m/%.*s%s/",                        \
294                     msg, (int)len, RExC_precomp, ellipses);                \
295     } STMT_END
296
297
298 /*
299  * Simple_vFAIL -- like FAIL, but marks the current location in the scan
300  */
301 #define Simple_vFAIL(m)                                                      \
302     STMT_START {                                                             \
303       unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
304                                                                              \
305       Perl_croak(aTHX_ "%s" REPORT_LOCATION,               \
306                  m, (int)offset, RExC_precomp, RExC_precomp + offset);     \
307     } STMT_END
308
309 /*
310  * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL()
311  */
312 #define vFAIL(m)                                                             \
313     STMT_START {                                                             \
314       if (!SIZE_ONLY)                                                        \
315             SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx);                 \
316       Simple_vFAIL(m);                                                       \
317     } STMT_END
318
319 /*
320  * Like Simple_vFAIL(), but accepts two arguments.
321  */
322 #define Simple_vFAIL2(m,a1)                                                  \
323     STMT_START {                                                             \
324       unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
325                                                                              \
326       S_re_croak2(aTHX_ m, REPORT_LOCATION, a1,       \
327                   (int)offset, RExC_precomp, RExC_precomp + offset);       \
328     } STMT_END
329
330 /*
331  * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL2().
332  */
333 #define vFAIL2(m,a1)                                                         \
334     STMT_START {                                                             \
335       if (!SIZE_ONLY)                                                        \
336             SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx);                 \
337       Simple_vFAIL2(m, a1);                                                  \
338     } STMT_END
339
340
341 /*
342  * Like Simple_vFAIL(), but accepts three arguments.
343  */
344 #define Simple_vFAIL3(m, a1, a2)                                             \
345     STMT_START {                                                             \
346       unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
347                                                                              \
348       S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2,   \
349                   (int)offset, RExC_precomp, RExC_precomp + offset);       \
350     } STMT_END
351
352 /*
353  * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL3().
354  */
355 #define vFAIL3(m,a1,a2)                                                      \
356     STMT_START {                                                             \
357       if (!SIZE_ONLY)                                                        \
358             SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx);                 \
359       Simple_vFAIL3(m, a1, a2);                                              \
360     } STMT_END
361
362 /*
363  * Like Simple_vFAIL(), but accepts four arguments.
364  */
365 #define Simple_vFAIL4(m, a1, a2, a3)                                         \
366     STMT_START {                                                             \
367       unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
368                                                                              \
369       S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3,\
370                   (int)offset, RExC_precomp, RExC_precomp + offset);       \
371     } STMT_END
372
373 /*
374  * Like Simple_vFAIL(), but accepts five arguments.
375  */
376 #define Simple_vFAIL5(m, a1, a2, a3, a4)                                     \
377     STMT_START {                                                             \
378       unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
379       S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3, a4,\
380                   (int)offset, RExC_precomp, RExC_precomp + offset);       \
381     } STMT_END
382
383
384 #define vWARN(loc,m)                                                         \
385     STMT_START {                                                             \
386         unsigned offset = strlen(RExC_precomp)-(RExC_end-(loc));          \
387         Perl_warner(aTHX_ WARN_REGEXP, "%s" REPORT_LOCATION,\
388                  m, (int)offset, RExC_precomp, RExC_precomp + offset);          \
389     } STMT_END                                                               \
390
391
392 #define vWARN2(loc, m, a1)                                                   \
393     STMT_START {                                                             \
394         unsigned offset = strlen(RExC_precomp)-(RExC_end-(loc));          \
395         Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION,\
396                  a1,                                                         \
397                  (int)offset, RExC_precomp, RExC_precomp + offset);        \
398     } STMT_END
399
400 #define vWARN3(loc, m, a1, a2)                                               \
401     STMT_START {                                                             \
402       unsigned offset = strlen(RExC_precomp) - (RExC_end - (loc));        \
403         Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION,                    \
404                  a1, a2,                                                     \
405                  (int)offset, RExC_precomp, RExC_precomp + offset);        \
406     } STMT_END
407
408 #define vWARN4(loc, m, a1, a2, a3)                                           \
409     STMT_START {                                                             \
410       unsigned offset = strlen(RExC_precomp)-(RExC_end-(loc));            \
411         Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION,\
412                  a1, a2, a3,                                                 \
413                  (int)offset, RExC_precomp, RExC_precomp + offset);        \
414     } STMT_END
415
416
417 /* Allow for side effects in s */
418 #define REGC(c,s) STMT_START { if (!SIZE_ONLY) *(s) = (c); else (s);} STMT_END
419
420 static void clear_re(pTHXo_ void *r);
421
422 /* Mark that we cannot extend a found fixed substring at this point.
423    Updata the longest found anchored substring and the longest found
424    floating substrings if needed. */
425
426 STATIC void
427 S_scan_commit(pTHX_ RExC_state_t *pRExC_state, scan_data_t *data)
428 {
429     STRLEN l = CHR_SVLEN(data->last_found);
430     STRLEN old_l = CHR_SVLEN(*data->longest);
431
432     if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
433         sv_setsv(*data->longest, data->last_found);
434         if (*data->longest == data->longest_fixed) {
435             data->offset_fixed = l ? data->last_start_min : data->pos_min;
436             if (data->flags & SF_BEFORE_EOL)
437                 data->flags
438                     |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
439             else
440                 data->flags &= ~SF_FIX_BEFORE_EOL;
441         }
442         else {
443             data->offset_float_min = l ? data->last_start_min : data->pos_min;
444             data->offset_float_max = (l
445                                       ? data->last_start_max
446                                       : data->pos_min + data->pos_delta);
447             if (data->flags & SF_BEFORE_EOL)
448                 data->flags
449                     |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
450             else
451                 data->flags &= ~SF_FL_BEFORE_EOL;
452         }
453     }
454     SvCUR_set(data->last_found, 0);
455     data->last_end = -1;
456     data->flags &= ~SF_BEFORE_EOL;
457 }
458
459 /* Can match anything (initialization) */
460 STATIC void
461 S_cl_anything(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
462 {
463     int value;
464
465     ANYOF_CLASS_ZERO(cl);
466     for (value = 0; value < 256; ++value)
467         ANYOF_BITMAP_SET(cl, value);
468     cl->flags = ANYOF_EOS|ANYOF_UNICODE_ALL;
469     if (LOC)
470         cl->flags |= ANYOF_LOCALE;
471 }
472
473 /* Can match anything (initialization) */
474 STATIC int
475 S_cl_is_anything(pTHX_ struct regnode_charclass_class *cl)
476 {
477     int value;
478
479     for (value = 0; value <= ANYOF_MAX; value += 2)
480         if (ANYOF_CLASS_TEST(cl, value) && ANYOF_CLASS_TEST(cl, value + 1))
481             return 1;
482     if (!(cl->flags & ANYOF_UNICODE_ALL))
483         return 0;
484     for (value = 0; value < 256; ++value)
485         if (!ANYOF_BITMAP_TEST(cl, value))
486             return 0;
487     return 1;
488 }
489
490 /* Can match anything (initialization) */
491 STATIC void
492 S_cl_init(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
493 {
494     Zero(cl, 1, struct regnode_charclass_class);
495     cl->type = ANYOF;
496     cl_anything(pRExC_state, cl);
497 }
498
499 STATIC void
500 S_cl_init_zero(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
501 {
502     Zero(cl, 1, struct regnode_charclass_class);
503     cl->type = ANYOF;
504     cl_anything(pRExC_state, cl);
505     if (LOC)
506         cl->flags |= ANYOF_LOCALE;
507 }
508
509 /* 'And' a given class with another one.  Can create false positives */
510 /* We assume that cl is not inverted */
511 STATIC void
512 S_cl_and(pTHX_ struct regnode_charclass_class *cl,
513          struct regnode_charclass_class *and_with)
514 {
515     if (!(and_with->flags & ANYOF_CLASS)
516         && !(cl->flags & ANYOF_CLASS)
517         && (and_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
518         && !(and_with->flags & ANYOF_FOLD)
519         && !(cl->flags & ANYOF_FOLD)) {
520         int i;
521
522         if (and_with->flags & ANYOF_INVERT)
523             for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
524                 cl->bitmap[i] &= ~and_with->bitmap[i];
525         else
526             for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
527                 cl->bitmap[i] &= and_with->bitmap[i];
528     } /* XXXX: logic is complicated otherwise, leave it along for a moment. */
529     if (!(and_with->flags & ANYOF_EOS))
530         cl->flags &= ~ANYOF_EOS;
531
532     if (cl->flags & ANYOF_UNICODE_ALL && and_with->flags & ANYOF_UNICODE) {
533         cl->flags &= ~ANYOF_UNICODE_ALL;
534         cl->flags |= ANYOF_UNICODE;
535         ARG_SET(cl, ARG(and_with));
536     }
537     if (!(and_with->flags & ANYOF_UNICODE_ALL))
538         cl->flags &= ~ANYOF_UNICODE_ALL;
539     if (!(and_with->flags & (ANYOF_UNICODE|ANYOF_UNICODE_ALL)))
540         cl->flags &= ~ANYOF_UNICODE;
541 }
542
543 /* 'OR' a given class with another one.  Can create false positives */
544 /* We assume that cl is not inverted */
545 STATIC void
546 S_cl_or(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl, struct regnode_charclass_class *or_with)
547 {
548     if (or_with->flags & ANYOF_INVERT) {
549         /* We do not use
550          * (B1 | CL1) | (!B2 & !CL2) = (B1 | !B2 & !CL2) | (CL1 | (!B2 & !CL2))
551          *   <= (B1 | !B2) | (CL1 | !CL2)
552          * which is wasteful if CL2 is small, but we ignore CL2:
553          *   (B1 | CL1) | (!B2 & !CL2) <= (B1 | CL1) | !B2 = (B1 | !B2) | CL1
554          * XXXX Can we handle case-fold?  Unclear:
555          *   (OK1(i) | OK1(i')) | !(OK1(i) | OK1(i')) =
556          *   (OK1(i) | OK1(i')) | (!OK1(i) & !OK1(i'))
557          */
558         if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
559              && !(or_with->flags & ANYOF_FOLD)
560              && !(cl->flags & ANYOF_FOLD) ) {
561             int i;
562
563             for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
564                 cl->bitmap[i] |= ~or_with->bitmap[i];
565         } /* XXXX: logic is complicated otherwise */
566         else {
567             cl_anything(pRExC_state, cl);
568         }
569     } else {
570         /* (B1 | CL1) | (B2 | CL2) = (B1 | B2) | (CL1 | CL2)) */
571         if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
572              && (!(or_with->flags & ANYOF_FOLD)
573                  || (cl->flags & ANYOF_FOLD)) ) {
574             int i;
575
576             /* OR char bitmap and class bitmap separately */
577             for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
578                 cl->bitmap[i] |= or_with->bitmap[i];
579             if (or_with->flags & ANYOF_CLASS) {
580                 for (i = 0; i < ANYOF_CLASSBITMAP_SIZE; i++)
581                     cl->classflags[i] |= or_with->classflags[i];
582                 cl->flags |= ANYOF_CLASS;
583             }
584         }
585         else { /* XXXX: logic is complicated, leave it along for a moment. */
586             cl_anything(pRExC_state, cl);
587         }
588     }
589     if (or_with->flags & ANYOF_EOS)
590         cl->flags |= ANYOF_EOS;
591
592     if (cl->flags & ANYOF_UNICODE && or_with->flags & ANYOF_UNICODE &&
593         ARG(cl) != ARG(or_with)) {
594         cl->flags |= ANYOF_UNICODE_ALL;
595         cl->flags &= ~ANYOF_UNICODE;
596     }
597     if (or_with->flags & ANYOF_UNICODE_ALL) {
598         cl->flags |= ANYOF_UNICODE_ALL;
599         cl->flags &= ~ANYOF_UNICODE;
600     }
601 }
602
603 /* REx optimizer.  Converts nodes into quickier variants "in place".
604    Finds fixed substrings.  */
605
606 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
607    to the position after last scanned or to NULL. */
608
609 STATIC I32
610 S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
611                         /* scanp: Start here (read-write). */
612                         /* deltap: Write maxlen-minlen here. */
613                         /* last: Stop before this one. */
614 {
615     I32 min = 0, pars = 0, code;
616     regnode *scan = *scanp, *next;
617     I32 delta = 0;
618     int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
619     int is_inf_internal = 0;            /* The studied chunk is infinite */
620     I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
621     scan_data_t data_fake;
622     struct regnode_charclass_class and_with; /* Valid if flags & SCF_DO_STCLASS_OR */
623
624     while (scan && OP(scan) != END && scan < last) {
625         /* Peephole optimizer: */
626
627         if (PL_regkind[(U8)OP(scan)] == EXACT) {
628             /* Merge several consecutive EXACTish nodes into one. */
629             regnode *n = regnext(scan);
630             U32 stringok = 1;
631 #ifdef DEBUGGING
632             regnode *stop = scan;
633 #endif
634
635             next = scan + NODE_SZ_STR(scan);
636             /* Skip NOTHING, merge EXACT*. */
637             while (n &&
638                    ( PL_regkind[(U8)OP(n)] == NOTHING ||
639                      (stringok && (OP(n) == OP(scan))))
640                    && NEXT_OFF(n)
641                    && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
642                 if (OP(n) == TAIL || n > next)
643                     stringok = 0;
644                 if (PL_regkind[(U8)OP(n)] == NOTHING) {
645                     NEXT_OFF(scan) += NEXT_OFF(n);
646                     next = n + NODE_STEP_REGNODE;
647 #ifdef DEBUGGING
648                     if (stringok)
649                         stop = n;
650 #endif
651                     n = regnext(n);
652                 }
653                 else if (stringok) {
654                     int oldl = STR_LEN(scan);
655                     regnode *nnext = regnext(n);
656
657                     if (oldl + STR_LEN(n) > U8_MAX)
658                         break;
659                     NEXT_OFF(scan) += NEXT_OFF(n);
660                     STR_LEN(scan) += STR_LEN(n);
661                     next = n + NODE_SZ_STR(n);
662                     /* Now we can overwrite *n : */
663                     Move(STRING(n), STRING(scan) + oldl, STR_LEN(n), char);
664 #ifdef DEBUGGING
665                     stop = next - 1;
666 #endif
667                     n = nnext;
668                 }
669             }
670 #ifdef DEBUGGING
671             /* Allow dumping */
672             n = scan + NODE_SZ_STR(scan);
673             while (n <= stop) {
674                 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
675                     OP(n) = OPTIMIZED;
676                     NEXT_OFF(n) = 0;
677                 }
678                 n++;
679             }
680 #endif
681         }
682         /* Follow the next-chain of the current node and optimize
683            away all the NOTHINGs from it.  */
684         if (OP(scan) != CURLYX) {
685             int max = (reg_off_by_arg[OP(scan)]
686                        ? I32_MAX
687                        /* I32 may be smaller than U16 on CRAYs! */
688                        : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
689             int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
690             int noff;
691             regnode *n = scan;
692         
693             /* Skip NOTHING and LONGJMP. */
694             while ((n = regnext(n))
695                    && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
696                        || ((OP(n) == LONGJMP) && (noff = ARG(n))))
697                    && off + noff < max)
698                 off += noff;
699             if (reg_off_by_arg[OP(scan)])
700                 ARG(scan) = off;
701             else
702                 NEXT_OFF(scan) = off;
703         }
704         /* The principal pseudo-switch.  Cannot be a switch, since we
705            look into several different things.  */
706         if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
707                    || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
708             next = regnext(scan);
709             code = OP(scan);
710         
711             if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
712                 I32 max1 = 0, min1 = I32_MAX, num = 0;
713                 struct regnode_charclass_class accum;
714                 
715                 if (flags & SCF_DO_SUBSTR) /* XXXX Add !SUSPEND? */
716                     scan_commit(pRExC_state, data); /* Cannot merge strings after this. */
717                 if (flags & SCF_DO_STCLASS)
718                     cl_init_zero(pRExC_state, &accum);
719                 while (OP(scan) == code) {
720                     I32 deltanext, minnext, f = 0, fake;
721                     struct regnode_charclass_class this_class;
722
723                     num++;
724                     data_fake.flags = 0;
725                     if (data) {         
726                         data_fake.whilem_c = data->whilem_c;
727                         data_fake.last_closep = data->last_closep;
728                     }
729                     else
730                         data_fake.last_closep = &fake;
731                     next = regnext(scan);
732                     scan = NEXTOPER(scan);
733                     if (code != BRANCH)
734                         scan = NEXTOPER(scan);
735                     if (flags & SCF_DO_STCLASS) {
736                         cl_init(pRExC_state, &this_class);
737                         data_fake.start_class = &this_class;
738                         f = SCF_DO_STCLASS_AND;
739                     }           
740                     if (flags & SCF_WHILEM_VISITED_POS)
741                         f |= SCF_WHILEM_VISITED_POS;
742                     /* we suppose the run is continuous, last=next...*/
743                     minnext = study_chunk(pRExC_state, &scan, &deltanext,
744                                           next, &data_fake, f);
745                     if (min1 > minnext)
746                         min1 = minnext;
747                     if (max1 < minnext + deltanext)
748                         max1 = minnext + deltanext;
749                     if (deltanext == I32_MAX)
750                         is_inf = is_inf_internal = 1;
751                     scan = next;
752                     if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
753                         pars++;
754                     if (data && (data_fake.flags & SF_HAS_EVAL))
755                         data->flags |= SF_HAS_EVAL;
756                     if (data)
757                         data->whilem_c = data_fake.whilem_c;
758                     if (flags & SCF_DO_STCLASS)
759                         cl_or(pRExC_state, &accum, &this_class);
760                     if (code == SUSPEND)
761                         break;
762                 }
763                 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
764                     min1 = 0;
765                 if (flags & SCF_DO_SUBSTR) {
766                     data->pos_min += min1;
767                     data->pos_delta += max1 - min1;
768                     if (max1 != min1 || is_inf)
769                         data->longest = &(data->longest_float);
770                 }
771                 min += min1;
772                 delta += max1 - min1;
773                 if (flags & SCF_DO_STCLASS_OR) {
774                     cl_or(pRExC_state, data->start_class, &accum);
775                     if (min1) {
776                         cl_and(data->start_class, &and_with);
777                         flags &= ~SCF_DO_STCLASS;
778                     }
779                 }
780                 else if (flags & SCF_DO_STCLASS_AND) {
781                     if (min1) {
782                         cl_and(data->start_class, &accum);
783                         flags &= ~SCF_DO_STCLASS;
784                     }
785                     else {
786                         /* Switch to OR mode: cache the old value of
787                          * data->start_class */
788                         StructCopy(data->start_class, &and_with,
789                                    struct regnode_charclass_class);
790                         flags &= ~SCF_DO_STCLASS_AND;
791                         StructCopy(&accum, data->start_class,
792                                    struct regnode_charclass_class);
793                         flags |= SCF_DO_STCLASS_OR;
794                         data->start_class->flags |= ANYOF_EOS;
795                     }
796                 }
797             }
798             else if (code == BRANCHJ)   /* single branch is optimized. */
799                 scan = NEXTOPER(NEXTOPER(scan));
800             else                        /* single branch is optimized. */
801                 scan = NEXTOPER(scan);
802             continue;
803         }
804         else if (OP(scan) == EXACT) {
805             I32 l = STR_LEN(scan);
806             UV uc = *((U8*)STRING(scan));
807             if (UTF) {
808                 U8 *s = (U8*)STRING(scan);
809                 l = utf8_length(s, s + l);
810                 uc = utf8_to_uv_simple(s, NULL);
811             }
812             min += l;
813             if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
814                 /* The code below prefers earlier match for fixed
815                    offset, later match for variable offset.  */
816                 if (data->last_end == -1) { /* Update the start info. */
817                     data->last_start_min = data->pos_min;
818                     data->last_start_max = is_inf
819                         ? I32_MAX : data->pos_min + data->pos_delta;
820                 }
821                 sv_catpvn(data->last_found, STRING(scan), STR_LEN(scan));
822                 data->last_end = data->pos_min + l;
823                 data->pos_min += l; /* As in the first entry. */
824                 data->flags &= ~SF_BEFORE_EOL;
825             }
826             if (flags & SCF_DO_STCLASS_AND) {
827                 /* Check whether it is compatible with what we know already! */
828                 int compat = 1;
829
830                 if (uc >= 0x100 ||
831                     !(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
832                     && !ANYOF_BITMAP_TEST(data->start_class, uc)
833                     && (!(data->start_class->flags & ANYOF_FOLD)
834                         || !ANYOF_BITMAP_TEST(data->start_class, PL_fold[uc])))
835                     compat = 0;
836                 ANYOF_CLASS_ZERO(data->start_class);
837                 ANYOF_BITMAP_ZERO(data->start_class);
838                 if (compat)
839                     ANYOF_BITMAP_SET(data->start_class, uc);
840                 data->start_class->flags &= ~ANYOF_EOS;
841                 if (uc < 0x100)
842                   data->start_class->flags &= ~ANYOF_UNICODE_ALL;
843             }
844             else if (flags & SCF_DO_STCLASS_OR) {
845                 /* false positive possible if the class is case-folded */
846                 if (uc < 0x100)
847                     ANYOF_BITMAP_SET(data->start_class, uc);
848                 else
849                     data->start_class->flags |= ANYOF_UNICODE_ALL;
850                 data->start_class->flags &= ~ANYOF_EOS;
851                 cl_and(data->start_class, &and_with);
852             }
853             flags &= ~SCF_DO_STCLASS;
854         }
855         else if (PL_regkind[(U8)OP(scan)] == EXACT) { /* But OP != EXACT! */
856             I32 l = STR_LEN(scan);
857             UV uc = *((U8*)STRING(scan));
858
859             /* Search for fixed substrings supports EXACT only. */
860             if (flags & SCF_DO_SUBSTR)
861                 scan_commit(pRExC_state, data);
862             if (UTF) {
863                 U8 *s = (U8 *)STRING(scan);
864                 l = utf8_length(s, s + l);
865                 uc = utf8_to_uv_simple(s, NULL);
866             }
867             min += l;
868             if (data && (flags & SCF_DO_SUBSTR))
869                 data->pos_min += l;
870             if (flags & SCF_DO_STCLASS_AND) {
871                 /* Check whether it is compatible with what we know already! */
872                 int compat = 1;
873
874                 if (uc >= 0x100 ||
875                     !(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
876                     && !ANYOF_BITMAP_TEST(data->start_class, uc)
877                     && !ANYOF_BITMAP_TEST(data->start_class, PL_fold[uc]))
878                     compat = 0;
879                 ANYOF_CLASS_ZERO(data->start_class);
880                 ANYOF_BITMAP_ZERO(data->start_class);
881                 if (compat) {
882                     ANYOF_BITMAP_SET(data->start_class, uc);
883                     data->start_class->flags &= ~ANYOF_EOS;
884                     data->start_class->flags |= ANYOF_FOLD;
885                     if (OP(scan) == EXACTFL)
886                         data->start_class->flags |= ANYOF_LOCALE;
887                 }
888             }
889             else if (flags & SCF_DO_STCLASS_OR) {
890                 if (data->start_class->flags & ANYOF_FOLD) {
891                     /* false positive possible if the class is case-folded.
892                        Assume that the locale settings are the same... */
893                     if (uc < 0x100)
894                         ANYOF_BITMAP_SET(data->start_class, uc);
895                     data->start_class->flags &= ~ANYOF_EOS;
896                 }
897                 cl_and(data->start_class, &and_with);
898             }
899             flags &= ~SCF_DO_STCLASS;
900         }
901         else if (strchr((char*)PL_varies,OP(scan))) {
902             I32 mincount, maxcount, minnext, deltanext, fl;
903             I32 f = flags, pos_before = 0;
904             regnode *oscan = scan;
905             struct regnode_charclass_class this_class;
906             struct regnode_charclass_class *oclass = NULL;
907
908             switch (PL_regkind[(U8)OP(scan)]) {
909             case WHILEM:                /* End of (?:...)* . */
910                 scan = NEXTOPER(scan);
911                 goto finish;
912             case PLUS:
913                 if (flags & (SCF_DO_SUBSTR | SCF_DO_STCLASS)) {
914                     next = NEXTOPER(scan);
915                     if (OP(next) == EXACT || (flags & SCF_DO_STCLASS)) {
916                         mincount = 1;
917                         maxcount = REG_INFTY;
918                         next = regnext(scan);
919                         scan = NEXTOPER(scan);
920                         goto do_curly;
921                     }
922                 }
923                 if (flags & SCF_DO_SUBSTR)
924                     data->pos_min++;
925                 min++;
926                 /* Fall through. */
927             case STAR:
928                 if (flags & SCF_DO_STCLASS) {
929                     mincount = 0;
930                     maxcount = REG_INFTY;
931                     next = regnext(scan);
932                     scan = NEXTOPER(scan);
933                     goto do_curly;
934                 }
935                 is_inf = is_inf_internal = 1;
936                 scan = regnext(scan);
937                 if (flags & SCF_DO_SUBSTR) {
938                     scan_commit(pRExC_state, data); /* Cannot extend fixed substrings */
939                     data->longest = &(data->longest_float);
940                 }
941                 goto optimize_curly_tail;
942             case CURLY:
943                 mincount = ARG1(scan);
944                 maxcount = ARG2(scan);
945                 next = regnext(scan);
946                 if (OP(scan) == CURLYX) {
947                     I32 lp = (data ? *(data->last_closep) : 0);
948
949                     scan->flags = ((lp <= U8_MAX) ? lp : U8_MAX);
950                 }
951                 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
952               do_curly:
953                 if (flags & SCF_DO_SUBSTR) {
954                     if (mincount == 0) scan_commit(pRExC_state,data); /* Cannot extend fixed substrings */
955                     pos_before = data->pos_min;
956                 }
957                 if (data) {
958                     fl = data->flags;
959                     data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
960                     if (is_inf)
961                         data->flags |= SF_IS_INF;
962                 }
963                 if (flags & SCF_DO_STCLASS) {
964                     cl_init(pRExC_state, &this_class);
965                     oclass = data->start_class;
966                     data->start_class = &this_class;
967                     f |= SCF_DO_STCLASS_AND;
968                     f &= ~SCF_DO_STCLASS_OR;
969                 }
970                 /* These are the cases when once a subexpression
971                    fails at a particular position, it cannot succeed
972                    even after backtracking at the enclosing scope.
973                 
974                    XXXX what if minimal match and we are at the
975                         initial run of {n,m}? */
976                 if ((mincount != maxcount - 1) && (maxcount != REG_INFTY))
977                     f &= ~SCF_WHILEM_VISITED_POS;
978
979                 /* This will finish on WHILEM, setting scan, or on NULL: */
980                 minnext = study_chunk(pRExC_state, &scan, &deltanext, last, data,
981                                       mincount == 0
982                                         ? (f & ~SCF_DO_SUBSTR) : f);
983
984                 if (flags & SCF_DO_STCLASS)
985                     data->start_class = oclass;
986                 if (mincount == 0 || minnext == 0) {
987                     if (flags & SCF_DO_STCLASS_OR) {
988                         cl_or(pRExC_state, data->start_class, &this_class);
989                     }
990                     else if (flags & SCF_DO_STCLASS_AND) {
991                         /* Switch to OR mode: cache the old value of
992                          * data->start_class */
993                         StructCopy(data->start_class, &and_with,
994                                    struct regnode_charclass_class);
995                         flags &= ~SCF_DO_STCLASS_AND;
996                         StructCopy(&this_class, data->start_class,
997                                    struct regnode_charclass_class);
998                         flags |= SCF_DO_STCLASS_OR;
999                         data->start_class->flags |= ANYOF_EOS;
1000                     }
1001                 } else {                /* Non-zero len */
1002                     if (flags & SCF_DO_STCLASS_OR) {
1003                         cl_or(pRExC_state, data->start_class, &this_class);
1004                         cl_and(data->start_class, &and_with);
1005                     }
1006                     else if (flags & SCF_DO_STCLASS_AND)
1007                         cl_and(data->start_class, &this_class);
1008                     flags &= ~SCF_DO_STCLASS;
1009                 }
1010                 if (!scan)              /* It was not CURLYX, but CURLY. */
1011                     scan = next;
1012                 if (ckWARN(WARN_REGEXP) && (minnext + deltanext == 0)
1013                     && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
1014                     && maxcount <= REG_INFTY/3) /* Complement check for big count */
1015                 {
1016                     vWARN(RExC_parse,
1017                           "Quantifier unexpected on zero-length expression");
1018                 }
1019
1020                 min += minnext * mincount;
1021                 is_inf_internal |= ((maxcount == REG_INFTY
1022                                      && (minnext + deltanext) > 0)
1023                                     || deltanext == I32_MAX);
1024                 is_inf |= is_inf_internal;
1025                 delta += (minnext + deltanext) * maxcount - minnext * mincount;
1026
1027                 /* Try powerful optimization CURLYX => CURLYN. */
1028                 if (  OP(oscan) == CURLYX && data
1029                       && data->flags & SF_IN_PAR
1030                       && !(data->flags & SF_HAS_EVAL)
1031                       && !deltanext && minnext == 1 ) {
1032                     /* Try to optimize to CURLYN.  */
1033                     regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
1034                     regnode *nxt1 = nxt, *nxt2;
1035
1036                     /* Skip open. */
1037                     nxt = regnext(nxt);
1038                     if (!strchr((char*)PL_simple,OP(nxt))
1039                         && !(PL_regkind[(U8)OP(nxt)] == EXACT
1040                              && STR_LEN(nxt) == 1))
1041                         goto nogo;
1042                     nxt2 = nxt;
1043                     nxt = regnext(nxt);
1044                     if (OP(nxt) != CLOSE)
1045                         goto nogo;
1046                     /* Now we know that nxt2 is the only contents: */
1047                     oscan->flags = ARG(nxt);
1048                     OP(oscan) = CURLYN;
1049                     OP(nxt1) = NOTHING; /* was OPEN. */
1050 #ifdef DEBUGGING
1051                     OP(nxt1 + 1) = OPTIMIZED; /* was count. */
1052                     NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
1053                     NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
1054                     OP(nxt) = OPTIMIZED;        /* was CLOSE. */
1055                     OP(nxt + 1) = OPTIMIZED; /* was count. */
1056                     NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
1057 #endif
1058                 }
1059               nogo:
1060
1061                 /* Try optimization CURLYX => CURLYM. */
1062                 if (  OP(oscan) == CURLYX && data
1063                       && !(data->flags & SF_HAS_PAR)
1064                       && !(data->flags & SF_HAS_EVAL)
1065                       && !deltanext  ) {
1066                     /* XXXX How to optimize if data == 0? */
1067                     /* Optimize to a simpler form.  */
1068                     regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
1069                     regnode *nxt2;
1070
1071                     OP(oscan) = CURLYM;
1072                     while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
1073                             && (OP(nxt2) != WHILEM))
1074                         nxt = nxt2;
1075                     OP(nxt2)  = SUCCEED; /* Whas WHILEM */
1076                     /* Need to optimize away parenths. */
1077                     if (data->flags & SF_IN_PAR) {
1078                         /* Set the parenth number.  */
1079                         regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
1080
1081                         if (OP(nxt) != CLOSE)
1082                             FAIL("Panic opt close");
1083                         oscan->flags = ARG(nxt);
1084                         OP(nxt1) = OPTIMIZED;   /* was OPEN. */
1085                         OP(nxt) = OPTIMIZED;    /* was CLOSE. */
1086 #ifdef DEBUGGING
1087                         OP(nxt1 + 1) = OPTIMIZED; /* was count. */
1088                         OP(nxt + 1) = OPTIMIZED; /* was count. */
1089                         NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
1090                         NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
1091 #endif
1092 #if 0
1093                         while ( nxt1 && (OP(nxt1) != WHILEM)) {
1094                             regnode *nnxt = regnext(nxt1);
1095                         
1096                             if (nnxt == nxt) {
1097                                 if (reg_off_by_arg[OP(nxt1)])
1098                                     ARG_SET(nxt1, nxt2 - nxt1);
1099                                 else if (nxt2 - nxt1 < U16_MAX)
1100                                     NEXT_OFF(nxt1) = nxt2 - nxt1;
1101                                 else
1102                                     OP(nxt) = NOTHING;  /* Cannot beautify */
1103                             }
1104                             nxt1 = nnxt;
1105                         }
1106 #endif
1107                         /* Optimize again: */
1108                         study_chunk(pRExC_state, &nxt1, &deltanext, nxt,
1109                                     NULL, 0);
1110                     }
1111                     else
1112                         oscan->flags = 0;
1113                 }
1114                 else if ((OP(oscan) == CURLYX)
1115                          && (flags & SCF_WHILEM_VISITED_POS)
1116                          /* See the comment on a similar expression above.
1117                             However, this time it not a subexpression
1118                             we care about, but the expression itself. */
1119                          && (maxcount == REG_INFTY)
1120                          && data && ++data->whilem_c < 16) {
1121                     /* This stays as CURLYX, we can put the count/of pair. */
1122                     /* Find WHILEM (as in regexec.c) */
1123                     regnode *nxt = oscan + NEXT_OFF(oscan);
1124
1125                     if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
1126                         nxt += ARG(nxt);
1127                     PREVOPER(nxt)->flags = data->whilem_c
1128                         | (RExC_whilem_seen << 4); /* On WHILEM */
1129                 }
1130                 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
1131                     pars++;
1132                 if (flags & SCF_DO_SUBSTR) {
1133                     SV *last_str = Nullsv;
1134                     int counted = mincount != 0;
1135
1136                     if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
1137                         I32 b = pos_before >= data->last_start_min
1138                             ? pos_before : data->last_start_min;
1139                         STRLEN l;
1140                         char *s = SvPV(data->last_found, l);
1141                         I32 old = b - data->last_start_min;
1142
1143                         if (UTF)
1144                             old = utf8_hop((U8*)s, old) - (U8*)s;
1145                         
1146                         l -= old;
1147                         /* Get the added string: */
1148                         last_str = newSVpvn(s  + old, l);
1149                         if (deltanext == 0 && pos_before == b) {
1150                             /* What was added is a constant string */
1151                             if (mincount > 1) {
1152                                 SvGROW(last_str, (mincount * l) + 1);
1153                                 repeatcpy(SvPVX(last_str) + l,
1154                                           SvPVX(last_str), l, mincount - 1);
1155                                 SvCUR(last_str) *= mincount;
1156                                 /* Add additional parts. */
1157                                 SvCUR_set(data->last_found,
1158                                           SvCUR(data->last_found) - l);
1159                                 sv_catsv(data->last_found, last_str);
1160                                 data->last_end += l * (mincount - 1);
1161                             }
1162                         } else {
1163                             /* start offset must point into the last copy */
1164                             data->last_start_min += minnext * (mincount - 1);
1165                             data->last_start_max += is_inf ? 0 : (maxcount - 1)
1166                                 * (minnext + data->pos_delta);
1167                         }
1168                     }
1169                     /* It is counted once already... */
1170                     data->pos_min += minnext * (mincount - counted);
1171                     data->pos_delta += - counted * deltanext +
1172                         (minnext + deltanext) * maxcount - minnext * mincount;
1173                     if (mincount != maxcount) {
1174                          /* Cannot extend fixed substrings found inside
1175                             the group.  */
1176                         scan_commit(pRExC_state,data);
1177                         if (mincount && last_str) {
1178                             sv_setsv(data->last_found, last_str);
1179                             data->last_end = data->pos_min;
1180                             data->last_start_min =
1181                                 data->pos_min - CHR_SVLEN(last_str);
1182                             data->last_start_max = is_inf
1183                                 ? I32_MAX
1184                                 : data->pos_min + data->pos_delta
1185                                 - CHR_SVLEN(last_str);
1186                         }
1187                         data->longest = &(data->longest_float);
1188                     }
1189                     SvREFCNT_dec(last_str);
1190                 }
1191                 if (data && (fl & SF_HAS_EVAL))
1192                     data->flags |= SF_HAS_EVAL;
1193               optimize_curly_tail:
1194                 if (OP(oscan) != CURLYX) {
1195                     while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
1196                            && NEXT_OFF(next))
1197                         NEXT_OFF(oscan) += NEXT_OFF(next);
1198                 }
1199                 continue;
1200             default:                    /* REF and CLUMP only? */
1201                 if (flags & SCF_DO_SUBSTR) {
1202                     scan_commit(pRExC_state,data);      /* Cannot expect anything... */
1203                     data->longest = &(data->longest_float);
1204                 }
1205                 is_inf = is_inf_internal = 1;
1206                 if (flags & SCF_DO_STCLASS_OR)
1207                     cl_anything(pRExC_state, data->start_class);
1208                 flags &= ~SCF_DO_STCLASS;
1209                 break;
1210             }
1211         }
1212         else if (strchr((char*)PL_simple,OP(scan))) {
1213             int value;
1214
1215             if (flags & SCF_DO_SUBSTR) {
1216                 scan_commit(pRExC_state,data);
1217                 data->pos_min++;
1218             }
1219             min++;
1220             if (flags & SCF_DO_STCLASS) {
1221                 data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */
1222
1223                 /* Some of the logic below assumes that switching
1224                    locale on will only add false positives. */
1225                 switch (PL_regkind[(U8)OP(scan)]) {
1226                 case SANY:
1227                 default:
1228                   do_default:
1229                     /* Perl_croak(aTHX_ "panic: unexpected simple REx opcode %d", OP(scan)); */
1230                     if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
1231                         cl_anything(pRExC_state, data->start_class);
1232                     break;
1233                 case REG_ANY:
1234                     if (OP(scan) == SANY)
1235                         goto do_default;
1236                     if (flags & SCF_DO_STCLASS_OR) { /* Everything but \n */
1237                         value = (ANYOF_BITMAP_TEST(data->start_class,'\n')
1238                                  || (data->start_class->flags & ANYOF_CLASS));
1239                         cl_anything(pRExC_state, data->start_class);
1240                     }
1241                     if (flags & SCF_DO_STCLASS_AND || !value)
1242                         ANYOF_BITMAP_CLEAR(data->start_class,'\n');
1243                     break;
1244                 case ANYOF:
1245                     if (flags & SCF_DO_STCLASS_AND)
1246                         cl_and(data->start_class,
1247                                (struct regnode_charclass_class*)scan);
1248                     else
1249                         cl_or(pRExC_state, data->start_class,
1250                               (struct regnode_charclass_class*)scan);
1251                     break;
1252                 case ALNUM:
1253                     if (flags & SCF_DO_STCLASS_AND) {
1254                         if (!(data->start_class->flags & ANYOF_LOCALE)) {
1255                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
1256                             for (value = 0; value < 256; value++)
1257                                 if (!isALNUM(value))
1258                                     ANYOF_BITMAP_CLEAR(data->start_class, value);
1259                         }
1260                     }
1261                     else {
1262                         if (data->start_class->flags & ANYOF_LOCALE)
1263                             ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
1264                         else {
1265                             for (value = 0; value < 256; value++)
1266                                 if (isALNUM(value))
1267                                     ANYOF_BITMAP_SET(data->start_class, value);                 
1268                         }
1269                     }
1270                     break;
1271                 case ALNUML:
1272                     if (flags & SCF_DO_STCLASS_AND) {
1273                         if (data->start_class->flags & ANYOF_LOCALE)
1274                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
1275                     }
1276                     else {
1277                         ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
1278                         data->start_class->flags |= ANYOF_LOCALE;
1279                     }
1280                     break;
1281                 case NALNUM:
1282                     if (flags & SCF_DO_STCLASS_AND) {
1283                         if (!(data->start_class->flags & ANYOF_LOCALE)) {
1284                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
1285                             for (value = 0; value < 256; value++)
1286                                 if (isALNUM(value))
1287                                     ANYOF_BITMAP_CLEAR(data->start_class, value);
1288                         }
1289                     }
1290                     else {
1291                         if (data->start_class->flags & ANYOF_LOCALE)
1292                             ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
1293                         else {
1294                             for (value = 0; value < 256; value++)
1295                                 if (!isALNUM(value))
1296                                     ANYOF_BITMAP_SET(data->start_class, value);                 
1297                         }
1298                     }
1299                     break;
1300                 case NALNUML:
1301                     if (flags & SCF_DO_STCLASS_AND) {
1302                         if (data->start_class->flags & ANYOF_LOCALE)
1303                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
1304                     }
1305                     else {
1306                         data->start_class->flags |= ANYOF_LOCALE;
1307                         ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
1308                     }
1309                     break;
1310                 case SPACE:
1311                     if (flags & SCF_DO_STCLASS_AND) {
1312                         if (!(data->start_class->flags & ANYOF_LOCALE)) {
1313                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
1314                             for (value = 0; value < 256; value++)
1315                                 if (!isSPACE(value))
1316                                     ANYOF_BITMAP_CLEAR(data->start_class, value);
1317                         }
1318                     }
1319                     else {
1320                         if (data->start_class->flags & ANYOF_LOCALE)
1321                             ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
1322                         else {
1323                             for (value = 0; value < 256; value++)
1324                                 if (isSPACE(value))
1325                                     ANYOF_BITMAP_SET(data->start_class, value);                 
1326                         }
1327                     }
1328                     break;
1329                 case SPACEL:
1330                     if (flags & SCF_DO_STCLASS_AND) {
1331                         if (data->start_class->flags & ANYOF_LOCALE)
1332                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
1333                     }
1334                     else {
1335                         data->start_class->flags |= ANYOF_LOCALE;
1336                         ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
1337                     }
1338                     break;
1339                 case NSPACE:
1340                     if (flags & SCF_DO_STCLASS_AND) {
1341                         if (!(data->start_class->flags & ANYOF_LOCALE)) {
1342                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
1343                             for (value = 0; value < 256; value++)
1344                                 if (isSPACE(value))
1345                                     ANYOF_BITMAP_CLEAR(data->start_class, value);
1346                         }
1347                     }
1348                     else {
1349                         if (data->start_class->flags & ANYOF_LOCALE)
1350                             ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
1351                         else {
1352                             for (value = 0; value < 256; value++)
1353                                 if (!isSPACE(value))
1354                                     ANYOF_BITMAP_SET(data->start_class, value);                 
1355                         }
1356                     }
1357                     break;
1358                 case NSPACEL:
1359                     if (flags & SCF_DO_STCLASS_AND) {
1360                         if (data->start_class->flags & ANYOF_LOCALE) {
1361                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
1362                             for (value = 0; value < 256; value++)
1363                                 if (!isSPACE(value))
1364                                     ANYOF_BITMAP_CLEAR(data->start_class, value);
1365                         }
1366                     }
1367                     else {
1368                         data->start_class->flags |= ANYOF_LOCALE;
1369                         ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
1370                     }
1371                     break;
1372                 case DIGIT:
1373                     if (flags & SCF_DO_STCLASS_AND) {
1374                         ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NDIGIT);
1375                         for (value = 0; value < 256; value++)
1376                             if (!isDIGIT(value))
1377                                 ANYOF_BITMAP_CLEAR(data->start_class, value);
1378                     }
1379                     else {
1380                         if (data->start_class->flags & ANYOF_LOCALE)
1381                             ANYOF_CLASS_SET(data->start_class,ANYOF_DIGIT);
1382                         else {
1383                             for (value = 0; value < 256; value++)
1384                                 if (isDIGIT(value))
1385                                     ANYOF_BITMAP_SET(data->start_class, value);                 
1386                         }
1387                     }
1388                     break;
1389                 case NDIGIT:
1390                     if (flags & SCF_DO_STCLASS_AND) {
1391                         ANYOF_CLASS_CLEAR(data->start_class,ANYOF_DIGIT);
1392                         for (value = 0; value < 256; value++)
1393                             if (isDIGIT(value))
1394                                 ANYOF_BITMAP_CLEAR(data->start_class, value);
1395                     }
1396                     else {
1397                         if (data->start_class->flags & ANYOF_LOCALE)
1398                             ANYOF_CLASS_SET(data->start_class,ANYOF_NDIGIT);
1399                         else {
1400                             for (value = 0; value < 256; value++)
1401                                 if (!isDIGIT(value))
1402                                     ANYOF_BITMAP_SET(data->start_class, value);                 
1403                         }
1404                     }
1405                     break;
1406                 }
1407                 if (flags & SCF_DO_STCLASS_OR)
1408                     cl_and(data->start_class, &and_with);
1409                 flags &= ~SCF_DO_STCLASS;
1410             }
1411         }
1412         else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
1413             data->flags |= (OP(scan) == MEOL
1414                             ? SF_BEFORE_MEOL
1415                             : SF_BEFORE_SEOL);
1416         }
1417         else if (  PL_regkind[(U8)OP(scan)] == BRANCHJ
1418                  /* Lookbehind, or need to calculate parens/evals/stclass: */
1419                    && (scan->flags || data || (flags & SCF_DO_STCLASS))
1420                    && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
1421             /* Lookahead/lookbehind */
1422             I32 deltanext, minnext, fake = 0;
1423             regnode *nscan;
1424             struct regnode_charclass_class intrnl;
1425             int f = 0;
1426
1427             data_fake.flags = 0;
1428             if (data) {         
1429                 data_fake.whilem_c = data->whilem_c;
1430                 data_fake.last_closep = data->last_closep;
1431             }
1432             else
1433                 data_fake.last_closep = &fake;
1434             if ( flags & SCF_DO_STCLASS && !scan->flags
1435                  && OP(scan) == IFMATCH ) { /* Lookahead */
1436                 cl_init(pRExC_state, &intrnl);
1437                 data_fake.start_class = &intrnl;
1438                 f |= SCF_DO_STCLASS_AND;
1439             }
1440             if (flags & SCF_WHILEM_VISITED_POS)
1441                 f |= SCF_WHILEM_VISITED_POS;
1442             next = regnext(scan);
1443             nscan = NEXTOPER(NEXTOPER(scan));
1444             minnext = study_chunk(pRExC_state, &nscan, &deltanext, last, &data_fake, f);
1445             if (scan->flags) {
1446                 if (deltanext) {
1447                     vFAIL("Variable length lookbehind not implemented");
1448                 }
1449                 else if (minnext > U8_MAX) {
1450                     vFAIL2("Lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
1451                 }
1452                 scan->flags = minnext;
1453             }
1454             if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
1455                 pars++;
1456             if (data && (data_fake.flags & SF_HAS_EVAL))
1457                 data->flags |= SF_HAS_EVAL;
1458             if (data)
1459                 data->whilem_c = data_fake.whilem_c;
1460             if (f & SCF_DO_STCLASS_AND) {
1461                 int was = (data->start_class->flags & ANYOF_EOS);
1462
1463                 cl_and(data->start_class, &intrnl);
1464                 if (was)
1465                     data->start_class->flags |= ANYOF_EOS;
1466             }
1467         }
1468         else if (OP(scan) == OPEN) {
1469             pars++;
1470         }
1471         else if (OP(scan) == CLOSE) {
1472             if (ARG(scan) == is_par) {
1473                 next = regnext(scan);
1474
1475                 if ( next && (OP(next) != WHILEM) && next < last)
1476                     is_par = 0;         /* Disable optimization */
1477             }
1478             if (data)
1479                 *(data->last_closep) = ARG(scan);
1480         }
1481         else if (OP(scan) == EVAL) {
1482                 if (data)
1483                     data->flags |= SF_HAS_EVAL;
1484         }
1485         else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded follows */
1486                 if (flags & SCF_DO_SUBSTR) {
1487                     scan_commit(pRExC_state,data);
1488                     data->longest = &(data->longest_float);
1489                 }
1490                 is_inf = is_inf_internal = 1;
1491                 if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
1492                     cl_anything(pRExC_state, data->start_class);
1493                 flags &= ~SCF_DO_STCLASS;
1494         }
1495         /* Else: zero-length, ignore. */
1496         scan = regnext(scan);
1497     }
1498
1499   finish:
1500     *scanp = scan;
1501     *deltap = is_inf_internal ? I32_MAX : delta;
1502     if (flags & SCF_DO_SUBSTR && is_inf)
1503         data->pos_delta = I32_MAX - data->pos_min;
1504     if (is_par > U8_MAX)
1505         is_par = 0;
1506     if (is_par && pars==1 && data) {
1507         data->flags |= SF_IN_PAR;
1508         data->flags &= ~SF_HAS_PAR;
1509     }
1510     else if (pars && data) {
1511         data->flags |= SF_HAS_PAR;
1512         data->flags &= ~SF_IN_PAR;
1513     }
1514     if (flags & SCF_DO_STCLASS_OR)
1515         cl_and(data->start_class, &and_with);
1516     return min;
1517 }
1518
1519 STATIC I32
1520 S_add_data(pTHX_ RExC_state_t *pRExC_state, I32 n, char *s)
1521 {
1522     if (RExC_rx->data) {
1523         Renewc(RExC_rx->data,
1524                sizeof(*RExC_rx->data) + sizeof(void*) * (RExC_rx->data->count + n - 1),
1525                char, struct reg_data);
1526         Renew(RExC_rx->data->what, RExC_rx->data->count + n, U8);
1527         RExC_rx->data->count += n;
1528     }
1529     else {
1530         Newc(1207, RExC_rx->data, sizeof(*RExC_rx->data) + sizeof(void*) * (n - 1),
1531              char, struct reg_data);
1532         New(1208, RExC_rx->data->what, n, U8);
1533         RExC_rx->data->count = n;
1534     }
1535     Copy(s, RExC_rx->data->what + RExC_rx->data->count - n, n, U8);
1536     return RExC_rx->data->count - n;
1537 }
1538
1539 void
1540 Perl_reginitcolors(pTHX)
1541 {
1542     int i = 0;
1543     char *s = PerlEnv_getenv("PERL_RE_COLORS");
1544         
1545     if (s) {
1546         PL_colors[0] = s = savepv(s);
1547         while (++i < 6) {
1548             s = strchr(s, '\t');
1549             if (s) {
1550                 *s = '\0';
1551                 PL_colors[i] = ++s;
1552             }
1553             else
1554                 PL_colors[i] = s = "";
1555         }
1556     } else {
1557         while (i < 6)
1558             PL_colors[i++] = "";
1559     }
1560     PL_colorset = 1;
1561 }
1562
1563
1564 /*
1565  - pregcomp - compile a regular expression into internal code
1566  *
1567  * We can't allocate space until we know how big the compiled form will be,
1568  * but we can't compile it (and thus know how big it is) until we've got a
1569  * place to put the code.  So we cheat:  we compile it twice, once with code
1570  * generation turned off and size counting turned on, and once "for real".
1571  * This also means that we don't allocate space until we are sure that the
1572  * thing really will compile successfully, and we never have to move the
1573  * code and thus invalidate pointers into it.  (Note that it has to be in
1574  * one piece because free() must be able to free it all.) [NB: not true in perl]
1575  *
1576  * Beware that the optimization-preparation code in here knows about some
1577  * of the structure of the compiled regexp.  [I'll say.]
1578  */
1579 regexp *
1580 Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
1581 {
1582     register regexp *r;
1583     regnode *scan;
1584     regnode *first;
1585     I32 flags;
1586     I32 minlen = 0;
1587     I32 sawplus = 0;
1588     I32 sawopen = 0;
1589     scan_data_t data;
1590     RExC_state_t RExC_state;
1591     RExC_state_t *pRExC_state = &RExC_state;
1592
1593     if (exp == NULL)
1594         FAIL("NULL regexp argument");
1595
1596     /* XXXX This looks very suspicious... */
1597     if (pm->op_pmdynflags & PMdf_CMP_UTF8)
1598         RExC_utf8 = 1;
1599     else
1600         RExC_utf8 = 0;
1601
1602     RExC_precomp = savepvn(exp, xend - exp);
1603     DEBUG_r(if (!PL_colorset) reginitcolors());
1604     DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling REx%s `%s%*s%s'\n",
1605                       PL_colors[4],PL_colors[5],PL_colors[0],
1606                       (int)(xend - exp), RExC_precomp, PL_colors[1]));
1607     RExC_flags16 = pm->op_pmflags;
1608     RExC_sawback = 0;
1609
1610     RExC_seen = 0;
1611     RExC_seen_zerolen = *exp == '^' ? -1 : 0;
1612     RExC_seen_evals = 0;
1613     RExC_extralen = 0;
1614
1615     /* First pass: determine size, legality. */
1616     RExC_parse = exp;
1617     RExC_end = xend;
1618     RExC_naughty = 0;
1619     RExC_npar = 1;
1620     RExC_size = 0L;
1621     RExC_emit = &PL_regdummy;
1622     RExC_whilem_seen = 0;
1623 #if 0 /* REGC() is (currently) a NOP at the first pass.
1624        * Clever compilers notice this and complain. --jhi */
1625     REGC((U8)REG_MAGIC, (char*)RExC_emit);
1626 #endif
1627     if (reg(pRExC_state, 0, &flags) == NULL) {
1628         Safefree(RExC_precomp);
1629         RExC_precomp = Nullch;
1630         return(NULL);
1631     }
1632     DEBUG_r(PerlIO_printf(Perl_debug_log, "size %"IVdf" ", (IV)RExC_size));
1633
1634     /* Small enough for pointer-storage convention?
1635        If extralen==0, this means that we will not need long jumps. */
1636     if (RExC_size >= 0x10000L && RExC_extralen)
1637         RExC_size += RExC_extralen;
1638     else
1639         RExC_extralen = 0;
1640     if (RExC_whilem_seen > 15)
1641         RExC_whilem_seen = 15;
1642
1643     /* Allocate space and initialize. */
1644     Newc(1001, r, sizeof(regexp) + (unsigned)RExC_size * sizeof(regnode),
1645          char, regexp);
1646     if (r == NULL)
1647         FAIL("Regexp out of space");
1648
1649 #ifdef DEBUGGING
1650     /* avoid reading uninitialized memory in DEBUGGING code in study_chunk() */
1651     Zero(r, sizeof(regexp) + (unsigned)RExC_size * sizeof(regnode), char);
1652 #endif
1653     r->refcnt = 1;
1654     r->prelen = xend - exp;
1655     r->precomp = RExC_precomp;
1656     r->subbeg = NULL;
1657     r->reganch = pm->op_pmflags & PMf_COMPILETIME;
1658     r->nparens = RExC_npar - 1; /* set early to validate backrefs */
1659
1660     r->substrs = 0;                     /* Useful during FAIL. */
1661     r->startp = 0;                      /* Useful during FAIL. */
1662     r->endp = 0;                        /* Useful during FAIL. */
1663
1664     RExC_rx = r;
1665
1666     /* Second pass: emit code. */
1667     RExC_parse = exp;
1668     RExC_end = xend;
1669     RExC_naughty = 0;
1670     RExC_npar = 1;
1671     RExC_emit = r->program;
1672     /* Store the count of eval-groups for security checks: */
1673     RExC_emit->next_off = ((RExC_seen_evals > U16_MAX) ? U16_MAX : RExC_seen_evals);
1674     REGC((U8)REG_MAGIC, (char*) RExC_emit++);
1675     r->data = 0;
1676     if (reg(pRExC_state, 0, &flags) == NULL)
1677         return(NULL);
1678
1679     /* Dig out information for optimizations. */
1680     r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
1681     pm->op_pmflags = RExC_flags16;
1682     if (UTF)
1683         r->reganch |= ROPT_UTF8;
1684     r->regstclass = NULL;
1685     if (RExC_naughty >= 10)     /* Probably an expensive pattern. */
1686         r->reganch |= ROPT_NAUGHTY;
1687     scan = r->program + 1;              /* First BRANCH. */
1688
1689     /* XXXX To minimize changes to RE engine we always allocate
1690        3-units-long substrs field. */
1691     Newz(1004, r->substrs, 1, struct reg_substr_data);
1692
1693     StructCopy(&zero_scan_data, &data, scan_data_t);
1694     /* XXXX Should not we check for something else?  Usually it is OPEN1... */
1695     if (OP(scan) != BRANCH) {   /* Only one top-level choice. */
1696         I32 fake;
1697         STRLEN longest_float_length, longest_fixed_length;
1698         struct regnode_charclass_class ch_class;
1699         int stclass_flag;
1700         I32 last_close = 0;
1701
1702         first = scan;
1703         /* Skip introductions and multiplicators >= 1. */
1704         while ((OP(first) == OPEN && (sawopen = 1)) ||
1705                /* An OR of *one* alternative - should not happen now. */
1706             (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
1707             (OP(first) == PLUS) ||
1708             (OP(first) == MINMOD) ||
1709                /* An {n,m} with n>0 */
1710             (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
1711                 if (OP(first) == PLUS)
1712                     sawplus = 1;
1713                 else
1714                     first += regarglen[(U8)OP(first)];
1715                 first = NEXTOPER(first);
1716         }
1717
1718         /* Starting-point info. */
1719       again:
1720         if (PL_regkind[(U8)OP(first)] == EXACT) {
1721             if (OP(first) == EXACT)
1722                 ;       /* Empty, get anchored substr later. */
1723             else if ((OP(first) == EXACTF || OP(first) == EXACTFL))
1724                 r->regstclass = first;
1725         }
1726         else if (strchr((char*)PL_simple,OP(first)))
1727             r->regstclass = first;
1728         else if (PL_regkind[(U8)OP(first)] == BOUND ||
1729                  PL_regkind[(U8)OP(first)] == NBOUND)
1730             r->regstclass = first;
1731         else if (PL_regkind[(U8)OP(first)] == BOL) {
1732             r->reganch |= (OP(first) == MBOL
1733                            ? ROPT_ANCH_MBOL
1734                            : (OP(first) == SBOL
1735                               ? ROPT_ANCH_SBOL
1736                               : ROPT_ANCH_BOL));
1737             first = NEXTOPER(first);
1738             goto again;
1739         }
1740         else if (OP(first) == GPOS) {
1741             r->reganch |= ROPT_ANCH_GPOS;
1742             first = NEXTOPER(first);
1743             goto again;
1744         }
1745         else if ((OP(first) == STAR &&
1746             PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
1747             !(r->reganch & ROPT_ANCH) )
1748         {
1749             /* turn .* into ^.* with an implied $*=1 */
1750             int type = OP(NEXTOPER(first));
1751
1752             if (type == REG_ANY)
1753                 type = ROPT_ANCH_MBOL;
1754             else
1755                 type = ROPT_ANCH_SBOL;
1756
1757             r->reganch |= type | ROPT_IMPLICIT;
1758             first = NEXTOPER(first);
1759             goto again;
1760         }
1761         if (sawplus && (!sawopen || !RExC_sawback)
1762             && !(RExC_seen & REG_SEEN_EVAL)) /* May examine pos and $& */
1763             /* x+ must match at the 1st pos of run of x's */
1764             r->reganch |= ROPT_SKIP;
1765
1766         /* Scan is after the zeroth branch, first is atomic matcher. */
1767         DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n",
1768                               (IV)(first - scan + 1)));
1769         /*
1770         * If there's something expensive in the r.e., find the
1771         * longest literal string that must appear and make it the
1772         * regmust.  Resolve ties in favor of later strings, since
1773         * the regstart check works with the beginning of the r.e.
1774         * and avoiding duplication strengthens checking.  Not a
1775         * strong reason, but sufficient in the absence of others.
1776         * [Now we resolve ties in favor of the earlier string if
1777         * it happens that c_offset_min has been invalidated, since the
1778         * earlier string may buy us something the later one won't.]
1779         */
1780         minlen = 0;
1781
1782         data.longest_fixed = newSVpvn("",0);
1783         data.longest_float = newSVpvn("",0);
1784         data.last_found = newSVpvn("",0);
1785         data.longest = &(data.longest_fixed);
1786         first = scan;
1787         if (!r->regstclass) {
1788             cl_init(pRExC_state, &ch_class);
1789             data.start_class = &ch_class;
1790             stclass_flag = SCF_DO_STCLASS_AND;
1791         } else                          /* XXXX Check for BOUND? */
1792             stclass_flag = 0;
1793         data.last_closep = &last_close;
1794
1795         minlen = study_chunk(pRExC_state, &first, &fake, scan + RExC_size, /* Up to end */
1796                              &data, SCF_DO_SUBSTR | SCF_WHILEM_VISITED_POS | stclass_flag);
1797         if ( RExC_npar == 1 && data.longest == &(data.longest_fixed)
1798              && data.last_start_min == 0 && data.last_end > 0
1799              && !RExC_seen_zerolen
1800              && (!(RExC_seen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
1801             r->reganch |= ROPT_CHECK_ALL;
1802         scan_commit(pRExC_state, &data);
1803         SvREFCNT_dec(data.last_found);
1804
1805         longest_float_length = CHR_SVLEN(data.longest_float);
1806         if (longest_float_length
1807             || (data.flags & SF_FL_BEFORE_EOL
1808                 && (!(data.flags & SF_FL_BEFORE_MEOL)
1809                     || (RExC_flags16 & PMf_MULTILINE)))) {
1810             int t;
1811
1812             if (SvCUR(data.longest_fixed)                       /* ok to leave SvCUR */
1813                 && data.offset_fixed == data.offset_float_min
1814                 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
1815                     goto remove_float;          /* As in (a)+. */
1816
1817             r->float_substr = data.longest_float;
1818             r->float_min_offset = data.offset_float_min;
1819             r->float_max_offset = data.offset_float_max;
1820             t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
1821                        && (!(data.flags & SF_FL_BEFORE_MEOL)
1822                            || (RExC_flags16 & PMf_MULTILINE)));
1823             fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
1824         }
1825         else {
1826           remove_float:
1827             r->float_substr = Nullsv;
1828             SvREFCNT_dec(data.longest_float);
1829             longest_float_length = 0;
1830         }
1831
1832         longest_fixed_length = CHR_SVLEN(data.longest_fixed);
1833         if (longest_fixed_length
1834             || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1835                 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1836                     || (RExC_flags16 & PMf_MULTILINE)))) {
1837             int t;
1838
1839             r->anchored_substr = data.longest_fixed;
1840             r->anchored_offset = data.offset_fixed;
1841             t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
1842                  && (!(data.flags & SF_FIX_BEFORE_MEOL)
1843                      || (RExC_flags16 & PMf_MULTILINE)));
1844             fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
1845         }
1846         else {
1847             r->anchored_substr = Nullsv;
1848             SvREFCNT_dec(data.longest_fixed);
1849             longest_fixed_length = 0;
1850         }
1851         if (r->regstclass
1852             && (OP(r->regstclass) == REG_ANY || OP(r->regstclass) == SANY))
1853             r->regstclass = NULL;
1854         if ((!r->anchored_substr || r->anchored_offset) && stclass_flag
1855             && !(data.start_class->flags & ANYOF_EOS)
1856             && !cl_is_anything(data.start_class)) {
1857             SV *sv;
1858             I32 n = add_data(pRExC_state, 1, "f");
1859
1860             New(1006, RExC_rx->data->data[n], 1,
1861                 struct regnode_charclass_class);
1862             StructCopy(data.start_class,
1863                        (struct regnode_charclass_class*)RExC_rx->data->data[n],
1864                        struct regnode_charclass_class);
1865             r->regstclass = (regnode*)RExC_rx->data->data[n];
1866             r->reganch &= ~ROPT_SKIP;   /* Used in find_byclass(). */
1867             PL_regdata = r->data; /* for regprop() */
1868             DEBUG_r((sv = sv_newmortal(),
1869                      regprop(sv, (regnode*)data.start_class),
1870                      PerlIO_printf(Perl_debug_log, "synthetic stclass `%s'.\n",
1871                                    SvPVX(sv))));
1872         }
1873
1874         /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1875         if (longest_fixed_length > longest_float_length) {
1876             r->check_substr = r->anchored_substr;
1877             r->check_offset_min = r->check_offset_max = r->anchored_offset;
1878             if (r->reganch & ROPT_ANCH_SINGLE)
1879                 r->reganch |= ROPT_NOSCAN;
1880         }
1881         else {
1882             r->check_substr = r->float_substr;
1883             r->check_offset_min = data.offset_float_min;
1884             r->check_offset_max = data.offset_float_max;
1885         }
1886         /* XXXX Currently intuiting is not compatible with ANCH_GPOS.
1887            This should be changed ASAP!  */
1888         if (r->check_substr && !(r->reganch & ROPT_ANCH_GPOS)) {
1889             r->reganch |= RE_USE_INTUIT;
1890             if (SvTAIL(r->check_substr))
1891                 r->reganch |= RE_INTUIT_TAIL;
1892         }
1893     }
1894     else {
1895         /* Several toplevels. Best we can is to set minlen. */
1896         I32 fake;
1897         struct regnode_charclass_class ch_class;
1898         I32 last_close = 0;
1899         
1900         DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1901         scan = r->program + 1;
1902         cl_init(pRExC_state, &ch_class);
1903         data.start_class = &ch_class;
1904         data.last_closep = &last_close;
1905         minlen = study_chunk(pRExC_state, &scan, &fake, scan + RExC_size, &data, SCF_DO_STCLASS_AND|SCF_WHILEM_VISITED_POS);
1906         r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1907         if (!(data.start_class->flags & ANYOF_EOS)
1908             && !cl_is_anything(data.start_class)) {
1909             SV *sv;
1910             I32 n = add_data(pRExC_state, 1, "f");
1911
1912             New(1006, RExC_rx->data->data[n], 1,
1913                 struct regnode_charclass_class);
1914             StructCopy(data.start_class,
1915                        (struct regnode_charclass_class*)RExC_rx->data->data[n],
1916                        struct regnode_charclass_class);
1917             r->regstclass = (regnode*)RExC_rx->data->data[n];
1918             r->reganch &= ~ROPT_SKIP;   /* Used in find_byclass(). */
1919             DEBUG_r((sv = sv_newmortal(),
1920                      regprop(sv, (regnode*)data.start_class),
1921                      PerlIO_printf(Perl_debug_log, "synthetic stclass `%s'.\n",
1922                                    SvPVX(sv))));
1923         }
1924     }
1925
1926     r->minlen = minlen;
1927     if (RExC_seen & REG_SEEN_GPOS)
1928         r->reganch |= ROPT_GPOS_SEEN;
1929     if (RExC_seen & REG_SEEN_LOOKBEHIND)
1930         r->reganch |= ROPT_LOOKBEHIND_SEEN;
1931     if (RExC_seen & REG_SEEN_EVAL)
1932         r->reganch |= ROPT_EVAL_SEEN;
1933     Newz(1002, r->startp, RExC_npar, I32);
1934     Newz(1002, r->endp, RExC_npar, I32);
1935     PL_regdata = r->data; /* for regprop() */
1936     DEBUG_r(regdump(r));
1937     return(r);
1938 }
1939
1940 /*
1941  - reg - regular expression, i.e. main body or parenthesized thing
1942  *
1943  * Caller must absorb opening parenthesis.
1944  *
1945  * Combining parenthesis handling with the base level of regular expression
1946  * is a trifle forced, but the need to tie the tails of the branches to what
1947  * follows makes it hard to avoid.
1948  */
1949 STATIC regnode *
1950 S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp)
1951     /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1952 {
1953     register regnode *ret;              /* Will be the head of the group. */
1954     register regnode *br;
1955     register regnode *lastbr;
1956     register regnode *ender = 0;
1957     register I32 parno = 0;
1958     I32 flags, oregflags = RExC_flags16, have_branch = 0, open = 0;
1959     char *oregcomp_parse = RExC_parse;
1960     char c;
1961
1962     *flagp = 0;                         /* Tentatively. */
1963
1964     /* Make an OPEN node, if parenthesized. */
1965     if (paren) {
1966         if (*RExC_parse == '?') {
1967             U16 posflags = 0, negflags = 0;
1968             U16 *flagsp = &posflags;
1969             int logical = 0;
1970             char *seqstart = RExC_parse;
1971
1972             RExC_parse++;
1973             paren = *RExC_parse++;
1974             ret = NULL;                 /* For look-ahead/behind. */
1975             switch (paren) {
1976             case '<':
1977                 RExC_seen |= REG_SEEN_LOOKBEHIND;
1978                 if (*RExC_parse == '!')
1979                     paren = ',';
1980                 if (*RExC_parse != '=' && *RExC_parse != '!')
1981                     goto unknown;
1982                 RExC_parse++;
1983             case '=':
1984             case '!':
1985                 RExC_seen_zerolen++;
1986             case ':':
1987             case '>':
1988                 break;
1989             case '$':
1990             case '@':
1991                 vFAIL2("Sequence (?%c...) not implemented", (int)paren);
1992                 break;
1993             case '#':
1994                 while (*RExC_parse && *RExC_parse != ')')
1995                     RExC_parse++;
1996                 if (*RExC_parse != ')')
1997                     FAIL("Sequence (?#... not terminated");
1998                 nextchar(pRExC_state);
1999                 *flagp = TRYAGAIN;
2000                 return NULL;
2001             case 'p':
2002                 if (SIZE_ONLY)
2003                     vWARN(RExC_parse, "(?p{}) is deprecated - use (??{})");
2004                 /* FALL THROUGH*/
2005             case '?':
2006                 logical = 1;
2007                 paren = *RExC_parse++;
2008                 /* FALL THROUGH */
2009             case '{':
2010             {
2011                 I32 count = 1, n = 0;
2012                 char c;
2013                 char *s = RExC_parse;
2014                 SV *sv;
2015                 OP_4tree *sop, *rop;
2016
2017                 RExC_seen_zerolen++;
2018                 RExC_seen |= REG_SEEN_EVAL;
2019                 while (count && (c = *RExC_parse)) {
2020                     if (c == '\\' && RExC_parse[1])
2021                         RExC_parse++;
2022                     else if (c == '{')
2023                         count++;
2024                     else if (c == '}')
2025                         count--;
2026                     RExC_parse++;
2027                 }
2028                 if (*RExC_parse != ')')
2029                 {
2030                     RExC_parse = s;             
2031                     vFAIL("Sequence (?{...}) not terminated or not {}-balanced");
2032                 }
2033                 if (!SIZE_ONLY) {
2034                     AV *av;
2035                 
2036                     if (RExC_parse - 1 - s)
2037                         sv = newSVpvn(s, RExC_parse - 1 - s);
2038                     else
2039                         sv = newSVpvn("", 0);
2040
2041                     ENTER;
2042                     Perl_save_re_context(aTHX);
2043                     rop = sv_compile_2op(sv, &sop, "re", &av);
2044                     LEAVE;
2045
2046                     n = add_data(pRExC_state, 3, "nop");
2047                     RExC_rx->data->data[n] = (void*)rop;
2048                     RExC_rx->data->data[n+1] = (void*)sop;
2049                     RExC_rx->data->data[n+2] = (void*)av;
2050                     SvREFCNT_dec(sv);
2051                 }
2052                 else {                                          /* First pass */
2053                     if (PL_reginterp_cnt < ++RExC_seen_evals
2054                         && PL_curcop != &PL_compiling)
2055                         /* No compiled RE interpolated, has runtime
2056                            components ===> unsafe.  */
2057                         FAIL("Eval-group not allowed at runtime, use re 'eval'");
2058                     if (PL_tainted)
2059                         FAIL("Eval-group in insecure regular expression");
2060                 }
2061                 
2062                 nextchar(pRExC_state);
2063                 if (logical) {
2064                     ret = reg_node(pRExC_state, LOGICAL);
2065                     if (!SIZE_ONLY)
2066                         ret->flags = 2;
2067                     regtail(pRExC_state, ret, reganode(pRExC_state, EVAL, n));
2068                     return ret;
2069                 }
2070                 return reganode(pRExC_state, EVAL, n);
2071             }
2072             case '(':
2073             {
2074                 if (RExC_parse[0] == '?') {
2075                     if (RExC_parse[1] == '=' || RExC_parse[1] == '!'
2076                         || RExC_parse[1] == '<'
2077                         || RExC_parse[1] == '{') { /* Lookahead or eval. */
2078                         I32 flag;
2079                         
2080                         ret = reg_node(pRExC_state, LOGICAL);
2081                         if (!SIZE_ONLY)
2082                             ret->flags = 1;
2083                         regtail(pRExC_state, ret, reg(pRExC_state, 1, &flag));
2084                         goto insert_if;
2085                     }
2086                 }
2087                 else if (RExC_parse[0] >= '1' && RExC_parse[0] <= '9' ) {
2088                     parno = atoi(RExC_parse++);
2089
2090                     while (isDIGIT(*RExC_parse))
2091                         RExC_parse++;
2092                     ret = reganode(pRExC_state, GROUPP, parno);
2093                     if ((c = *nextchar(pRExC_state)) != ')')
2094                         vFAIL("Switch condition not recognized");
2095                   insert_if:
2096                     regtail(pRExC_state, ret, reganode(pRExC_state, IFTHEN, 0));
2097                     br = regbranch(pRExC_state, &flags, 1);
2098                     if (br == NULL)
2099                         br = reganode(pRExC_state, LONGJMP, 0);
2100                     else
2101                         regtail(pRExC_state, br, reganode(pRExC_state, LONGJMP, 0));
2102                     c = *nextchar(pRExC_state);
2103                     if (flags&HASWIDTH)
2104                         *flagp |= HASWIDTH;
2105                     if (c == '|') {
2106                         lastbr = reganode(pRExC_state, IFTHEN, 0); /* Fake one for optimizer. */
2107                         regbranch(pRExC_state, &flags, 1);
2108                         regtail(pRExC_state, ret, lastbr);
2109                         if (flags&HASWIDTH)
2110                             *flagp |= HASWIDTH;
2111                         c = *nextchar(pRExC_state);
2112                     }
2113                     else
2114                         lastbr = NULL;
2115                     if (c != ')')
2116                         vFAIL("Switch (?(condition)... contains too many branches");
2117                     ender = reg_node(pRExC_state, TAIL);
2118                     regtail(pRExC_state, br, ender);
2119                     if (lastbr) {
2120                         regtail(pRExC_state, lastbr, ender);
2121                         regtail(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender);
2122                     }
2123                     else
2124                         regtail(pRExC_state, ret, ender);
2125                     return ret;
2126                 }
2127                 else {
2128                     vFAIL2("Unknown switch condition (?(%.2s", RExC_parse);
2129                 }
2130             }
2131             case 0:
2132                 RExC_parse--; /* for vFAIL to print correctly */
2133                 vFAIL("Sequence (? incomplete");
2134                 break;
2135             default:
2136                 --RExC_parse;
2137               parse_flags:
2138                 while (*RExC_parse && strchr("iogcmsx", *RExC_parse)) {
2139                     if (*RExC_parse != 'o')
2140                         pmflag(flagsp, *RExC_parse);
2141                     ++RExC_parse;
2142                 }
2143                 if (*RExC_parse == '-') {
2144                     flagsp = &negflags;
2145                     ++RExC_parse;
2146                     goto parse_flags;
2147                 }
2148                 RExC_flags16 |= posflags;
2149                 RExC_flags16 &= ~negflags;
2150                 if (*RExC_parse == ':') {
2151                     RExC_parse++;
2152                     paren = ':';
2153                     break;
2154                 }               
2155               unknown:
2156                 if (*RExC_parse != ')') {
2157                     RExC_parse++;
2158                     vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
2159                 }
2160                 nextchar(pRExC_state);
2161                 *flagp = TRYAGAIN;
2162                 return NULL;
2163             }
2164         }
2165         else {
2166             parno = RExC_npar;
2167             RExC_npar++;
2168             ret = reganode(pRExC_state, OPEN, parno);
2169             open = 1;
2170         }
2171     }
2172     else
2173         ret = NULL;
2174
2175     /* Pick up the branches, linking them together. */
2176     br = regbranch(pRExC_state, &flags, 1);
2177     if (br == NULL)
2178         return(NULL);
2179     if (*RExC_parse == '|') {
2180         if (!SIZE_ONLY && RExC_extralen) {
2181             reginsert(pRExC_state, BRANCHJ, br);
2182         }
2183         else
2184             reginsert(pRExC_state, BRANCH, br);
2185         have_branch = 1;
2186         if (SIZE_ONLY)
2187             RExC_extralen += 1;         /* For BRANCHJ-BRANCH. */
2188     }
2189     else if (paren == ':') {
2190         *flagp |= flags&SIMPLE;
2191     }
2192     if (open) {                         /* Starts with OPEN. */
2193         regtail(pRExC_state, ret, br);          /* OPEN -> first. */
2194     }
2195     else if (paren != '?')              /* Not Conditional */
2196         ret = br;
2197     if (flags&HASWIDTH)
2198         *flagp |= HASWIDTH;
2199     *flagp |= flags&SPSTART;
2200     lastbr = br;
2201     while (*RExC_parse == '|') {
2202         if (!SIZE_ONLY && RExC_extralen) {
2203             ender = reganode(pRExC_state, LONGJMP,0);
2204             regtail(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
2205         }
2206         if (SIZE_ONLY)
2207             RExC_extralen += 2;         /* Account for LONGJMP. */
2208         nextchar(pRExC_state);
2209         br = regbranch(pRExC_state, &flags, 0);
2210         if (br == NULL)
2211             return(NULL);
2212         regtail(pRExC_state, lastbr, br);               /* BRANCH -> BRANCH. */
2213         lastbr = br;
2214         if (flags&HASWIDTH)
2215             *flagp |= HASWIDTH;
2216         *flagp |= flags&SPSTART;
2217     }
2218
2219     if (have_branch || paren != ':') {
2220         /* Make a closing node, and hook it on the end. */
2221         switch (paren) {
2222         case ':':
2223             ender = reg_node(pRExC_state, TAIL);
2224             break;
2225         case 1:
2226             ender = reganode(pRExC_state, CLOSE, parno);
2227             break;
2228         case '<':
2229         case ',':
2230         case '=':
2231         case '!':
2232             *flagp &= ~HASWIDTH;
2233             /* FALL THROUGH */
2234         case '>':
2235             ender = reg_node(pRExC_state, SUCCEED);
2236             break;
2237         case 0:
2238             ender = reg_node(pRExC_state, END);
2239             break;
2240         }
2241         regtail(pRExC_state, lastbr, ender);
2242
2243         if (have_branch) {
2244             /* Hook the tails of the branches to the closing node. */
2245             for (br = ret; br != NULL; br = regnext(br)) {
2246                 regoptail(pRExC_state, br, ender);
2247             }
2248         }
2249     }
2250
2251     {
2252         char *p;
2253         static char parens[] = "=!<,>";
2254
2255         if (paren && (p = strchr(parens, paren))) {
2256             int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
2257             int flag = (p - parens) > 1;
2258
2259             if (paren == '>')
2260                 node = SUSPEND, flag = 0;
2261             reginsert(pRExC_state, node,ret);
2262             ret->flags = flag;
2263             regtail(pRExC_state, ret, reg_node(pRExC_state, TAIL));
2264         }
2265     }
2266
2267     /* Check for proper termination. */
2268     if (paren) {
2269         RExC_flags16 = oregflags;
2270         if (RExC_parse >= RExC_end || *nextchar(pRExC_state) != ')') {
2271             RExC_parse = oregcomp_parse;
2272             vFAIL("Unmatched (");
2273         }
2274     }
2275     else if (!paren && RExC_parse < RExC_end) {
2276         if (*RExC_parse == ')') {
2277             RExC_parse++;
2278             vFAIL("Unmatched )");
2279         }
2280         else
2281             FAIL("Junk on end of regexp");      /* "Can't happen". */
2282         /* NOTREACHED */
2283     }
2284
2285     return(ret);
2286 }
2287
2288 /*
2289  - regbranch - one alternative of an | operator
2290  *
2291  * Implements the concatenation operator.
2292  */
2293 STATIC regnode *
2294 S_regbranch(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, I32 first)
2295 {
2296     register regnode *ret;
2297     register regnode *chain = NULL;
2298     register regnode *latest;
2299     I32 flags = 0, c = 0;
2300
2301     if (first)
2302         ret = NULL;
2303     else {
2304         if (!SIZE_ONLY && RExC_extralen)
2305             ret = reganode(pRExC_state, BRANCHJ,0);
2306         else
2307             ret = reg_node(pRExC_state, BRANCH);
2308     }
2309         
2310     if (!first && SIZE_ONLY)
2311         RExC_extralen += 1;                     /* BRANCHJ */
2312
2313     *flagp = WORST;                     /* Tentatively. */
2314
2315     RExC_parse--;
2316     nextchar(pRExC_state);
2317     while (RExC_parse < RExC_end && *RExC_parse != '|' && *RExC_parse != ')') {
2318         flags &= ~TRYAGAIN;
2319         latest = regpiece(pRExC_state, &flags);
2320         if (latest == NULL) {
2321             if (flags & TRYAGAIN)
2322                 continue;
2323             return(NULL);
2324         }
2325         else if (ret == NULL)
2326             ret = latest;
2327         *flagp |= flags&HASWIDTH;
2328         if (chain == NULL)      /* First piece. */
2329             *flagp |= flags&SPSTART;
2330         else {
2331             RExC_naughty++;
2332             regtail(pRExC_state, chain, latest);
2333         }
2334         chain = latest;
2335         c++;
2336     }
2337     if (chain == NULL) {        /* Loop ran zero times. */
2338         chain = reg_node(pRExC_state, NOTHING);
2339         if (ret == NULL)
2340             ret = chain;
2341     }
2342     if (c == 1) {
2343         *flagp |= flags&SIMPLE;
2344     }
2345
2346     return(ret);
2347 }
2348
2349 /*
2350  - regpiece - something followed by possible [*+?]
2351  *
2352  * Note that the branching code sequences used for ? and the general cases
2353  * of * and + are somewhat optimized:  they use the same NOTHING node as
2354  * both the endmarker for their branch list and the body of the last branch.
2355  * It might seem that this node could be dispensed with entirely, but the
2356  * endmarker role is not redundant.
2357  */
2358 STATIC regnode *
2359 S_regpiece(pTHX_ RExC_state_t *pRExC_state, I32 *flagp)
2360 {
2361     register regnode *ret;
2362     register char op;
2363     register char *next;
2364     I32 flags;
2365     char *origparse = RExC_parse;
2366     char *maxpos;
2367     I32 min;
2368     I32 max = REG_INFTY;
2369
2370     ret = regatom(pRExC_state, &flags);
2371     if (ret == NULL) {
2372         if (flags & TRYAGAIN)
2373             *flagp |= TRYAGAIN;
2374         return(NULL);
2375     }
2376
2377     op = *RExC_parse;
2378
2379     if (op == '{' && regcurly(RExC_parse)) {
2380         next = RExC_parse + 1;
2381         maxpos = Nullch;
2382         while (isDIGIT(*next) || *next == ',') {
2383             if (*next == ',') {
2384                 if (maxpos)
2385                     break;
2386                 else
2387                     maxpos = next;
2388             }
2389             next++;
2390         }
2391         if (*next == '}') {             /* got one */
2392             if (!maxpos)
2393                 maxpos = next;
2394             RExC_parse++;
2395             min = atoi(RExC_parse);
2396             if (*maxpos == ',')
2397                 maxpos++;
2398             else
2399                 maxpos = RExC_parse;
2400             max = atoi(maxpos);
2401             if (!max && *maxpos != '0')
2402                 max = REG_INFTY;                /* meaning "infinity" */
2403             else if (max >= REG_INFTY)
2404                 vFAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
2405             RExC_parse = next;
2406             nextchar(pRExC_state);
2407
2408         do_curly:
2409             if ((flags&SIMPLE)) {
2410                 RExC_naughty += 2 + RExC_naughty / 2;
2411                 reginsert(pRExC_state, CURLY, ret);
2412             }
2413             else {
2414                 regnode *w = reg_node(pRExC_state, WHILEM);
2415
2416                 w->flags = 0;
2417                 regtail(pRExC_state, ret, w);
2418                 if (!SIZE_ONLY && RExC_extralen) {
2419                     reginsert(pRExC_state, LONGJMP,ret);
2420                     reginsert(pRExC_state, NOTHING,ret);
2421                     NEXT_OFF(ret) = 3;  /* Go over LONGJMP. */
2422                 }
2423                 reginsert(pRExC_state, CURLYX,ret);
2424                 if (!SIZE_ONLY && RExC_extralen)
2425                     NEXT_OFF(ret) = 3;  /* Go over NOTHING to LONGJMP. */
2426                 regtail(pRExC_state, ret, reg_node(pRExC_state, NOTHING));
2427                 if (SIZE_ONLY)
2428                     RExC_whilem_seen++, RExC_extralen += 3;
2429                 RExC_naughty += 4 + RExC_naughty;       /* compound interest */
2430             }
2431             ret->flags = 0;
2432
2433             if (min > 0)
2434                 *flagp = WORST;
2435             if (max > 0)
2436                 *flagp |= HASWIDTH;
2437             if (max && max < min)
2438                 vFAIL("Can't do {n,m} with n > m");
2439             if (!SIZE_ONLY) {
2440                 ARG1_SET(ret, min);
2441                 ARG2_SET(ret, max);
2442             }
2443
2444             goto nest_check;
2445         }
2446     }
2447
2448     if (!ISMULT1(op)) {
2449         *flagp = flags;
2450         return(ret);
2451     }
2452
2453 #if 0                           /* Now runtime fix should be reliable. */
2454
2455     /* if this is reinstated, don't forget to put this back into perldiag:
2456
2457             =item Regexp *+ operand could be empty at {#} in regex m/%s/
2458
2459            (F) The part of the regexp subject to either the * or + quantifier
2460            could match an empty string. The {#} shows in the regular
2461            expression about where the problem was discovered.
2462
2463     */
2464
2465     if (!(flags&HASWIDTH) && op != '?')
2466       vFAIL("Regexp *+ operand could be empty");
2467 #endif
2468
2469     nextchar(pRExC_state);
2470
2471     *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
2472
2473     if (op == '*' && (flags&SIMPLE)) {
2474         reginsert(pRExC_state, STAR, ret);
2475         ret->flags = 0;
2476         RExC_naughty += 4;
2477     }
2478     else if (op == '*') {
2479         min = 0;
2480         goto do_curly;
2481     }
2482     else if (op == '+' && (flags&SIMPLE)) {
2483         reginsert(pRExC_state, PLUS, ret);
2484         ret->flags = 0;
2485         RExC_naughty += 3;
2486     }
2487     else if (op == '+') {
2488         min = 1;
2489         goto do_curly;
2490     }
2491     else if (op == '?') {
2492         min = 0; max = 1;
2493         goto do_curly;
2494     }
2495   nest_check:
2496     if (ckWARN(WARN_REGEXP) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
2497         vWARN3(RExC_parse,
2498                "%.*s matches null string many times",
2499                RExC_parse - origparse,
2500                origparse);
2501     }
2502
2503     if (*RExC_parse == '?') {
2504         nextchar(pRExC_state);
2505         reginsert(pRExC_state, MINMOD, ret);
2506         regtail(pRExC_state, ret, ret + NODE_STEP_REGNODE);
2507     }
2508     if (ISMULT2(RExC_parse)) {
2509         RExC_parse++;
2510         vFAIL("Nested quantifiers");
2511     }
2512
2513     return(ret);
2514 }
2515
2516 /*
2517  - regatom - the lowest level
2518  *
2519  * Optimization:  gobbles an entire sequence of ordinary characters so that
2520  * it can turn them into a single node, which is smaller to store and
2521  * faster to run.  Backslashed characters are exceptions, each becoming a
2522  * separate node; the code is simpler that way and it's not worth fixing.
2523  *
2524  * [Yes, it is worth fixing, some scripts can run twice the speed.] */
2525 STATIC regnode *
2526 S_regatom(pTHX_ RExC_state_t *pRExC_state, I32 *flagp)
2527 {
2528     register regnode *ret = 0;
2529     I32 flags;
2530
2531     *flagp = WORST;             /* Tentatively. */
2532
2533 tryagain:
2534     switch (*RExC_parse) {
2535     case '^':
2536         RExC_seen_zerolen++;
2537         nextchar(pRExC_state);
2538         if (RExC_flags16 & PMf_MULTILINE)
2539             ret = reg_node(pRExC_state, MBOL);
2540         else if (RExC_flags16 & PMf_SINGLELINE)
2541             ret = reg_node(pRExC_state, SBOL);
2542         else
2543             ret = reg_node(pRExC_state, BOL);
2544         break;
2545     case '$':
2546         nextchar(pRExC_state);
2547         if (*RExC_parse)
2548             RExC_seen_zerolen++;
2549         if (RExC_flags16 & PMf_MULTILINE)
2550             ret = reg_node(pRExC_state, MEOL);
2551         else if (RExC_flags16 & PMf_SINGLELINE)
2552             ret = reg_node(pRExC_state, SEOL);
2553         else
2554             ret = reg_node(pRExC_state, EOL);
2555         break;
2556     case '.':
2557         nextchar(pRExC_state);
2558         if (RExC_flags16 & PMf_SINGLELINE)
2559             ret = reg_node(pRExC_state, SANY);
2560         else
2561             ret = reg_node(pRExC_state, REG_ANY);
2562         *flagp |= HASWIDTH|SIMPLE;
2563         RExC_naughty++;
2564         break;
2565     case '[':
2566     {
2567         char *oregcomp_parse = ++RExC_parse;
2568         ret = regclass(pRExC_state);
2569         if (*RExC_parse != ']') {
2570             RExC_parse = oregcomp_parse;
2571             vFAIL("Unmatched [");
2572         }
2573         nextchar(pRExC_state);
2574         *flagp |= HASWIDTH|SIMPLE;
2575         break;
2576     }
2577     case '(':
2578         nextchar(pRExC_state);
2579         ret = reg(pRExC_state, 1, &flags);
2580         if (ret == NULL) {
2581                 if (flags & TRYAGAIN) {
2582                     if (RExC_parse == RExC_end) {
2583                          /* Make parent create an empty node if needed. */
2584                         *flagp |= TRYAGAIN;
2585                         return(NULL);
2586                     }
2587                     goto tryagain;
2588                 }
2589                 return(NULL);
2590         }
2591         *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
2592         break;
2593     case '|':
2594     case ')':
2595         if (flags & TRYAGAIN) {
2596             *flagp |= TRYAGAIN;
2597             return NULL;
2598         }
2599         vFAIL("Internal urp");
2600                                 /* Supposed to be caught earlier. */
2601         break;
2602     case '{':
2603         if (!regcurly(RExC_parse)) {
2604             RExC_parse++;
2605             goto defchar;
2606         }
2607         /* FALL THROUGH */
2608     case '?':
2609     case '+':
2610     case '*':
2611         RExC_parse++;
2612         vFAIL("Quantifier follows nothing");
2613         break;
2614     case '\\':
2615         switch (*++RExC_parse) {
2616         case 'A':
2617             RExC_seen_zerolen++;
2618             ret = reg_node(pRExC_state, SBOL);
2619             *flagp |= SIMPLE;
2620             nextchar(pRExC_state);
2621             break;
2622         case 'G':
2623             ret = reg_node(pRExC_state, GPOS);
2624             RExC_seen |= REG_SEEN_GPOS;
2625             *flagp |= SIMPLE;
2626             nextchar(pRExC_state);
2627             break;
2628         case 'Z':
2629             ret = reg_node(pRExC_state, SEOL);
2630             *flagp |= SIMPLE;
2631             nextchar(pRExC_state);
2632             break;
2633         case 'z':
2634             ret = reg_node(pRExC_state, EOS);
2635             *flagp |= SIMPLE;
2636             RExC_seen_zerolen++;                /* Do not optimize RE away */
2637             nextchar(pRExC_state);
2638             break;
2639         case 'C':
2640             ret = reg_node(pRExC_state, SANY);
2641             *flagp |= HASWIDTH|SIMPLE;
2642             nextchar(pRExC_state);
2643             break;
2644         case 'X':
2645             ret = reg_node(pRExC_state, CLUMP);
2646             *flagp |= HASWIDTH;
2647             nextchar(pRExC_state);
2648             if (UTF && !PL_utf8_mark)
2649                 is_utf8_mark((U8*)"~");         /* preload table */
2650             break;
2651         case 'w':
2652             ret = reg_node(pRExC_state, LOC ? ALNUML     : ALNUM);
2653             *flagp |= HASWIDTH|SIMPLE;
2654             nextchar(pRExC_state);
2655             if (UTF && !PL_utf8_alnum)
2656                 is_utf8_alnum((U8*)"a");        /* preload table */
2657             break;
2658         case 'W':
2659             ret = reg_node(pRExC_state, LOC ? NALNUML     : NALNUM);
2660             *flagp |= HASWIDTH|SIMPLE;
2661             nextchar(pRExC_state);
2662             if (UTF && !PL_utf8_alnum)
2663                 is_utf8_alnum((U8*)"a");        /* preload table */
2664             break;
2665         case 'b':
2666             RExC_seen_zerolen++;
2667             RExC_seen |= REG_SEEN_LOOKBEHIND;
2668             ret = reg_node(pRExC_state, LOC ? BOUNDL     : BOUND);
2669             *flagp |= SIMPLE;
2670             nextchar(pRExC_state);
2671             if (UTF && !PL_utf8_alnum)
2672                 is_utf8_alnum((U8*)"a");        /* preload table */
2673             break;
2674         case 'B':
2675             RExC_seen_zerolen++;
2676             RExC_seen |= REG_SEEN_LOOKBEHIND;
2677             ret = reg_node(pRExC_state, LOC ? NBOUNDL     : NBOUND);
2678             *flagp |= SIMPLE;
2679             nextchar(pRExC_state);
2680             if (UTF && !PL_utf8_alnum)
2681                 is_utf8_alnum((U8*)"a");        /* preload table */
2682             break;
2683         case 's':
2684             ret = reg_node(pRExC_state, LOC ? SPACEL     : SPACE);
2685             *flagp |= HASWIDTH|SIMPLE;
2686             nextchar(pRExC_state);
2687             if (UTF && !PL_utf8_space)
2688                 is_utf8_space((U8*)" ");        /* preload table */
2689             break;
2690         case 'S':
2691             ret = reg_node(pRExC_state, LOC ? NSPACEL     : NSPACE);
2692             *flagp |= HASWIDTH|SIMPLE;
2693             nextchar(pRExC_state);
2694             if (UTF && !PL_utf8_space)
2695                 is_utf8_space((U8*)" ");        /* preload table */
2696             break;
2697         case 'd':
2698             ret = reg_node(pRExC_state, DIGIT);
2699             *flagp |= HASWIDTH|SIMPLE;
2700             nextchar(pRExC_state);
2701             if (UTF && !PL_utf8_digit)
2702                 is_utf8_digit((U8*)"1");        /* preload table */
2703             break;
2704         case 'D':
2705             ret = reg_node(pRExC_state, NDIGIT);
2706             *flagp |= HASWIDTH|SIMPLE;
2707             nextchar(pRExC_state);
2708             if (UTF && !PL_utf8_digit)
2709                 is_utf8_digit((U8*)"1");        /* preload table */
2710             break;
2711         case 'p':
2712         case 'P':
2713             {   /* a lovely hack--pretend we saw [\pX] instead */
2714                 char* oldregxend = RExC_end;
2715
2716                 if (RExC_parse[1] == '{') {
2717                     RExC_end = strchr(RExC_parse, '}');
2718                     if (!RExC_end) {
2719                         RExC_parse += 2;
2720                         RExC_end = oldregxend;
2721                         vFAIL("Missing right brace on \\p{}");
2722                     }
2723                     RExC_end++;
2724                 }
2725                 else
2726                     RExC_end = RExC_parse + 2;
2727                 RExC_parse--;
2728
2729                 ret = regclass(pRExC_state);
2730
2731                 RExC_end = oldregxend;
2732                 RExC_parse--;
2733                 nextchar(pRExC_state);
2734                 *flagp |= HASWIDTH|SIMPLE;
2735             }
2736             break;
2737         case 'n':
2738         case 'r':
2739         case 't':
2740         case 'f':
2741         case 'e':
2742         case 'a':
2743         case 'x':
2744         case 'c':
2745         case '0':
2746             goto defchar;
2747         case '1': case '2': case '3': case '4':
2748         case '5': case '6': case '7': case '8': case '9':
2749             {
2750                 I32 num = atoi(RExC_parse);
2751
2752                 if (num > 9 && num >= RExC_npar)
2753                     goto defchar;
2754                 else {
2755                     while (isDIGIT(*RExC_parse))
2756                         RExC_parse++;
2757
2758                     if (!SIZE_ONLY && num > RExC_rx->nparens)
2759                         vFAIL("Reference to nonexistent group");
2760                     RExC_sawback = 1;
2761                     ret = reganode(pRExC_state, FOLD
2762                                    ? (LOC ? REFFL : REFF)
2763                                    : REF, num);
2764                     *flagp |= HASWIDTH;
2765                     RExC_parse--;
2766                     nextchar(pRExC_state);
2767                 }
2768             }
2769             break;
2770         case '\0':
2771             if (RExC_parse >= RExC_end)
2772                 FAIL("Trailing \\");
2773             /* FALL THROUGH */
2774         default:
2775             /* Do not generate `unrecognized' warnings here, we fall
2776                back into the quick-grab loop below */
2777             goto defchar;
2778         }
2779         break;
2780
2781     case '#':
2782         if (RExC_flags16 & PMf_EXTENDED) {
2783             while (RExC_parse < RExC_end && *RExC_parse != '\n') RExC_parse++;
2784             if (RExC_parse < RExC_end)
2785                 goto tryagain;
2786         }
2787         /* FALL THROUGH */
2788
2789     default: {
2790             register STRLEN len;
2791             register UV ender;
2792             register char *p;
2793             char *oldp, *s;
2794             STRLEN numlen;
2795
2796             RExC_parse++;
2797
2798         defchar:
2799             ret = reg_node(pRExC_state, FOLD
2800                           ? (LOC ? EXACTFL : EXACTF)
2801                           : EXACT);
2802             s = STRING(ret);
2803             for (len = 0, p = RExC_parse - 1;
2804               len < 127 && p < RExC_end;
2805               len++)
2806             {
2807                 oldp = p;
2808
2809                 if (RExC_flags16 & PMf_EXTENDED)
2810                     p = regwhite(p, RExC_end);
2811                 switch (*p) {
2812                 case '^':
2813                 case '$':
2814                 case '.':
2815                 case '[':
2816                 case '(':
2817                 case ')':
2818                 case '|':
2819                     goto loopdone;
2820                 case '\\':
2821                     switch (*++p) {
2822                     case 'A':
2823                     case 'G':
2824                     case 'Z':
2825                     case 'z':
2826                     case 'w':
2827                     case 'W':
2828                     case 'b':
2829                     case 'B':
2830                     case 's':
2831                     case 'S':
2832                     case 'd':
2833                     case 'D':
2834                     case 'p':
2835                     case 'P':
2836                         --p;
2837                         goto loopdone;
2838                     case 'n':
2839                         ender = '\n';
2840                         p++;
2841                         break;
2842                     case 'r':
2843                         ender = '\r';
2844                         p++;
2845                         break;
2846                     case 't':
2847                         ender = '\t';
2848                         p++;
2849                         break;
2850                     case 'f':
2851                         ender = '\f';
2852                         p++;
2853                         break;
2854                     case 'e':
2855 #ifdef ASCIIish
2856                           ender = '\033';
2857 #else
2858                           ender = '\047';
2859 #endif
2860                         p++;
2861                         break;
2862                     case 'a':
2863 #ifdef ASCIIish
2864                           ender = '\007';
2865 #else
2866                           ender = '\057';
2867 #endif
2868                         p++;
2869                         break;
2870                     case 'x':
2871                         if (*++p == '{') {
2872                             char* e = strchr(p, '}');
2873         
2874                             if (!e) {
2875                                 RExC_parse = p + 1;
2876                                 vFAIL("Missing right brace on \\x{}");
2877                             }
2878                             else {
2879                                 numlen = 1;     /* allow underscores */
2880                                 ender = (UV)scan_hex(p + 1, e - p - 1, &numlen);
2881                                 /* numlen is generous */
2882                                 if (numlen + len >= 127) {
2883                                     p--;
2884                                     goto loopdone;
2885                                 }
2886                                 p = e + 1;
2887                             }
2888                         }
2889                         else {
2890                             numlen = 0;         /* disallow underscores */
2891                             ender = (UV)scan_hex(p, 2, &numlen);
2892                             p += numlen;
2893                         }
2894                         break;
2895                     case 'c':
2896                         p++;
2897                         ender = UCHARAT(p++);
2898                         ender = toCTRL(ender);
2899                         break;
2900                     case '0': case '1': case '2': case '3':case '4':
2901                     case '5': case '6': case '7': case '8':case '9':
2902                         if (*p == '0' ||
2903                           (isDIGIT(p[1]) && atoi(p) >= RExC_npar) ) {
2904                             numlen = 0;         /* disallow underscores */
2905                             ender = (UV)scan_oct(p, 3, &numlen);
2906                             p += numlen;
2907                         }
2908                         else {
2909                             --p;
2910                             goto loopdone;
2911                         }
2912                         break;
2913                     case '\0':
2914                         if (p >= RExC_end)
2915                             FAIL("Trailing \\");
2916                         /* FALL THROUGH */
2917                     default:
2918                         if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(*p))
2919                             vWARN2(p +1, "Unrecognized escape \\%c passed through", *p);
2920                         goto normal_default;
2921                     }
2922                     break;
2923                 default:
2924                   normal_default:
2925                     if (UTF8_IS_START(*p) && UTF) {
2926                         ender = utf8_to_uv((U8*)p, RExC_end - p,
2927                                                &numlen, 0);
2928                         p += numlen;
2929                     }
2930                     else
2931                         ender = *p++;
2932                     break;
2933                 }
2934                 if (RExC_flags16 & PMf_EXTENDED)
2935                     p = regwhite(p, RExC_end);
2936                 if (UTF && FOLD) {
2937                     if (LOC)
2938                         ender = toLOWER_LC_uni(ender);
2939                     else
2940                         ender = toLOWER_uni(ender);
2941                 }
2942                 if (ISMULT2(p)) { /* Back off on ?+*. */
2943                     if (len)
2944                         p = oldp;
2945                     /* ender is a Unicode value so it can be > 0xff --
2946                      * in other words, do not use UTF8_IS_CONTINUED(). */
2947                     else if (ender >= 0x80 && UTF) {
2948                         reguni(pRExC_state, ender, s, &numlen);
2949                         s += numlen;
2950                         len += numlen;
2951                     }
2952                     else {
2953                         len++;
2954                         REGC(ender, s++);
2955                     }
2956                     break;
2957                 }
2958                 /* ender is a Unicode value so it can be > 0xff --
2959                  * in other words, do not use UTF8_IS_CONTINUED(). */
2960                 if (ender >= 0x80 && UTF) {
2961                     reguni(pRExC_state, ender, s, &numlen);
2962                     s += numlen;
2963                     len += numlen - 1;
2964                 }
2965                 else
2966                     REGC(ender, s++);
2967             }
2968         loopdone:
2969             RExC_parse = p - 1;
2970             nextchar(pRExC_state);
2971             {
2972                 /* len is STRLEN which is unsigned, need to copy to signed */
2973                 IV iv = len;
2974                 if (iv < 0)
2975                     vFAIL("Internal disaster");
2976             }
2977             if (len > 0)
2978                 *flagp |= HASWIDTH;
2979             if (len == 1)
2980                 *flagp |= SIMPLE;
2981             if (!SIZE_ONLY)
2982                 STR_LEN(ret) = len;
2983             if (SIZE_ONLY)
2984                 RExC_size += STR_SZ(len);
2985             else
2986                 RExC_emit += STR_SZ(len);
2987         }
2988         break;
2989     }
2990
2991     return(ret);
2992 }
2993
2994 STATIC char *
2995 S_regwhite(pTHX_ char *p, char *e)
2996 {
2997     while (p < e) {
2998         if (isSPACE(*p))
2999             ++p;
3000         else if (*p == '#') {
3001             do {
3002                 p++;
3003             } while (p < e && *p != '\n');
3004         }
3005         else
3006             break;
3007     }
3008     return p;
3009 }
3010
3011 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
3012    Character classes ([:foo:]) can also be negated ([:^foo:]).
3013    Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
3014    Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
3015    but trigger warnings because they are currently unimplemented. */
3016 STATIC I32
3017 S_regpposixcc(pTHX_ RExC_state_t *pRExC_state, I32 value)
3018 {
3019     char *posixcc = 0;
3020     I32 namedclass = OOB_NAMEDCLASS;
3021
3022     if (value == '[' && RExC_parse + 1 < RExC_end &&
3023         /* I smell either [: or [= or [. -- POSIX has been here, right? */
3024         (*RExC_parse == ':' ||
3025          *RExC_parse == '=' ||
3026          *RExC_parse == '.')) {
3027         char  c = *RExC_parse;
3028         char* s = RExC_parse++;
3029         
3030         while (RExC_parse < RExC_end && *RExC_parse != c)
3031             RExC_parse++;
3032         if (RExC_parse == RExC_end)
3033             /* Grandfather lone [:, [=, [. */
3034             RExC_parse = s;
3035         else {
3036             char* t = RExC_parse++; /* skip over the c */
3037
3038             if (*RExC_parse == ']') {
3039                 RExC_parse++; /* skip over the ending ] */
3040                 posixcc = s + 1;
3041                 if (*s == ':') {
3042                     I32 complement = *posixcc == '^' ? *posixcc++ : 0;
3043                     I32 skip = 5; /* the most common skip */
3044
3045                     switch (*posixcc) {
3046                     case 'a':
3047                         if (strnEQ(posixcc, "alnum", 5))
3048                             namedclass =
3049                                 complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
3050                         else if (strnEQ(posixcc, "alpha", 5))
3051                             namedclass =
3052                                 complement ? ANYOF_NALPHA : ANYOF_ALPHA;
3053                         else if (strnEQ(posixcc, "ascii", 5))
3054                             namedclass =
3055                                 complement ? ANYOF_NASCII : ANYOF_ASCII;
3056                         break;
3057                     case 'b':
3058                         if (strnEQ(posixcc, "blank", 5))
3059                             namedclass =
3060                                 complement ? ANYOF_NBLANK : ANYOF_BLANK;
3061                         break;
3062                     case 'c':
3063                         if (strnEQ(posixcc, "cntrl", 5))
3064                             namedclass =
3065                                 complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
3066                         break;
3067                     case 'd':
3068                         if (strnEQ(posixcc, "digit", 5))
3069                             namedclass =
3070                                 complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
3071                         break;
3072                     case 'g':
3073                         if (strnEQ(posixcc, "graph", 5))
3074                             namedclass =
3075                                 complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
3076                         break;
3077                     case 'l':
3078                         if (strnEQ(posixcc, "lower", 5))
3079                             namedclass =
3080                                 complement ? ANYOF_NLOWER : ANYOF_LOWER;
3081                         break;
3082                     case 'p':
3083                         if (strnEQ(posixcc, "print", 5))
3084                             namedclass =
3085                                 complement ? ANYOF_NPRINT : ANYOF_PRINT;
3086                         else if (strnEQ(posixcc, "punct", 5))
3087                             namedclass =
3088                                 complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
3089                         break;
3090                     case 's':
3091                         if (strnEQ(posixcc, "space", 5))
3092                             namedclass =
3093                                 complement ? ANYOF_NPSXSPC : ANYOF_PSXSPC;
3094                         break;
3095                     case 'u':
3096                         if (strnEQ(posixcc, "upper", 5))
3097                             namedclass =
3098                                 complement ? ANYOF_NUPPER : ANYOF_UPPER;
3099                         break;
3100                     case 'w': /* this is not POSIX, this is the Perl \w */
3101                         if (strnEQ(posixcc, "word", 4)) {
3102                             namedclass =
3103                                 complement ? ANYOF_NALNUM : ANYOF_ALNUM;
3104                             skip = 4;
3105                         }
3106                         break;
3107                     case 'x':
3108                         if (strnEQ(posixcc, "xdigit", 6)) {
3109                             namedclass =
3110                                 complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
3111                             skip = 6;
3112                         }
3113                         break;
3114                     }
3115                     if (namedclass == OOB_NAMEDCLASS ||
3116                         posixcc[skip] != ':' ||
3117                         posixcc[skip+1] != ']')
3118                     {
3119                         Simple_vFAIL3("POSIX class [:%.*s:] unknown",
3120                                       t - s - 1, s + 1);
3121                     }
3122                 } else if (!SIZE_ONLY) {
3123                     /* [[=foo=]] and [[.foo.]] are still future. */
3124
3125                     /* adjust RExC_parse so the warning shows after
3126                        the class closes */
3127                     while (*RExC_parse && *RExC_parse != ']')
3128                         RExC_parse++;
3129                     Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
3130                 }
3131             } else {
3132                 /* Maternal grandfather:
3133                  * "[:" ending in ":" but not in ":]" */
3134                 RExC_parse = s;
3135             }
3136         }
3137     }
3138
3139     return namedclass;
3140 }
3141
3142 STATIC void
3143 S_checkposixcc(pTHX_ RExC_state_t *pRExC_state)
3144 {
3145     if (!SIZE_ONLY && ckWARN(WARN_REGEXP) &&
3146         (*RExC_parse == ':' ||
3147          *RExC_parse == '=' ||
3148          *RExC_parse == '.')) {
3149         char *s = RExC_parse;
3150         char  c = *s++;
3151
3152         while(*s && isALNUM(*s))
3153             s++;
3154         if (*s && c == *s && s[1] == ']') {
3155             vWARN3(s+2, "POSIX syntax [%c %c] belongs inside character classes", c, c);
3156
3157             /* [[=foo=]] and [[.foo.]] are still future. */
3158             if (c == '=' || c == '.')
3159             {
3160                 /* adjust RExC_parse so the error shows after
3161                    the class closes */
3162                 while (*RExC_parse && *RExC_parse++ != ']')
3163                     ;
3164                 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
3165             }
3166         }
3167     }
3168 }
3169
3170 STATIC regnode *
3171 S_regclass(pTHX_ RExC_state_t *pRExC_state)
3172 {
3173     register UV value;
3174     register IV lastvalue = OOB_UNICODE;
3175     register IV range = 0;
3176     register regnode *ret;
3177     STRLEN numlen;
3178     IV namedclass;
3179     char *rangebegin;
3180     bool need_class = 0;
3181     SV *listsv;
3182     register char *e;
3183     UV n;
3184     bool dont_optimize_invert = FALSE;
3185
3186     ret = reganode(pRExC_state, ANYOF, 0);
3187
3188     if (!SIZE_ONLY)
3189         ANYOF_FLAGS(ret) = 0;
3190
3191     if (*RExC_parse == '^') {   /* Complement of range. */
3192         RExC_naughty++;
3193         RExC_parse++;
3194         if (!SIZE_ONLY)
3195             ANYOF_FLAGS(ret) |= ANYOF_INVERT;
3196     }
3197
3198     if (SIZE_ONLY)
3199         RExC_size += ANYOF_SKIP;
3200     else {
3201         RExC_emit += ANYOF_SKIP;
3202         if (FOLD)
3203             ANYOF_FLAGS(ret) |= ANYOF_FOLD;
3204         if (LOC)
3205             ANYOF_FLAGS(ret) |= ANYOF_LOCALE;
3206         ANYOF_BITMAP_ZERO(ret);
3207         listsv = newSVpvn("# comment\n", 10);
3208     }
3209
3210     if (!SIZE_ONLY && ckWARN(WARN_REGEXP))
3211         checkposixcc(pRExC_state);
3212
3213     if (*RExC_parse == ']' || *RExC_parse == '-')
3214         goto charclassloop;             /* allow 1st char to be ] or - */
3215
3216     while (RExC_parse < RExC_end && *RExC_parse != ']') {
3217
3218     charclassloop:
3219
3220         namedclass = OOB_NAMEDCLASS; /* initialize as illegal */
3221
3222         if (!range)
3223             rangebegin = RExC_parse;
3224         if (UTF) {
3225             value = utf8_to_uv((U8*)RExC_parse,
3226                                RExC_end - RExC_parse,
3227                                &numlen, 0);
3228             RExC_parse += numlen;
3229         }
3230         else
3231             value = UCHARAT(RExC_parse++);
3232         if (value == '[')
3233             namedclass = regpposixcc(pRExC_state, value);
3234         else if (value == '\\') {
3235             if (UTF) {
3236                 value = utf8_to_uv((U8*)RExC_parse,
3237                                    RExC_end - RExC_parse,
3238                                    &numlen, 0);
3239                 RExC_parse += numlen;
3240             }
3241             else
3242                 value = UCHARAT(RExC_parse++);
3243             /* Some compilers cannot handle switching on 64-bit integer
3244              * values, therefore value cannot be an UV.  Yes, this will
3245              * be a problem later if we want switch on Unicode.
3246              * A similar issue a little bit later when switching on
3247              * namedclass. --jhi */
3248             switch ((I32)value) {
3249             case 'w':   namedclass = ANYOF_ALNUM;       break;
3250             case 'W':   namedclass = ANYOF_NALNUM;      break;
3251             case 's':   namedclass = ANYOF_SPACE;       break;
3252             case 'S':   namedclass = ANYOF_NSPACE;      break;
3253             case 'd':   namedclass = ANYOF_DIGIT;       break;
3254             case 'D':   namedclass = ANYOF_NDIGIT;      break;
3255             case 'p':
3256             case 'P':
3257                 if (*RExC_parse == '{') {
3258                     e = strchr(RExC_parse++, '}');
3259                     if (!e)
3260                         vFAIL("Missing right brace on \\p{}");
3261                     n = e - RExC_parse;
3262                 }
3263                 else {
3264                     e = RExC_parse;
3265                     n = 1;
3266                 }
3267                 if (!SIZE_ONLY) {
3268                     if (value == 'p')
3269                         Perl_sv_catpvf(aTHX_ listsv,
3270                                        "+utf8::%.*s\n", (int)n, RExC_parse);
3271                     else
3272                         Perl_sv_catpvf(aTHX_ listsv,
3273                                        "!utf8::%.*s\n", (int)n, RExC_parse);
3274                 }
3275                 RExC_parse = e + 1;
3276                 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
3277                 continue;
3278             case 'n':   value = '\n';                   break;
3279             case 'r':   value = '\r';                   break;
3280             case 't':   value = '\t';                   break;
3281             case 'f':   value = '\f';                   break;
3282             case 'b':   value = '\b';                   break;
3283 #ifdef ASCIIish
3284             case 'e':   value = '\033';                 break;
3285             case 'a':   value = '\007';                 break;
3286 #else
3287             case 'e':   value = '\047';                 break;
3288             case 'a':   value = '\057';                 break;
3289 #endif
3290             case 'x':
3291                 if (*RExC_parse == '{') {
3292                     e = strchr(RExC_parse++, '}');
3293                     if (!e)
3294                         vFAIL("Missing right brace on \\x{}");
3295                     numlen = 1;         /* allow underscores */
3296                     value = (UV)scan_hex(RExC_parse,
3297                                          e - RExC_parse,
3298                                          &numlen);
3299                     RExC_parse = e + 1;
3300                 }
3301                 else {
3302                     numlen = 0;         /* disallow underscores */
3303                     value = (UV)scan_hex(RExC_parse, 2, &numlen);
3304                     RExC_parse += numlen;
3305                 }
3306                 break;
3307             case 'c':
3308                 value = UCHARAT(RExC_parse++);
3309                 value = toCTRL(value);
3310                 break;
3311             case '0': case '1': case '2': case '3': case '4':
3312             case '5': case '6': case '7': case '8': case '9':
3313                 numlen = 0;             /* disallow underscores */
3314                 value = (UV)scan_oct(--RExC_parse, 3, &numlen);
3315                 RExC_parse += numlen;
3316                 break;
3317             default:
3318                 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(value))
3319                     vWARN2(RExC_parse,
3320                            "Unrecognized escape \\%c in character class passed through",
3321                            (int)value);
3322                 break;
3323             }
3324         } /* end of \blah */
3325
3326         if (namedclass > OOB_NAMEDCLASS) { /* this is a named class \blah */
3327
3328             if (!SIZE_ONLY && !need_class)
3329                 ANYOF_CLASS_ZERO(ret);
3330
3331             need_class = 1;
3332
3333             /* a bad range like a-\d, a-[:digit:] ? */
3334             if (range) {
3335                 if (!SIZE_ONLY) {
3336                     if (ckWARN(WARN_REGEXP))
3337                         vWARN4(RExC_parse,
3338                                "False [] range \"%*.*s\"",
3339                                RExC_parse - rangebegin,
3340                                RExC_parse - rangebegin,
3341                                rangebegin);
3342                     if (lastvalue < 256) {
3343                         ANYOF_BITMAP_SET(ret, lastvalue);
3344                         ANYOF_BITMAP_SET(ret, '-');
3345                     }
3346                     else {
3347                         ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
3348                         Perl_sv_catpvf(aTHX_ listsv,
3349                                        /* 0x002D is Unicode for '-' */
3350                                        "%04"UVxf"\n002D\n", (UV)lastvalue);
3351                     }
3352                 }
3353
3354                 range = 0; /* this was not a true range */
3355             }
3356
3357             if (!SIZE_ONLY) {
3358                 /* Possible truncation here but in some 64-bit environments
3359                  * the compiler gets heartburn about switch on 64-bit values.
3360                  * A similar issue a little earlier when switching on value.
3361                  * --jhi */
3362                 switch ((I32)namedclass) {
3363                 case ANYOF_ALNUM:
3364                     if (LOC)
3365                         ANYOF_CLASS_SET(ret, ANYOF_ALNUM);
3366                     else {
3367                         for (value = 0; value < 256; value++)
3368                             if (isALNUM(value))
3369                                 ANYOF_BITMAP_SET(ret, value);
3370                     }
3371                     dont_optimize_invert = TRUE;
3372                     Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n");    
3373                     break;
3374                 case ANYOF_NALNUM:
3375                     if (LOC)
3376                         ANYOF_CLASS_SET(ret, ANYOF_NALNUM);
3377                     else {
3378                         for (value = 0; value < 256; value++)
3379                             if (!isALNUM(value))
3380                                 ANYOF_BITMAP_SET(ret, value);
3381                     }
3382                     dont_optimize_invert = TRUE;
3383                     Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n");
3384                     break;
3385                 case ANYOF_ALNUMC:
3386                     if (LOC)
3387                         ANYOF_CLASS_SET(ret, ANYOF_ALNUMC);
3388                     else {
3389                         for (value = 0; value < 256; value++)
3390                             if (isALNUMC(value))
3391                                 ANYOF_BITMAP_SET(ret, value);
3392                     }
3393                     dont_optimize_invert = TRUE;
3394                     Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n");
3395                     break;
3396                 case ANYOF_NALNUMC:
3397                     if (LOC)
3398                         ANYOF_CLASS_SET(ret, ANYOF_NALNUMC);
3399                     else {
3400                         for (value = 0; value < 256; value++)
3401                             if (!isALNUMC(value))
3402                                 ANYOF_BITMAP_SET(ret, value);
3403                     }
3404                     dont_optimize_invert = TRUE;
3405                     Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n");
3406                     break;
3407                 case ANYOF_ALPHA:
3408                     if (LOC)
3409                         ANYOF_CLASS_SET(ret, ANYOF_ALPHA);
3410                     else {
3411                         for (value = 0; value < 256; value++)
3412                             if (isALPHA(value))
3413                                 ANYOF_BITMAP_SET(ret, value);
3414                     }
3415                     dont_optimize_invert = TRUE;
3416                     Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n");
3417                     break;
3418                 case ANYOF_NALPHA:
3419                     if (LOC)
3420                         ANYOF_CLASS_SET(ret, ANYOF_NALPHA);
3421                     else {
3422                         for (value = 0; value < 256; value++)
3423                             if (!isALPHA(value))
3424                                 ANYOF_BITMAP_SET(ret, value);
3425                     }
3426                     dont_optimize_invert = TRUE;
3427                     Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n");
3428                     break;
3429                 case ANYOF_ASCII:
3430                     if (LOC)
3431                         ANYOF_CLASS_SET(ret, ANYOF_ASCII);
3432                     else {
3433 #ifdef ASCIIish
3434                         for (value = 0; value < 128; value++)
3435                             ANYOF_BITMAP_SET(ret, value);
3436 #else  /* EBCDIC */
3437                         for (value = 0; value < 256; value++)
3438                             if (isASCII(value))
3439                                 ANYOF_BITMAP_SET(ret, value);
3440 #endif /* EBCDIC */
3441                     }
3442                     dont_optimize_invert = TRUE;
3443                     Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n");
3444                     break;
3445                 case ANYOF_NASCII:
3446                     if (LOC)
3447                         ANYOF_CLASS_SET(ret, ANYOF_NASCII);
3448                     else {
3449 #ifdef ASCIIish
3450                         for (value = 128; value < 256; value++)
3451                             ANYOF_BITMAP_SET(ret, value);
3452 #else  /* EBCDIC */
3453                         for (value = 0; value < 256; value++)
3454                             if (!isASCII(value))
3455                                 ANYOF_BITMAP_SET(ret, value);
3456 #endif /* EBCDIC */
3457                     }
3458                     dont_optimize_invert = TRUE;
3459                     Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n");
3460                     break;
3461                 case ANYOF_BLANK:
3462                     if (LOC)
3463                         ANYOF_CLASS_SET(ret, ANYOF_BLANK);
3464                     else {
3465                         for (value = 0; value < 256; value++)
3466                             if (isBLANK(value))
3467                                 ANYOF_BITMAP_SET(ret, value);
3468                     }
3469                     dont_optimize_invert = TRUE;
3470                     Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsBlank\n");
3471                     break;
3472                 case ANYOF_NBLANK:
3473                     if (LOC)
3474                         ANYOF_CLASS_SET(ret, ANYOF_NBLANK);
3475                     else {
3476                         for (value = 0; value < 256; value++)
3477                             if (!isBLANK(value))
3478                                 ANYOF_BITMAP_SET(ret, value);
3479                     }
3480                     dont_optimize_invert = TRUE;
3481                     Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsBlank\n");
3482                     break;
3483                 case ANYOF_CNTRL:
3484                     if (LOC)
3485                         ANYOF_CLASS_SET(ret, ANYOF_CNTRL);
3486                     else {
3487                         for (value = 0; value < 256; value++)
3488                             if (isCNTRL(value))
3489                                 ANYOF_BITMAP_SET(ret, value);
3490                     }
3491                     dont_optimize_invert = TRUE;
3492                     Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n");
3493                     break;
3494                 case ANYOF_NCNTRL:
3495                     if (LOC)
3496                         ANYOF_CLASS_SET(ret, ANYOF_NCNTRL);
3497                     else {
3498                         for (value = 0; value < 256; value++)
3499                             if (!isCNTRL(value))
3500                                 ANYOF_BITMAP_SET(ret, value);
3501                     }
3502                     dont_optimize_invert = TRUE;
3503                     Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n");
3504                     break;
3505                 case ANYOF_DIGIT:
3506                     if (LOC)
3507                         ANYOF_CLASS_SET(ret, ANYOF_DIGIT);
3508                     else {
3509                         /* consecutive digits assumed */
3510                         for (value = '0'; value <= '9'; value++)
3511                             ANYOF_BITMAP_SET(ret, value);
3512                     }
3513                     dont_optimize_invert = TRUE;
3514                     Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n");
3515                     break;
3516                 case ANYOF_NDIGIT:
3517                     if (LOC)
3518                         ANYOF_CLASS_SET(ret, ANYOF_NDIGIT);
3519                     else {
3520                         /* consecutive digits assumed */
3521                         for (value = 0; value < '0'; value++)
3522                             ANYOF_BITMAP_SET(ret, value);
3523                         for (value = '9' + 1; value < 256; value++)
3524                             ANYOF_BITMAP_SET(ret, value);
3525                     }
3526                     dont_optimize_invert = TRUE;
3527                     Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n");
3528                     break;
3529                 case ANYOF_GRAPH:
3530                     if (LOC)
3531                         ANYOF_CLASS_SET(ret, ANYOF_GRAPH);
3532                     else {
3533                         for (value = 0; value < 256; value++)
3534                             if (isGRAPH(value))
3535                                 ANYOF_BITMAP_SET(ret, value);
3536                     }
3537                     dont_optimize_invert = TRUE;
3538                     Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n");
3539                     break;
3540                 case ANYOF_NGRAPH:
3541                     if (LOC)
3542                         ANYOF_CLASS_SET(ret, ANYOF_NGRAPH);
3543                     else {
3544                         for (value = 0; value < 256; value++)
3545                             if (!isGRAPH(value))
3546                                 ANYOF_BITMAP_SET(ret, value);
3547                     }
3548                     dont_optimize_invert = TRUE;
3549                     Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n");
3550                     break;
3551                 case ANYOF_LOWER:
3552                     if (LOC)
3553                         ANYOF_CLASS_SET(ret, ANYOF_LOWER);
3554                     else {
3555                         for (value = 0; value < 256; value++)
3556                             if (isLOWER(value))
3557                                 ANYOF_BITMAP_SET(ret, value);
3558                     }
3559                     dont_optimize_invert = TRUE;
3560                     Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n");
3561                     break;
3562                 case ANYOF_NLOWER:
3563                     if (LOC)
3564                         ANYOF_CLASS_SET(ret, ANYOF_NLOWER);
3565                     else {
3566                         for (value = 0; value < 256; value++)
3567                             if (!isLOWER(value))
3568                                 ANYOF_BITMAP_SET(ret, value);
3569                     }
3570                     dont_optimize_invert = TRUE;
3571                     Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n");
3572                     break;
3573                 case ANYOF_PRINT:
3574                     if (LOC)
3575                         ANYOF_CLASS_SET(ret, ANYOF_PRINT);
3576                     else {
3577                         for (value = 0; value < 256; value++)
3578                             if (isPRINT(value))
3579                                 ANYOF_BITMAP_SET(ret, value);
3580                     }
3581                     dont_optimize_invert = TRUE;
3582                     Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n");
3583                     break;
3584                 case ANYOF_NPRINT:
3585                     if (LOC)
3586                         ANYOF_CLASS_SET(ret, ANYOF_NPRINT);
3587                     else {
3588                         for (value = 0; value < 256; value++)
3589                             if (!isPRINT(value))
3590                                 ANYOF_BITMAP_SET(ret, value);
3591                     }
3592                     dont_optimize_invert = TRUE;
3593                     Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n");
3594                     break;
3595                 case ANYOF_PSXSPC:
3596                     if (LOC)
3597                         ANYOF_CLASS_SET(ret, ANYOF_PSXSPC);
3598                     else {
3599                         for (value = 0; value < 256; value++)
3600                             if (isPSXSPC(value))
3601                                 ANYOF_BITMAP_SET(ret, value);
3602                     }
3603                     dont_optimize_invert = TRUE;
3604                     Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n");
3605                     break;
3606                 case ANYOF_NPSXSPC:
3607                     if (LOC)
3608                         ANYOF_CLASS_SET(ret, ANYOF_NPSXSPC);
3609                     else {
3610                         for (value = 0; value < 256; value++)
3611                             if (!isPSXSPC(value))
3612                                 ANYOF_BITMAP_SET(ret, value);
3613                     }
3614                     dont_optimize_invert = TRUE;
3615                     Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n");
3616                     break;
3617                 case ANYOF_PUNCT:
3618                     if (LOC)
3619                         ANYOF_CLASS_SET(ret, ANYOF_PUNCT);
3620                     else {
3621                         for (value = 0; value < 256; value++)
3622                             if (isPUNCT(value))
3623                                 ANYOF_BITMAP_SET(ret, value);
3624                     }
3625                     dont_optimize_invert = TRUE;
3626                     Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n");
3627                     break;
3628                 case ANYOF_NPUNCT:
3629                     if (LOC)
3630                         ANYOF_CLASS_SET(ret, ANYOF_NPUNCT);
3631                     else {
3632                         for (value = 0; value < 256; value++)
3633                             if (!isPUNCT(value))
3634                                 ANYOF_BITMAP_SET(ret, value);
3635                     }
3636                     dont_optimize_invert = TRUE;
3637                     Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n");
3638                     break;
3639                 case ANYOF_SPACE:
3640                     if (LOC)
3641                         ANYOF_CLASS_SET(ret, ANYOF_SPACE);
3642                     else {
3643                         for (value = 0; value < 256; value++)
3644                             if (isSPACE(value))
3645                                 ANYOF_BITMAP_SET(ret, value);
3646                     }
3647                     dont_optimize_invert = TRUE;
3648                     Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpacePerl\n");
3649                     break;
3650                 case ANYOF_NSPACE:
3651                     if (LOC)
3652                         ANYOF_CLASS_SET(ret, ANYOF_NSPACE);
3653                     else {
3654                         for (value = 0; value < 256; value++)
3655                             if (!isSPACE(value))
3656                                 ANYOF_BITMAP_SET(ret, value);
3657                     }
3658                     dont_optimize_invert = TRUE;
3659                     Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpacePerl\n");
3660                     break;
3661                 case ANYOF_UPPER:
3662                     if (LOC)
3663                         ANYOF_CLASS_SET(ret, ANYOF_UPPER);
3664                     else {
3665                         for (value = 0; value < 256; value++)
3666                             if (isUPPER(value))
3667                                 ANYOF_BITMAP_SET(ret, value);
3668                     }
3669                     dont_optimize_invert = TRUE;
3670                     Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n");
3671                     break;
3672                 case ANYOF_NUPPER:
3673                     if (LOC)
3674                         ANYOF_CLASS_SET(ret, ANYOF_NUPPER);
3675                     else {
3676                         for (value = 0; value < 256; value++)
3677                             if (!isUPPER(value))
3678                                 ANYOF_BITMAP_SET(ret, value);
3679                     }
3680                     dont_optimize_invert = TRUE;
3681                     Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n");
3682                     break;
3683                 case ANYOF_XDIGIT:
3684                     if (LOC)
3685                         ANYOF_CLASS_SET(ret, ANYOF_XDIGIT);
3686                     else {
3687                         for (value = 0; value < 256; value++)
3688                             if (isXDIGIT(value))
3689                                 ANYOF_BITMAP_SET(ret, value);
3690                     }
3691                     dont_optimize_invert = TRUE;
3692                     Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n");
3693                     break;
3694                 case ANYOF_NXDIGIT:
3695                     if (LOC)
3696                         ANYOF_CLASS_SET(ret, ANYOF_NXDIGIT);
3697                     else {
3698                         for (value = 0; value < 256; value++)
3699                             if (!isXDIGIT(value))
3700                                 ANYOF_BITMAP_SET(ret, value);
3701                     }
3702                     dont_optimize_invert = TRUE;
3703                     Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n");
3704                     break;
3705                 default:
3706                     vFAIL("Invalid [::] class");
3707                     break;
3708                 }
3709                 if (LOC)
3710                     ANYOF_FLAGS(ret) |= ANYOF_CLASS;
3711                 continue;
3712             }
3713         } /* end of namedclass \blah */
3714
3715         if (range) {
3716             if (lastvalue > value) /* b-a */ {
3717                 Simple_vFAIL4("Invalid [] range \"%*.*s\"",
3718                               RExC_parse - rangebegin,
3719                               RExC_parse - rangebegin,
3720                               rangebegin);
3721             }
3722             range = 0; /* not a true range */
3723         }
3724         else {
3725             lastvalue = value; /* save the beginning of the range */
3726             if (*RExC_parse == '-' && RExC_parse+1 < RExC_end &&
3727                 RExC_parse[1] != ']') {
3728                 RExC_parse++;
3729
3730                 /* a bad range like \w-, [:word:]- ? */
3731                 if (namedclass > OOB_NAMEDCLASS) {
3732                     if (ckWARN(WARN_REGEXP))
3733                         vWARN4(RExC_parse,
3734                                "False [] range \"%*.*s\"",
3735                                RExC_parse - rangebegin,
3736                                RExC_parse - rangebegin,
3737                                rangebegin);
3738                     if (!SIZE_ONLY)
3739                         ANYOF_BITMAP_SET(ret, '-');
3740                 } else
3741                     range = 1;  /* yeah, it's a range! */
3742                 continue;       /* but do it the next time */
3743             }
3744         }
3745
3746         /* now is the next time */
3747         if (!SIZE_ONLY) {
3748             if (lastvalue < 256 && value < 256) {
3749 #ifndef ASCIIish /* EBCDIC, for example. */
3750                 if ((isLOWER(lastvalue) && isLOWER(value)) ||
3751                     (isUPPER(lastvalue) && isUPPER(value)))
3752                 {
3753                     IV i;
3754                     if (isLOWER(lastvalue)) {
3755                         for (i = lastvalue; i <= value; i++)
3756                             if (isLOWER(i))
3757                                 ANYOF_BITMAP_SET(ret, i);
3758                     } else {
3759                         for (i = lastvalue; i <= value; i++)
3760                             if (isUPPER(i))
3761                                 ANYOF_BITMAP_SET(ret, i);
3762                     }
3763                 }
3764                 else
3765 #endif
3766                     for ( ; lastvalue <= value; lastvalue++)
3767                         ANYOF_BITMAP_SET(ret, lastvalue);
3768             } else {
3769                 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
3770                 if (lastvalue < value)
3771                     Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\t%04"UVxf"\n",
3772                                    (UV)lastvalue, (UV)value);
3773                 else
3774                     Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n",
3775                                    (UV)value);
3776             }
3777         }
3778
3779         range = 0; /* this range (if it was one) is done now */
3780     }
3781
3782     if (need_class) {
3783         if (SIZE_ONLY)
3784             RExC_size += ANYOF_CLASS_ADD_SKIP;
3785         else
3786             RExC_emit += ANYOF_CLASS_ADD_SKIP;
3787     }
3788
3789     /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
3790     if (!SIZE_ONLY &&
3791         (ANYOF_FLAGS(ret) &
3792          /* If the only flag is folding (plus possibly inversion). */
3793          (ANYOF_FLAGS_ALL ^ ANYOF_INVERT) == ANYOF_FOLD)) {
3794         for (value = 0; value < 256; ++value) {
3795             if (ANYOF_BITMAP_TEST(ret, value)) {
3796                 IV fold = PL_fold[value];
3797
3798                 if (fold != value)
3799                     ANYOF_BITMAP_SET(ret, fold);
3800             }
3801         }
3802         ANYOF_FLAGS(ret) &= ~ANYOF_FOLD;
3803     }
3804
3805     /* optimize inverted simple patterns (e.g. [^a-z]) */
3806     if (!SIZE_ONLY && !dont_optimize_invert &&
3807         /* If the only flag is inversion. */
3808         (ANYOF_FLAGS(ret) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
3809         for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
3810             ANYOF_BITMAP(ret)[value] ^= ANYOF_FLAGS_ALL;
3811         ANYOF_FLAGS(ret) = ANYOF_UNICODE_ALL;
3812     }
3813
3814     if (!SIZE_ONLY) {
3815         AV *av = newAV();
3816         SV *rv;
3817
3818         av_store(av, 0, listsv);
3819         av_store(av, 1, NULL);
3820         rv = newRV_noinc((SV*)av);
3821         n = add_data(pRExC_state, 1, "s");
3822         RExC_rx->data->data[n] = (void*)rv;
3823         ARG_SET(ret, n);
3824     }
3825
3826     return ret;
3827 }
3828
3829 STATIC char*
3830 S_nextchar(pTHX_ RExC_state_t *pRExC_state)
3831 {
3832     char* retval = RExC_parse++;
3833
3834     for (;;) {
3835         if (*RExC_parse == '(' && RExC_parse[1] == '?' &&
3836                 RExC_parse[2] == '#') {
3837             while (*RExC_parse && *RExC_parse != ')')
3838                 RExC_parse++;
3839             RExC_parse++;
3840             continue;
3841         }
3842         if (RExC_flags16 & PMf_EXTENDED) {
3843             if (isSPACE(*RExC_parse)) {
3844                 RExC_parse++;
3845                 continue;
3846             }
3847             else if (*RExC_parse == '#') {
3848                 while (*RExC_parse && *RExC_parse != '\n')
3849                     RExC_parse++;
3850                 RExC_parse++;
3851                 continue;
3852             }
3853         }
3854         return retval;
3855     }
3856 }
3857
3858 /*
3859 - reg_node - emit a node
3860 */
3861 STATIC regnode *                        /* Location. */
3862 S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op)
3863 {
3864     register regnode *ret;
3865     register regnode *ptr;
3866
3867     ret = RExC_emit;
3868     if (SIZE_ONLY) {
3869         SIZE_ALIGN(RExC_size);
3870         RExC_size += 1;
3871         return(ret);
3872     }
3873
3874     NODE_ALIGN_FILL(ret);
3875     ptr = ret;
3876     FILL_ADVANCE_NODE(ptr, op);
3877     RExC_emit = ptr;
3878
3879     return(ret);
3880 }
3881
3882 /*
3883 - reganode - emit a node with an argument
3884 */
3885 STATIC regnode *                        /* Location. */
3886 S_reganode(pTHX_ RExC_state_t *pRExC_state, U8 op, U32 arg)
3887 {
3888     register regnode *ret;
3889     register regnode *ptr;
3890
3891     ret = RExC_emit;
3892     if (SIZE_ONLY) {
3893         SIZE_ALIGN(RExC_size);
3894         RExC_size += 2;
3895         return(ret);
3896     }
3897
3898     NODE_ALIGN_FILL(ret);
3899     ptr = ret;
3900     FILL_ADVANCE_NODE_ARG(ptr, op, arg);
3901     RExC_emit = ptr;
3902
3903     return(ret);
3904 }
3905
3906 /*
3907 - reguni - emit (if appropriate) a Unicode character
3908 */
3909 STATIC void
3910 S_reguni(pTHX_ RExC_state_t *pRExC_state, UV uv, char* s, STRLEN* lenp)
3911 {
3912     *lenp = SIZE_ONLY ? UNISKIP(uv) : (uv_to_utf8((U8*)s, uv) - (U8*)s);
3913 }
3914
3915 /*
3916 - reginsert - insert an operator in front of already-emitted operand
3917 *
3918 * Means relocating the operand.
3919 */
3920 STATIC void
3921 S_reginsert(pTHX_ RExC_state_t *pRExC_state, U8 op, regnode *opnd)
3922 {
3923     register regnode *src;
3924     register regnode *dst;
3925     register regnode *place;
3926     register int offset = regarglen[(U8)op];
3927
3928 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
3929
3930     if (SIZE_ONLY) {
3931         RExC_size += NODE_STEP_REGNODE + offset;
3932         return;
3933     }
3934
3935     src = RExC_emit;
3936     RExC_emit += NODE_STEP_REGNODE + offset;
3937     dst = RExC_emit;
3938     while (src > opnd)
3939         StructCopy(--src, --dst, regnode);
3940
3941     place = opnd;               /* Op node, where operand used to be. */
3942     src = NEXTOPER(place);
3943     FILL_ADVANCE_NODE(place, op);
3944     Zero(src, offset, regnode);
3945 }
3946
3947 /*
3948 - regtail - set the next-pointer at the end of a node chain of p to val.
3949 */
3950 STATIC void
3951 S_regtail(pTHX_ RExC_state_t *pRExC_state, regnode *p, regnode *val)
3952 {
3953     register regnode *scan;
3954     register regnode *temp;
3955
3956     if (SIZE_ONLY)
3957         return;
3958
3959     /* Find last node. */
3960     scan = p;
3961     for (;;) {
3962         temp = regnext(scan);
3963         if (temp == NULL)
3964             break;
3965         scan = temp;
3966     }
3967
3968     if (reg_off_by_arg[OP(scan)]) {
3969         ARG_SET(scan, val - scan);
3970     }
3971     else {
3972         NEXT_OFF(scan) = val - scan;
3973     }
3974 }
3975
3976 /*
3977 - regoptail - regtail on operand of first argument; nop if operandless
3978 */
3979 STATIC void
3980 S_regoptail(pTHX_ RExC_state_t *pRExC_state, regnode *p, regnode *val)
3981 {
3982     /* "Operandless" and "op != BRANCH" are synonymous in practice. */
3983     if (p == NULL || SIZE_ONLY)
3984         return;
3985     if (PL_regkind[(U8)OP(p)] == BRANCH) {
3986         regtail(pRExC_state, NEXTOPER(p), val);
3987     }
3988     else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
3989         regtail(pRExC_state, NEXTOPER(NEXTOPER(p)), val);
3990     }
3991     else
3992         return;
3993 }
3994
3995 /*
3996  - regcurly - a little FSA that accepts {\d+,?\d*}
3997  */
3998 STATIC I32
3999 S_regcurly(pTHX_ register char *s)
4000 {
4001     if (*s++ != '{')
4002         return FALSE;
4003     if (!isDIGIT(*s))
4004         return FALSE;
4005     while (isDIGIT(*s))
4006         s++;
4007     if (*s == ',')
4008         s++;
4009     while (isDIGIT(*s))
4010         s++;
4011     if (*s != '}')
4012         return FALSE;
4013     return TRUE;
4014 }
4015
4016
4017 STATIC regnode *
4018 S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
4019 {
4020 #ifdef DEBUGGING
4021     register U8 op = EXACT;     /* Arbitrary non-END op. */
4022     register regnode *next;
4023
4024     while (op != END && (!last || node < last)) {
4025         /* While that wasn't END last time... */
4026
4027         NODE_ALIGN(node);
4028         op = OP(node);
4029         if (op == CLOSE)
4030             l--;        
4031         next = regnext(node);
4032         /* Where, what. */
4033         if (OP(node) == OPTIMIZED)
4034             goto after_print;
4035         regprop(sv, node);
4036         PerlIO_printf(Perl_debug_log, "%4"IVdf":%*s%s", (IV)(node - start),
4037                       (int)(2*l + 1), "", SvPVX(sv));
4038         if (next == NULL)               /* Next ptr. */
4039             PerlIO_printf(Perl_debug_log, "(0)");
4040         else
4041             PerlIO_printf(Perl_debug_log, "(%"IVdf")", (IV)(next - start));
4042         (void)PerlIO_putc(Perl_debug_log, '\n');
4043       after_print:
4044         if (PL_regkind[(U8)op] == BRANCHJ) {
4045             register regnode *nnode = (OP(next) == LONGJMP
4046                                        ? regnext(next)
4047                                        : next);
4048             if (last && nnode > last)
4049                 nnode = last;
4050             node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
4051         }
4052         else if (PL_regkind[(U8)op] == BRANCH) {
4053             node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
4054         }
4055         else if ( op == CURLY) {   /* `next' might be very big: optimizer */
4056             node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
4057                              NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
4058         }
4059         else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
4060             node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
4061                              next, sv, l + 1);
4062         }
4063         else if ( op == PLUS || op == STAR) {
4064             node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
4065         }
4066         else if (op == ANYOF) {
4067             node = NEXTOPER(node);
4068             node += ANYOF_SKIP;
4069         }
4070         else if (PL_regkind[(U8)op] == EXACT) {
4071             /* Literal string, where present. */
4072             node += NODE_SZ_STR(node) - 1;
4073             node = NEXTOPER(node);
4074         }
4075         else {
4076             node = NEXTOPER(node);
4077             node += regarglen[(U8)op];
4078         }
4079         if (op == CURLYX || op == OPEN)
4080             l++;
4081         else if (op == WHILEM)
4082             l--;
4083     }
4084 #endif  /* DEBUGGING */
4085     return node;
4086 }
4087
4088 /*
4089  - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
4090  */
4091 void
4092 Perl_regdump(pTHX_ regexp *r)
4093 {
4094 #ifdef DEBUGGING
4095     SV *sv = sv_newmortal();
4096
4097     (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
4098
4099     /* Header fields of interest. */
4100     if (r->anchored_substr)
4101         PerlIO_printf(Perl_debug_log,
4102                       "anchored `%s%.*s%s'%s at %"IVdf" ",
4103                       PL_colors[0],
4104                       (int)(SvCUR(r->anchored_substr) - (SvTAIL(r->anchored_substr)!=0)),
4105                       SvPVX(r->anchored_substr),
4106                       PL_colors[1],
4107                       SvTAIL(r->anchored_substr) ? "$" : "",
4108                       (IV)r->anchored_offset);
4109     if (r->float_substr)
4110         PerlIO_printf(Perl_debug_log,
4111                       "floating `%s%.*s%s'%s at %"IVdf"..%"UVuf" ",
4112                       PL_colors[0],
4113                       (int)(SvCUR(r->float_substr) - (SvTAIL(r->float_substr)!=0)),
4114                       SvPVX(r->float_substr),
4115                       PL_colors[1],
4116                       SvTAIL(r->float_substr) ? "$" : "",
4117                       (IV)r->float_min_offset, (UV)r->float_max_offset);
4118     if (r->check_substr)
4119         PerlIO_printf(Perl_debug_log,
4120                       r->check_substr == r->float_substr
4121                       ? "(checking floating" : "(checking anchored");
4122     if (r->reganch & ROPT_NOSCAN)
4123         PerlIO_printf(Perl_debug_log, " noscan");
4124     if (r->reganch & ROPT_CHECK_ALL)
4125         PerlIO_printf(Perl_debug_log, " isall");
4126     if (r->check_substr)
4127         PerlIO_printf(Perl_debug_log, ") ");
4128
4129     if (r->regstclass) {
4130         regprop(sv, r->regstclass);
4131         PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
4132     }
4133     if (r->reganch & ROPT_ANCH) {
4134         PerlIO_printf(Perl_debug_log, "anchored");
4135         if (r->reganch & ROPT_ANCH_BOL)
4136             PerlIO_printf(Perl_debug_log, "(BOL)");
4137         if (r->reganch & ROPT_ANCH_MBOL)
4138             PerlIO_printf(Perl_debug_log, "(MBOL)");
4139         if (r->reganch & ROPT_ANCH_SBOL)
4140             PerlIO_printf(Perl_debug_log, "(SBOL)");
4141         if (r->reganch & ROPT_ANCH_GPOS)
4142             PerlIO_printf(Perl_debug_log, "(GPOS)");
4143         PerlIO_putc(Perl_debug_log, ' ');
4144     }
4145     if (r->reganch & ROPT_GPOS_SEEN)
4146         PerlIO_printf(Perl_debug_log, "GPOS ");
4147     if (r->reganch & ROPT_SKIP)
4148         PerlIO_printf(Perl_debug_log, "plus ");
4149     if (r->reganch & ROPT_IMPLICIT)
4150         PerlIO_printf(Perl_debug_log, "implicit ");
4151     PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
4152     if (r->reganch & ROPT_EVAL_SEEN)
4153         PerlIO_printf(Perl_debug_log, "with eval ");
4154     PerlIO_printf(Perl_debug_log, "\n");
4155 #endif  /* DEBUGGING */
4156 }
4157
4158 STATIC void
4159 S_put_byte(pTHX_ SV *sv, int c)
4160 {
4161     if (isCNTRL(c) || c == 127 || c == 255 || !isPRINT(c))
4162         Perl_sv_catpvf(aTHX_ sv, "\\%o", c);
4163     else if (c == '-' || c == ']' || c == '\\' || c == '^')
4164         Perl_sv_catpvf(aTHX_ sv, "\\%c", c);
4165     else
4166         Perl_sv_catpvf(aTHX_ sv, "%c", c);
4167 }
4168
4169 /*
4170 - regprop - printable representation of opcode
4171 */
4172 void
4173 Perl_regprop(pTHX_ SV *sv, regnode *o)
4174 {
4175 #ifdef DEBUGGING
4176     register int k;
4177
4178     sv_setpvn(sv, "", 0);
4179     if (OP(o) >= reg_num)               /* regnode.type is unsigned */
4180         /* It would be nice to FAIL() here, but this may be called from
4181            regexec.c, and it would be hard to supply pRExC_state. */
4182         Perl_croak(aTHX_ "Corrupted regexp opcode");
4183     sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
4184
4185     k = PL_regkind[(U8)OP(o)];
4186
4187     if (k == EXACT)
4188         Perl_sv_catpvf(aTHX_ sv, " <%s%.*s%s>", PL_colors[0],
4189                        STR_LEN(o), STRING(o), PL_colors[1]);
4190     else if (k == CURLY) {
4191         if (OP(o) == CURLYM || OP(o) == CURLYN || OP(o) == CURLYX)
4192             Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
4193         Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
4194     }
4195     else if (k == WHILEM && o->flags)                   /* Ordinal/of */
4196         Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
4197     else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
4198         Perl_sv_catpvf(aTHX_ sv, "%d", (int)ARG(o));    /* Parenth number */
4199     else if (k == LOGICAL)
4200         Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags);     /* 2: embedded, otherwise 1 */
4201     else if (k == ANYOF) {
4202         int i, rangestart = -1;
4203         U8 flags = ANYOF_FLAGS(o);
4204         const char * const anyofs[] = { /* Should be syncronized with
4205                                          * ANYOF_ #xdefines in regcomp.h */
4206             "\\w",
4207             "\\W",
4208             "\\s",
4209             "\\S",
4210             "\\d",
4211             "\\D",
4212             "[:alnum:]",
4213             "[:^alnum:]",
4214             "[:alpha:]",
4215             "[:^alpha:]",
4216             "[:ascii:]",
4217             "[:^ascii:]",
4218             "[:ctrl:]",
4219             "[:^ctrl:]",
4220             "[:graph:]",
4221             "[:^graph:]",
4222             "[:lower:]",
4223             "[:^lower:]",
4224             "[:print:]",
4225             "[:^print:]",
4226             "[:punct:]",
4227             "[:^punct:]",
4228             "[:upper:]",
4229             "[:^upper:]",
4230             "[:xdigit:]",
4231             "[:^xdigit:]",
4232             "[:space:]",
4233             "[:^space:]",
4234             "[:blank:]",
4235             "[:^blank:]"
4236         };
4237
4238         if (flags & ANYOF_LOCALE)
4239             sv_catpv(sv, "{loc}");
4240         if (flags & ANYOF_FOLD)
4241             sv_catpv(sv, "{i}");
4242         Perl_sv_catpvf(aTHX_ sv, "[%s", PL_colors[0]);
4243         if (flags & ANYOF_INVERT)
4244             sv_catpv(sv, "^");
4245         for (i = 0; i <= 256; i++) {
4246             if (i < 256 && ANYOF_BITMAP_TEST(o,i)) {
4247                 if (rangestart == -1)
4248                     rangestart = i;
4249             } else if (rangestart != -1) {
4250                 if (i <= rangestart + 3)
4251                     for (; rangestart < i; rangestart++)
4252                         put_byte(sv, rangestart);
4253                 else {
4254                     put_byte(sv, rangestart);
4255                     sv_catpv(sv, "-");
4256                     put_byte(sv, i - 1);
4257                 }
4258                 rangestart = -1;
4259             }
4260         }
4261
4262         if (o->flags & ANYOF_CLASS)
4263             for (i = 0; i < sizeof(anyofs)/sizeof(char*); i++)
4264                 if (ANYOF_CLASS_TEST(o,i))
4265                     sv_catpv(sv, anyofs[i]);
4266
4267         if (flags & ANYOF_UNICODE)
4268             sv_catpv(sv, "{unicode}");
4269         else if (flags & ANYOF_UNICODE_ALL)
4270             sv_catpv(sv, "{all-unicode}");
4271
4272         {
4273             SV *lv;
4274             SV *sw = regclass_swash(o, FALSE, &lv);
4275         
4276             if (lv) {
4277                 if (sw) {
4278                     UV i;
4279                     U8 s[UTF8_MAXLEN+1];
4280                 
4281                     for (i = 0; i <= 256; i++) { /* just the first 256 */
4282                         U8 *e = uv_to_utf8(s, i);
4283                         
4284                         if (i < 256 && swash_fetch(sw, s)) {
4285                             if (rangestart == -1)
4286                                 rangestart = i;
4287                         } else if (rangestart != -1) {
4288                             U8 *p;
4289                         
4290                             if (i <= rangestart + 3)
4291                                 for (; rangestart < i; rangestart++) {
4292                                     for(e = uv_to_utf8(s, rangestart), p = s; p < e; p++)
4293                                         put_byte(sv, *p);
4294                                 }
4295                             else {
4296                                 for (e = uv_to_utf8(s, rangestart), p = s; p < e; p++)
4297                                     put_byte(sv, *p);
4298                                 sv_catpv(sv, "-");
4299                                     for (e = uv_to_utf8(s, i - 1), p = s; p < e; p++)
4300                                         put_byte(sv, *p);
4301                                 }
4302                                 rangestart = -1;
4303                             }
4304                         }
4305                         
4306                     sv_catpv(sv, "..."); /* et cetera */
4307                 }
4308
4309                 {
4310                     char *s = savepv(SvPVX(lv));
4311                     char *origs = s;
4312                 
4313                     while(*s && *s != '\n') s++;
4314                 
4315                     if (*s == '\n') {
4316                         char *t = ++s;
4317                         
4318                         while (*s) {
4319                             if (*s == '\n')
4320                                 *s = ' ';
4321                             s++;
4322                         }
4323                         if (s[-1] == ' ')
4324                             s[-1] = 0;
4325                         
4326                         sv_catpv(sv, t);
4327                     }
4328                 
4329                     Safefree(origs);
4330                 }
4331             }
4332         }
4333
4334         Perl_sv_catpvf(aTHX_ sv, "%s]", PL_colors[1]);
4335     }
4336     else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
4337         Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
4338 #endif  /* DEBUGGING */
4339 }
4340
4341 SV *
4342 Perl_re_intuit_string(pTHX_ regexp *prog)
4343 {                               /* Assume that RE_INTUIT is set */
4344     DEBUG_r(
4345         {   STRLEN n_a;
4346             char *s = SvPV(prog->check_substr,n_a);
4347
4348             if (!PL_colorset) reginitcolors();
4349             PerlIO_printf(Perl_debug_log,
4350                       "%sUsing REx substr:%s `%s%.60s%s%s'\n",
4351                       PL_colors[4],PL_colors[5],PL_colors[0],
4352                       s,
4353                       PL_colors[1],
4354                       (strlen(s) > 60 ? "..." : ""));
4355         } );
4356
4357     return prog->check_substr;
4358 }
4359
4360 void
4361 Perl_pregfree(pTHX_ struct regexp *r)
4362 {
4363     DEBUG_r(if (!PL_colorset) reginitcolors());
4364
4365     if (!r || (--r->refcnt > 0))
4366         return;
4367     DEBUG_r(PerlIO_printf(Perl_debug_log,
4368                       "%sFreeing REx:%s `%s%.60s%s%s'\n",
4369                       PL_colors[4],PL_colors[5],PL_colors[0],
4370                       r->precomp,
4371                       PL_colors[1],
4372                       (strlen(r->precomp) > 60 ? "..." : "")));
4373
4374     if (r->precomp)
4375         Safefree(r->precomp);
4376     if (RX_MATCH_COPIED(r))
4377         Safefree(r->subbeg);
4378     if (r->substrs) {
4379         if (r->anchored_substr)
4380             SvREFCNT_dec(r->anchored_substr);
4381         if (r->float_substr)
4382             SvREFCNT_dec(r->float_substr);
4383         Safefree(r->substrs);
4384     }
4385     if (r->data) {
4386         int n = r->data->count;
4387         AV* new_comppad = NULL;
4388         AV* old_comppad;
4389         SV** old_curpad;
4390
4391         while (--n >= 0) {
4392             switch (r->data->what[n]) {
4393             case 's':
4394                 SvREFCNT_dec((SV*)r->data->data[n]);
4395                 break;
4396             case 'f':
4397                 Safefree(r->data->data[n]);
4398                 break;
4399             case 'p':
4400                 new_comppad = (AV*)r->data->data[n];
4401                 break;
4402             case 'o':
4403                 if (new_comppad == NULL)
4404                     Perl_croak(aTHX_ "panic: pregfree comppad");
4405                 old_comppad = PL_comppad;
4406                 old_curpad = PL_curpad;
4407                 /* Watch out for global destruction's random ordering. */
4408                 if (SvTYPE(new_comppad) == SVt_PVAV) {
4409                     PL_comppad = new_comppad;
4410                     PL_curpad = AvARRAY(new_comppad);
4411                 }
4412                 else
4413                     PL_curpad = NULL;
4414                 op_free((OP_4tree*)r->data->data[n]);
4415                 PL_comppad = old_comppad;
4416                 PL_curpad = old_curpad;
4417                 SvREFCNT_dec((SV*)new_comppad);
4418                 new_comppad = NULL;
4419                 break;
4420             case 'n':
4421                 break;
4422             default:
4423                 Perl_croak(aTHX_ "panic: regfree data code '%c'", r->data->what[n]);
4424             }
4425         }
4426         Safefree(r->data->what);
4427         Safefree(r->data);
4428     }
4429     Safefree(r->startp);
4430     Safefree(r->endp);
4431     Safefree(r);
4432 }
4433
4434 /*
4435  - regnext - dig the "next" pointer out of a node
4436  *
4437  * [Note, when REGALIGN is defined there are two places in regmatch()
4438  * that bypass this code for speed.]
4439  */
4440 regnode *
4441 Perl_regnext(pTHX_ register regnode *p)
4442 {
4443     register I32 offset;
4444
4445     if (p == &PL_regdummy)
4446         return(NULL);
4447
4448     offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
4449     if (offset == 0)
4450         return(NULL);
4451
4452     return(p+offset);
4453 }
4454
4455 STATIC void     
4456 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
4457 {
4458     va_list args;
4459     STRLEN l1 = strlen(pat1);
4460     STRLEN l2 = strlen(pat2);
4461     char buf[512];
4462     SV *msv;
4463     char *message;
4464
4465     if (l1 > 510)
4466         l1 = 510;
4467     if (l1 + l2 > 510)
4468         l2 = 510 - l1;
4469     Copy(pat1, buf, l1 , char);
4470     Copy(pat2, buf + l1, l2 , char);
4471     buf[l1 + l2] = '\n';
4472     buf[l1 + l2 + 1] = '\0';
4473 #ifdef I_STDARG
4474     /* ANSI variant takes additional second argument */
4475     va_start(args, pat2);
4476 #else
4477     va_start(args);
4478 #endif
4479     msv = vmess(buf, &args);
4480     va_end(args);
4481     message = SvPV(msv,l1);
4482     if (l1 > 512)
4483         l1 = 512;
4484     Copy(message, buf, l1 , char);
4485     buf[l1] = '\0';                     /* Overwrite \n */
4486     Perl_croak(aTHX_ "%s", buf);
4487 }
4488
4489 /* XXX Here's a total kludge.  But we need to re-enter for swash routines. */
4490
4491 void
4492 Perl_save_re_context(pTHX)
4493 {
4494 #if 0
4495     SAVEPPTR(RExC_precomp);             /* uncompiled string. */
4496     SAVEI32(RExC_npar);         /* () count. */
4497     SAVEI32(RExC_size);         /* Code size. */
4498     SAVEI16(RExC_flags16);              /* are we folding, multilining? */
4499     SAVEVPTR(RExC_rx);          /* from regcomp.c */
4500     SAVEI32(RExC_seen);         /* from regcomp.c */
4501     SAVEI32(RExC_sawback);              /* Did we see \1, ...? */
4502     SAVEI32(RExC_naughty);              /* How bad is this pattern? */
4503     SAVEVPTR(RExC_emit);                /* Code-emit pointer; &regdummy = don't */
4504     SAVEPPTR(RExC_end);         /* End of input for compile */
4505     SAVEPPTR(RExC_parse);               /* Input-scan pointer. */
4506 #endif
4507
4508     SAVEI32(PL_reg_flags);              /* from regexec.c */
4509     SAVEPPTR(PL_bostr);
4510     SAVEPPTR(PL_reginput);              /* String-input pointer. */
4511     SAVEPPTR(PL_regbol);                /* Beginning of input, for ^ check. */
4512     SAVEPPTR(PL_regeol);                /* End of input, for $ check. */
4513     SAVEVPTR(PL_regstartp);             /* Pointer to startp array. */
4514     SAVEVPTR(PL_regendp);               /* Ditto for endp. */
4515     SAVEVPTR(PL_reglastparen);          /* Similarly for lastparen. */
4516     SAVEPPTR(PL_regtill);               /* How far we are required to go. */
4517     SAVEI8(PL_regprev);                 /* char before regbol, \n if none */
4518     SAVEGENERICPV(PL_reg_start_tmp);            /* from regexec.c */
4519     PL_reg_start_tmp = 0;
4520     SAVEI32(PL_reg_start_tmpl);         /* from regexec.c */
4521     PL_reg_start_tmpl = 0;
4522     SAVEVPTR(PL_regdata);
4523     SAVEI32(PL_reg_eval_set);           /* from regexec.c */
4524     SAVEI32(PL_regnarrate);             /* from regexec.c */
4525     SAVEVPTR(PL_regprogram);            /* from regexec.c */
4526     SAVEINT(PL_regindent);              /* from regexec.c */
4527     SAVEVPTR(PL_regcc);                 /* from regexec.c */
4528     SAVEVPTR(PL_curcop);
4529     SAVEVPTR(PL_reg_call_cc);           /* from regexec.c */
4530     SAVEVPTR(PL_reg_re);                /* from regexec.c */
4531     SAVEPPTR(PL_reg_ganch);             /* from regexec.c */
4532     SAVESPTR(PL_reg_sv);                /* from regexec.c */
4533     SAVEVPTR(PL_reg_magic);             /* from regexec.c */
4534     SAVEI32(PL_reg_oldpos);                     /* from regexec.c */
4535     SAVEVPTR(PL_reg_oldcurpm);          /* from regexec.c */
4536     SAVEVPTR(PL_reg_curpm);             /* from regexec.c */
4537     SAVEI32(PL_regnpar);                /* () count. */
4538 #ifdef DEBUGGING
4539     SAVEPPTR(PL_reg_starttry);          /* from regexec.c */
4540 #endif
4541 }
4542
4543 #ifdef PERL_OBJECT
4544 #include "XSUB.h"
4545 #undef this
4546 #define this pPerl
4547 #endif
4548
4549 static void
4550 clear_re(pTHXo_ void *r)
4551 {
4552     ReREFCNT_dec((regexp *)r);
4553 }