5 * "A fair jaw-cracker dwarf-language must be." --Samwise Gamgee
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!
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.
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.
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
27 /* need access to debugger hooks */
28 # if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
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
45 # define PERL_NO_GET_CONTEXT
50 * pregcomp and pregexec -- regsub and regerror are not used in perl
52 * Copyright (c) 1986 by University of Toronto.
53 * Written by Henry Spencer. Not derived from licensed software.
55 * Permission is granted to anyone to use this software for any
56 * purpose on any computer system, and to redistribute it freely,
57 * subject to the following restrictions:
59 * 1. The author is not responsible for the consequences of use of
60 * this software, no matter how awful, even if they arise
63 * 2. The origin of this software must not be misrepresented, either
64 * by explicit claim or by omission.
66 * 3. Altered versions must be plainly marked as such, and must not
67 * be misrepresented as being the original software.
70 **** Alterations to Henry's code are...
72 **** Copyright (c) 1991-1999, Larry Wall
74 **** You may distribute under the terms of either the GNU General Public
75 **** License or the Artistic License, as specified in the README file.
78 * Beware that some of this code is subtly aware of the way operator
79 * precedence is structured in regular expressions. Serious changes in
80 * regular-expression syntax might require a total rethink.
83 #define PERL_IN_REGCOMP_C
86 #ifdef PERL_IN_XSUB_RE
87 # if defined(PERL_CAPI) || defined(PERL_OBJECT)
102 # if defined(BUGGY_MSC6)
103 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
104 # pragma optimize("a",off)
105 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
106 # pragma optimize("w",on )
107 # endif /* BUGGY_MSC6 */
111 #define STATIC static
114 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
115 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
116 ((*s) == '{' && regcurly(s)))
118 #define PERL_META "^$.[()|?+*\\"
120 #define META "^$.[()|?+*\\"
124 #undef SPSTART /* dratted cpp namespace... */
127 * Flags to be passed up and down.
129 #define WORST 0 /* Worst case. */
130 #define HASWIDTH 0x1 /* Known to match non-null strings. */
131 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
132 #define SPSTART 0x4 /* Starts with * or +. */
133 #define TRYAGAIN 0x8 /* Weeded out a declaration. */
136 * Forward declarations for pregcomp()'s friends.
139 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
142 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
143 #define SF_BEFORE_SEOL 0x1
144 #define SF_BEFORE_MEOL 0x2
145 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
146 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
149 # define SF_FIX_SHIFT_EOL (0+2)
150 # define SF_FL_SHIFT_EOL (0+4)
152 # define SF_FIX_SHIFT_EOL (+2)
153 # define SF_FL_SHIFT_EOL (+4)
156 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
157 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
159 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
160 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
161 #define SF_IS_INF 0x40
162 #define SF_HAS_PAR 0x80
163 #define SF_IN_PAR 0x100
164 #define SF_HAS_EVAL 0x200
165 #define SCF_DO_SUBSTR 0x400
168 #define UTF (PL_reg_flags & RF_utf8)
169 #define LOC (PL_regflags & PMf_LOCALE)
170 #define FOLD (PL_regflags & PMf_FOLD)
172 #define OOB_CHAR8 1234
173 #define OOB_UTF8 123456
175 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
176 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
178 static void clear_re(pTHXo_ void *r);
181 S_scan_commit(pTHX_ scan_data_t *data)
184 STRLEN l = CHR_SVLEN(data->last_found);
185 STRLEN old_l = CHR_SVLEN(*data->longest);
187 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
188 sv_setsv(*data->longest, data->last_found);
189 if (*data->longest == data->longest_fixed) {
190 data->offset_fixed = l ? data->last_start_min : data->pos_min;
191 if (data->flags & SF_BEFORE_EOL)
193 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
195 data->flags &= ~SF_FIX_BEFORE_EOL;
198 data->offset_float_min = l ? data->last_start_min : data->pos_min;
199 data->offset_float_max = (l
200 ? data->last_start_max
201 : data->pos_min + data->pos_delta);
202 if (data->flags & SF_BEFORE_EOL)
204 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
206 data->flags &= ~SF_FL_BEFORE_EOL;
209 SvCUR_set(data->last_found, 0);
211 data->flags &= ~SF_BEFORE_EOL;
214 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
215 to the position after last scanned or to NULL. */
218 S_study_chunk(pTHX_ regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
219 /* scanp: Start here (read-write). */
220 /* deltap: Write maxlen-minlen here. */
221 /* last: Stop before this one. */
224 I32 min = 0, pars = 0, code;
225 regnode *scan = *scanp, *next;
227 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
228 int is_inf_internal = 0; /* The studied chunk is infinite */
229 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
230 scan_data_t data_fake;
232 while (scan && OP(scan) != END && scan < last) {
233 /* Peephole optimizer: */
235 if (PL_regkind[(U8)OP(scan)] == EXACT) {
236 regnode *n = regnext(scan);
239 regnode *stop = scan;
242 next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
243 /* Skip NOTHING, merge EXACT*. */
245 ( PL_regkind[(U8)OP(n)] == NOTHING ||
246 (stringok && (OP(n) == OP(scan))))
248 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
249 if (OP(n) == TAIL || n > next)
251 if (PL_regkind[(U8)OP(n)] == NOTHING) {
252 NEXT_OFF(scan) += NEXT_OFF(n);
253 next = n + NODE_STEP_REGNODE;
261 int oldl = *OPERAND(scan);
262 regnode *nnext = regnext(n);
264 if (oldl + *OPERAND(n) > U8_MAX)
266 NEXT_OFF(scan) += NEXT_OFF(n);
267 *OPERAND(scan) += *OPERAND(n);
268 next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2;
269 /* Now we can overwrite *n : */
270 Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1,
271 *OPERAND(n) + 1, char);
281 n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
283 /* Purify reports a benign UMR here sometimes, because we
284 * don't initialize the OP() slot of a node when that node
285 * is occupied by just the trailing null of the string in
287 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
296 if (OP(scan) != CURLYX) {
297 int max = (reg_off_by_arg[OP(scan)]
299 /* I32 may be smaller than U16 on CRAYs! */
300 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
301 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
305 /* Skip NOTHING and LONGJMP. */
306 while ((n = regnext(n))
307 && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
308 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
311 if (reg_off_by_arg[OP(scan)])
314 NEXT_OFF(scan) = off;
316 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
317 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
318 next = regnext(scan);
321 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
322 I32 max1 = 0, min1 = I32_MAX, num = 0;
324 if (flags & SCF_DO_SUBSTR)
326 while (OP(scan) == code) {
327 I32 deltanext, minnext;
331 next = regnext(scan);
332 scan = NEXTOPER(scan);
334 scan = NEXTOPER(scan);
335 /* We suppose the run is continuous, last=next...*/
336 minnext = study_chunk(&scan, &deltanext, next,
340 if (max1 < minnext + deltanext)
341 max1 = minnext + deltanext;
342 if (deltanext == I32_MAX)
343 is_inf = is_inf_internal = 1;
345 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
347 if (data && (data_fake.flags & SF_HAS_EVAL))
348 data->flags |= SF_HAS_EVAL;
352 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
354 if (flags & SCF_DO_SUBSTR) {
355 data->pos_min += min1;
356 data->pos_delta += max1 - min1;
357 if (max1 != min1 || is_inf)
358 data->longest = &(data->longest_float);
361 delta += max1 - min1;
363 else if (code == BRANCHJ) /* single branch is optimized. */
364 scan = NEXTOPER(NEXTOPER(scan));
365 else /* single branch is optimized. */
366 scan = NEXTOPER(scan);
369 else if (OP(scan) == EXACT) {
370 I32 l = *OPERAND(scan);
372 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
373 unsigned char *e = s + l;
382 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
383 /* The code below prefers earlier match for fixed
384 offset, later match for variable offset. */
385 if (data->last_end == -1) { /* Update the start info. */
386 data->last_start_min = data->pos_min;
387 data->last_start_max = is_inf
388 ? I32_MAX : data->pos_min + data->pos_delta;
390 sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), *OPERAND(scan));
391 data->last_end = data->pos_min + l;
392 data->pos_min += l; /* As in the first entry. */
393 data->flags &= ~SF_BEFORE_EOL;
396 else if (PL_regkind[(U8)OP(scan)] == EXACT) {
397 I32 l = *OPERAND(scan);
398 if (flags & SCF_DO_SUBSTR)
401 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
402 unsigned char *e = s + l;
411 if (data && (flags & SCF_DO_SUBSTR))
414 else if (strchr((char*)PL_varies,OP(scan))) {
415 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
416 regnode *oscan = scan;
418 switch (PL_regkind[(U8)OP(scan)]) {
420 scan = NEXTOPER(scan);
423 if (flags & SCF_DO_SUBSTR) {
424 next = NEXTOPER(scan);
425 if (OP(next) == EXACT) {
427 maxcount = REG_INFTY;
428 next = regnext(scan);
429 scan = NEXTOPER(scan);
433 if (flags & SCF_DO_SUBSTR)
438 is_inf = is_inf_internal = 1;
439 scan = regnext(scan);
440 if (flags & SCF_DO_SUBSTR) {
442 data->longest = &(data->longest_float);
444 goto optimize_curly_tail;
446 mincount = ARG1(scan);
447 maxcount = ARG2(scan);
448 next = regnext(scan);
449 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
451 if (flags & SCF_DO_SUBSTR) {
452 if (mincount == 0) scan_commit(data);
453 pos_before = data->pos_min;
457 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
459 data->flags |= SF_IS_INF;
461 /* This will finish on WHILEM, setting scan, or on NULL: */
462 minnext = study_chunk(&scan, &deltanext, last, data,
464 ? (flags & ~SCF_DO_SUBSTR) : flags);
465 if (!scan) /* It was not CURLYX, but CURLY. */
467 if (ckWARN(WARN_UNSAFE) && (minnext + deltanext == 0)
468 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
469 && maxcount <= REG_INFTY/3) /* Complement check for big count */
470 Perl_warner(aTHX_ WARN_UNSAFE,
471 "Strange *+?{} on zero-length expression");
472 min += minnext * mincount;
473 is_inf_internal |= (maxcount == REG_INFTY
474 && (minnext + deltanext) > 0
475 || deltanext == I32_MAX);
476 is_inf |= is_inf_internal;
477 delta += (minnext + deltanext) * maxcount - minnext * mincount;
479 /* Try powerful optimization CURLYX => CURLYN. */
480 if ( OP(oscan) == CURLYX && data
481 && data->flags & SF_IN_PAR
482 && !(data->flags & SF_HAS_EVAL)
483 && !deltanext && minnext == 1 ) {
484 /* Try to optimize to CURLYN. */
485 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
486 regnode *nxt1 = nxt, *nxt2;
490 if (!strchr((char*)PL_simple,OP(nxt))
491 && !(PL_regkind[(U8)OP(nxt)] == EXACT
492 && *OPERAND(nxt) == 1))
496 if (OP(nxt) != CLOSE)
498 /* Now we know that nxt2 is the only contents: */
499 oscan->flags = ARG(nxt);
501 OP(nxt1) = NOTHING; /* was OPEN. */
503 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
504 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
505 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
506 OP(nxt) = OPTIMIZED; /* was CLOSE. */
507 OP(nxt + 1) = OPTIMIZED; /* was count. */
508 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
513 /* Try optimization CURLYX => CURLYM. */
514 if ( OP(oscan) == CURLYX && data
515 && !(data->flags & SF_HAS_PAR)
516 && !(data->flags & SF_HAS_EVAL)
518 /* XXXX How to optimize if data == 0? */
519 /* Optimize to a simpler form. */
520 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
524 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
525 && (OP(nxt2) != WHILEM))
527 OP(nxt2) = SUCCEED; /* Whas WHILEM */
528 /* Need to optimize away parenths. */
529 if (data->flags & SF_IN_PAR) {
530 /* Set the parenth number. */
531 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
533 if (OP(nxt) != CLOSE)
534 FAIL("panic opt close");
535 oscan->flags = ARG(nxt);
536 OP(nxt1) = OPTIMIZED; /* was OPEN. */
537 OP(nxt) = OPTIMIZED; /* was CLOSE. */
539 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
540 OP(nxt + 1) = OPTIMIZED; /* was count. */
541 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
542 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
545 while ( nxt1 && (OP(nxt1) != WHILEM)) {
546 regnode *nnxt = regnext(nxt1);
549 if (reg_off_by_arg[OP(nxt1)])
550 ARG_SET(nxt1, nxt2 - nxt1);
551 else if (nxt2 - nxt1 < U16_MAX)
552 NEXT_OFF(nxt1) = nxt2 - nxt1;
554 OP(nxt) = NOTHING; /* Cannot beautify */
559 /* Optimize again: */
560 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
565 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
567 if (flags & SCF_DO_SUBSTR) {
568 SV *last_str = Nullsv;
569 int counted = mincount != 0;
571 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
572 I32 b = pos_before >= data->last_start_min
573 ? pos_before : data->last_start_min;
575 char *s = SvPV(data->last_found, l);
576 I32 old = b - data->last_start_min;
579 old = utf8_hop((U8*)s, old) - (U8*)s;
582 /* Get the added string: */
583 last_str = newSVpvn(s + old, l);
584 if (deltanext == 0 && pos_before == b) {
585 /* What was added is a constant string */
587 SvGROW(last_str, (mincount * l) + 1);
588 repeatcpy(SvPVX(last_str) + l,
589 SvPVX(last_str), l, mincount - 1);
590 SvCUR(last_str) *= mincount;
591 /* Add additional parts. */
592 SvCUR_set(data->last_found,
593 SvCUR(data->last_found) - l);
594 sv_catsv(data->last_found, last_str);
595 data->last_end += l * (mincount - 1);
599 /* It is counted once already... */
600 data->pos_min += minnext * (mincount - counted);
601 data->pos_delta += - counted * deltanext +
602 (minnext + deltanext) * maxcount - minnext * mincount;
603 if (mincount != maxcount) {
605 if (mincount && last_str) {
606 sv_setsv(data->last_found, last_str);
607 data->last_end = data->pos_min;
608 data->last_start_min =
609 data->pos_min - CHR_SVLEN(last_str);
610 data->last_start_max = is_inf
612 : data->pos_min + data->pos_delta
613 - CHR_SVLEN(last_str);
615 data->longest = &(data->longest_float);
617 SvREFCNT_dec(last_str);
619 if (data && (fl & SF_HAS_EVAL))
620 data->flags |= SF_HAS_EVAL;
622 if (OP(oscan) != CURLYX) {
623 while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
625 NEXT_OFF(oscan) += NEXT_OFF(next);
628 default: /* REF only? */
629 if (flags & SCF_DO_SUBSTR) {
631 data->longest = &(data->longest_float);
633 is_inf = is_inf_internal = 1;
637 else if (strchr((char*)PL_simple,OP(scan)) || PL_regkind[(U8)OP(scan)] == ANYUTF8) {
638 if (flags & SCF_DO_SUBSTR) {
644 else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
645 data->flags |= (OP(scan) == MEOL
649 else if (PL_regkind[(U8)OP(scan)] == BRANCHJ
650 && (scan->flags || data)
651 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
652 I32 deltanext, minnext;
656 next = regnext(scan);
657 nscan = NEXTOPER(NEXTOPER(scan));
658 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
661 FAIL("variable length lookbehind not implemented");
663 else if (minnext > U8_MAX) {
664 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
666 scan->flags = minnext;
668 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
670 if (data && (data_fake.flags & SF_HAS_EVAL))
671 data->flags |= SF_HAS_EVAL;
673 else if (OP(scan) == OPEN) {
676 else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
677 next = regnext(scan);
679 if ( next && (OP(next) != WHILEM) && next < last)
680 is_par = 0; /* Disable optimization */
682 else if (OP(scan) == EVAL) {
684 data->flags |= SF_HAS_EVAL;
686 else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded */
687 if (flags & SCF_DO_SUBSTR) {
689 data->longest = &(data->longest_float);
691 is_inf = is_inf_internal = 1;
693 /* Else: zero-length, ignore. */
694 scan = regnext(scan);
699 *deltap = is_inf_internal ? I32_MAX : delta;
700 if (flags & SCF_DO_SUBSTR && is_inf)
701 data->pos_delta = I32_MAX - data->pos_min;
704 if (is_par && pars==1 && data) {
705 data->flags |= SF_IN_PAR;
706 data->flags &= ~SF_HAS_PAR;
708 else if (pars && data) {
709 data->flags |= SF_HAS_PAR;
710 data->flags &= ~SF_IN_PAR;
716 S_add_data(pTHX_ I32 n, char *s)
719 if (PL_regcomp_rx->data) {
720 Renewc(PL_regcomp_rx->data,
721 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
722 char, struct reg_data);
723 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
724 PL_regcomp_rx->data->count += n;
727 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
728 char, struct reg_data);
729 New(1208, PL_regcomp_rx->data->what, n, U8);
730 PL_regcomp_rx->data->count = n;
732 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
733 return PL_regcomp_rx->data->count - n;
737 Perl_reginitcolors(pTHX)
741 char *s = PerlEnv_getenv("PERL_RE_COLORS");
744 PL_colors[0] = s = savepv(s);
752 PL_colors[i] = s = "";
762 - pregcomp - compile a regular expression into internal code
764 * We can't allocate space until we know how big the compiled form will be,
765 * but we can't compile it (and thus know how big it is) until we've got a
766 * place to put the code. So we cheat: we compile it twice, once with code
767 * generation turned off and size counting turned on, and once "for real".
768 * This also means that we don't allocate space until we are sure that the
769 * thing really will compile successfully, and we never have to move the
770 * code and thus invalidate pointers into it. (Note that it has to be in
771 * one piece because free() must be able to free it all.) [NB: not true in perl]
773 * Beware that the optimization-preparation code in here knows about some
774 * of the structure of the compiled regexp. [I'll say.]
777 Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
792 FAIL("NULL regexp argument");
794 if (PL_curcop == &PL_compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
795 PL_reg_flags |= RF_utf8;
799 PL_regprecomp = savepvn(exp, xend - exp);
800 DEBUG_r(if (!PL_colorset) reginitcolors());
801 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling%s RE `%s%*s%s'\n",
802 PL_colors[4],PL_colors[5],PL_colors[0],
803 xend - exp, PL_regprecomp, PL_colors[1]));
804 PL_regflags = pm->op_pmflags;
808 PL_seen_zerolen = *exp == '^' ? -1 : 0;
812 /* First pass: determine size, legality. */
813 PL_regcomp_parse = exp;
818 PL_regcode = &PL_regdummy;
819 regc((U8)REG_MAGIC, (char*)PL_regcode);
820 if (reg(0, &flags) == NULL) {
821 Safefree(PL_regprecomp);
822 PL_regprecomp = Nullch;
825 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
827 /* Small enough for pointer-storage convention?
828 If extralen==0, this means that we will not need long jumps. */
829 if (PL_regsize >= 0x10000L && PL_extralen)
830 PL_regsize += PL_extralen;
834 /* Allocate space and initialize. */
835 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
838 FAIL("regexp out of space");
840 r->prelen = xend - exp;
841 r->precomp = PL_regprecomp;
843 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
844 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
846 r->substrs = 0; /* Useful during FAIL. */
847 r->startp = 0; /* Useful during FAIL. */
848 r->endp = 0; /* Useful during FAIL. */
852 /* Second pass: emit code. */
853 PL_regcomp_parse = exp;
857 PL_regcode = r->program;
858 /* Store the count of eval-groups for security checks: */
859 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
860 regc((U8)REG_MAGIC, (char*) PL_regcode++);
862 if (reg(0, &flags) == NULL)
865 /* Dig out information for optimizations. */
866 r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
867 pm->op_pmflags = PL_regflags;
869 r->reganch |= ROPT_UTF8;
870 r->regstclass = NULL;
871 if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
872 r->reganch |= ROPT_NAUGHTY;
873 scan = r->program + 1; /* First BRANCH. */
875 /* XXXX To minimize changes to RE engine we always allocate
876 3-units-long substrs field. */
877 Newz(1004, r->substrs, 1, struct reg_substr_data);
879 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
882 STRLEN longest_float_length, longest_fixed_length;
884 StructCopy(&zero_scan_data, &data, scan_data_t);
886 /* Skip introductions and multiplicators >= 1. */
887 while ((OP(first) == OPEN && (sawopen = 1)) ||
888 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
889 (OP(first) == PLUS) ||
890 (OP(first) == MINMOD) ||
891 (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
892 if (OP(first) == PLUS)
895 first += regarglen[(U8)OP(first)];
896 first = NEXTOPER(first);
899 /* Starting-point info. */
901 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
902 else if (strchr((char*)PL_simple+4,OP(first)))
903 r->regstclass = first;
904 else if (PL_regkind[(U8)OP(first)] == BOUND ||
905 PL_regkind[(U8)OP(first)] == NBOUND)
906 r->regstclass = first;
907 else if (PL_regkind[(U8)OP(first)] == BOL) {
908 r->reganch |= (OP(first) == MBOL
913 first = NEXTOPER(first);
916 else if (OP(first) == GPOS) {
917 r->reganch |= ROPT_ANCH_GPOS;
918 first = NEXTOPER(first);
921 else if ((OP(first) == STAR &&
922 PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
923 !(r->reganch & ROPT_ANCH) )
925 /* turn .* into ^.* with an implied $*=1 */
926 int type = OP(NEXTOPER(first));
928 if (type == REG_ANY || type == ANYUTF8)
929 type = ROPT_ANCH_MBOL;
931 type = ROPT_ANCH_SBOL;
933 r->reganch |= type | ROPT_IMPLICIT;
934 first = NEXTOPER(first);
937 if (sawplus && (!sawopen || !PL_regsawback)
938 && !(PL_regseen & REG_SEEN_EVAL)) /* May examine pos and $& */
939 /* x+ must match at the 1st pos of run of x's */
940 r->reganch |= ROPT_SKIP;
942 /* Scan is after the zeroth branch, first is atomic matcher. */
943 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
946 * If there's something expensive in the r.e., find the
947 * longest literal string that must appear and make it the
948 * regmust. Resolve ties in favor of later strings, since
949 * the regstart check works with the beginning of the r.e.
950 * and avoiding duplication strengthens checking. Not a
951 * strong reason, but sufficient in the absence of others.
952 * [Now we resolve ties in favor of the earlier string if
953 * it happens that c_offset_min has been invalidated, since the
954 * earlier string may buy us something the later one won't.]
958 data.longest_fixed = newSVpvn("",0);
959 data.longest_float = newSVpvn("",0);
960 data.last_found = newSVpvn("",0);
961 data.longest = &(data.longest_fixed);
964 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
965 &data, SCF_DO_SUBSTR);
966 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
967 && data.last_start_min == 0 && data.last_end > 0
969 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
970 r->reganch |= ROPT_CHECK_ALL;
972 SvREFCNT_dec(data.last_found);
974 longest_float_length = CHR_SVLEN(data.longest_float);
975 if (longest_float_length
976 || (data.flags & SF_FL_BEFORE_EOL
977 && (!(data.flags & SF_FL_BEFORE_MEOL)
978 || (PL_regflags & PMf_MULTILINE)))) {
981 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
982 && data.offset_fixed == data.offset_float_min
983 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
984 goto remove_float; /* As in (a)+. */
986 r->float_substr = data.longest_float;
987 r->float_min_offset = data.offset_float_min;
988 r->float_max_offset = data.offset_float_max;
989 t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
990 && (!(data.flags & SF_FL_BEFORE_MEOL)
991 || (PL_regflags & PMf_MULTILINE)));
992 fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
996 r->float_substr = Nullsv;
997 SvREFCNT_dec(data.longest_float);
998 longest_float_length = 0;
1001 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
1002 if (longest_fixed_length
1003 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1004 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1005 || (PL_regflags & PMf_MULTILINE)))) {
1008 r->anchored_substr = data.longest_fixed;
1009 r->anchored_offset = data.offset_fixed;
1010 t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
1011 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1012 || (PL_regflags & PMf_MULTILINE)));
1013 fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
1016 r->anchored_substr = Nullsv;
1017 SvREFCNT_dec(data.longest_fixed);
1018 longest_fixed_length = 0;
1021 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1022 if (longest_fixed_length > longest_float_length) {
1023 r->check_substr = r->anchored_substr;
1024 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1025 if (r->reganch & ROPT_ANCH_SINGLE)
1026 r->reganch |= ROPT_NOSCAN;
1029 r->check_substr = r->float_substr;
1030 r->check_offset_min = data.offset_float_min;
1031 r->check_offset_max = data.offset_float_max;
1033 if (r->check_substr) {
1034 r->reganch |= RE_USE_INTUIT;
1035 if (SvTAIL(r->check_substr))
1036 r->reganch |= RE_INTUIT_TAIL;
1040 /* Several toplevels. Best we can is to set minlen. */
1043 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1044 scan = r->program + 1;
1045 minlen = study_chunk(&scan, &fake, scan + PL_regsize, NULL, 0);
1046 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1050 if (PL_regseen & REG_SEEN_GPOS)
1051 r->reganch |= ROPT_GPOS_SEEN;
1052 if (PL_regseen & REG_SEEN_LOOKBEHIND)
1053 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1054 if (PL_regseen & REG_SEEN_EVAL)
1055 r->reganch |= ROPT_EVAL_SEEN;
1056 Newz(1002, r->startp, PL_regnpar, I32);
1057 Newz(1002, r->endp, PL_regnpar, I32);
1058 DEBUG_r(regdump(r));
1063 - reg - regular expression, i.e. main body or parenthesized thing
1065 * Caller must absorb opening parenthesis.
1067 * Combining parenthesis handling with the base level of regular expression
1068 * is a trifle forced, but the need to tie the tails of the branches to what
1069 * follows makes it hard to avoid.
1072 S_reg(pTHX_ I32 paren, I32 *flagp)
1073 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1076 register regnode *ret; /* Will be the head of the group. */
1077 register regnode *br;
1078 register regnode *lastbr;
1079 register regnode *ender = 0;
1080 register I32 parno = 0;
1081 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
1084 *flagp = 0; /* Tentatively. */
1086 /* Make an OPEN node, if parenthesized. */
1088 if (*PL_regcomp_parse == '?') {
1089 U16 posflags = 0, negflags = 0;
1090 U16 *flagsp = &posflags;
1094 paren = *PL_regcomp_parse++;
1095 ret = NULL; /* For look-ahead/behind. */
1098 PL_regseen |= REG_SEEN_LOOKBEHIND;
1099 if (*PL_regcomp_parse == '!')
1101 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
1112 FAIL2("Sequence (?%c...) not implemented", (int)paren);
1115 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1117 if (*PL_regcomp_parse != ')')
1118 FAIL("Sequence (?#... not terminated");
1124 paren = *PL_regcomp_parse++;
1129 I32 count = 1, n = 0;
1131 char *s = PL_regcomp_parse;
1133 OP_4tree *sop, *rop;
1136 PL_regseen |= REG_SEEN_EVAL;
1137 while (count && (c = *PL_regcomp_parse)) {
1138 if (c == '\\' && PL_regcomp_parse[1])
1146 if (*PL_regcomp_parse != ')')
1147 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1151 if (PL_regcomp_parse - 1 - s)
1152 sv = newSVpvn(s, PL_regcomp_parse - 1 - s);
1154 sv = newSVpvn("", 0);
1156 rop = sv_compile_2op(sv, &sop, "re", &av);
1158 n = add_data(3, "nop");
1159 PL_regcomp_rx->data->data[n] = (void*)rop;
1160 PL_regcomp_rx->data->data[n+1] = (void*)sop;
1161 PL_regcomp_rx->data->data[n+2] = (void*)av;
1164 else { /* First pass */
1165 if (PL_reginterp_cnt < ++PL_seen_evals
1166 && PL_curcop != &PL_compiling)
1167 /* No compiled RE interpolated, has runtime
1168 components ===> unsafe. */
1169 FAIL("Eval-group not allowed at runtime, use re 'eval'");
1171 FAIL("Eval-group in insecure regular expression");
1176 ret = reg_node(LOGICAL);
1179 regtail(ret, reganode(EVAL, n));
1182 return reganode(EVAL, n);
1186 if (PL_regcomp_parse[0] == '?') {
1187 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1188 || PL_regcomp_parse[1] == '<'
1189 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
1192 ret = reg_node(LOGICAL);
1195 regtail(ret, reg(1, &flag));
1199 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
1200 parno = atoi(PL_regcomp_parse++);
1202 while (isDIGIT(*PL_regcomp_parse))
1204 ret = reganode(GROUPP, parno);
1205 if ((c = *nextchar()) != ')')
1206 FAIL2("Switch (?(number%c not recognized", c);
1208 regtail(ret, reganode(IFTHEN, 0));
1209 br = regbranch(&flags, 1);
1211 br = reganode(LONGJMP, 0);
1213 regtail(br, reganode(LONGJMP, 0));
1218 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1219 regbranch(&flags, 1);
1220 regtail(ret, lastbr);
1228 FAIL("Switch (?(condition)... contains too many branches");
1229 ender = reg_node(TAIL);
1232 regtail(lastbr, ender);
1233 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1236 regtail(ret, ender);
1240 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
1244 FAIL("Sequence (? incomplete");
1249 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1250 if (*PL_regcomp_parse != 'o')
1251 pmflag(flagsp, *PL_regcomp_parse);
1254 if (*PL_regcomp_parse == '-') {
1259 PL_regflags |= posflags;
1260 PL_regflags &= ~negflags;
1261 if (*PL_regcomp_parse == ':') {
1267 if (*PL_regcomp_parse != ')')
1268 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
1277 ret = reganode(OPEN, parno);
1284 /* Pick up the branches, linking them together. */
1285 br = regbranch(&flags, 1);
1288 if (*PL_regcomp_parse == '|') {
1289 if (!SIZE_ONLY && PL_extralen) {
1290 reginsert(BRANCHJ, br);
1293 reginsert(BRANCH, br);
1296 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
1298 else if (paren == ':') {
1299 *flagp |= flags&SIMPLE;
1301 if (open) { /* Starts with OPEN. */
1302 regtail(ret, br); /* OPEN -> first. */
1304 else if (paren != '?') /* Not Conditional */
1308 *flagp |= flags&SPSTART;
1310 while (*PL_regcomp_parse == '|') {
1311 if (!SIZE_ONLY && PL_extralen) {
1312 ender = reganode(LONGJMP,0);
1313 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1316 PL_extralen += 2; /* Account for LONGJMP. */
1318 br = regbranch(&flags, 0);
1321 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1325 *flagp |= flags&SPSTART;
1328 if (have_branch || paren != ':') {
1329 /* Make a closing node, and hook it on the end. */
1332 ender = reg_node(TAIL);
1335 ender = reganode(CLOSE, parno);
1341 *flagp &= ~HASWIDTH;
1344 ender = reg_node(SUCCEED);
1347 ender = reg_node(END);
1350 regtail(lastbr, ender);
1353 /* Hook the tails of the branches to the closing node. */
1354 for (br = ret; br != NULL; br = regnext(br)) {
1355 regoptail(br, ender);
1362 static char parens[] = "=!<,>";
1364 if (paren && (p = strchr(parens, paren))) {
1365 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1366 int flag = (p - parens) > 1;
1369 node = SUSPEND, flag = 0;
1370 reginsert(node,ret);
1372 regtail(ret, reg_node(TAIL));
1376 /* Check for proper termination. */
1378 PL_regflags = oregflags;
1379 if (PL_regcomp_parse >= PL_regxend || *nextchar() != ')') {
1380 FAIL("unmatched () in regexp");
1383 else if (!paren && PL_regcomp_parse < PL_regxend) {
1384 if (*PL_regcomp_parse == ')') {
1385 FAIL("unmatched () in regexp");
1388 FAIL("junk on end of regexp"); /* "Can't happen". */
1396 - regbranch - one alternative of an | operator
1398 * Implements the concatenation operator.
1401 S_regbranch(pTHX_ I32 *flagp, I32 first)
1404 register regnode *ret;
1405 register regnode *chain = NULL;
1406 register regnode *latest;
1407 I32 flags = 0, c = 0;
1412 if (!SIZE_ONLY && PL_extralen)
1413 ret = reganode(BRANCHJ,0);
1415 ret = reg_node(BRANCH);
1418 if (!first && SIZE_ONLY)
1419 PL_extralen += 1; /* BRANCHJ */
1421 *flagp = WORST; /* Tentatively. */
1425 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
1427 latest = regpiece(&flags);
1428 if (latest == NULL) {
1429 if (flags & TRYAGAIN)
1433 else if (ret == NULL)
1435 *flagp |= flags&HASWIDTH;
1436 if (chain == NULL) /* First piece. */
1437 *flagp |= flags&SPSTART;
1440 regtail(chain, latest);
1445 if (chain == NULL) { /* Loop ran zero times. */
1446 chain = reg_node(NOTHING);
1451 *flagp |= flags&SIMPLE;
1458 - regpiece - something followed by possible [*+?]
1460 * Note that the branching code sequences used for ? and the general cases
1461 * of * and + are somewhat optimized: they use the same NOTHING node as
1462 * both the endmarker for their branch list and the body of the last branch.
1463 * It might seem that this node could be dispensed with entirely, but the
1464 * endmarker role is not redundant.
1467 S_regpiece(pTHX_ I32 *flagp)
1470 register regnode *ret;
1472 register char *next;
1474 char *origparse = PL_regcomp_parse;
1477 I32 max = REG_INFTY;
1479 ret = regatom(&flags);
1481 if (flags & TRYAGAIN)
1486 op = *PL_regcomp_parse;
1488 if (op == '{' && regcurly(PL_regcomp_parse)) {
1489 next = PL_regcomp_parse + 1;
1491 while (isDIGIT(*next) || *next == ',') {
1500 if (*next == '}') { /* got one */
1504 min = atoi(PL_regcomp_parse);
1508 maxpos = PL_regcomp_parse;
1510 if (!max && *maxpos != '0')
1511 max = REG_INFTY; /* meaning "infinity" */
1512 else if (max >= REG_INFTY)
1513 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
1514 PL_regcomp_parse = next;
1518 if ((flags&SIMPLE)) {
1519 PL_regnaughty += 2 + PL_regnaughty / 2;
1520 reginsert(CURLY, ret);
1523 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
1524 regtail(ret, reg_node(WHILEM));
1525 if (!SIZE_ONLY && PL_extralen) {
1526 reginsert(LONGJMP,ret);
1527 reginsert(NOTHING,ret);
1528 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1530 reginsert(CURLYX,ret);
1531 if (!SIZE_ONLY && PL_extralen)
1532 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1533 regtail(ret, reg_node(NOTHING));
1543 if (max && max < min)
1544 FAIL("Can't do {n,m} with n > m");
1559 #if 0 /* Now runtime fix should be reliable. */
1560 if (!(flags&HASWIDTH) && op != '?')
1561 FAIL("regexp *+ operand could be empty");
1566 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
1568 if (op == '*' && (flags&SIMPLE)) {
1569 reginsert(STAR, ret);
1573 else if (op == '*') {
1577 else if (op == '+' && (flags&SIMPLE)) {
1578 reginsert(PLUS, ret);
1582 else if (op == '+') {
1586 else if (op == '?') {
1591 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
1592 Perl_warner(aTHX_ WARN_UNSAFE, "%.*s matches null string many times",
1593 PL_regcomp_parse - origparse, origparse);
1596 if (*PL_regcomp_parse == '?') {
1598 reginsert(MINMOD, ret);
1599 regtail(ret, ret + NODE_STEP_REGNODE);
1601 if (ISMULT2(PL_regcomp_parse))
1602 FAIL("nested *?+ in regexp");
1608 - regatom - the lowest level
1610 * Optimization: gobbles an entire sequence of ordinary characters so that
1611 * it can turn them into a single node, which is smaller to store and
1612 * faster to run. Backslashed characters are exceptions, each becoming a
1613 * separate node; the code is simpler that way and it's not worth fixing.
1615 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1618 S_regatom(pTHX_ I32 *flagp)
1621 register regnode *ret = 0;
1624 *flagp = WORST; /* Tentatively. */
1627 switch (*PL_regcomp_parse) {
1631 if (PL_regflags & PMf_MULTILINE)
1632 ret = reg_node(MBOL);
1633 else if (PL_regflags & PMf_SINGLELINE)
1634 ret = reg_node(SBOL);
1636 ret = reg_node(BOL);
1639 if (PL_regcomp_parse[1])
1642 if (PL_regflags & PMf_MULTILINE)
1643 ret = reg_node(MEOL);
1644 else if (PL_regflags & PMf_SINGLELINE)
1645 ret = reg_node(SEOL);
1647 ret = reg_node(EOL);
1652 if (PL_regflags & PMf_SINGLELINE)
1653 ret = reg_node(SANYUTF8);
1655 ret = reg_node(ANYUTF8);
1659 if (PL_regflags & PMf_SINGLELINE)
1660 ret = reg_node(SANY);
1662 ret = reg_node(REG_ANY);
1663 *flagp |= HASWIDTH|SIMPLE;
1669 ret = (UTF ? regclassutf8() : regclass());
1670 if (*PL_regcomp_parse != ']')
1671 FAIL("unmatched [] in regexp");
1673 *flagp |= HASWIDTH|SIMPLE;
1677 ret = reg(1, &flags);
1679 if (flags & TRYAGAIN)
1683 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
1687 if (flags & TRYAGAIN) {
1691 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
1692 /* Supposed to be caught earlier. */
1695 if (!regcurly(PL_regcomp_parse)) {
1703 FAIL("?+*{} follows nothing in regexp");
1706 switch (*++PL_regcomp_parse) {
1709 ret = reg_node(SBOL);
1714 ret = reg_node(GPOS);
1715 PL_regseen |= REG_SEEN_GPOS;
1720 ret = reg_node(SEOL);
1725 ret = reg_node(EOS);
1727 PL_seen_zerolen++; /* Do not optimize RE away */
1731 ret = reg_node(SANY);
1732 *flagp |= HASWIDTH|SIMPLE;
1736 ret = reg_node(CLUMP);
1739 if (UTF && !PL_utf8_mark)
1740 is_utf8_mark((U8*)"~"); /* preload table */
1745 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
1746 : (LOC ? ALNUML : ALNUM));
1747 *flagp |= HASWIDTH|SIMPLE;
1749 if (UTF && !PL_utf8_alnum)
1750 is_utf8_alnum((U8*)"a"); /* preload table */
1755 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
1756 : (LOC ? NALNUML : NALNUM));
1757 *flagp |= HASWIDTH|SIMPLE;
1759 if (UTF && !PL_utf8_alnum)
1760 is_utf8_alnum((U8*)"a"); /* preload table */
1764 PL_regseen |= REG_SEEN_LOOKBEHIND;
1767 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
1768 : (LOC ? BOUNDL : BOUND));
1771 if (UTF && !PL_utf8_alnum)
1772 is_utf8_alnum((U8*)"a"); /* preload table */
1776 PL_regseen |= REG_SEEN_LOOKBEHIND;
1779 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
1780 : (LOC ? NBOUNDL : NBOUND));
1783 if (UTF && !PL_utf8_alnum)
1784 is_utf8_alnum((U8*)"a"); /* preload table */
1789 ? (LOC ? SPACELUTF8 : SPACEUTF8)
1790 : (LOC ? SPACEL : SPACE));
1791 *flagp |= HASWIDTH|SIMPLE;
1793 if (UTF && !PL_utf8_space)
1794 is_utf8_space((U8*)" "); /* preload table */
1799 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
1800 : (LOC ? NSPACEL : NSPACE));
1801 *flagp |= HASWIDTH|SIMPLE;
1803 if (UTF && !PL_utf8_space)
1804 is_utf8_space((U8*)" "); /* preload table */
1807 ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
1808 *flagp |= HASWIDTH|SIMPLE;
1810 if (UTF && !PL_utf8_digit)
1811 is_utf8_digit((U8*)"1"); /* preload table */
1814 ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
1815 *flagp |= HASWIDTH|SIMPLE;
1817 if (UTF && !PL_utf8_digit)
1818 is_utf8_digit((U8*)"1"); /* preload table */
1822 { /* a lovely hack--pretend we saw [\pX] instead */
1823 char* oldregxend = PL_regxend;
1825 if (PL_regcomp_parse[1] == '{') {
1826 PL_regxend = strchr(PL_regcomp_parse, '}');
1828 FAIL("Missing right brace on \\p{}");
1832 PL_regxend = PL_regcomp_parse + 2;
1835 ret = regclassutf8();
1837 PL_regxend = oldregxend;
1840 *flagp |= HASWIDTH|SIMPLE;
1853 case '1': case '2': case '3': case '4':
1854 case '5': case '6': case '7': case '8': case '9':
1856 I32 num = atoi(PL_regcomp_parse);
1858 if (num > 9 && num >= PL_regnpar)
1861 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
1862 FAIL("reference to nonexistent group");
1865 ? (LOC ? REFFL : REFF)
1868 while (isDIGIT(*PL_regcomp_parse))
1876 if (PL_regcomp_parse >= PL_regxend)
1877 FAIL("trailing \\ in regexp");
1880 /* Do not generate `unrecognized' warnings here, we fall
1881 back into the quick-grab loop below */
1887 if (PL_regflags & PMf_EXTENDED) {
1888 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1889 if (PL_regcomp_parse < PL_regxend)
1905 ? (LOC ? EXACTFL : EXACTF)
1907 s = (char *) OPERAND(ret);
1908 regc(0, s++); /* save spot for len */
1909 for (len = 0, p = PL_regcomp_parse - 1;
1910 len < 127 && p < PL_regxend;
1915 if (PL_regflags & PMf_EXTENDED)
1916 p = regwhite(p, PL_regxend);
1970 char* e = strchr(p, '}');
1973 FAIL("Missing right brace on \\x{}");
1975 ender = scan_hex(p + 1, e - p, &numlen);
1976 if (numlen + len >= 127) { /* numlen is generous */
1983 FAIL("Can't use \\x{} without 'use utf8' declaration");
1986 ender = scan_hex(p, 2, &numlen);
1992 ender = UCHARAT(p++);
1993 ender = toCTRL(ender);
1995 case '0': case '1': case '2': case '3':case '4':
1996 case '5': case '6': case '7': case '8':case '9':
1998 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
1999 ender = scan_oct(p, 3, &numlen);
2008 if (p >= PL_regxend)
2009 FAIL("trailing \\ in regexp");
2012 if (!SIZE_ONLY && ckWARN(WARN_UNSAFE) && isALPHA(*p))
2013 Perl_warner(aTHX_ WARN_UNSAFE,
2014 "/%.127s/: Unrecognized escape \\%c passed through",
2017 goto normal_default;
2022 if ((*p & 0xc0) == 0xc0 && UTF) {
2023 ender = utf8_to_uv((U8*)p, &numlen);
2030 if (PL_regflags & PMf_EXTENDED)
2031 p = regwhite(p, PL_regxend);
2034 ender = toLOWER_LC_uni(ender);
2036 ender = toLOWER_uni(ender);
2038 if (ISMULT2(p)) { /* Back off on ?+*. */
2041 else if (ender >= 0x80 && UTF) {
2042 reguni(ender, s, &numlen);
2052 if (ender >= 0x80 && UTF) {
2053 reguni(ender, s, &numlen);
2061 PL_regcomp_parse = p - 1;
2064 FAIL("internal disaster in regexp");
2070 *OPERAND(ret) = len;
2073 PL_regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2076 PL_regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2086 S_regwhite(pTHX_ char *p, char *e)
2091 else if (*p == '#') {
2094 } while (p < e && *p != '\n');
2102 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
2103 Character classes ([:foo:]) can also be negated ([:^foo:]).
2104 Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
2105 Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
2106 but trigger warnings because they are currently unimplemented. */
2108 S_regpposixcc(pTHX_ I32 value)
2112 I32 namedclass = -1;
2114 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2115 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2116 (*PL_regcomp_parse == ':' ||
2117 *PL_regcomp_parse == '=' ||
2118 *PL_regcomp_parse == '.')) {
2119 char c = *PL_regcomp_parse;
2120 char* s = PL_regcomp_parse++;
2122 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != c)
2124 if (PL_regcomp_parse == PL_regxend)
2125 /* Grandfather lone [:, [=, [. */
2126 PL_regcomp_parse = s;
2128 char* t = PL_regcomp_parse++; /* skip over the c */
2130 if (*PL_regcomp_parse == ']') {
2131 PL_regcomp_parse++; /* skip over the ending ] */
2134 I32 complement = *posixcc == '^' ? *posixcc++ : 0;
2135 I32 skip = 5; /* the most common skip */
2139 if (strnEQ(posixcc, "alnum", 5))
2141 complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
2142 else if (strnEQ(posixcc, "alpha", 5))
2144 complement ? ANYOF_NALPHA : ANYOF_ALPHA;
2145 else if (strnEQ(posixcc, "ascii", 5))
2147 complement ? ANYOF_NASCII : ANYOF_ASCII;
2150 if (strnEQ(posixcc, "cntrl", 5))
2152 complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
2155 if (strnEQ(posixcc, "digit", 5))
2157 complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
2160 if (strnEQ(posixcc, "graph", 5))
2162 complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
2165 if (strnEQ(posixcc, "lower", 5))
2167 complement ? ANYOF_NLOWER : ANYOF_LOWER;
2170 if (strnEQ(posixcc, "print", 5))
2172 complement ? ANYOF_NPRINT : ANYOF_PRINT;
2173 else if (strnEQ(posixcc, "punct", 5))
2175 complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
2178 if (strnEQ(posixcc, "space", 5))
2180 complement ? ANYOF_NSPACE : ANYOF_SPACE;
2182 if (strnEQ(posixcc, "upper", 5))
2184 complement ? ANYOF_NUPPER : ANYOF_UPPER;
2186 case 'w': /* this is not POSIX, this is the Perl \w */
2187 if (strnEQ(posixcc, "word", 4)) {
2189 complement ? ANYOF_NALNUM : ANYOF_ALNUM;
2194 if (strnEQ(posixcc, "xdigit", 6)) {
2196 complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
2201 if ((namedclass == -1 ||
2202 !(posixcc + skip + 2 < PL_regxend &&
2203 (posixcc[skip] == ':' &&
2204 posixcc[skip + 1] == ']'))))
2205 Perl_croak(aTHX_ "Character class [:%.*s:] unknown",
2207 } else if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
2208 /* [[=foo=]] and [[.foo.]] are still future. */
2209 Perl_warner(aTHX_ WARN_UNSAFE,
2210 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2212 /* Maternal grandfather:
2213 * "[:" ending in ":" but not in ":]" */
2214 PL_regcomp_parse = s;
2223 S_checkposixcc(pTHX)
2225 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY &&
2226 (*PL_regcomp_parse == ':' ||
2227 *PL_regcomp_parse == '=' ||
2228 *PL_regcomp_parse == '.')) {
2229 char *s = PL_regcomp_parse;
2232 while(*s && isALNUM(*s))
2234 if (*s && c == *s && s[1] == ']') {
2235 Perl_warner(aTHX_ WARN_UNSAFE,
2236 "Character class syntax [%c %c] belongs inside character classes", c, c);
2237 if (c == '=' || c == '.')
2238 Perl_warner(aTHX_ WARN_UNSAFE,
2239 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2248 register char *opnd, *s;
2250 register I32 lastvalue = OOB_CHAR8;
2251 register I32 range = 0;
2252 register regnode *ret;
2257 s = opnd = (char *) OPERAND(PL_regcode);
2258 ret = reg_node(ANYOF);
2259 for (value = 0; value < ANYOF_SIZE; value++)
2261 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2265 ANYOF_FLAGS(opnd) |= ANYOF_INVERT;
2268 PL_regcode += ANY_SKIP;
2270 ANYOF_FLAGS(opnd) |= ANYOF_FOLD;
2272 ANYOF_FLAGS(opnd) |= ANYOF_LOCALE;
2275 PL_regsize += ANY_SKIP;
2280 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2281 goto skipcond; /* allow 1st char to be ] or - */
2282 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2285 value = UCHARAT(PL_regcomp_parse++);
2287 namedclass = regpposixcc(value);
2288 else if (value == '\\') {
2289 value = UCHARAT(PL_regcomp_parse++);
2291 case 'w': namedclass = ANYOF_ALNUM; break;
2292 case 'W': namedclass = ANYOF_NALNUM; break;
2293 case 's': namedclass = ANYOF_SPACE; break;
2294 case 'S': namedclass = ANYOF_NSPACE; break;
2295 case 'd': namedclass = ANYOF_DIGIT; break;
2296 case 'D': namedclass = ANYOF_NDIGIT; break;
2297 case 'n': value = '\n'; break;
2298 case 'r': value = '\r'; break;
2299 case 't': value = '\t'; break;
2300 case 'f': value = '\f'; break;
2301 case 'b': value = '\b'; break;
2302 case 'e': value = '\033'; break;
2303 case 'a': value = '\007'; break;
2305 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2306 PL_regcomp_parse += numlen;
2309 value = UCHARAT(PL_regcomp_parse++);
2310 value = toCTRL(value);
2312 case '0': case '1': case '2': case '3': case '4':
2313 case '5': case '6': case '7': case '8': case '9':
2314 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2315 PL_regcomp_parse += numlen;
2319 if (!SIZE_ONLY && namedclass > -1) {
2320 switch (namedclass) {
2323 ANYOF_CLASS_SET(opnd, ANYOF_ALNUM);
2325 for (value = 0; value < 256; value++)
2327 ANYOF_BITMAP_SET(opnd, value);
2332 ANYOF_CLASS_SET(opnd, ANYOF_NALNUM);
2334 for (value = 0; value < 256; value++)
2335 if (!isALNUM(value))
2336 ANYOF_BITMAP_SET(opnd, value);
2341 ANYOF_CLASS_SET(opnd, ANYOF_SPACE);
2343 for (value = 0; value < 256; value++)
2345 ANYOF_BITMAP_SET(opnd, value);
2350 ANYOF_CLASS_SET(opnd, ANYOF_NSPACE);
2352 for (value = 0; value < 256; value++)
2353 if (!isSPACE(value))
2354 ANYOF_BITMAP_SET(opnd, value);
2359 ANYOF_CLASS_SET(opnd, ANYOF_DIGIT);
2361 for (value = '0'; value <= '9'; value++)
2362 ANYOF_BITMAP_SET(opnd, value);
2367 ANYOF_CLASS_SET(opnd, ANYOF_NDIGIT);
2369 for (value = 0; value < '0'; value++)
2370 ANYOF_BITMAP_SET(opnd, value);
2371 for (value = '9' + 1; value < 256; value++)
2372 ANYOF_BITMAP_SET(opnd, value);
2377 ANYOF_CLASS_SET(opnd, ANYOF_NALNUMC);
2379 for (value = 0; value < 256; value++)
2380 if (!isALNUMC(value))
2381 ANYOF_BITMAP_SET(opnd, value);
2386 ANYOF_CLASS_SET(opnd, ANYOF_ALNUMC);
2388 for (value = 0; value < 256; value++)
2389 if (isALNUMC(value))
2390 ANYOF_BITMAP_SET(opnd, value);
2395 ANYOF_CLASS_SET(opnd, ANYOF_ALPHA);
2397 for (value = 0; value < 256; value++)
2399 ANYOF_BITMAP_SET(opnd, value);
2404 ANYOF_CLASS_SET(opnd, ANYOF_NALPHA);
2406 for (value = 0; value < 256; value++)
2407 if (!isALPHA(value))
2408 ANYOF_BITMAP_SET(opnd, value);
2413 ANYOF_CLASS_SET(opnd, ANYOF_ASCII);
2415 for (value = 0; value < 128; value++)
2416 ANYOF_BITMAP_SET(opnd, value);
2421 ANYOF_CLASS_SET(opnd, ANYOF_NASCII);
2423 for (value = 128; value < 256; value++)
2424 ANYOF_BITMAP_SET(opnd, value);
2429 ANYOF_CLASS_SET(opnd, ANYOF_CNTRL);
2431 for (value = 0; value < 256; value++)
2433 ANYOF_BITMAP_SET(opnd, value);
2435 lastvalue = OOB_CHAR8;
2439 ANYOF_CLASS_SET(opnd, ANYOF_NCNTRL);
2441 for (value = 0; value < 256; value++)
2442 if (!isCNTRL(value))
2443 ANYOF_BITMAP_SET(opnd, value);
2448 ANYOF_CLASS_SET(opnd, ANYOF_GRAPH);
2450 for (value = 0; value < 256; value++)
2452 ANYOF_BITMAP_SET(opnd, value);
2457 ANYOF_CLASS_SET(opnd, ANYOF_NGRAPH);
2459 for (value = 0; value < 256; value++)
2460 if (!isGRAPH(value))
2461 ANYOF_BITMAP_SET(opnd, value);
2466 ANYOF_CLASS_SET(opnd, ANYOF_LOWER);
2468 for (value = 0; value < 256; value++)
2470 ANYOF_BITMAP_SET(opnd, value);
2475 ANYOF_CLASS_SET(opnd, ANYOF_NLOWER);
2477 for (value = 0; value < 256; value++)
2478 if (!isLOWER(value))
2479 ANYOF_BITMAP_SET(opnd, value);
2484 ANYOF_CLASS_SET(opnd, ANYOF_PRINT);
2486 for (value = 0; value < 256; value++)
2488 ANYOF_BITMAP_SET(opnd, value);
2493 ANYOF_CLASS_SET(opnd, ANYOF_NPRINT);
2495 for (value = 0; value < 256; value++)
2496 if (!isPRINT(value))
2497 ANYOF_BITMAP_SET(opnd, value);
2502 ANYOF_CLASS_SET(opnd, ANYOF_PUNCT);
2504 for (value = 0; value < 256; value++)
2506 ANYOF_BITMAP_SET(opnd, value);
2511 ANYOF_CLASS_SET(opnd, ANYOF_NPUNCT);
2513 for (value = 0; value < 256; value++)
2514 if (!isPUNCT(value))
2515 ANYOF_BITMAP_SET(opnd, value);
2520 ANYOF_CLASS_SET(opnd, ANYOF_UPPER);
2522 for (value = 0; value < 256; value++)
2524 ANYOF_BITMAP_SET(opnd, value);
2529 ANYOF_CLASS_SET(opnd, ANYOF_NUPPER);
2531 for (value = 0; value < 256; value++)
2532 if (!isUPPER(value))
2533 ANYOF_BITMAP_SET(opnd, value);
2538 ANYOF_CLASS_SET(opnd, ANYOF_XDIGIT);
2540 for (value = 0; value < 256; value++)
2541 if (isXDIGIT(value))
2542 ANYOF_BITMAP_SET(opnd, value);
2547 ANYOF_CLASS_SET(opnd, ANYOF_NXDIGIT);
2549 for (value = 0; value < 256; value++)
2550 if (!isXDIGIT(value))
2551 ANYOF_BITMAP_SET(opnd, value);
2555 FAIL("invalid [::] class in regexp");
2559 ANYOF_FLAGS(opnd) |= ANYOF_CLASS;
2560 lastvalue = OOB_CHAR8;
2564 if (lastvalue > value)
2565 FAIL("invalid [] range in regexp");
2570 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2571 PL_regcomp_parse[1] != ']') {
2574 continue; /* do it next time */
2579 if ((isLOWER(lastvalue) && isLOWER(value)) ||
2580 (isUPPER(lastvalue) && isUPPER(value)))
2583 if (isLOWER(lastvalue)) {
2584 for (i = lastvalue; i <= value; i++)
2586 ANYOF_BITMAP_SET(opnd, i);
2588 for (i = lastvalue; i <= value; i++)
2590 ANYOF_BITMAP_SET(opnd, i);
2595 for ( ; lastvalue <= value; lastvalue++)
2596 ANYOF_BITMAP_SET(opnd, lastvalue);
2600 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2602 (ANYOF_FLAGS(opnd) & (ANYOF_FLAGS_ALL ^ ANYOF_INVERT)) == ANYOF_FOLD) {
2603 for (value = 0; value < 256; ++value) {
2604 if (ANYOF_BITMAP_TEST(opnd, value)) {
2605 I32 cf = PL_fold[value];
2606 ANYOF_BITMAP_SET(opnd, cf);
2609 ANYOF_FLAGS(opnd) &= ~ANYOF_FOLD;
2611 /* optimize inverted simple patterns (e.g. [^a-z]) */
2612 if (!SIZE_ONLY && (ANYOF_FLAGS(opnd) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
2613 for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
2614 opnd[ANYOF_BITMAP_OFFSET + value] ^= ANYOF_FLAGS_ALL;
2615 ANYOF_FLAGS(opnd) = 0;
2621 S_regclassutf8(pTHX)
2624 register char *opnd, *e;
2626 register U32 lastvalue = OOB_UTF8;
2627 register I32 range = 0;
2628 register regnode *ret;
2635 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2639 flags |= ANYOF_INVERT;
2643 flags |= ANYOF_FOLD;
2645 flags |= ANYOF_LOCALE;
2646 listsv = newSVpvn("# comment\n",10);
2651 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2652 goto skipcond; /* allow 1st char to be ] or - */
2654 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2657 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2658 PL_regcomp_parse += numlen;
2661 namedclass = regpposixcc(value);
2662 else if (value == '\\') {
2663 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2664 PL_regcomp_parse += numlen;
2666 case 'w': namedclass = ANYOF_ALNUM; break;
2667 case 'W': namedclass = ANYOF_NALNUM; break;
2668 case 's': namedclass = ANYOF_SPACE; break;
2669 case 'S': namedclass = ANYOF_NSPACE; break;
2670 case 'd': namedclass = ANYOF_DIGIT; break;
2671 case 'D': namedclass = ANYOF_NDIGIT; break;
2674 if (*PL_regcomp_parse == '{') {
2675 e = strchr(PL_regcomp_parse++, '}');
2677 FAIL("Missing right brace on \\p{}");
2678 n = e - PL_regcomp_parse;
2681 e = PL_regcomp_parse;
2686 Perl_sv_catpvf(aTHX_ listsv,
2687 "+utf8::%.*s\n", n, PL_regcomp_parse);
2689 Perl_sv_catpvf(aTHX_ listsv,
2690 "!utf8::%.*s\n", n, PL_regcomp_parse);
2692 PL_regcomp_parse = e + 1;
2693 lastvalue = OOB_UTF8;
2695 case 'n': value = '\n'; break;
2696 case 'r': value = '\r'; break;
2697 case 't': value = '\t'; break;
2698 case 'f': value = '\f'; break;
2699 case 'b': value = '\b'; break;
2700 case 'e': value = '\033'; break;
2701 case 'a': value = '\007'; break;
2703 if (*PL_regcomp_parse == '{') {
2704 e = strchr(PL_regcomp_parse++, '}');
2706 FAIL("Missing right brace on \\x{}");
2707 value = scan_hex(PL_regcomp_parse,
2708 e - PL_regcomp_parse,
2710 PL_regcomp_parse = e + 1;
2713 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2714 PL_regcomp_parse += numlen;
2718 value = UCHARAT(PL_regcomp_parse++);
2719 value = toCTRL(value);
2721 case '0': case '1': case '2': case '3': case '4':
2722 case '5': case '6': case '7': case '8': case '9':
2723 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2724 PL_regcomp_parse += numlen;
2728 if (!SIZE_ONLY && namedclass > -1) {
2729 switch (namedclass) {
2731 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n"); break;
2733 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n"); break;
2735 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n"); break;
2737 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n"); break;
2739 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n"); break;
2741 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n"); break;
2743 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n"); break;
2745 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n"); break;
2747 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n"); break;
2749 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n"); break;
2751 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n"); break;
2753 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n"); break;
2755 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n"); break;
2757 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n"); break;
2759 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n"); break;
2761 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n"); break;
2763 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n"); break;
2765 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n"); break;
2767 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n"); break;
2769 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n"); break;
2771 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n"); break;
2773 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n"); break;
2775 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n"); break;
2777 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n"); break;
2779 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n"); break;
2781 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n"); break;
2786 if (lastvalue > value)
2787 FAIL("invalid [] range in regexp");
2789 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\n", lastvalue, value);
2795 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2796 PL_regcomp_parse[1] != ']') {
2799 continue; /* do it next time */
2802 Perl_sv_catpvf(aTHX_ listsv, "%04x\n", value);
2806 ret = reganode(ANYOFUTF8, 0);
2809 SV *rv = swash_init("utf8", "", listsv, 1, 0);
2810 SvREFCNT_dec(listsv);
2811 n = add_data(1,"s");
2812 PL_regcomp_rx->data->data[n] = (void*)rv;
2813 ARG1_SET(ret, flags);
2824 char* retval = PL_regcomp_parse++;
2827 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2828 PL_regcomp_parse[2] == '#') {
2829 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2834 if (PL_regflags & PMf_EXTENDED) {
2835 if (isSPACE(*PL_regcomp_parse)) {
2839 else if (*PL_regcomp_parse == '#') {
2840 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2851 - reg_node - emit a node
2853 STATIC regnode * /* Location. */
2854 S_reg_node(pTHX_ U8 op)
2857 register regnode *ret;
2858 register regnode *ptr;
2862 SIZE_ALIGN(PL_regsize);
2867 NODE_ALIGN_FILL(ret);
2869 FILL_ADVANCE_NODE(ptr, op);
2876 - reganode - emit a node with an argument
2878 STATIC regnode * /* Location. */
2879 S_reganode(pTHX_ U8 op, U32 arg)
2882 register regnode *ret;
2883 register regnode *ptr;
2887 SIZE_ALIGN(PL_regsize);
2892 NODE_ALIGN_FILL(ret);
2894 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
2901 - regc - emit (if appropriate) a Unicode character
2904 S_reguni(pTHX_ UV uv, char* s, I32* lenp)
2909 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
2912 *lenp = uv_to_utf8((U8*)s, uv) - (U8*)s;
2917 - regc - emit (if appropriate) a byte of code
2920 S_regc(pTHX_ U8 b, char* s)
2928 - reginsert - insert an operator in front of already-emitted operand
2930 * Means relocating the operand.
2933 S_reginsert(pTHX_ U8 op, regnode *opnd)
2936 register regnode *src;
2937 register regnode *dst;
2938 register regnode *place;
2939 register int offset = regarglen[(U8)op];
2941 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2944 PL_regsize += NODE_STEP_REGNODE + offset;
2949 PL_regcode += NODE_STEP_REGNODE + offset;
2952 StructCopy(--src, --dst, regnode);
2954 place = opnd; /* Op node, where operand used to be. */
2955 src = NEXTOPER(place);
2956 FILL_ADVANCE_NODE(place, op);
2957 Zero(src, offset, regnode);
2961 - regtail - set the next-pointer at the end of a node chain of p to val.
2964 S_regtail(pTHX_ regnode *p, regnode *val)
2967 register regnode *scan;
2968 register regnode *temp;
2969 register I32 offset;
2974 /* Find last node. */
2977 temp = regnext(scan);
2983 if (reg_off_by_arg[OP(scan)]) {
2984 ARG_SET(scan, val - scan);
2987 NEXT_OFF(scan) = val - scan;
2992 - regoptail - regtail on operand of first argument; nop if operandless
2995 S_regoptail(pTHX_ regnode *p, regnode *val)
2998 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
2999 if (p == NULL || SIZE_ONLY)
3001 if (PL_regkind[(U8)OP(p)] == BRANCH) {
3002 regtail(NEXTOPER(p), val);
3004 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
3005 regtail(NEXTOPER(NEXTOPER(p)), val);
3012 - regcurly - a little FSA that accepts {\d+,?\d*}
3015 S_regcurly(pTHX_ register char *s)
3034 S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
3037 register U8 op = EXACT; /* Arbitrary non-END op. */
3038 register regnode *next, *onode;
3040 while (op != END && (!last || node < last)) {
3041 /* While that wasn't END last time... */
3047 next = regnext(node);
3049 if (OP(node) == OPTIMIZED)
3052 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
3053 2*l + 1, "", SvPVX(sv));
3054 if (next == NULL) /* Next ptr. */
3055 PerlIO_printf(Perl_debug_log, "(0)");
3057 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
3058 (void)PerlIO_putc(Perl_debug_log, '\n');
3060 if (PL_regkind[(U8)op] == BRANCHJ) {
3061 register regnode *nnode = (OP(next) == LONGJMP
3064 if (last && nnode > last)
3066 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
3068 else if (PL_regkind[(U8)op] == BRANCH) {
3069 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
3071 else if ( op == CURLY) { /* `next' might be very big: optimizer */
3072 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
3073 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
3075 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
3076 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
3079 else if ( op == PLUS || op == STAR) {
3080 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
3082 else if (op == ANYOF) {
3083 node = NEXTOPER(node);
3086 else if (PL_regkind[(U8)op] == EXACT) {
3087 /* Literal string, where present. */
3088 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
3089 node = NEXTOPER(node);
3092 node = NEXTOPER(node);
3093 node += regarglen[(U8)op];
3095 if (op == CURLYX || op == OPEN)
3097 else if (op == WHILEM)
3100 #endif /* DEBUGGING */
3105 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
3108 Perl_regdump(pTHX_ regexp *r)
3112 SV *sv = sv_newmortal();
3114 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
3116 /* Header fields of interest. */
3117 if (r->anchored_substr)
3118 PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
3120 SvPVX(r->anchored_substr),
3122 SvTAIL(r->anchored_substr) ? "$" : "",
3123 r->anchored_offset);
3124 if (r->float_substr)
3125 PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
3127 SvPVX(r->float_substr),
3129 SvTAIL(r->float_substr) ? "$" : "",
3130 r->float_min_offset, r->float_max_offset);
3131 if (r->check_substr)
3132 PerlIO_printf(Perl_debug_log,
3133 r->check_substr == r->float_substr
3134 ? "(checking floating" : "(checking anchored");
3135 if (r->reganch & ROPT_NOSCAN)
3136 PerlIO_printf(Perl_debug_log, " noscan");
3137 if (r->reganch & ROPT_CHECK_ALL)
3138 PerlIO_printf(Perl_debug_log, " isall");
3139 if (r->check_substr)
3140 PerlIO_printf(Perl_debug_log, ") ");
3142 if (r->regstclass) {
3143 regprop(sv, r->regstclass);
3144 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
3146 if (r->reganch & ROPT_ANCH) {
3147 PerlIO_printf(Perl_debug_log, "anchored");
3148 if (r->reganch & ROPT_ANCH_BOL)
3149 PerlIO_printf(Perl_debug_log, "(BOL)");
3150 if (r->reganch & ROPT_ANCH_MBOL)
3151 PerlIO_printf(Perl_debug_log, "(MBOL)");
3152 if (r->reganch & ROPT_ANCH_SBOL)
3153 PerlIO_printf(Perl_debug_log, "(SBOL)");
3154 if (r->reganch & ROPT_ANCH_GPOS)
3155 PerlIO_printf(Perl_debug_log, "(GPOS)");
3156 PerlIO_putc(Perl_debug_log, ' ');
3158 if (r->reganch & ROPT_GPOS_SEEN)
3159 PerlIO_printf(Perl_debug_log, "GPOS ");
3160 if (r->reganch & ROPT_SKIP)
3161 PerlIO_printf(Perl_debug_log, "plus ");
3162 if (r->reganch & ROPT_IMPLICIT)
3163 PerlIO_printf(Perl_debug_log, "implicit ");
3164 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
3165 if (r->reganch & ROPT_EVAL_SEEN)
3166 PerlIO_printf(Perl_debug_log, "with eval ");
3167 PerlIO_printf(Perl_debug_log, "\n");
3168 #endif /* DEBUGGING */
3172 - regprop - printable representation of opcode
3175 Perl_regprop(pTHX_ SV *sv, regnode *o)
3181 sv_setpvn(sv, "", 0);
3182 if (OP(o) >= reg_num) /* regnode.type is unsigned */
3183 FAIL("corrupted regexp opcode");
3184 sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
3186 k = PL_regkind[(U8)OP(o)];
3189 Perl_sv_catpvf(aTHX_ sv, " <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
3190 else if (k == CURLY) {
3191 if (OP(o) == CURLYM || OP(o) == CURLYN)
3192 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
3193 Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
3195 else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
3196 Perl_sv_catpvf(aTHX_ sv, "%d", ARG(o)); /* Parenth number */
3197 else if (k == LOGICAL)
3198 Perl_sv_catpvf(aTHX_ sv, "[%d]", ARG(o)); /* 2: embedded, otherwise 1 */
3199 else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
3200 Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
3201 #endif /* DEBUGGING */
3205 Perl_re_intuit_string(pTHX_ regexp *prog)
3206 { /* Assume that RE_INTUIT is set */
3209 char *s = SvPV(prog->check_substr,n_a);
3211 if (!PL_colorset) reginitcolors();
3212 PerlIO_printf(Perl_debug_log,
3213 "%sUsing REx substr:%s `%s%.60s%s%s'\n",
3214 PL_colors[4],PL_colors[5],PL_colors[0],
3217 (strlen(s) > 60 ? "..." : ""));
3220 return prog->check_substr;
3224 Perl_pregfree(pTHX_ struct regexp *r)
3227 DEBUG_r(if (!PL_colorset) reginitcolors());
3228 DEBUG_r(PerlIO_printf(Perl_debug_log,
3229 "%sFreeing REx:%s `%s%.60s%s%s'\n",
3230 PL_colors[4],PL_colors[5],PL_colors[0],
3233 (strlen(r->precomp) > 60 ? "..." : "")));
3236 if (!r || (--r->refcnt > 0))
3239 Safefree(r->precomp);
3240 if (RX_MATCH_COPIED(r))
3241 Safefree(r->subbeg);
3243 if (r->anchored_substr)
3244 SvREFCNT_dec(r->anchored_substr);
3245 if (r->float_substr)
3246 SvREFCNT_dec(r->float_substr);
3247 Safefree(r->substrs);
3250 int n = r->data->count;
3251 AV* new_comppad = NULL;
3256 switch (r->data->what[n]) {
3258 SvREFCNT_dec((SV*)r->data->data[n]);
3261 new_comppad = (AV*)r->data->data[n];
3264 if (new_comppad == NULL)
3265 Perl_croak(aTHX_ "panic: pregfree comppad");
3266 old_comppad = PL_comppad;
3267 old_curpad = PL_curpad;
3268 PL_comppad = new_comppad;
3269 PL_curpad = AvARRAY(new_comppad);
3270 op_free((OP_4tree*)r->data->data[n]);
3271 PL_comppad = old_comppad;
3272 PL_curpad = old_curpad;
3273 SvREFCNT_dec((SV*)new_comppad);
3279 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
3282 Safefree(r->data->what);
3285 Safefree(r->startp);
3291 - regnext - dig the "next" pointer out of a node
3293 * [Note, when REGALIGN is defined there are two places in regmatch()
3294 * that bypass this code for speed.]
3297 Perl_regnext(pTHX_ register regnode *p)
3300 register I32 offset;
3302 if (p == &PL_regdummy)
3305 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
3313 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
3316 STRLEN l1 = strlen(pat1);
3317 STRLEN l2 = strlen(pat2);
3326 Copy(pat1, buf, l1 , char);
3327 Copy(pat2, buf + l1, l2 , char);
3328 buf[l1 + l2] = '\n';
3329 buf[l1 + l2 + 1] = '\0';
3331 /* ANSI variant takes additional second argument */
3332 va_start(args, pat2);
3336 msv = mess(buf, &args);
3338 message = SvPV(msv,l1);
3341 Copy(message, buf, l1 , char);
3342 buf[l1] = '\0'; /* Overwrite \n */
3343 Perl_croak(aTHX_ "%s", buf);
3346 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
3349 Perl_save_re_context(pTHX)
3353 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
3354 SAVEI32(PL_regnpar); /* () count. */
3355 SAVEI32(PL_regsize); /* Code size. */
3356 SAVEI16(PL_regflags); /* are we folding, multilining? */
3357 SAVEPPTR(PL_reginput); /* String-input pointer. */
3358 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
3359 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
3360 SAVESPTR(PL_regstartp); /* Pointer to startp array. */
3361 SAVESPTR(PL_regendp); /* Ditto for endp. */
3362 SAVESPTR(PL_reglastparen); /* Similarly for lastparen. */
3363 SAVEPPTR(PL_regtill); /* How far we are required to go. */
3364 SAVEI32(PL_regprev); /* char before regbol, \n if none */
3365 SAVESPTR(PL_reg_start_tmp); /* from regexec.c */
3366 PL_reg_start_tmp = 0;
3367 SAVEFREEPV(PL_reg_start_tmp);
3368 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
3369 PL_reg_start_tmpl = 0;
3370 SAVESPTR(PL_regdata);
3371 SAVEI32(PL_reg_flags); /* from regexec.c */
3372 SAVEI32(PL_reg_eval_set); /* from regexec.c */
3373 SAVEI32(PL_regnarrate); /* from regexec.c */
3374 SAVESPTR(PL_regprogram); /* from regexec.c */
3375 SAVEINT(PL_regindent); /* from regexec.c */
3376 SAVESPTR(PL_regcc); /* from regexec.c */
3377 SAVESPTR(PL_curcop);
3378 SAVESPTR(PL_regcomp_rx); /* from regcomp.c */
3379 SAVEI32(PL_regseen); /* from regcomp.c */
3380 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
3381 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
3382 SAVESPTR(PL_regcode); /* Code-emit pointer; ®dummy = don't */
3383 SAVEPPTR(PL_regxend); /* End of input for compile */
3384 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */
3385 SAVESPTR(PL_reg_call_cc); /* from regexec.c */
3386 SAVESPTR(PL_reg_re); /* from regexec.c */
3387 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
3388 SAVESPTR(PL_reg_sv); /* from regexec.c */
3389 SAVESPTR(PL_reg_magic); /* from regexec.c */
3390 SAVEI32(PL_reg_oldpos); /* from regexec.c */
3391 SAVESPTR(PL_reg_oldcurpm); /* from regexec.c */
3392 SAVESPTR(PL_reg_curpm); /* from regexec.c */
3394 SAVEPPTR(PL_reg_starttry); /* from regexec.c */
3406 clear_re(pTHXo_ void *r)
3408 ReREFCNT_dec((regexp *)r);