This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
9c9fc1415f2c11fd0a82bed7a1c2fe457775ff4c
[perl5.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 #endif 
45
46 /*SUPPRESS 112*/
47 /*
48  * pregcomp and pregexec -- regsub and regerror are not used in perl
49  *
50  *      Copyright (c) 1986 by University of Toronto.
51  *      Written by Henry Spencer.  Not derived from licensed software.
52  *
53  *      Permission is granted to anyone to use this software for any
54  *      purpose on any computer system, and to redistribute it freely,
55  *      subject to the following restrictions:
56  *
57  *      1. The author is not responsible for the consequences of use of
58  *              this software, no matter how awful, even if they arise
59  *              from defects in it.
60  *
61  *      2. The origin of this software must not be misrepresented, either
62  *              by explicit claim or by omission.
63  *
64  *      3. Altered versions must be plainly marked as such, and must not
65  *              be misrepresented as being the original software.
66  *
67  *
68  ****    Alterations to Henry's code are...
69  ****
70  ****    Copyright (c) 1991-1999, Larry Wall
71  ****
72  ****    You may distribute under the terms of either the GNU General Public
73  ****    License or the Artistic License, as specified in the README file.
74
75  *
76  * Beware that some of this code is subtly aware of the way operator
77  * precedence is structured in regular expressions.  Serious changes in
78  * regular-expression syntax might require a total rethink.
79  */
80 #include "EXTERN.h"
81 #define PERL_IN_REGCOMP_C
82 #include "perl.h"
83
84 #ifndef PERL_IN_XSUB_RE
85 #  include "INTERN.h"
86 #endif
87
88 #define REG_COMP_C
89 #include "regcomp.h"
90
91 #ifdef op
92 #undef op
93 #endif /* op */
94
95 #ifdef MSDOS
96 # if defined(BUGGY_MSC6)
97  /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
98  # pragma optimize("a",off)
99  /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
100  # pragma optimize("w",on )
101 # endif /* BUGGY_MSC6 */
102 #endif /* MSDOS */
103
104 #ifndef STATIC
105 #define STATIC  static
106 #endif
107
108 #define ISMULT1(c)      ((c) == '*' || (c) == '+' || (c) == '?')
109 #define ISMULT2(s)      ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
110         ((*s) == '{' && regcurly(s)))
111 #ifdef atarist
112 #define PERL_META       "^$.[()|?+*\\"
113 #else
114 #define META    "^$.[()|?+*\\"
115 #endif
116
117 #ifdef SPSTART
118 #undef SPSTART          /* dratted cpp namespace... */
119 #endif
120 /*
121  * Flags to be passed up and down.
122  */
123 #define WORST           0       /* Worst case. */
124 #define HASWIDTH        0x1     /* Known to match non-null strings. */
125 #define SIMPLE          0x2     /* Simple enough to be STAR/PLUS operand. */
126 #define SPSTART         0x4     /* Starts with * or +. */
127 #define TRYAGAIN        0x8     /* Weeded out a declaration. */
128
129 /*
130  * Forward declarations for pregcomp()'s friends.
131  */
132
133 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
134                                       0, 0, 0 };
135
136 #define SF_BEFORE_EOL           (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
137 #define SF_BEFORE_SEOL          0x1
138 #define SF_BEFORE_MEOL          0x2
139 #define SF_FIX_BEFORE_EOL       (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
140 #define SF_FL_BEFORE_EOL        (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
141
142 #ifdef NO_UNARY_PLUS
143 #  define SF_FIX_SHIFT_EOL      (0+2)
144 #  define SF_FL_SHIFT_EOL               (0+4)
145 #else
146 #  define SF_FIX_SHIFT_EOL      (+2)
147 #  define SF_FL_SHIFT_EOL               (+4)
148 #endif
149
150 #define SF_FIX_BEFORE_SEOL      (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
151 #define SF_FIX_BEFORE_MEOL      (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
152
153 #define SF_FL_BEFORE_SEOL       (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
154 #define SF_FL_BEFORE_MEOL       (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
155 #define SF_IS_INF               0x40
156 #define SF_HAS_PAR              0x80
157 #define SF_IN_PAR               0x100
158 #define SF_HAS_EVAL             0x200
159 #define SCF_DO_SUBSTR           0x400
160
161 #define RF_utf8         8
162 #define UTF (PL_reg_flags & RF_utf8)
163 #define LOC (PL_regflags & PMf_LOCALE)
164 #define FOLD (PL_regflags & PMf_FOLD)
165
166 #define OOB_CHAR8               1234
167 #define OOB_UTF8                123456
168
169 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
170 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
171
172 STATIC void
173 S_clear_re(pTHX_ void *r)
174 {
175     ReREFCNT_dec((regexp *)r);
176 }
177
178 STATIC void
179 S_scan_commit(pTHX_ scan_data_t *data)
180 {
181     dTHR;
182     STRLEN l = CHR_SVLEN(data->last_found);
183     STRLEN old_l = CHR_SVLEN(*data->longest);
184     
185     if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
186         sv_setsv(*data->longest, data->last_found);
187         if (*data->longest == data->longest_fixed) {
188             data->offset_fixed = l ? data->last_start_min : data->pos_min;
189             if (data->flags & SF_BEFORE_EOL)
190                 data->flags 
191                     |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
192             else
193                 data->flags &= ~SF_FIX_BEFORE_EOL;
194         }
195         else {
196             data->offset_float_min = l ? data->last_start_min : data->pos_min;
197             data->offset_float_max = (l 
198                                       ? data->last_start_max 
199                                       : data->pos_min + data->pos_delta);
200             if (data->flags & SF_BEFORE_EOL)
201                 data->flags 
202                     |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
203             else
204                 data->flags &= ~SF_FL_BEFORE_EOL;
205         }
206     }
207     SvCUR_set(data->last_found, 0);
208     data->last_end = -1;
209     data->flags &= ~SF_BEFORE_EOL;
210 }
211
212 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
213    to the position after last scanned or to NULL. */
214
215 STATIC I32
216 S_study_chunk(pTHX_ regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
217                         /* scanp: Start here (read-write). */
218                         /* deltap: Write maxlen-minlen here. */
219                         /* last: Stop before this one. */
220 {
221     dTHR;
222     I32 min = 0, pars = 0, code;
223     regnode *scan = *scanp, *next;
224     I32 delta = 0;
225     int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
226     int is_inf_internal = 0;            /* The studied chunk is infinite */
227     I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
228     scan_data_t data_fake;
229     
230     while (scan && OP(scan) != END && scan < last) {
231         /* Peephole optimizer: */
232
233         if (PL_regkind[(U8)OP(scan)] == EXACT) {
234             regnode *n = regnext(scan);
235             U32 stringok = 1;
236 #ifdef DEBUGGING
237             regnode *stop = scan;
238 #endif 
239
240             next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
241             /* Skip NOTHING, merge EXACT*. */
242             while (n &&
243                    ( PL_regkind[(U8)OP(n)] == NOTHING || 
244                      (stringok && (OP(n) == OP(scan))))
245                    && NEXT_OFF(n)
246                    && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
247                 if (OP(n) == TAIL || n > next)
248                     stringok = 0;
249                 if (PL_regkind[(U8)OP(n)] == NOTHING) {
250                     NEXT_OFF(scan) += NEXT_OFF(n);
251                     next = n + NODE_STEP_REGNODE;
252 #ifdef DEBUGGING
253                     if (stringok)
254                         stop = n;
255 #endif 
256                     n = regnext(n);
257                 }
258                 else {
259                     int oldl = *OPERAND(scan);
260                     regnode *nnext = regnext(n);
261                     
262                     if (oldl + *OPERAND(n) > U8_MAX) 
263                         break;
264                     NEXT_OFF(scan) += NEXT_OFF(n);
265                     *OPERAND(scan) += *OPERAND(n);
266                     next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2;
267                     /* Now we can overwrite *n : */
268                     Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1,
269                          *OPERAND(n) + 1, char);
270 #ifdef DEBUGGING
271                     if (stringok)
272                         stop = next - 1;
273 #endif 
274                     n = nnext;
275                 }
276             }
277 #ifdef DEBUGGING
278             /* Allow dumping */
279             n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
280             while (n <= stop) {
281                 /* Purify reports a benign UMR here sometimes, because we
282                  * don't initialize the OP() slot of a node when that node
283                  * is occupied by just the trailing null of the string in
284                  * an EXACT node */
285                 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
286                     OP(n) = OPTIMIZED;
287                     NEXT_OFF(n) = 0;
288                 }
289                 n++;
290             }
291 #endif 
292
293         }
294         if (OP(scan) != CURLYX) {
295             int max = (reg_off_by_arg[OP(scan)]
296                        ? I32_MAX
297                        /* I32 may be smaller than U16 on CRAYs! */
298                        : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
299             int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
300             int noff;
301             regnode *n = scan;
302             
303             /* Skip NOTHING and LONGJMP. */
304             while ((n = regnext(n))
305                    && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
306                        || ((OP(n) == LONGJMP) && (noff = ARG(n))))
307                    && off + noff < max)
308                 off += noff;
309             if (reg_off_by_arg[OP(scan)])
310                 ARG(scan) = off;
311             else 
312                 NEXT_OFF(scan) = off;
313         }
314         if (OP(scan) == BRANCH || OP(scan) == BRANCHJ 
315                    || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
316             next = regnext(scan);
317             code = OP(scan);
318             
319             if (OP(next) == code || code == IFTHEN || code == SUSPEND) { 
320                 I32 max1 = 0, min1 = I32_MAX, num = 0;
321                 
322                 if (flags & SCF_DO_SUBSTR)
323                     scan_commit(data);
324                 while (OP(scan) == code) {
325                     I32 deltanext, minnext;
326
327                     num++;
328                     data_fake.flags = 0;
329                     next = regnext(scan);
330                     scan = NEXTOPER(scan);
331                     if (code != BRANCH)
332                         scan = NEXTOPER(scan);
333                     /* We suppose the run is continuous, last=next...*/
334                     minnext = study_chunk(&scan, &deltanext, next,
335                                           &data_fake, 0);
336                     if (min1 > minnext) 
337                         min1 = minnext;
338                     if (max1 < minnext + deltanext)
339                         max1 = minnext + deltanext;
340                     if (deltanext == I32_MAX)
341                         is_inf = is_inf_internal = 1;
342                     scan = next;
343                     if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
344                         pars++;
345                     if (data && (data_fake.flags & SF_HAS_EVAL))
346                         data->flags |= SF_HAS_EVAL;
347                     if (code == SUSPEND) 
348                         break;
349                 }
350                 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
351                     min1 = 0;
352                 if (flags & SCF_DO_SUBSTR) {
353                     data->pos_min += min1;
354                     data->pos_delta += max1 - min1;
355                     if (max1 != min1 || is_inf)
356                         data->longest = &(data->longest_float);
357                 }
358                 min += min1;
359                 delta += max1 - min1;
360             }
361             else if (code == BRANCHJ)   /* single branch is optimized. */
362                 scan = NEXTOPER(NEXTOPER(scan));
363             else                        /* single branch is optimized. */
364                 scan = NEXTOPER(scan);
365             continue;
366         }
367         else if (OP(scan) == EXACT) {
368             I32 l = *OPERAND(scan);
369             if (UTF) {
370                 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
371                 unsigned char *e = s + l;
372                 I32 newl = 0;
373                 while (s < e) {
374                     newl++;
375                     s += UTF8SKIP(s);
376                 }
377                 l = newl;
378             }
379             min += l;
380             if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
381                 /* The code below prefers earlier match for fixed
382                    offset, later match for variable offset.  */
383                 if (data->last_end == -1) { /* Update the start info. */
384                     data->last_start_min = data->pos_min;
385                     data->last_start_max = is_inf
386                         ? I32_MAX : data->pos_min + data->pos_delta; 
387                 }
388                 sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), *OPERAND(scan));
389                 data->last_end = data->pos_min + l;
390                 data->pos_min += l; /* As in the first entry. */
391                 data->flags &= ~SF_BEFORE_EOL;
392             }
393         }
394         else if (PL_regkind[(U8)OP(scan)] == EXACT) {
395             I32 l = *OPERAND(scan);
396             if (flags & SCF_DO_SUBSTR) 
397                 scan_commit(data);
398             if (UTF) {
399                 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
400                 unsigned char *e = s + l;
401                 I32 newl = 0;
402                 while (s < e) {
403                     newl++;
404                     s += UTF8SKIP(s);
405                 }
406                 l = newl;
407             }
408             min += l;
409             if (data && (flags & SCF_DO_SUBSTR))
410                 data->pos_min += l;
411         }
412         else if (strchr((char*)PL_varies,OP(scan))) {
413             I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
414             regnode *oscan = scan;
415             
416             switch (PL_regkind[(U8)OP(scan)]) {
417             case WHILEM:
418                 scan = NEXTOPER(scan);
419                 goto finish;
420             case PLUS:
421                 if (flags & SCF_DO_SUBSTR) {
422                     next = NEXTOPER(scan);
423                     if (OP(next) == EXACT) {
424                         mincount = 1; 
425                         maxcount = REG_INFTY; 
426                         next = regnext(scan);
427                         scan = NEXTOPER(scan);
428                         goto do_curly;
429                     }
430                 }
431                 if (flags & SCF_DO_SUBSTR)
432                     data->pos_min++;
433                 min++;
434                 /* Fall through. */
435             case STAR:
436                 is_inf = is_inf_internal = 1; 
437                 scan = regnext(scan);
438                 if (flags & SCF_DO_SUBSTR) {
439                     scan_commit(data);
440                     data->longest = &(data->longest_float);
441                 }
442                 goto optimize_curly_tail;
443             case CURLY:
444                 mincount = ARG1(scan); 
445                 maxcount = ARG2(scan);
446                 next = regnext(scan);
447                 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
448               do_curly:
449                 if (flags & SCF_DO_SUBSTR) {
450                     if (mincount == 0) scan_commit(data);
451                     pos_before = data->pos_min;
452                 }
453                 if (data) {
454                     fl = data->flags;
455                     data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
456                     if (is_inf)
457                         data->flags |= SF_IS_INF;
458                 }
459                 /* This will finish on WHILEM, setting scan, or on NULL: */
460                 minnext = study_chunk(&scan, &deltanext, last, data, 
461                                       mincount == 0 
462                                         ? (flags & ~SCF_DO_SUBSTR) : flags);
463                 if (!scan)              /* It was not CURLYX, but CURLY. */
464                     scan = next;
465                 if (ckWARN(WARN_UNSAFE) && (minnext + deltanext == 0) 
466                     && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
467                     && maxcount <= REG_INFTY/3) /* Complement check for big count */
468                     Perl_warner(aTHX_ WARN_UNSAFE, "Strange *+?{} on zero-length expression");
469                 min += minnext * mincount;
470                 is_inf_internal |= (maxcount == REG_INFTY 
471                                     && (minnext + deltanext) > 0
472                                    || deltanext == I32_MAX);
473                 is_inf |= is_inf_internal;
474                 delta += (minnext + deltanext) * maxcount - minnext * mincount;
475
476                 /* Try powerful optimization CURLYX => CURLYN. */
477                 if (  OP(oscan) == CURLYX && data 
478                       && data->flags & SF_IN_PAR
479                       && !(data->flags & SF_HAS_EVAL)
480                       && !deltanext && minnext == 1 ) {
481                     /* Try to optimize to CURLYN.  */
482                     regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
483                     regnode *nxt1 = nxt, *nxt2;
484
485                     /* Skip open. */
486                     nxt = regnext(nxt);
487                     if (!strchr((char*)PL_simple,OP(nxt))
488                         && !(PL_regkind[(U8)OP(nxt)] == EXACT
489                              && *OPERAND(nxt) == 1)) 
490                         goto nogo;
491                     nxt2 = nxt;
492                     nxt = regnext(nxt);
493                     if (OP(nxt) != CLOSE) 
494                         goto nogo;
495                     /* Now we know that nxt2 is the only contents: */
496                     oscan->flags = ARG(nxt);
497                     OP(oscan) = CURLYN;
498                     OP(nxt1) = NOTHING; /* was OPEN. */
499 #ifdef DEBUGGING
500                     OP(nxt1 + 1) = OPTIMIZED; /* was count. */
501                     NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
502                     NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
503                     OP(nxt) = OPTIMIZED;        /* was CLOSE. */
504                     OP(nxt + 1) = OPTIMIZED; /* was count. */
505                     NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
506 #endif 
507                 }
508               nogo:
509
510                 /* Try optimization CURLYX => CURLYM. */
511                 if (  OP(oscan) == CURLYX && data 
512                       && !(data->flags & SF_HAS_PAR)
513                       && !(data->flags & SF_HAS_EVAL)
514                       && !deltanext  ) {
515                     /* XXXX How to optimize if data == 0? */
516                     /* Optimize to a simpler form.  */
517                     regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
518                     regnode *nxt2;
519
520                     OP(oscan) = CURLYM;
521                     while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
522                             && (OP(nxt2) != WHILEM)) 
523                         nxt = nxt2;
524                     OP(nxt2)  = SUCCEED; /* Whas WHILEM */
525                     /* Need to optimize away parenths. */
526                     if (data->flags & SF_IN_PAR) {
527                         /* Set the parenth number.  */
528                         regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
529
530                         if (OP(nxt) != CLOSE) 
531                             FAIL("panic opt close");
532                         oscan->flags = ARG(nxt);
533                         OP(nxt1) = OPTIMIZED;   /* was OPEN. */
534                         OP(nxt) = OPTIMIZED;    /* was CLOSE. */
535 #ifdef DEBUGGING
536                         OP(nxt1 + 1) = OPTIMIZED; /* was count. */
537                         OP(nxt + 1) = OPTIMIZED; /* was count. */
538                         NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
539                         NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
540 #endif 
541 #if 0
542                         while ( nxt1 && (OP(nxt1) != WHILEM)) {
543                             regnode *nnxt = regnext(nxt1);
544                             
545                             if (nnxt == nxt) {
546                                 if (reg_off_by_arg[OP(nxt1)])
547                                     ARG_SET(nxt1, nxt2 - nxt1);
548                                 else if (nxt2 - nxt1 < U16_MAX)
549                                     NEXT_OFF(nxt1) = nxt2 - nxt1;
550                                 else
551                                     OP(nxt) = NOTHING;  /* Cannot beautify */
552                             }
553                             nxt1 = nnxt;
554                         }
555 #endif
556                         /* Optimize again: */
557                         study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
558                     }
559                     else
560                         oscan->flags = 0;
561                 }
562                 if (data && fl & (SF_HAS_PAR|SF_IN_PAR)) 
563                     pars++;
564                 if (flags & SCF_DO_SUBSTR) {
565                     SV *last_str = Nullsv;
566                     int counted = mincount != 0;
567
568                     if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
569                         I32 b = pos_before >= data->last_start_min 
570                             ? pos_before : data->last_start_min;
571                         STRLEN l;
572                         char *s = SvPV(data->last_found, l);
573                         I32 old = b - data->last_start_min;
574
575                         if (UTF)
576                             old = utf8_hop((U8*)s, old) - (U8*)s;
577                         
578                         l -= old;
579                         /* Get the added string: */
580                         last_str = newSVpvn(s  + old, l);
581                         if (deltanext == 0 && pos_before == b) {
582                             /* What was added is a constant string */
583                             if (mincount > 1) {
584                                 SvGROW(last_str, (mincount * l) + 1);
585                                 repeatcpy(SvPVX(last_str) + l, 
586                                           SvPVX(last_str), l, mincount - 1);
587                                 SvCUR(last_str) *= mincount;
588                                 /* Add additional parts. */
589                                 SvCUR_set(data->last_found, 
590                                           SvCUR(data->last_found) - l);
591                                 sv_catsv(data->last_found, last_str);
592                                 data->last_end += l * (mincount - 1);
593                             }
594                         }
595                     }
596                     /* It is counted once already... */
597                     data->pos_min += minnext * (mincount - counted);
598                     data->pos_delta += - counted * deltanext +
599                         (minnext + deltanext) * maxcount - minnext * mincount;
600                     if (mincount != maxcount) {
601                         scan_commit(data);
602                         if (mincount && last_str) {
603                             sv_setsv(data->last_found, last_str);
604                             data->last_end = data->pos_min;
605                             data->last_start_min = 
606                                 data->pos_min - CHR_SVLEN(last_str);
607                             data->last_start_max = is_inf 
608                                 ? I32_MAX 
609                                 : data->pos_min + data->pos_delta
610                                 - CHR_SVLEN(last_str);
611                         }
612                         data->longest = &(data->longest_float);
613                     }
614                     SvREFCNT_dec(last_str);
615                 }
616                 if (data && (fl & SF_HAS_EVAL))
617                     data->flags |= SF_HAS_EVAL;
618               optimize_curly_tail:
619                 if (OP(oscan) != CURLYX) {
620                     while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
621                            && NEXT_OFF(next))
622                         NEXT_OFF(oscan) += NEXT_OFF(next);
623                 }
624                 continue;
625             default:                    /* REF only? */
626                 if (flags & SCF_DO_SUBSTR) {
627                     scan_commit(data);
628                     data->longest = &(data->longest_float);
629                 }
630                 is_inf = is_inf_internal = 1;
631                 break;
632             }
633         }
634         else if (strchr((char*)PL_simple,OP(scan)) || PL_regkind[(U8)OP(scan)] == ANYUTF8) {
635             if (flags & SCF_DO_SUBSTR) {
636                 scan_commit(data);
637                 data->pos_min++;
638             }
639             min++;
640         }
641         else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
642             data->flags |= (OP(scan) == MEOL
643                             ? SF_BEFORE_MEOL
644                             : SF_BEFORE_SEOL);
645         }
646         else if (PL_regkind[(U8)OP(scan)] == BRANCHJ
647                    && (scan->flags || data)
648                    && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
649             I32 deltanext, minnext;
650             regnode *nscan;
651
652             data_fake.flags = 0;
653             next = regnext(scan);
654             nscan = NEXTOPER(NEXTOPER(scan));
655             minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
656             if (scan->flags) {
657                 if (deltanext) {
658                     FAIL("variable length lookbehind not implemented");
659                 }
660                 else if (minnext > U8_MAX) {
661                     FAIL2("lookbehind longer than %d not implemented", U8_MAX);
662                 }
663                 scan->flags = minnext;
664             }
665             if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
666                 pars++;
667             if (data && (data_fake.flags & SF_HAS_EVAL))
668                 data->flags |= SF_HAS_EVAL;
669         }
670         else if (OP(scan) == OPEN) {
671             pars++;
672         }
673         else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
674             next = regnext(scan);
675
676             if ( next && (OP(next) != WHILEM) && next < last)
677                 is_par = 0;             /* Disable optimization */
678         }
679         else if (OP(scan) == EVAL) {
680                 if (data)
681                     data->flags |= SF_HAS_EVAL;
682         }
683         else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded */
684                 if (flags & SCF_DO_SUBSTR) {
685                     scan_commit(data);
686                     data->longest = &(data->longest_float);
687                 }
688                 is_inf = is_inf_internal = 1;
689         }
690         /* Else: zero-length, ignore. */
691         scan = regnext(scan);
692     }
693
694   finish:
695     *scanp = scan;
696     *deltap = is_inf_internal ? I32_MAX : delta;
697     if (flags & SCF_DO_SUBSTR && is_inf) 
698         data->pos_delta = I32_MAX - data->pos_min;
699     if (is_par > U8_MAX)
700         is_par = 0;
701     if (is_par && pars==1 && data) {
702         data->flags |= SF_IN_PAR;
703         data->flags &= ~SF_HAS_PAR;
704     }
705     else if (pars && data) {
706         data->flags |= SF_HAS_PAR;
707         data->flags &= ~SF_IN_PAR;
708     }
709     return min;
710 }
711
712 STATIC I32
713 S_add_data(pTHX_ I32 n, char *s)
714 {
715     dTHR;
716     if (PL_regcomp_rx->data) {
717         Renewc(PL_regcomp_rx->data, 
718                sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1), 
719                char, struct reg_data);
720         Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
721         PL_regcomp_rx->data->count += n;
722     }
723     else {
724         Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
725              char, struct reg_data);
726         New(1208, PL_regcomp_rx->data->what, n, U8);
727         PL_regcomp_rx->data->count = n;
728     }
729     Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
730     return PL_regcomp_rx->data->count - n;
731 }
732
733 void
734 Perl_reginitcolors(pTHX)
735 {
736     dTHR;
737     int i = 0;
738     char *s = PerlEnv_getenv("PERL_RE_COLORS");
739             
740     if (s) {
741         PL_colors[0] = s = savepv(s);
742         while (++i < 6) {
743             s = strchr(s, '\t');
744             if (s) {
745                 *s = '\0';
746                 PL_colors[i] = ++s;
747             }
748             else
749                 PL_colors[i] = s = "";
750         }
751     } else {
752         while (i < 6) 
753             PL_colors[i++] = "";
754     }
755     PL_colorset = 1;
756 }
757
758 /*
759  - pregcomp - compile a regular expression into internal code
760  *
761  * We can't allocate space until we know how big the compiled form will be,
762  * but we can't compile it (and thus know how big it is) until we've got a
763  * place to put the code.  So we cheat:  we compile it twice, once with code
764  * generation turned off and size counting turned on, and once "for real".
765  * This also means that we don't allocate space until we are sure that the
766  * thing really will compile successfully, and we never have to move the
767  * code and thus invalidate pointers into it.  (Note that it has to be in
768  * one piece because free() must be able to free it all.) [NB: not true in perl]
769  *
770  * Beware that the optimization-preparation code in here knows about some
771  * of the structure of the compiled regexp.  [I'll say.]
772  */
773 regexp *
774 Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
775 {
776     dTHR;
777     register regexp *r;
778     regnode *scan;
779     SV **longest;
780     SV *longest_fixed;
781     SV *longest_float;
782     regnode *first;
783     I32 flags;
784     I32 minlen = 0;
785     I32 sawplus = 0;
786     I32 sawopen = 0;
787
788     if (exp == NULL)
789         FAIL("NULL regexp argument");
790
791     if (PL_curcop == &PL_compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
792         PL_reg_flags |= RF_utf8;
793     else
794         PL_reg_flags = 0;
795
796     PL_regprecomp = savepvn(exp, xend - exp);
797     DEBUG_r(if (!PL_colorset) reginitcolors());
798     DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling%s RE `%s%*s%s'\n",
799                       PL_colors[4],PL_colors[5],PL_colors[0],
800                       xend - exp, PL_regprecomp, PL_colors[1]));
801     PL_regflags = pm->op_pmflags;
802     PL_regsawback = 0;
803
804     PL_regseen = 0;
805     PL_seen_zerolen = *exp == '^' ? -1 : 0;
806     PL_seen_evals = 0;
807     PL_extralen = 0;
808
809     /* First pass: determine size, legality. */
810     PL_regcomp_parse = exp;
811     PL_regxend = xend;
812     PL_regnaughty = 0;
813     PL_regnpar = 1;
814     PL_regsize = 0L;
815     PL_regcode = &PL_regdummy;
816     regc((U8)REG_MAGIC, (char*)PL_regcode);
817     if (reg(0, &flags) == NULL) {
818         Safefree(PL_regprecomp);
819         PL_regprecomp = Nullch;
820         return(NULL);
821     }
822     DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
823
824     /* Small enough for pointer-storage convention?
825        If extralen==0, this means that we will not need long jumps. */
826     if (PL_regsize >= 0x10000L && PL_extralen)
827         PL_regsize += PL_extralen;
828     else
829         PL_extralen = 0;
830
831     /* Allocate space and initialize. */
832     Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
833          char, regexp);
834     if (r == NULL)
835         FAIL("regexp out of space");
836     r->refcnt = 1;
837     r->prelen = xend - exp;
838     r->precomp = PL_regprecomp;
839     r->subbeg = NULL;
840     r->reganch = pm->op_pmflags & PMf_COMPILETIME;
841     r->nparens = PL_regnpar - 1;        /* set early to validate backrefs */
842
843     r->substrs = 0;                     /* Useful during FAIL. */
844     r->startp = 0;                      /* Useful during FAIL. */
845     r->endp = 0;                        /* Useful during FAIL. */
846
847     PL_regcomp_rx = r;
848
849     /* Second pass: emit code. */
850     PL_regcomp_parse = exp;
851     PL_regxend = xend;
852     PL_regnaughty = 0;
853     PL_regnpar = 1;
854     PL_regcode = r->program;
855     /* Store the count of eval-groups for security checks: */
856     PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
857     regc((U8)REG_MAGIC, (char*) PL_regcode++);
858     r->data = 0;
859     if (reg(0, &flags) == NULL)
860         return(NULL);
861
862     /* Dig out information for optimizations. */
863     r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
864     pm->op_pmflags = PL_regflags;
865     if (UTF)
866         r->reganch |= ROPT_UTF8;
867     r->regstclass = NULL;
868     if (PL_regnaughty >= 10)    /* Probably an expensive pattern. */
869         r->reganch |= ROPT_NAUGHTY;
870     scan = r->program + 1;              /* First BRANCH. */
871
872     /* XXXX To minimize changes to RE engine we always allocate
873        3-units-long substrs field. */
874     Newz(1004, r->substrs, 1, struct reg_substr_data);
875
876     if (OP(scan) != BRANCH) {   /* Only one top-level choice. */
877         scan_data_t data;
878         I32 fake;
879         STRLEN longest_float_length, longest_fixed_length;
880
881         StructCopy(&zero_scan_data, &data, scan_data_t);
882         first = scan;
883         /* Skip introductions and multiplicators >= 1. */
884         while ((OP(first) == OPEN && (sawopen = 1)) ||
885             (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
886             (OP(first) == PLUS) ||
887             (OP(first) == MINMOD) ||
888             (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
889                 if (OP(first) == PLUS)
890                     sawplus = 1;
891                 else
892                     first += regarglen[(U8)OP(first)];
893                 first = NEXTOPER(first);
894         }
895
896         /* Starting-point info. */
897       again:
898         if (OP(first) == EXACT);        /* Empty, get anchored substr later. */
899         else if (strchr((char*)PL_simple+4,OP(first)))
900             r->regstclass = first;
901         else if (PL_regkind[(U8)OP(first)] == BOUND ||
902                  PL_regkind[(U8)OP(first)] == NBOUND)
903             r->regstclass = first;
904         else if (PL_regkind[(U8)OP(first)] == BOL) {
905             r->reganch |= (OP(first) == MBOL
906                            ? ROPT_ANCH_MBOL
907                            : (OP(first) == SBOL
908                               ? ROPT_ANCH_SBOL
909                               : ROPT_ANCH_BOL));
910             first = NEXTOPER(first);
911             goto again;
912         }
913         else if (OP(first) == GPOS) {
914             r->reganch |= ROPT_ANCH_GPOS;
915             first = NEXTOPER(first);
916             goto again;
917         }
918         else if ((OP(first) == STAR &&
919             PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
920             !(r->reganch & ROPT_ANCH) )
921         {
922             /* turn .* into ^.* with an implied $*=1 */
923             int type = OP(NEXTOPER(first));
924
925             if (type == REG_ANY || type == ANYUTF8)
926                 type = ROPT_ANCH_MBOL;
927             else
928                 type = ROPT_ANCH_SBOL;
929
930             r->reganch |= type | ROPT_IMPLICIT;
931             first = NEXTOPER(first);
932             goto again;
933         }
934         if (sawplus && (!sawopen || !PL_regsawback) 
935             && !(PL_regseen & REG_SEEN_EVAL)) /* May examine pos and $& */
936             /* x+ must match at the 1st pos of run of x's */
937             r->reganch |= ROPT_SKIP;
938
939         /* Scan is after the zeroth branch, first is atomic matcher. */
940         DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n", 
941                               first - scan + 1));
942         /*
943         * If there's something expensive in the r.e., find the
944         * longest literal string that must appear and make it the
945         * regmust.  Resolve ties in favor of later strings, since
946         * the regstart check works with the beginning of the r.e.
947         * and avoiding duplication strengthens checking.  Not a
948         * strong reason, but sufficient in the absence of others.
949         * [Now we resolve ties in favor of the earlier string if
950         * it happens that c_offset_min has been invalidated, since the
951         * earlier string may buy us something the later one won't.]
952         */
953         minlen = 0;
954
955         data.longest_fixed = newSVpvn("",0);
956         data.longest_float = newSVpvn("",0);
957         data.last_found = newSVpvn("",0);
958         data.longest = &(data.longest_fixed);
959         first = scan;
960         
961         minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
962                              &data, SCF_DO_SUBSTR);
963         if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
964              && data.last_start_min == 0 && data.last_end > 0 
965              && !PL_seen_zerolen
966              && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
967             r->reganch |= ROPT_CHECK_ALL;
968         scan_commit(&data);
969         SvREFCNT_dec(data.last_found);
970
971         longest_float_length = CHR_SVLEN(data.longest_float);
972         if (longest_float_length
973             || (data.flags & SF_FL_BEFORE_EOL
974                 && (!(data.flags & SF_FL_BEFORE_MEOL)
975                     || (PL_regflags & PMf_MULTILINE)))) {
976             int t;
977
978             if (SvCUR(data.longest_fixed)                       /* ok to leave SvCUR */
979                 && data.offset_fixed == data.offset_float_min
980                 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
981                     goto remove_float;          /* As in (a)+. */
982
983             r->float_substr = data.longest_float;
984             r->float_min_offset = data.offset_float_min;
985             r->float_max_offset = data.offset_float_max;
986             t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
987                        && (!(data.flags & SF_FL_BEFORE_MEOL)
988                            || (PL_regflags & PMf_MULTILINE)));
989             fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
990         }
991         else {
992           remove_float:
993             r->float_substr = Nullsv;
994             SvREFCNT_dec(data.longest_float);
995             longest_float_length = 0;
996         }
997
998         longest_fixed_length = CHR_SVLEN(data.longest_fixed);
999         if (longest_fixed_length
1000             || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1001                 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1002                     || (PL_regflags & PMf_MULTILINE)))) {
1003             int t;
1004
1005             r->anchored_substr = data.longest_fixed;
1006             r->anchored_offset = data.offset_fixed;
1007             t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
1008                  && (!(data.flags & SF_FIX_BEFORE_MEOL)
1009                      || (PL_regflags & PMf_MULTILINE)));
1010             fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
1011         }
1012         else {
1013             r->anchored_substr = Nullsv;
1014             SvREFCNT_dec(data.longest_fixed);
1015             longest_fixed_length = 0;
1016         }
1017
1018         /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1019         if (longest_fixed_length > longest_float_length) {
1020             r->check_substr = r->anchored_substr;
1021             r->check_offset_min = r->check_offset_max = r->anchored_offset;
1022             if (r->reganch & ROPT_ANCH_SINGLE)
1023                 r->reganch |= ROPT_NOSCAN;
1024         }
1025         else {
1026             r->check_substr = r->float_substr;
1027             r->check_offset_min = data.offset_float_min;
1028             r->check_offset_max = data.offset_float_max;
1029         }
1030         if (r->check_substr) {
1031             r->reganch |= RE_USE_INTUIT;
1032             if (SvTAIL(r->check_substr))
1033                 r->reganch |= RE_INTUIT_TAIL;
1034         }
1035     }
1036     else {
1037         /* Several toplevels. Best we can is to set minlen. */
1038         I32 fake;
1039         
1040         DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1041         scan = r->program + 1;
1042         minlen = study_chunk(&scan, &fake, scan + PL_regsize, NULL, 0);
1043         r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1044     }
1045
1046     r->minlen = minlen;
1047     if (PL_regseen & REG_SEEN_GPOS) 
1048         r->reganch |= ROPT_GPOS_SEEN;
1049     if (PL_regseen & REG_SEEN_LOOKBEHIND)
1050         r->reganch |= ROPT_LOOKBEHIND_SEEN;
1051     if (PL_regseen & REG_SEEN_EVAL)
1052         r->reganch |= ROPT_EVAL_SEEN;
1053     Newz(1002, r->startp, PL_regnpar, I32);
1054     Newz(1002, r->endp, PL_regnpar, I32);
1055     DEBUG_r(regdump(r));
1056     return(r);
1057 }
1058
1059 /*
1060  - reg - regular expression, i.e. main body or parenthesized thing
1061  *
1062  * Caller must absorb opening parenthesis.
1063  *
1064  * Combining parenthesis handling with the base level of regular expression
1065  * is a trifle forced, but the need to tie the tails of the branches to what
1066  * follows makes it hard to avoid.
1067  */
1068 STATIC regnode *
1069 S_reg(pTHX_ I32 paren, I32 *flagp)
1070     /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1071 {
1072     dTHR;
1073     register regnode *ret;              /* Will be the head of the group. */
1074     register regnode *br;
1075     register regnode *lastbr;
1076     register regnode *ender = 0;
1077     register I32 parno = 0;
1078     I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
1079     char c;
1080
1081     *flagp = 0;                         /* Tentatively. */
1082
1083     /* Make an OPEN node, if parenthesized. */
1084     if (paren) {
1085         if (*PL_regcomp_parse == '?') {
1086             U16 posflags = 0, negflags = 0;
1087             U16 *flagsp = &posflags;
1088             int logical = 0;
1089
1090             PL_regcomp_parse++;
1091             paren = *PL_regcomp_parse++;
1092             ret = NULL;                 /* For look-ahead/behind. */
1093             switch (paren) {
1094             case '<':
1095                 PL_regseen |= REG_SEEN_LOOKBEHIND;
1096                 if (*PL_regcomp_parse == '!') 
1097                     paren = ',';
1098                 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!') 
1099                     goto unknown;
1100                 PL_regcomp_parse++;
1101             case '=':
1102             case '!':
1103                 PL_seen_zerolen++;
1104             case ':':
1105             case '>':
1106                 break;
1107             case '$':
1108             case '@':
1109                 FAIL2("Sequence (?%c...) not implemented", (int)paren);
1110                 break;
1111             case '#':
1112                 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1113                     PL_regcomp_parse++;
1114                 if (*PL_regcomp_parse != ')')
1115                     FAIL("Sequence (?#... not terminated");
1116                 nextchar();
1117                 *flagp = TRYAGAIN;
1118                 return NULL;
1119             case 'p':
1120                 logical = 1;
1121                 paren = *PL_regcomp_parse++;
1122                 /* FALL THROUGH */
1123             case '{':
1124             {
1125                 dTHR;
1126                 I32 count = 1, n = 0;
1127                 char c;
1128                 char *s = PL_regcomp_parse;
1129                 SV *sv;
1130                 OP_4tree *sop, *rop;
1131
1132                 PL_seen_zerolen++;
1133                 PL_regseen |= REG_SEEN_EVAL;
1134                 while (count && (c = *PL_regcomp_parse)) {
1135                     if (c == '\\' && PL_regcomp_parse[1])
1136                         PL_regcomp_parse++;
1137                     else if (c == '{') 
1138                         count++;
1139                     else if (c == '}') 
1140                         count--;
1141                     PL_regcomp_parse++;
1142                 }
1143                 if (*PL_regcomp_parse != ')')
1144                     FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1145                 if (!SIZE_ONLY) {
1146                     AV *av;
1147                     
1148                     if (PL_regcomp_parse - 1 - s) 
1149                         sv = newSVpvn(s, PL_regcomp_parse - 1 - s);
1150                     else
1151                         sv = newSVpvn("", 0);
1152
1153                     rop = sv_compile_2op(sv, &sop, "re", &av);
1154
1155                     n = add_data(3, "nop");
1156                     PL_regcomp_rx->data->data[n] = (void*)rop;
1157                     PL_regcomp_rx->data->data[n+1] = (void*)sop;
1158                     PL_regcomp_rx->data->data[n+2] = (void*)av;
1159                     SvREFCNT_dec(sv);
1160                 }
1161                 else {                                          /* First pass */
1162                     if (PL_reginterp_cnt < ++PL_seen_evals
1163                         && PL_curcop != &PL_compiling)
1164                         /* No compiled RE interpolated, has runtime
1165                            components ===> unsafe.  */
1166                         FAIL("Eval-group not allowed at runtime, use re 'eval'");
1167                     if (PL_tainted)
1168                         FAIL("Eval-group in insecure regular expression");
1169                 }
1170                 
1171                 nextchar();
1172                 if (logical) {
1173                     ret = reg_node(LOGICAL);
1174                     if (!SIZE_ONLY)
1175                         ret->flags = 2;
1176                     regtail(ret, reganode(EVAL, n));
1177                     return ret;
1178                 }
1179                 return reganode(EVAL, n);
1180             }
1181             case '(':
1182             {
1183                 if (PL_regcomp_parse[0] == '?') {
1184                     if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!' 
1185                         || PL_regcomp_parse[1] == '<' 
1186                         || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
1187                         I32 flag;
1188                         
1189                         ret = reg_node(LOGICAL);
1190                         if (!SIZE_ONLY)
1191                             ret->flags = 1;
1192                         regtail(ret, reg(1, &flag));
1193                         goto insert_if;
1194                     } 
1195                 }
1196                 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
1197                     parno = atoi(PL_regcomp_parse++);
1198
1199                     while (isDIGIT(*PL_regcomp_parse))
1200                         PL_regcomp_parse++;
1201                     ret = reganode(GROUPP, parno);
1202                     if ((c = *nextchar()) != ')')
1203                         FAIL2("Switch (?(number%c not recognized", c);
1204                   insert_if:
1205                     regtail(ret, reganode(IFTHEN, 0));
1206                     br = regbranch(&flags, 1);
1207                     if (br == NULL)
1208                         br = reganode(LONGJMP, 0);
1209                     else
1210                         regtail(br, reganode(LONGJMP, 0));
1211                     c = *nextchar();
1212                     if (flags&HASWIDTH)
1213                         *flagp |= HASWIDTH;
1214                     if (c == '|') {
1215                         lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1216                         regbranch(&flags, 1);
1217                         regtail(ret, lastbr);
1218                         if (flags&HASWIDTH)
1219                             *flagp |= HASWIDTH;
1220                         c = *nextchar();
1221                     }
1222                     else
1223                         lastbr = NULL;
1224                     if (c != ')')
1225                         FAIL("Switch (?(condition)... contains too many branches");
1226                     ender = reg_node(TAIL);
1227                     regtail(br, ender);
1228                     if (lastbr) {
1229                         regtail(lastbr, ender);
1230                         regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1231                     }
1232                     else
1233                         regtail(ret, ender);
1234                     return ret;
1235                 }
1236                 else {
1237                     FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
1238                 }
1239             }
1240             case 0:
1241                 FAIL("Sequence (? incomplete");
1242                 break;
1243             default:
1244                 --PL_regcomp_parse;
1245               parse_flags:
1246                 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1247                     if (*PL_regcomp_parse != 'o')
1248                         pmflag(flagsp, *PL_regcomp_parse);
1249                     ++PL_regcomp_parse;
1250                 }
1251                 if (*PL_regcomp_parse == '-') {
1252                     flagsp = &negflags;
1253                     ++PL_regcomp_parse;
1254                     goto parse_flags;
1255                 }
1256                 PL_regflags |= posflags;
1257                 PL_regflags &= ~negflags;
1258                 if (*PL_regcomp_parse == ':') {
1259                     PL_regcomp_parse++;
1260                     paren = ':';
1261                     break;
1262                 }               
1263               unknown:
1264                 if (*PL_regcomp_parse != ')')
1265                     FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
1266                 nextchar();
1267                 *flagp = TRYAGAIN;
1268                 return NULL;
1269             }
1270         }
1271         else {
1272             parno = PL_regnpar;
1273             PL_regnpar++;
1274             ret = reganode(OPEN, parno);
1275             open = 1;
1276         }
1277     }
1278     else
1279         ret = NULL;
1280
1281     /* Pick up the branches, linking them together. */
1282     br = regbranch(&flags, 1);
1283     if (br == NULL)
1284         return(NULL);
1285     if (*PL_regcomp_parse == '|') {
1286         if (!SIZE_ONLY && PL_extralen) {
1287             reginsert(BRANCHJ, br);
1288         }
1289         else
1290             reginsert(BRANCH, br);
1291         have_branch = 1;
1292         if (SIZE_ONLY)
1293             PL_extralen += 1;           /* For BRANCHJ-BRANCH. */
1294     }
1295     else if (paren == ':') {
1296         *flagp |= flags&SIMPLE;
1297     }
1298     if (open) {                         /* Starts with OPEN. */
1299         regtail(ret, br);               /* OPEN -> first. */
1300     }
1301     else if (paren != '?')              /* Not Conditional */
1302         ret = br;
1303     if (flags&HASWIDTH)
1304         *flagp |= HASWIDTH;
1305     *flagp |= flags&SPSTART;
1306     lastbr = br;
1307     while (*PL_regcomp_parse == '|') {
1308         if (!SIZE_ONLY && PL_extralen) {
1309             ender = reganode(LONGJMP,0);
1310             regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1311         }
1312         if (SIZE_ONLY)
1313             PL_extralen += 2;           /* Account for LONGJMP. */
1314         nextchar();
1315         br = regbranch(&flags, 0);
1316         if (br == NULL)
1317             return(NULL);
1318         regtail(lastbr, br);            /* BRANCH -> BRANCH. */
1319         lastbr = br;
1320         if (flags&HASWIDTH)
1321             *flagp |= HASWIDTH;
1322         *flagp |= flags&SPSTART;
1323     }
1324
1325     if (have_branch || paren != ':') {
1326         /* Make a closing node, and hook it on the end. */
1327         switch (paren) {
1328         case ':':
1329             ender = reg_node(TAIL);
1330             break;
1331         case 1:
1332             ender = reganode(CLOSE, parno);
1333             break;
1334         case '<':
1335         case ',':
1336         case '=':
1337         case '!':
1338             *flagp &= ~HASWIDTH;
1339             /* FALL THROUGH */
1340         case '>':
1341             ender = reg_node(SUCCEED);
1342             break;
1343         case 0:
1344             ender = reg_node(END);
1345             break;
1346         }
1347         regtail(lastbr, ender);
1348
1349         if (have_branch) {
1350             /* Hook the tails of the branches to the closing node. */
1351             for (br = ret; br != NULL; br = regnext(br)) {
1352                 regoptail(br, ender);
1353             }
1354         }
1355     }
1356
1357     {
1358         char *p;
1359         static char parens[] = "=!<,>";
1360
1361         if (paren && (p = strchr(parens, paren))) {
1362             int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1363             int flag = (p - parens) > 1;
1364
1365             if (paren == '>')
1366                 node = SUSPEND, flag = 0;
1367             reginsert(node,ret);
1368             ret->flags = flag;
1369             regtail(ret, reg_node(TAIL));
1370         }
1371     }
1372
1373     /* Check for proper termination. */
1374     if (paren) {
1375         PL_regflags = oregflags;
1376         if (PL_regcomp_parse >= PL_regxend || *nextchar() != ')') {
1377             FAIL("unmatched () in regexp");
1378         }
1379     }
1380     else if (!paren && PL_regcomp_parse < PL_regxend) {
1381         if (*PL_regcomp_parse == ')') {
1382             FAIL("unmatched () in regexp");
1383         }
1384         else
1385             FAIL("junk on end of regexp");      /* "Can't happen". */
1386         /* NOTREACHED */
1387     }
1388
1389     return(ret);
1390 }
1391
1392 /*
1393  - regbranch - one alternative of an | operator
1394  *
1395  * Implements the concatenation operator.
1396  */
1397 STATIC regnode *
1398 S_regbranch(pTHX_ I32 *flagp, I32 first)
1399 {
1400     dTHR;
1401     register regnode *ret;
1402     register regnode *chain = NULL;
1403     register regnode *latest;
1404     I32 flags = 0, c = 0;
1405
1406     if (first) 
1407         ret = NULL;
1408     else {
1409         if (!SIZE_ONLY && PL_extralen) 
1410             ret = reganode(BRANCHJ,0);
1411         else
1412             ret = reg_node(BRANCH);
1413     }
1414         
1415     if (!first && SIZE_ONLY) 
1416         PL_extralen += 1;                       /* BRANCHJ */
1417     
1418     *flagp = WORST;                     /* Tentatively. */
1419
1420     PL_regcomp_parse--;
1421     nextchar();
1422     while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
1423         flags &= ~TRYAGAIN;
1424         latest = regpiece(&flags);
1425         if (latest == NULL) {
1426             if (flags & TRYAGAIN)
1427                 continue;
1428             return(NULL);
1429         }
1430         else if (ret == NULL)
1431             ret = latest;
1432         *flagp |= flags&HASWIDTH;
1433         if (chain == NULL)      /* First piece. */
1434             *flagp |= flags&SPSTART;
1435         else {
1436             PL_regnaughty++;
1437             regtail(chain, latest);
1438         }
1439         chain = latest;
1440         c++;
1441     }
1442     if (chain == NULL) {        /* Loop ran zero times. */
1443         chain = reg_node(NOTHING);
1444         if (ret == NULL)
1445             ret = chain;
1446     }
1447     if (c == 1) {
1448         *flagp |= flags&SIMPLE;
1449     }
1450
1451     return(ret);
1452 }
1453
1454 /*
1455  - regpiece - something followed by possible [*+?]
1456  *
1457  * Note that the branching code sequences used for ? and the general cases
1458  * of * and + are somewhat optimized:  they use the same NOTHING node as
1459  * both the endmarker for their branch list and the body of the last branch.
1460  * It might seem that this node could be dispensed with entirely, but the
1461  * endmarker role is not redundant.
1462  */
1463 STATIC regnode *
1464 S_regpiece(pTHX_ I32 *flagp)
1465 {
1466     dTHR;
1467     register regnode *ret;
1468     register char op;
1469     register char *next;
1470     I32 flags;
1471     char *origparse = PL_regcomp_parse;
1472     char *maxpos;
1473     I32 min;
1474     I32 max = REG_INFTY;
1475
1476     ret = regatom(&flags);
1477     if (ret == NULL) {
1478         if (flags & TRYAGAIN)
1479             *flagp |= TRYAGAIN;
1480         return(NULL);
1481     }
1482
1483     op = *PL_regcomp_parse;
1484
1485     if (op == '{' && regcurly(PL_regcomp_parse)) {
1486         next = PL_regcomp_parse + 1;
1487         maxpos = Nullch;
1488         while (isDIGIT(*next) || *next == ',') {
1489             if (*next == ',') {
1490                 if (maxpos)
1491                     break;
1492                 else
1493                     maxpos = next;
1494             }
1495             next++;
1496         }
1497         if (*next == '}') {             /* got one */
1498             if (!maxpos)
1499                 maxpos = next;
1500             PL_regcomp_parse++;
1501             min = atoi(PL_regcomp_parse);
1502             if (*maxpos == ',')
1503                 maxpos++;
1504             else
1505                 maxpos = PL_regcomp_parse;
1506             max = atoi(maxpos);
1507             if (!max && *maxpos != '0')
1508                 max = REG_INFTY;                /* meaning "infinity" */
1509             else if (max >= REG_INFTY)
1510                 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
1511             PL_regcomp_parse = next;
1512             nextchar();
1513
1514         do_curly:
1515             if ((flags&SIMPLE)) {
1516                 PL_regnaughty += 2 + PL_regnaughty / 2;
1517                 reginsert(CURLY, ret);
1518             }
1519             else {
1520                 PL_regnaughty += 4 + PL_regnaughty;     /* compound interest */
1521                 regtail(ret, reg_node(WHILEM));
1522                 if (!SIZE_ONLY && PL_extralen) {
1523                     reginsert(LONGJMP,ret);
1524                     reginsert(NOTHING,ret);
1525                     NEXT_OFF(ret) = 3;  /* Go over LONGJMP. */
1526                 }
1527                 reginsert(CURLYX,ret);
1528                 if (!SIZE_ONLY && PL_extralen)
1529                     NEXT_OFF(ret) = 3;  /* Go over NOTHING to LONGJMP. */
1530                 regtail(ret, reg_node(NOTHING));
1531                 if (SIZE_ONLY)
1532                     PL_extralen += 3;
1533             }
1534             ret->flags = 0;
1535
1536             if (min > 0)
1537                 *flagp = WORST;
1538             if (max > 0)
1539                 *flagp |= HASWIDTH;
1540             if (max && max < min)
1541                 FAIL("Can't do {n,m} with n > m");
1542             if (!SIZE_ONLY) {
1543                 ARG1_SET(ret, min);
1544                 ARG2_SET(ret, max);
1545             }
1546
1547             goto nest_check;
1548         }
1549     }
1550
1551     if (!ISMULT1(op)) {
1552         *flagp = flags;
1553         return(ret);
1554     }
1555
1556 #if 0                           /* Now runtime fix should be reliable. */
1557     if (!(flags&HASWIDTH) && op != '?')
1558       FAIL("regexp *+ operand could be empty");
1559 #endif 
1560
1561     nextchar();
1562
1563     *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
1564
1565     if (op == '*' && (flags&SIMPLE)) {
1566         reginsert(STAR, ret);
1567         ret->flags = 0;
1568         PL_regnaughty += 4;
1569     }
1570     else if (op == '*') {
1571         min = 0;
1572         goto do_curly;
1573     }
1574     else if (op == '+' && (flags&SIMPLE)) {
1575         reginsert(PLUS, ret);
1576         ret->flags = 0;
1577         PL_regnaughty += 3;
1578     }
1579     else if (op == '+') {
1580         min = 1;
1581         goto do_curly;
1582     }
1583     else if (op == '?') {
1584         min = 0; max = 1;
1585         goto do_curly;
1586     }
1587   nest_check:
1588     if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
1589         Perl_warner(aTHX_ WARN_UNSAFE, "%.*s matches null string many times",
1590             PL_regcomp_parse - origparse, origparse);
1591     }
1592
1593     if (*PL_regcomp_parse == '?') {
1594         nextchar();
1595         reginsert(MINMOD, ret);
1596         regtail(ret, ret + NODE_STEP_REGNODE);
1597     }
1598     if (ISMULT2(PL_regcomp_parse))
1599         FAIL("nested *?+ in regexp");
1600
1601     return(ret);
1602 }
1603
1604 /*
1605  - regatom - the lowest level
1606  *
1607  * Optimization:  gobbles an entire sequence of ordinary characters so that
1608  * it can turn them into a single node, which is smaller to store and
1609  * faster to run.  Backslashed characters are exceptions, each becoming a
1610  * separate node; the code is simpler that way and it's not worth fixing.
1611  *
1612  * [Yes, it is worth fixing, some scripts can run twice the speed.]
1613  */
1614 STATIC regnode *
1615 S_regatom(pTHX_ I32 *flagp)
1616 {
1617     dTHR;
1618     register regnode *ret = 0;
1619     I32 flags;
1620
1621     *flagp = WORST;             /* Tentatively. */
1622
1623 tryagain:
1624     switch (*PL_regcomp_parse) {
1625     case '^':
1626         PL_seen_zerolen++;
1627         nextchar();
1628         if (PL_regflags & PMf_MULTILINE)
1629             ret = reg_node(MBOL);
1630         else if (PL_regflags & PMf_SINGLELINE)
1631             ret = reg_node(SBOL);
1632         else
1633             ret = reg_node(BOL);
1634         break;
1635     case '$':
1636         if (PL_regcomp_parse[1]) 
1637             PL_seen_zerolen++;
1638         nextchar();
1639         if (PL_regflags & PMf_MULTILINE)
1640             ret = reg_node(MEOL);
1641         else if (PL_regflags & PMf_SINGLELINE)
1642             ret = reg_node(SEOL);
1643         else
1644             ret = reg_node(EOL);
1645         break;
1646     case '.':
1647         nextchar();
1648         if (UTF) {
1649             if (PL_regflags & PMf_SINGLELINE)
1650                 ret = reg_node(SANYUTF8);
1651             else
1652                 ret = reg_node(ANYUTF8);
1653             *flagp |= HASWIDTH;
1654         }
1655         else {
1656             if (PL_regflags & PMf_SINGLELINE)
1657                 ret = reg_node(SANY);
1658             else
1659                 ret = reg_node(REG_ANY);
1660             *flagp |= HASWIDTH|SIMPLE;
1661         }
1662         PL_regnaughty++;
1663         break;
1664     case '[':
1665         PL_regcomp_parse++;
1666         ret = (UTF ? regclassutf8() : regclass());
1667         if (*PL_regcomp_parse != ']')
1668             FAIL("unmatched [] in regexp");
1669         nextchar();
1670         *flagp |= HASWIDTH|SIMPLE;
1671         break;
1672     case '(':
1673         nextchar();
1674         ret = reg(1, &flags);
1675         if (ret == NULL) {
1676                 if (flags & TRYAGAIN)
1677                     goto tryagain;
1678                 return(NULL);
1679         }
1680         *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
1681         break;
1682     case '|':
1683     case ')':
1684         if (flags & TRYAGAIN) {
1685             *flagp |= TRYAGAIN;
1686             return NULL;
1687         }
1688         FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
1689                                 /* Supposed to be caught earlier. */
1690         break;
1691     case '{':
1692         if (!regcurly(PL_regcomp_parse)) {
1693             PL_regcomp_parse++;
1694             goto defchar;
1695         }
1696         /* FALL THROUGH */
1697     case '?':
1698     case '+':
1699     case '*':
1700         FAIL("?+*{} follows nothing in regexp");
1701         break;
1702     case '\\':
1703         switch (*++PL_regcomp_parse) {
1704         case 'A':
1705             PL_seen_zerolen++;
1706             ret = reg_node(SBOL);
1707             *flagp |= SIMPLE;
1708             nextchar();
1709             break;
1710         case 'G':
1711             ret = reg_node(GPOS);
1712             PL_regseen |= REG_SEEN_GPOS;
1713             *flagp |= SIMPLE;
1714             nextchar();
1715             break;
1716         case 'Z':
1717             ret = reg_node(SEOL);
1718             *flagp |= SIMPLE;
1719             nextchar();
1720             break;
1721         case 'z':
1722             ret = reg_node(EOS);
1723             *flagp |= SIMPLE;
1724             PL_seen_zerolen++;          /* Do not optimize RE away */
1725             nextchar();
1726             break;
1727         case 'C':
1728             ret = reg_node(SANY);
1729             *flagp |= HASWIDTH|SIMPLE;
1730             nextchar();
1731             break;
1732         case 'X':
1733             ret = reg_node(CLUMP);
1734             *flagp |= HASWIDTH;
1735             nextchar();
1736             if (UTF && !PL_utf8_mark)
1737                 is_utf8_mark((U8*)"~");         /* preload table */
1738             break;
1739         case 'w':
1740             ret = reg_node(
1741                 UTF
1742                     ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
1743                     : (LOC ? ALNUML     : ALNUM));
1744             *flagp |= HASWIDTH|SIMPLE;
1745             nextchar();
1746             if (UTF && !PL_utf8_alnum)
1747                 is_utf8_alnum((U8*)"a");        /* preload table */
1748             break;
1749         case 'W':
1750             ret = reg_node(
1751                 UTF
1752                     ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
1753                     : (LOC ? NALNUML     : NALNUM));
1754             *flagp |= HASWIDTH|SIMPLE;
1755             nextchar();
1756             if (UTF && !PL_utf8_alnum)
1757                 is_utf8_alnum((U8*)"a");        /* preload table */
1758             break;
1759         case 'b':
1760             PL_seen_zerolen++;
1761             PL_regseen |= REG_SEEN_LOOKBEHIND;
1762             ret = reg_node(
1763                 UTF
1764                     ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
1765                     : (LOC ? BOUNDL     : BOUND));
1766             *flagp |= SIMPLE;
1767             nextchar();
1768             if (UTF && !PL_utf8_alnum)
1769                 is_utf8_alnum((U8*)"a");        /* preload table */
1770             break;
1771         case 'B':
1772             PL_seen_zerolen++;
1773             PL_regseen |= REG_SEEN_LOOKBEHIND;
1774             ret = reg_node(
1775                 UTF
1776                     ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
1777                     : (LOC ? NBOUNDL     : NBOUND));
1778             *flagp |= SIMPLE;
1779             nextchar();
1780             if (UTF && !PL_utf8_alnum)
1781                 is_utf8_alnum((U8*)"a");        /* preload table */
1782             break;
1783         case 's':
1784             ret = reg_node(
1785                 UTF
1786                     ? (LOC ? SPACELUTF8 : SPACEUTF8)
1787                     : (LOC ? SPACEL     : SPACE));
1788             *flagp |= HASWIDTH|SIMPLE;
1789             nextchar();
1790             if (UTF && !PL_utf8_space)
1791                 is_utf8_space((U8*)" ");        /* preload table */
1792             break;
1793         case 'S':
1794             ret = reg_node(
1795                 UTF
1796                     ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
1797                     : (LOC ? NSPACEL     : NSPACE));
1798             *flagp |= HASWIDTH|SIMPLE;
1799             nextchar();
1800             if (UTF && !PL_utf8_space)
1801                 is_utf8_space((U8*)" ");        /* preload table */
1802             break;
1803         case 'd':
1804             ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
1805             *flagp |= HASWIDTH|SIMPLE;
1806             nextchar();
1807             if (UTF && !PL_utf8_digit)
1808                 is_utf8_digit((U8*)"1");        /* preload table */
1809             break;
1810         case 'D':
1811             ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
1812             *flagp |= HASWIDTH|SIMPLE;
1813             nextchar();
1814             if (UTF && !PL_utf8_digit)
1815                 is_utf8_digit((U8*)"1");        /* preload table */
1816             break;
1817         case 'p':
1818         case 'P':
1819             {   /* a lovely hack--pretend we saw [\pX] instead */
1820                 char* oldregxend = PL_regxend;
1821
1822                 if (PL_regcomp_parse[1] == '{') {
1823                     PL_regxend = strchr(PL_regcomp_parse, '}');
1824                     if (!PL_regxend)
1825                         FAIL("Missing right brace on \\p{}");
1826                     PL_regxend++;
1827                 }
1828                 else
1829                     PL_regxend = PL_regcomp_parse + 2;
1830                 PL_regcomp_parse--;
1831
1832                 ret = regclassutf8();
1833
1834                 PL_regxend = oldregxend;
1835                 PL_regcomp_parse--;
1836                 nextchar();
1837                 *flagp |= HASWIDTH|SIMPLE;
1838             }
1839             break;
1840         case 'n':
1841         case 'r':
1842         case 't':
1843         case 'f':
1844         case 'e':
1845         case 'a':
1846         case 'x':
1847         case 'c':
1848         case '0':
1849             goto defchar;
1850         case '1': case '2': case '3': case '4':
1851         case '5': case '6': case '7': case '8': case '9':
1852             {
1853                 I32 num = atoi(PL_regcomp_parse);
1854
1855                 if (num > 9 && num >= PL_regnpar)
1856                     goto defchar;
1857                 else {
1858                     if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
1859                         FAIL("reference to nonexistent group");
1860                     PL_regsawback = 1;
1861                     ret = reganode(FOLD
1862                                    ? (LOC ? REFFL : REFF)
1863                                    : REF, num);
1864                     *flagp |= HASWIDTH;
1865                     while (isDIGIT(*PL_regcomp_parse))
1866                         PL_regcomp_parse++;
1867                     PL_regcomp_parse--;
1868                     nextchar();
1869                 }
1870             }
1871             break;
1872         case '\0':
1873             if (PL_regcomp_parse >= PL_regxend)
1874                 FAIL("trailing \\ in regexp");
1875             /* FALL THROUGH */
1876         default:
1877             /* Do not generate `unrecognized' warnings here, we fall
1878                back into the quick-grab loop below */
1879             goto defchar;
1880         }
1881         break;
1882
1883     case '#':
1884         if (PL_regflags & PMf_EXTENDED) {
1885             while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1886             if (PL_regcomp_parse < PL_regxend)
1887                 goto tryagain;
1888         }
1889         /* FALL THROUGH */
1890
1891     default: {
1892             register I32 len;
1893             register UV ender;
1894             register char *p;
1895             char *oldp, *s;
1896             I32 numlen;
1897
1898             PL_regcomp_parse++;
1899
1900         defchar:
1901             ret = reg_node(FOLD
1902                           ? (LOC ? EXACTFL : EXACTF)
1903                           : EXACT);
1904             s = (char *) OPERAND(ret);
1905             regc(0, s++);               /* save spot for len */
1906             for (len = 0, p = PL_regcomp_parse - 1;
1907               len < 127 && p < PL_regxend;
1908               len++)
1909             {
1910                 oldp = p;
1911
1912                 if (PL_regflags & PMf_EXTENDED)
1913                     p = regwhite(p, PL_regxend);
1914                 switch (*p) {
1915                 case '^':
1916                 case '$':
1917                 case '.':
1918                 case '[':
1919                 case '(':
1920                 case ')':
1921                 case '|':
1922                     goto loopdone;
1923                 case '\\':
1924                     switch (*++p) {
1925                     case 'A':
1926                     case 'G':
1927                     case 'Z':
1928                     case 'z':
1929                     case 'w':
1930                     case 'W':
1931                     case 'b':
1932                     case 'B':
1933                     case 's':
1934                     case 'S':
1935                     case 'd':
1936                     case 'D':
1937                     case 'p':
1938                     case 'P':
1939                         --p;
1940                         goto loopdone;
1941                     case 'n':
1942                         ender = '\n';
1943                         p++;
1944                         break;
1945                     case 'r':
1946                         ender = '\r';
1947                         p++;
1948                         break;
1949                     case 't':
1950                         ender = '\t';
1951                         p++;
1952                         break;
1953                     case 'f':
1954                         ender = '\f';
1955                         p++;
1956                         break;
1957                     case 'e':
1958                         ender = '\033';
1959                         p++;
1960                         break;
1961                     case 'a':
1962                         ender = '\007';
1963                         p++;
1964                         break;
1965                     case 'x':
1966                         if (*++p == '{') {
1967                             char* e = strchr(p, '}');
1968          
1969                             if (!e)
1970                                 FAIL("Missing right brace on \\x{}");
1971                             else if (UTF) {
1972                                 ender = scan_hex(p + 1, e - p, &numlen);
1973                                 if (numlen + len >= 127) {      /* numlen is generous */
1974                                     p--;
1975                                     goto loopdone;
1976                                 }
1977                                 p = e + 1;
1978                             }
1979                             else
1980                                 FAIL("Can't use \\x{} without 'use utf8' declaration");
1981                         }
1982                         else {
1983                             ender = scan_hex(p, 2, &numlen);
1984                             p += numlen;
1985                         }
1986                         break;
1987                     case 'c':
1988                         p++;
1989                         ender = UCHARAT(p++);
1990                         ender = toCTRL(ender);
1991                         break;
1992                     case '0': case '1': case '2': case '3':case '4':
1993                     case '5': case '6': case '7': case '8':case '9':
1994                         if (*p == '0' ||
1995                           (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
1996                             ender = scan_oct(p, 3, &numlen);
1997                             p += numlen;
1998                         }
1999                         else {
2000                             --p;
2001                             goto loopdone;
2002                         }
2003                         break;
2004                     case '\0':
2005                         if (p >= PL_regxend)
2006                             FAIL("trailing \\ in regexp");
2007                         /* FALL THROUGH */
2008                     default:
2009                         if (!SIZE_ONLY && ckWARN(WARN_UNSAFE) && isALPHA(*p))
2010                             Perl_warner(aTHX_ WARN_UNSAFE, 
2011                                    "/%.127s/: Unrecognized escape \\%c passed through",
2012                                    PL_regprecomp,
2013                                    *p);
2014                         goto normal_default;
2015                     }
2016                     break;
2017                 default:
2018                   normal_default:
2019                     if ((*p & 0xc0) == 0xc0 && UTF) {
2020                         ender = utf8_to_uv((U8*)p, &numlen);
2021                         p += numlen;
2022                     }
2023                     else
2024                         ender = *p++;
2025                     break;
2026                 }
2027                 if (PL_regflags & PMf_EXTENDED)
2028                     p = regwhite(p, PL_regxend);
2029                 if (UTF && FOLD) {
2030                     if (LOC)
2031                         ender = toLOWER_LC_uni(ender);
2032                     else
2033                         ender = toLOWER_uni(ender);
2034                 }
2035                 if (ISMULT2(p)) { /* Back off on ?+*. */
2036                     if (len)
2037                         p = oldp;
2038                     else if (ender >= 0x80 && UTF) {
2039                         reguni(ender, s, &numlen);
2040                         s += numlen;
2041                         len += numlen;
2042                     }
2043                     else {
2044                         len++;
2045                         regc(ender, s++);
2046                     }
2047                     break;
2048                 }
2049                 if (ender >= 0x80 && UTF) {
2050                     reguni(ender, s, &numlen);
2051                     s += numlen;
2052                     len += numlen - 1;
2053                 }
2054                 else
2055                     regc(ender, s++);
2056             }
2057         loopdone:
2058             PL_regcomp_parse = p - 1;
2059             nextchar();
2060             if (len < 0)
2061                 FAIL("internal disaster in regexp");
2062             if (len > 0)
2063                 *flagp |= HASWIDTH;
2064             if (len == 1)
2065                 *flagp |= SIMPLE;
2066             if (!SIZE_ONLY)
2067                 *OPERAND(ret) = len;
2068             regc('\0', s++);
2069             if (SIZE_ONLY) {
2070                 PL_regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2071             }
2072             else {
2073                 PL_regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2074             }
2075         }
2076         break;
2077     }
2078
2079     return(ret);
2080 }
2081
2082 STATIC char *
2083 S_regwhite(pTHX_ char *p, char *e)
2084 {
2085     while (p < e) {
2086         if (isSPACE(*p))
2087             ++p;
2088         else if (*p == '#') {
2089             do {
2090                 p++;
2091             } while (p < e && *p != '\n');
2092         }
2093         else
2094             break;
2095     }
2096     return p;
2097 }
2098
2099 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
2100    Character classes ([:foo:]) can also be negated ([:^foo:]).
2101    Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
2102    Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
2103    but trigger warnings because they are currently unimplemented. */
2104 STATIC I32
2105 S_regpposixcc(pTHX_ I32 value)
2106 {
2107     dTHR;
2108     char *posixcc = 0;
2109     I32 namedclass = -1;
2110
2111     if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2112         /* I smell either [: or [= or [. -- POSIX has been here, right? */
2113         (*PL_regcomp_parse == ':' ||
2114          *PL_regcomp_parse == '=' ||
2115          *PL_regcomp_parse == '.')) {
2116         char  c = *PL_regcomp_parse;
2117         char* s = PL_regcomp_parse++;
2118             
2119         while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != c)
2120             PL_regcomp_parse++;
2121         if (PL_regcomp_parse == PL_regxend)
2122             /* Grandfather lone [:, [=, [. */
2123             PL_regcomp_parse = s;
2124         else {
2125             char* t = PL_regcomp_parse++; /* skip over the c */
2126
2127             if (*PL_regcomp_parse == ']') {
2128                 PL_regcomp_parse++; /* skip over the ending ] */
2129                 posixcc = s + 1;
2130                 if (*s == ':') {
2131                     I32 complement = *posixcc == '^' ? *posixcc++ : 0;
2132                     I32 skip = 5; /* the most common skip */
2133
2134                     switch (*posixcc) {
2135                     case 'a':
2136                         if (strnEQ(posixcc, "alnum", 5))
2137                             namedclass =
2138                                 complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
2139                         else if (strnEQ(posixcc, "alpha", 5))
2140                             namedclass =
2141                                 complement ? ANYOF_NALPHA : ANYOF_ALPHA;
2142                         else if (strnEQ(posixcc, "ascii", 5))
2143                             namedclass =
2144                                 complement ? ANYOF_NASCII : ANYOF_ASCII;
2145                         break;
2146                     case 'c':
2147                         if (strnEQ(posixcc, "cntrl", 5))
2148                             namedclass =
2149                                 complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
2150                         break;
2151                     case 'd':
2152                         if (strnEQ(posixcc, "digit", 5))
2153                             namedclass =
2154                                 complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
2155                         break;
2156                     case 'g':
2157                         if (strnEQ(posixcc, "graph", 5))
2158                             namedclass =
2159                                 complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
2160                         break;
2161                     case 'l':
2162                         if (strnEQ(posixcc, "lower", 5))
2163                             namedclass =
2164                                 complement ? ANYOF_NLOWER : ANYOF_LOWER;
2165                         break;
2166                     case 'p':
2167                         if (strnEQ(posixcc, "print", 5))
2168                             namedclass =
2169                                 complement ? ANYOF_NPRINT : ANYOF_PRINT;
2170                         else if (strnEQ(posixcc, "punct", 5))
2171                             namedclass =
2172                                 complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
2173                         break;
2174                     case 's':
2175                         if (strnEQ(posixcc, "space", 5))
2176                             namedclass =
2177                                 complement ? ANYOF_NSPACE : ANYOF_SPACE;
2178                     case 'u':
2179                         if (strnEQ(posixcc, "upper", 5))
2180                             namedclass =
2181                                 complement ? ANYOF_NUPPER : ANYOF_UPPER;
2182                         break;
2183                     case 'w': /* this is not POSIX, this is the Perl \w */
2184                         if (strnEQ(posixcc, "word", 4)) {
2185                             namedclass =
2186                                 complement ? ANYOF_NALNUM : ANYOF_ALNUM;
2187                             skip = 4;
2188                         }
2189                         break;
2190                     case 'x':
2191                         if (strnEQ(posixcc, "xdigit", 6)) {
2192                             namedclass =
2193                                 complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
2194                             skip = 6;
2195                         }
2196                         break;
2197                     }
2198                     if ((namedclass == -1 ||
2199                          !(posixcc + skip + 2 < PL_regxend &&
2200                            (posixcc[skip] == ':' &&
2201                             posixcc[skip + 1] == ']'))))
2202                         Perl_croak(aTHX_ "Character class [:%.*s:] unknown",
2203                                    t - s - 1, s + 1); 
2204                 } else if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
2205                     /* [[=foo=]] and [[.foo.]] are still future. */
2206                     Perl_warner(aTHX_ WARN_UNSAFE,
2207                                 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2208             } else {
2209                 /* Maternal grandfather:
2210                  * "[:" ending in ":" but not in ":]" */
2211                 PL_regcomp_parse = s;
2212             }
2213         }
2214     }
2215
2216     return namedclass;
2217 }
2218
2219 STATIC void
2220 S_checkposixcc(pTHX)
2221 {
2222     if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY &&
2223         (*PL_regcomp_parse == ':' ||
2224          *PL_regcomp_parse == '=' ||
2225          *PL_regcomp_parse == '.')) {
2226         char *s = PL_regcomp_parse;
2227         char  c = *s++;
2228
2229         while(*s && isALNUM(*s))
2230             s++;
2231         if (*s && c == *s && s[1] == ']') {
2232             Perl_warner(aTHX_ WARN_UNSAFE,
2233                         "Character class syntax [%c %c] belongs inside character classes", c, c);
2234             if (c == '=' || c == '.')
2235                 Perl_warner(aTHX_ WARN_UNSAFE,
2236                             "Character class syntax [%c %c] is reserved for future extensions", c, c);
2237         }
2238     }
2239 }
2240
2241 STATIC regnode *
2242 S_regclass(pTHX)
2243 {
2244     dTHR;
2245     register char *opnd, *s;
2246     register I32 value;
2247     register I32 lastvalue = OOB_CHAR8;
2248     register I32 range = 0;
2249     register regnode *ret;
2250     register I32 def;
2251     I32 numlen;
2252     I32 namedclass;
2253
2254     s = opnd = (char *) OPERAND(PL_regcode);
2255     ret = reg_node(ANYOF);
2256     for (value = 0; value < ANYOF_SIZE; value++)
2257         regc(0, s++);
2258     if (*PL_regcomp_parse == '^') {     /* Complement of range. */
2259         PL_regnaughty++;
2260         PL_regcomp_parse++;
2261         if (!SIZE_ONLY)
2262             ANYOF_FLAGS(opnd) |= ANYOF_INVERT;
2263     }
2264     if (!SIZE_ONLY) {
2265         PL_regcode += ANY_SKIP;
2266         if (FOLD)
2267             ANYOF_FLAGS(opnd) |= ANYOF_FOLD;
2268         if (LOC)
2269             ANYOF_FLAGS(opnd) |= ANYOF_LOCALE;
2270     }
2271     else {
2272         PL_regsize += ANY_SKIP;
2273     }
2274
2275     checkposixcc();
2276
2277     if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2278         goto skipcond;          /* allow 1st char to be ] or - */
2279     while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2280        skipcond:
2281         namedclass = -1;
2282         value = UCHARAT(PL_regcomp_parse++);
2283         if (value == '[')
2284             namedclass = regpposixcc(value);
2285         else if (value == '\\') {
2286             value = UCHARAT(PL_regcomp_parse++);
2287             switch (value) {
2288             case 'w':   namedclass = ANYOF_ALNUM;       break;
2289             case 'W':   namedclass = ANYOF_NALNUM;      break;
2290             case 's':   namedclass = ANYOF_SPACE;       break;
2291             case 'S':   namedclass = ANYOF_NSPACE;      break;
2292             case 'd':   namedclass = ANYOF_DIGIT;       break;
2293             case 'D':   namedclass = ANYOF_NDIGIT;      break;
2294             case 'n':   value = '\n';                   break;
2295             case 'r':   value = '\r';                   break;
2296             case 't':   value = '\t';                   break;
2297             case 'f':   value = '\f';                   break;
2298             case 'b':   value = '\b';                   break;
2299             case 'e':   value = '\033';                 break;
2300             case 'a':   value = '\007';                 break;
2301             case 'x':
2302                 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2303                 PL_regcomp_parse += numlen;
2304                 break;
2305             case 'c':
2306                 value = UCHARAT(PL_regcomp_parse++);
2307                 value = toCTRL(value);
2308                 break;
2309             case '0': case '1': case '2': case '3': case '4':
2310             case '5': case '6': case '7': case '8': case '9':
2311                 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2312                 PL_regcomp_parse += numlen;
2313                 break;
2314             }
2315         }
2316         if (!SIZE_ONLY && namedclass > -1) {
2317             switch (namedclass) {
2318             case ANYOF_ALNUM:
2319                 if (LOC)
2320                     ANYOF_CLASS_SET(opnd, ANYOF_ALNUM);
2321                 else {
2322                     for (value = 0; value < 256; value++)
2323                         if (isALNUM(value))
2324                             ANYOF_BITMAP_SET(opnd, value);
2325                 }
2326                 break;
2327             case ANYOF_NALNUM:
2328                 if (LOC)
2329                     ANYOF_CLASS_SET(opnd, ANYOF_NALNUM);
2330                 else {
2331                     for (value = 0; value < 256; value++)
2332                         if (!isALNUM(value))
2333                             ANYOF_BITMAP_SET(opnd, value);
2334                 }
2335                 break;
2336             case ANYOF_SPACE:
2337                 if (LOC)
2338                     ANYOF_CLASS_SET(opnd, ANYOF_SPACE);
2339                 else {
2340                     for (value = 0; value < 256; value++)
2341                         if (isSPACE(value))
2342                             ANYOF_BITMAP_SET(opnd, value);
2343                 }
2344                 break;
2345             case ANYOF_NSPACE:
2346                 if (LOC)
2347                     ANYOF_CLASS_SET(opnd, ANYOF_NSPACE);
2348                 else {
2349                     for (value = 0; value < 256; value++)
2350                         if (!isSPACE(value))
2351                             ANYOF_BITMAP_SET(opnd, value);
2352                 }
2353                 break;
2354             case ANYOF_DIGIT:
2355                 if (LOC)
2356                     ANYOF_CLASS_SET(opnd, ANYOF_DIGIT);
2357                 else {
2358                     for (value = '0'; value <= '9'; value++)
2359                         ANYOF_BITMAP_SET(opnd, value);
2360                 }
2361                 break;
2362             case ANYOF_NDIGIT:
2363                 if (LOC)
2364                     ANYOF_CLASS_SET(opnd, ANYOF_NDIGIT);
2365                 else {
2366                     for (value = 0; value < '0'; value++)
2367                         ANYOF_BITMAP_SET(opnd, value);
2368                     for (value = '9' + 1; value < 256; value++)
2369                         ANYOF_BITMAP_SET(opnd, value);
2370                 }
2371                 break;
2372             case ANYOF_NALNUMC:
2373                 if (LOC)
2374                     ANYOF_CLASS_SET(opnd, ANYOF_NALNUMC);
2375                 else {
2376                     for (value = 0; value < 256; value++)
2377                         if (!isALNUMC(value))
2378                             ANYOF_BITMAP_SET(opnd, value);
2379                 }
2380                 break;
2381             case ANYOF_ALNUMC:
2382                 if (LOC)
2383                     ANYOF_CLASS_SET(opnd, ANYOF_ALNUMC);
2384                 else {
2385                     for (value = 0; value < 256; value++)
2386                         if (isALNUMC(value))
2387                             ANYOF_BITMAP_SET(opnd, value);
2388                 }
2389                 break;
2390             case ANYOF_ALPHA:
2391                 if (LOC)
2392                     ANYOF_CLASS_SET(opnd, ANYOF_ALPHA);
2393                 else {
2394                     for (value = 0; value < 256; value++)
2395                         if (isALPHA(value))
2396                             ANYOF_BITMAP_SET(opnd, value);
2397                 }
2398                 break;
2399             case ANYOF_NALPHA:
2400                 if (LOC)
2401                     ANYOF_CLASS_SET(opnd, ANYOF_NALPHA);
2402                 else {
2403                     for (value = 0; value < 256; value++)
2404                         if (!isALPHA(value))
2405                             ANYOF_BITMAP_SET(opnd, value);
2406                 }
2407                 break;
2408             case ANYOF_ASCII:
2409                 if (LOC)
2410                     ANYOF_CLASS_SET(opnd, ANYOF_ASCII);
2411                 else {
2412                     for (value = 0; value < 128; value++)
2413                         ANYOF_BITMAP_SET(opnd, value);
2414                 }
2415                 break;
2416             case ANYOF_NASCII:
2417                 if (LOC)
2418                     ANYOF_CLASS_SET(opnd, ANYOF_NASCII);
2419                 else {
2420                     for (value = 128; value < 256; value++)
2421                         ANYOF_BITMAP_SET(opnd, value);
2422                 }
2423                 break;
2424             case ANYOF_CNTRL:
2425                 if (LOC)
2426                     ANYOF_CLASS_SET(opnd, ANYOF_CNTRL);
2427                 else {
2428                     for (value = 0; value < 256; value++)
2429                         if (isCNTRL(value))
2430                             ANYOF_BITMAP_SET(opnd, value);
2431                 }
2432                 lastvalue = OOB_CHAR8;
2433                 break;
2434             case ANYOF_NCNTRL:
2435                 if (LOC)
2436                     ANYOF_CLASS_SET(opnd, ANYOF_NCNTRL);
2437                 else {
2438                     for (value = 0; value < 256; value++)
2439                         if (!isCNTRL(value))
2440                             ANYOF_BITMAP_SET(opnd, value);
2441                 }
2442                 break;
2443             case ANYOF_GRAPH:
2444                 if (LOC)
2445                     ANYOF_CLASS_SET(opnd, ANYOF_GRAPH);
2446                 else {
2447                     for (value = 0; value < 256; value++)
2448                         if (isGRAPH(value))
2449                             ANYOF_BITMAP_SET(opnd, value);
2450                 }
2451                 break;
2452             case ANYOF_NGRAPH:
2453                 if (LOC)
2454                     ANYOF_CLASS_SET(opnd, ANYOF_NGRAPH);
2455                 else {
2456                     for (value = 0; value < 256; value++)
2457                         if (!isGRAPH(value))
2458                             ANYOF_BITMAP_SET(opnd, value);
2459                 }
2460                 break;
2461             case ANYOF_LOWER:
2462                 if (LOC)
2463                     ANYOF_CLASS_SET(opnd, ANYOF_LOWER);
2464                 else {
2465                     for (value = 0; value < 256; value++)
2466                         if (isLOWER(value))
2467                             ANYOF_BITMAP_SET(opnd, value);
2468                 }
2469                 break;
2470             case ANYOF_NLOWER:
2471                 if (LOC)
2472                     ANYOF_CLASS_SET(opnd, ANYOF_NLOWER);
2473                 else {
2474                     for (value = 0; value < 256; value++)
2475                         if (!isLOWER(value))
2476                             ANYOF_BITMAP_SET(opnd, value);
2477                 }
2478                 break;
2479             case ANYOF_PRINT:
2480                 if (LOC)
2481                     ANYOF_CLASS_SET(opnd, ANYOF_PRINT);
2482                 else {
2483                     for (value = 0; value < 256; value++)
2484                         if (isPRINT(value))
2485                             ANYOF_BITMAP_SET(opnd, value);
2486                 }
2487                 break;
2488             case ANYOF_NPRINT:
2489                 if (LOC)
2490                     ANYOF_CLASS_SET(opnd, ANYOF_NPRINT);
2491                 else {
2492                     for (value = 0; value < 256; value++)
2493                         if (!isPRINT(value))
2494                             ANYOF_BITMAP_SET(opnd, value);
2495                 }
2496                 break;
2497             case ANYOF_PUNCT:
2498                 if (LOC)
2499                     ANYOF_CLASS_SET(opnd, ANYOF_PUNCT);
2500                 else {
2501                     for (value = 0; value < 256; value++)
2502                         if (isPUNCT(value))
2503                             ANYOF_BITMAP_SET(opnd, value);
2504                 }
2505                 break;
2506             case ANYOF_NPUNCT:
2507                 if (LOC)
2508                     ANYOF_CLASS_SET(opnd, ANYOF_NPUNCT);
2509                 else {
2510                     for (value = 0; value < 256; value++)
2511                         if (!isPUNCT(value))
2512                             ANYOF_BITMAP_SET(opnd, value);
2513                 }
2514                 break;
2515             case ANYOF_UPPER:
2516                 if (LOC)
2517                     ANYOF_CLASS_SET(opnd, ANYOF_UPPER);
2518                 else {
2519                     for (value = 0; value < 256; value++)
2520                         if (isUPPER(value))
2521                             ANYOF_BITMAP_SET(opnd, value);
2522                 }
2523                 break;
2524             case ANYOF_NUPPER:
2525                 if (LOC)
2526                     ANYOF_CLASS_SET(opnd, ANYOF_NUPPER);
2527                 else {
2528                     for (value = 0; value < 256; value++)
2529                         if (!isUPPER(value))
2530                             ANYOF_BITMAP_SET(opnd, value);
2531                 }
2532                 break;
2533             case ANYOF_XDIGIT:
2534                 if (LOC)
2535                     ANYOF_CLASS_SET(opnd, ANYOF_XDIGIT);
2536                 else {
2537                     for (value = 0; value < 256; value++)
2538                         if (isXDIGIT(value))
2539                             ANYOF_BITMAP_SET(opnd, value);
2540                 }
2541                 break;
2542             case ANYOF_NXDIGIT:
2543                 if (LOC)
2544                     ANYOF_CLASS_SET(opnd, ANYOF_NXDIGIT);
2545                 else {
2546                     for (value = 0; value < 256; value++)
2547                         if (!isXDIGIT(value))
2548                             ANYOF_BITMAP_SET(opnd, value);
2549                 }
2550                 break;
2551             default:
2552                 FAIL("invalid [::] class in regexp");
2553                 break;
2554             }
2555             if (LOC)
2556                 ANYOF_FLAGS(opnd) |= ANYOF_CLASS;
2557             lastvalue = OOB_CHAR8;
2558         }
2559         else
2560         if (range) {
2561             if (lastvalue > value)
2562                 FAIL("invalid [] range in regexp");
2563             range = 0;
2564         }
2565         else {
2566             lastvalue = value;
2567             if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2568               PL_regcomp_parse[1] != ']') {
2569                 PL_regcomp_parse++;
2570                 range = 1;
2571                 continue;       /* do it next time */
2572             }
2573         }
2574         if (!SIZE_ONLY) {
2575 #ifndef ASCIIish
2576             if ((isLOWER(lastvalue) && isLOWER(value)) ||
2577                 (isUPPER(lastvalue) && isUPPER(value)))
2578             {
2579                 I32 i;
2580                 if (isLOWER(lastvalue)) {
2581                     for (i = lastvalue; i <= value; i++)
2582                         if (isLOWER(i))
2583                             ANYOF_BITMAP_SET(opnd, i);
2584                 } else {
2585                     for (i = lastvalue; i <= value; i++)
2586                         if (isUPPER(i))
2587                             ANYOF_BITMAP_SET(opnd, i);
2588                 }
2589             }
2590             else
2591 #endif
2592                 for ( ; lastvalue <= value; lastvalue++)
2593                     ANYOF_BITMAP_SET(opnd, lastvalue);
2594         }
2595         lastvalue = value;
2596     }
2597     /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2598     if (!SIZE_ONLY &&
2599         (ANYOF_FLAGS(opnd) & (ANYOF_FLAGS_ALL ^ ANYOF_INVERT)) == ANYOF_FOLD) {
2600         for (value = 0; value < 256; ++value) {
2601             if (ANYOF_BITMAP_TEST(opnd, value)) {
2602                 I32 cf = PL_fold[value];
2603                 ANYOF_BITMAP_SET(opnd, cf);
2604             }
2605         }
2606         ANYOF_FLAGS(opnd) &= ~ANYOF_FOLD;
2607     }
2608     /* optimize inverted simple patterns (e.g. [^a-z]) */
2609     if (!SIZE_ONLY && (ANYOF_FLAGS(opnd) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
2610         for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
2611             opnd[ANYOF_BITMAP_OFFSET + value] ^= ANYOF_FLAGS_ALL;
2612         ANYOF_FLAGS(opnd) = 0;
2613     }
2614     return ret;
2615 }
2616
2617 STATIC regnode *
2618 S_regclassutf8(pTHX)
2619 {
2620     dTHR;
2621     register char *opnd, *e;
2622     register U32 value;
2623     register U32 lastvalue = OOB_UTF8;
2624     register I32 range = 0;
2625     register regnode *ret;
2626     I32 numlen;
2627     I32 n;
2628     SV *listsv;
2629     U8 flags = 0;
2630     I32 namedclass;
2631
2632     if (*PL_regcomp_parse == '^') {     /* Complement of range. */
2633         PL_regnaughty++;
2634         PL_regcomp_parse++;
2635         if (!SIZE_ONLY)
2636             flags |= ANYOF_INVERT;
2637     }
2638     if (!SIZE_ONLY) {
2639         if (FOLD)
2640             flags |= ANYOF_FOLD;
2641         if (LOC)
2642             flags |= ANYOF_LOCALE;
2643         listsv = newSVpvn("# comment\n",10);
2644     }
2645
2646     checkposixcc();
2647
2648     if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2649         goto skipcond;          /* allow 1st char to be ] or - */
2650
2651     while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2652        skipcond:
2653         namedclass = -1;
2654         value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2655         PL_regcomp_parse += numlen;
2656
2657         if (value == '[')
2658             namedclass = regpposixcc(value);
2659         else if (value == '\\') {
2660             value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2661             PL_regcomp_parse += numlen;
2662             switch (value) {
2663             case 'w':           namedclass = ANYOF_ALNUM;               break;
2664             case 'W':           namedclass = ANYOF_NALNUM;              break;
2665             case 's':           namedclass = ANYOF_SPACE;               break;
2666             case 'S':           namedclass = ANYOF_NSPACE;              break;
2667             case 'd':           namedclass = ANYOF_DIGIT;               break;
2668             case 'D':           namedclass = ANYOF_NDIGIT;              break;
2669             case 'p':
2670             case 'P':
2671                 if (*PL_regcomp_parse == '{') {
2672                     e = strchr(PL_regcomp_parse++, '}');
2673                     if (!e)
2674                         FAIL("Missing right brace on \\p{}");
2675                     n = e - PL_regcomp_parse;
2676                 }
2677                 else {
2678                     e = PL_regcomp_parse;
2679                     n = 1;
2680                 }
2681                 if (!SIZE_ONLY) {
2682                     if (value == 'p')
2683                         Perl_sv_catpvf(aTHX_ listsv,
2684                                        "+utf8::%.*s\n", n, PL_regcomp_parse);
2685                     else
2686                         Perl_sv_catpvf(aTHX_ listsv,
2687                                        "!utf8::%.*s\n", n, PL_regcomp_parse);
2688                 }
2689                 PL_regcomp_parse = e + 1;
2690                 lastvalue = OOB_UTF8;
2691                 continue;
2692             case 'n':           value = '\n';           break;
2693             case 'r':           value = '\r';           break;
2694             case 't':           value = '\t';           break;
2695             case 'f':           value = '\f';           break;
2696             case 'b':           value = '\b';           break;
2697             case 'e':           value = '\033';         break;
2698             case 'a':           value = '\007';         break;
2699             case 'x':
2700                 if (*PL_regcomp_parse == '{') {
2701                     e = strchr(PL_regcomp_parse++, '}');
2702                     if (!e)
2703                         FAIL("Missing right brace on \\x{}");
2704                     value = scan_hex(PL_regcomp_parse,
2705                                      e - PL_regcomp_parse,
2706                                      &numlen);
2707                     PL_regcomp_parse = e + 1;
2708                 }
2709                 else {
2710                     value = scan_hex(PL_regcomp_parse, 2, &numlen);
2711                     PL_regcomp_parse += numlen;
2712                 }
2713                 break;
2714             case 'c':
2715                 value = UCHARAT(PL_regcomp_parse++);
2716                 value = toCTRL(value);
2717                 break;
2718             case '0': case '1': case '2': case '3': case '4':
2719             case '5': case '6': case '7': case '8': case '9':
2720                 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2721                 PL_regcomp_parse += numlen;
2722                 break;
2723             }
2724         }
2725         if (!SIZE_ONLY && namedclass > -1) {
2726             switch (namedclass) {
2727             case ANYOF_ALNUM:
2728                 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n");        break;
2729             case ANYOF_NALNUM:
2730                 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n");        break;
2731             case ANYOF_ALNUMC:
2732                 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n");       break;
2733             case ANYOF_NALNUMC:
2734                 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n");       break;
2735             case ANYOF_ALPHA:
2736                 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n");       break;
2737             case ANYOF_NALPHA:
2738                 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n");       break;
2739             case ANYOF_ASCII:
2740                 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n");       break;
2741             case ANYOF_NASCII:
2742                 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n");       break;
2743             case ANYOF_CNTRL:
2744                 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n");       break;
2745             case ANYOF_NCNTRL:
2746                 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n");       break;
2747             case ANYOF_GRAPH:
2748                 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n");       break;
2749             case ANYOF_NGRAPH:
2750                 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n");       break;
2751             case ANYOF_DIGIT:
2752                 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n");       break;
2753             case ANYOF_NDIGIT:
2754                 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n");       break;
2755             case ANYOF_LOWER:
2756                 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n");       break;
2757             case ANYOF_NLOWER:
2758                 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n");       break;
2759             case ANYOF_PRINT:
2760                 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n");       break;
2761             case ANYOF_NPRINT:
2762                 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n");       break;
2763             case ANYOF_PUNCT:
2764                 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n");       break;
2765             case ANYOF_NPUNCT:
2766                 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n");       break;
2767             case ANYOF_SPACE:
2768                 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n");       break;
2769             case ANYOF_NSPACE:
2770                 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n");       break;
2771             case ANYOF_UPPER:
2772                 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n");       break;
2773             case ANYOF_NUPPER:
2774                 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n");       break;
2775             case ANYOF_XDIGIT:
2776                 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n");      break;
2777             case ANYOF_NXDIGIT:
2778                 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n");      break;
2779             }
2780         }
2781         else
2782         if (range) {
2783             if (lastvalue > value)
2784                 FAIL("invalid [] range in regexp");
2785             if (!SIZE_ONLY)
2786                 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\n", lastvalue, value);
2787             lastvalue = value;
2788             range = 0;
2789         }
2790         else {
2791             lastvalue = value;
2792             if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2793               PL_regcomp_parse[1] != ']') {
2794                 PL_regcomp_parse++;
2795                 range = 1;
2796                 continue;       /* do it next time */
2797             }
2798             if (!SIZE_ONLY)
2799                 Perl_sv_catpvf(aTHX_ listsv, "%04x\n", value);
2800         }
2801     }
2802
2803     ret = reganode(ANYOFUTF8, 0);
2804
2805     if (!SIZE_ONLY) {
2806         SV *rv = swash_init("utf8", "", listsv, 1, 0);
2807         SvREFCNT_dec(listsv);
2808         n = add_data(1,"s");
2809         PL_regcomp_rx->data->data[n] = (void*)rv;
2810         ARG1_SET(ret, flags);
2811         ARG2_SET(ret, n);
2812     }
2813
2814     return ret;
2815 }
2816
2817 STATIC char*
2818 S_nextchar(pTHX)
2819 {
2820     dTHR;
2821     char* retval = PL_regcomp_parse++;
2822
2823     for (;;) {
2824         if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2825                 PL_regcomp_parse[2] == '#') {
2826             while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2827                 PL_regcomp_parse++;
2828             PL_regcomp_parse++;
2829             continue;
2830         }
2831         if (PL_regflags & PMf_EXTENDED) {
2832             if (isSPACE(*PL_regcomp_parse)) {
2833                 PL_regcomp_parse++;
2834                 continue;
2835             }
2836             else if (*PL_regcomp_parse == '#') {
2837                 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2838                     PL_regcomp_parse++;
2839                 PL_regcomp_parse++;
2840                 continue;
2841             }
2842         }
2843         return retval;
2844     }
2845 }
2846
2847 /*
2848 - reg_node - emit a node
2849 */
2850 STATIC regnode *                        /* Location. */
2851 S_reg_node(pTHX_ U8 op)
2852 {
2853     dTHR;
2854     register regnode *ret;
2855     register regnode *ptr;
2856
2857     ret = PL_regcode;
2858     if (SIZE_ONLY) {
2859         SIZE_ALIGN(PL_regsize);
2860         PL_regsize += 1;
2861         return(ret);
2862     }
2863
2864     NODE_ALIGN_FILL(ret);
2865     ptr = ret;
2866     FILL_ADVANCE_NODE(ptr, op);
2867     PL_regcode = ptr;
2868
2869     return(ret);
2870 }
2871
2872 /*
2873 - reganode - emit a node with an argument
2874 */
2875 STATIC regnode *                        /* Location. */
2876 S_reganode(pTHX_ U8 op, U32 arg)
2877 {
2878     dTHR;
2879     register regnode *ret;
2880     register regnode *ptr;
2881
2882     ret = PL_regcode;
2883     if (SIZE_ONLY) {
2884         SIZE_ALIGN(PL_regsize);
2885         PL_regsize += 2;
2886         return(ret);
2887     }
2888
2889     NODE_ALIGN_FILL(ret);
2890     ptr = ret;
2891     FILL_ADVANCE_NODE_ARG(ptr, op, arg);
2892     PL_regcode = ptr;
2893
2894     return(ret);
2895 }
2896
2897 /*
2898 - regc - emit (if appropriate) a Unicode character
2899 */
2900 STATIC void
2901 S_reguni(pTHX_ UV uv, char* s, I32* lenp)
2902 {
2903     dTHR;
2904     if (SIZE_ONLY) {
2905         U8 tmpbuf[10];
2906         *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
2907     }
2908     else
2909         *lenp = uv_to_utf8((U8*)s, uv) - (U8*)s;
2910
2911 }
2912
2913 /*
2914 - regc - emit (if appropriate) a byte of code
2915 */
2916 STATIC void
2917 S_regc(pTHX_ U8 b, char* s)
2918 {
2919     dTHR;
2920     if (!SIZE_ONLY)
2921         *s = b;
2922 }
2923
2924 /*
2925 - reginsert - insert an operator in front of already-emitted operand
2926 *
2927 * Means relocating the operand.
2928 */
2929 STATIC void
2930 S_reginsert(pTHX_ U8 op, regnode *opnd)
2931 {
2932     dTHR;
2933     register regnode *src;
2934     register regnode *dst;
2935     register regnode *place;
2936     register int offset = regarglen[(U8)op];
2937     
2938 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2939
2940     if (SIZE_ONLY) {
2941         PL_regsize += NODE_STEP_REGNODE + offset;
2942         return;
2943     }
2944
2945     src = PL_regcode;
2946     PL_regcode += NODE_STEP_REGNODE + offset;
2947     dst = PL_regcode;
2948     while (src > opnd)
2949         StructCopy(--src, --dst, regnode);
2950
2951     place = opnd;               /* Op node, where operand used to be. */
2952     src = NEXTOPER(place);
2953     FILL_ADVANCE_NODE(place, op);
2954     Zero(src, offset, regnode);
2955 }
2956
2957 /*
2958 - regtail - set the next-pointer at the end of a node chain of p to val.
2959 */
2960 STATIC void
2961 S_regtail(pTHX_ regnode *p, regnode *val)
2962 {
2963     dTHR;
2964     register regnode *scan;
2965     register regnode *temp;
2966     register I32 offset;
2967
2968     if (SIZE_ONLY)
2969         return;
2970
2971     /* Find last node. */
2972     scan = p;
2973     for (;;) {
2974         temp = regnext(scan);
2975         if (temp == NULL)
2976             break;
2977         scan = temp;
2978     }
2979
2980     if (reg_off_by_arg[OP(scan)]) {
2981         ARG_SET(scan, val - scan);
2982     }
2983     else {
2984         NEXT_OFF(scan) = val - scan;
2985     }
2986 }
2987
2988 /*
2989 - regoptail - regtail on operand of first argument; nop if operandless
2990 */
2991 STATIC void
2992 S_regoptail(pTHX_ regnode *p, regnode *val)
2993 {
2994     dTHR;
2995     /* "Operandless" and "op != BRANCH" are synonymous in practice. */
2996     if (p == NULL || SIZE_ONLY)
2997         return;
2998     if (PL_regkind[(U8)OP(p)] == BRANCH) {
2999         regtail(NEXTOPER(p), val);
3000     }
3001     else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
3002         regtail(NEXTOPER(NEXTOPER(p)), val);
3003     }
3004     else
3005         return;
3006 }
3007
3008 /*
3009  - regcurly - a little FSA that accepts {\d+,?\d*}
3010  */
3011 STATIC I32
3012 S_regcurly(pTHX_ register char *s)
3013 {
3014     if (*s++ != '{')
3015         return FALSE;
3016     if (!isDIGIT(*s))
3017         return FALSE;
3018     while (isDIGIT(*s))
3019         s++;
3020     if (*s == ',')
3021         s++;
3022     while (isDIGIT(*s))
3023         s++;
3024     if (*s != '}')
3025         return FALSE;
3026     return TRUE;
3027 }
3028
3029
3030 STATIC regnode *
3031 S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
3032 {
3033 #ifdef DEBUGGING
3034     register U8 op = EXACT;     /* Arbitrary non-END op. */
3035     register regnode *next, *onode;
3036
3037     while (op != END && (!last || node < last)) {
3038         /* While that wasn't END last time... */
3039
3040         NODE_ALIGN(node);
3041         op = OP(node);
3042         if (op == CLOSE)
3043             l--;        
3044         next = regnext(node);
3045         /* Where, what. */
3046         if (OP(node) == OPTIMIZED)
3047             goto after_print;
3048         regprop(sv, node);
3049         PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start, 
3050                       2*l + 1, "", SvPVX(sv));
3051         if (next == NULL)               /* Next ptr. */
3052             PerlIO_printf(Perl_debug_log, "(0)");
3053         else 
3054             PerlIO_printf(Perl_debug_log, "(%d)", next - start);
3055         (void)PerlIO_putc(Perl_debug_log, '\n');
3056       after_print:
3057         if (PL_regkind[(U8)op] == BRANCHJ) {
3058             register regnode *nnode = (OP(next) == LONGJMP 
3059                                        ? regnext(next) 
3060                                        : next);
3061             if (last && nnode > last)
3062                 nnode = last;
3063             node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
3064         }
3065         else if (PL_regkind[(U8)op] == BRANCH) {
3066             node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
3067         }
3068         else if ( op == CURLY) {   /* `next' might be very big: optimizer */
3069             node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
3070                              NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
3071         }
3072         else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
3073             node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
3074                              next, sv, l + 1);
3075         }
3076         else if ( op == PLUS || op == STAR) {
3077             node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
3078         }
3079         else if (op == ANYOF) {
3080             node = NEXTOPER(node);
3081             node += ANY_SKIP;
3082         }
3083         else if (PL_regkind[(U8)op] == EXACT) {
3084             /* Literal string, where present. */
3085             node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
3086             node = NEXTOPER(node);
3087         }
3088         else {
3089             node = NEXTOPER(node);
3090             node += regarglen[(U8)op];
3091         }
3092         if (op == CURLYX || op == OPEN)
3093             l++;
3094         else if (op == WHILEM)
3095             l--;
3096     }
3097 #endif  /* DEBUGGING */
3098     return node;
3099 }
3100
3101 /*
3102  - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
3103  */
3104 void
3105 Perl_regdump(pTHX_ regexp *r)
3106 {
3107 #ifdef DEBUGGING
3108     dTHR;
3109     SV *sv = sv_newmortal();
3110
3111     (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
3112
3113     /* Header fields of interest. */
3114     if (r->anchored_substr)
3115         PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ", 
3116                       PL_colors[0],
3117                       SvPVX(r->anchored_substr), 
3118                       PL_colors[1],
3119                       SvTAIL(r->anchored_substr) ? "$" : "",
3120                       r->anchored_offset);
3121     if (r->float_substr)
3122         PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ", 
3123                       PL_colors[0],
3124                       SvPVX(r->float_substr), 
3125                       PL_colors[1],
3126                       SvTAIL(r->float_substr) ? "$" : "",
3127                       r->float_min_offset, r->float_max_offset);
3128     if (r->check_substr)
3129         PerlIO_printf(Perl_debug_log, 
3130                       r->check_substr == r->float_substr 
3131                       ? "(checking floating" : "(checking anchored");
3132     if (r->reganch & ROPT_NOSCAN)
3133         PerlIO_printf(Perl_debug_log, " noscan");
3134     if (r->reganch & ROPT_CHECK_ALL)
3135         PerlIO_printf(Perl_debug_log, " isall");
3136     if (r->check_substr)
3137         PerlIO_printf(Perl_debug_log, ") ");
3138
3139     if (r->regstclass) {
3140         regprop(sv, r->regstclass);
3141         PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
3142     }
3143     if (r->reganch & ROPT_ANCH) {
3144         PerlIO_printf(Perl_debug_log, "anchored");
3145         if (r->reganch & ROPT_ANCH_BOL)
3146             PerlIO_printf(Perl_debug_log, "(BOL)");
3147         if (r->reganch & ROPT_ANCH_MBOL)
3148             PerlIO_printf(Perl_debug_log, "(MBOL)");
3149         if (r->reganch & ROPT_ANCH_SBOL)
3150             PerlIO_printf(Perl_debug_log, "(SBOL)");
3151         if (r->reganch & ROPT_ANCH_GPOS)
3152             PerlIO_printf(Perl_debug_log, "(GPOS)");
3153         PerlIO_putc(Perl_debug_log, ' ');
3154     }
3155     if (r->reganch & ROPT_GPOS_SEEN)
3156         PerlIO_printf(Perl_debug_log, "GPOS ");
3157     if (r->reganch & ROPT_SKIP)
3158         PerlIO_printf(Perl_debug_log, "plus ");
3159     if (r->reganch & ROPT_IMPLICIT)
3160         PerlIO_printf(Perl_debug_log, "implicit ");
3161     PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
3162     if (r->reganch & ROPT_EVAL_SEEN)
3163         PerlIO_printf(Perl_debug_log, "with eval ");
3164     PerlIO_printf(Perl_debug_log, "\n");
3165 #endif  /* DEBUGGING */
3166 }
3167
3168 /*
3169 - regprop - printable representation of opcode
3170 */
3171 void
3172 Perl_regprop(pTHX_ SV *sv, regnode *o)
3173 {
3174 #ifdef DEBUGGING
3175     dTHR;
3176     register int k;
3177
3178     sv_setpvn(sv, "", 0);
3179     if (OP(o) >= reg_num)               /* regnode.type is unsigned */
3180         FAIL("corrupted regexp opcode");
3181     sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
3182
3183     k = PL_regkind[(U8)OP(o)];
3184
3185     if (k == EXACT)
3186         Perl_sv_catpvf(aTHX_ sv, " <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
3187     else if (k == CURLY) {
3188         if (OP(o) == CURLYM || OP(o) == CURLYN)
3189             Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
3190         Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
3191     }
3192     else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
3193         Perl_sv_catpvf(aTHX_ sv, "%d", ARG(o)); /* Parenth number */
3194     else if (k == LOGICAL)
3195         Perl_sv_catpvf(aTHX_ sv, "[%d]", ARG(o));       /* 2: embedded, otherwise 1 */
3196     else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
3197         Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
3198 #endif  /* DEBUGGING */
3199 }
3200
3201 SV *
3202 Perl_re_intuit_string(pTHX_ regexp *prog)
3203 {                               /* Assume that RE_INTUIT is set */
3204     DEBUG_r(
3205         {   STRLEN n_a;
3206             char *s = SvPV(prog->check_substr,n_a);
3207
3208             if (!PL_colorset) reginitcolors();
3209             PerlIO_printf(Perl_debug_log,
3210                       "%sUsing REx substr:%s `%s%.60s%s%s'\n",
3211                       PL_colors[4],PL_colors[5],PL_colors[0],
3212                       s,
3213                       PL_colors[1],
3214                       (strlen(s) > 60 ? "..." : ""));
3215         } );
3216
3217     return prog->check_substr;
3218 }
3219
3220 void
3221 Perl_pregfree(pTHX_ struct regexp *r)
3222 {
3223     dTHR;
3224     DEBUG_r(PerlIO_printf(Perl_debug_log,
3225                       "%sFreeing REx:%s `%s%.60s%s%s'\n",
3226                       PL_colors[4],PL_colors[5],PL_colors[0],
3227                       r->precomp,
3228                       PL_colors[1],
3229                       (strlen(r->precomp) > 60 ? "..." : "")));
3230
3231
3232     if (!r || (--r->refcnt > 0))
3233         return;
3234     if (r->precomp)
3235         Safefree(r->precomp);
3236     if (RX_MATCH_COPIED(r))
3237         Safefree(r->subbeg);
3238     if (r->substrs) {
3239         if (r->anchored_substr)
3240             SvREFCNT_dec(r->anchored_substr);
3241         if (r->float_substr)
3242             SvREFCNT_dec(r->float_substr);
3243         Safefree(r->substrs);
3244     }
3245     if (r->data) {
3246         int n = r->data->count;
3247         AV* new_comppad = NULL;
3248         AV* old_comppad;
3249         SV** old_curpad;
3250
3251         while (--n >= 0) {
3252             switch (r->data->what[n]) {
3253             case 's':
3254                 SvREFCNT_dec((SV*)r->data->data[n]);
3255                 break;
3256             case 'p':
3257                 new_comppad = (AV*)r->data->data[n];
3258                 break;
3259             case 'o':
3260                 if (new_comppad == NULL)
3261                     Perl_croak(aTHX_ "panic: pregfree comppad");
3262                 old_comppad = PL_comppad;
3263                 old_curpad = PL_curpad;
3264                 PL_comppad = new_comppad;
3265                 PL_curpad = AvARRAY(new_comppad);
3266                 op_free((OP_4tree*)r->data->data[n]);
3267                 PL_comppad = old_comppad;
3268                 PL_curpad = old_curpad;
3269                 SvREFCNT_dec((SV*)new_comppad);
3270                 new_comppad = NULL;
3271                 break;
3272             case 'n':
3273                 break;
3274             default:
3275                 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
3276             }
3277         }
3278         Safefree(r->data->what);
3279         Safefree(r->data);
3280     }
3281     Safefree(r->startp);
3282     Safefree(r->endp);
3283     Safefree(r);
3284 }
3285
3286 /*
3287  - regnext - dig the "next" pointer out of a node
3288  *
3289  * [Note, when REGALIGN is defined there are two places in regmatch()
3290  * that bypass this code for speed.]
3291  */
3292 regnode *
3293 Perl_regnext(pTHX_ register regnode *p)
3294 {
3295     dTHR;
3296     register I32 offset;
3297
3298     if (p == &PL_regdummy)
3299         return(NULL);
3300
3301     offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
3302     if (offset == 0)
3303         return(NULL);
3304
3305     return(p+offset);
3306 }
3307
3308 STATIC void     
3309 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
3310 {
3311     va_list args;
3312     STRLEN l1 = strlen(pat1);
3313     STRLEN l2 = strlen(pat2);
3314     char buf[512];
3315     SV *msv;
3316     char *message;
3317
3318     if (l1 > 510)
3319         l1 = 510;
3320     if (l1 + l2 > 510)
3321         l2 = 510 - l1;
3322     Copy(pat1, buf, l1 , char);
3323     Copy(pat2, buf + l1, l2 , char);
3324     buf[l1 + l2] = '\n';
3325     buf[l1 + l2 + 1] = '\0';
3326 #ifdef I_STDARG
3327     /* ANSI variant takes additional second argument */
3328     va_start(args, pat2);
3329 #else
3330     va_start(args);
3331 #endif
3332     msv = mess(buf, &args);
3333     va_end(args);
3334     message = SvPV(msv,l1);
3335     if (l1 > 512)
3336         l1 = 512;
3337     Copy(message, buf, l1 , char);
3338     buf[l1] = '\0';                     /* Overwrite \n */
3339     Perl_croak(aTHX_ "%s", buf);
3340 }
3341
3342 /* XXX Here's a total kludge.  But we need to re-enter for swash routines. */
3343
3344 void
3345 Perl_save_re_context(pTHX)
3346 {                   
3347     dTHR;
3348     SAVEPPTR(PL_bostr);
3349     SAVEPPTR(PL_regprecomp);            /* uncompiled string. */
3350     SAVEI32(PL_regnpar);                /* () count. */
3351     SAVEI32(PL_regsize);                /* Code size. */
3352     SAVEI16(PL_regflags);               /* are we folding, multilining? */
3353     SAVEPPTR(PL_reginput);              /* String-input pointer. */
3354     SAVEPPTR(PL_regbol);                /* Beginning of input, for ^ check. */
3355     SAVEPPTR(PL_regeol);                /* End of input, for $ check. */
3356     SAVESPTR(PL_regstartp);             /* Pointer to startp array. */
3357     SAVESPTR(PL_regendp);               /* Ditto for endp. */
3358     SAVESPTR(PL_reglastparen);          /* Similarly for lastparen. */
3359     SAVEPPTR(PL_regtill);               /* How far we are required to go. */
3360     SAVEI32(PL_regprev);                /* char before regbol, \n if none */
3361     SAVESPTR(PL_reg_start_tmp);         /* from regexec.c */
3362     PL_reg_start_tmp = 0;
3363     SAVEFREEPV(PL_reg_start_tmp);
3364     SAVEI32(PL_reg_start_tmpl);         /* from regexec.c */
3365     PL_reg_start_tmpl = 0;
3366     SAVESPTR(PL_regdata);
3367     SAVEI32(PL_reg_flags);              /* from regexec.c */
3368     SAVEI32(PL_reg_eval_set);           /* from regexec.c */
3369     SAVEI32(PL_regnarrate);             /* from regexec.c */
3370     SAVESPTR(PL_regprogram);            /* from regexec.c */
3371     SAVEINT(PL_regindent);              /* from regexec.c */
3372     SAVESPTR(PL_regcc);                 /* from regexec.c */
3373     SAVESPTR(PL_curcop);
3374     SAVESPTR(PL_regcomp_rx);            /* from regcomp.c */
3375     SAVEI32(PL_regseen);                /* from regcomp.c */
3376     SAVEI32(PL_regsawback);             /* Did we see \1, ...? */
3377     SAVEI32(PL_regnaughty);             /* How bad is this pattern? */
3378     SAVESPTR(PL_regcode);               /* Code-emit pointer; &regdummy = don't */
3379     SAVEPPTR(PL_regxend);               /* End of input for compile */
3380     SAVEPPTR(PL_regcomp_parse);         /* Input-scan pointer. */
3381     SAVESPTR(PL_reg_call_cc);           /* from regexec.c */
3382     SAVESPTR(PL_reg_re);                /* from regexec.c */
3383     SAVEPPTR(PL_reg_ganch);             /* from regexec.c */
3384     SAVESPTR(PL_reg_sv);                /* from regexec.c */
3385     SAVESPTR(PL_reg_magic);             /* from regexec.c */
3386     SAVEI32(PL_reg_oldpos);                     /* from regexec.c */
3387     SAVESPTR(PL_reg_oldcurpm);          /* from regexec.c */
3388     SAVESPTR(PL_reg_curpm);             /* from regexec.c */
3389 #ifdef DEBUGGING
3390     SAVEPPTR(PL_reg_starttry);          /* from regexec.c */    
3391 #endif
3392 }