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. */
135 /* Length of a variant. */
137 typedef struct scan_data_t {
143 I32 last_end; /* min value, <0 unless valid. */
146 SV **longest; /* Either &l_fixed, or &l_float. */
150 I32 offset_float_min;
151 I32 offset_float_max;
154 struct regnode_charclass_class *start_class;
158 * Forward declarations for pregcomp()'s friends.
161 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
164 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
165 #define SF_BEFORE_SEOL 0x1
166 #define SF_BEFORE_MEOL 0x2
167 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
168 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
171 # define SF_FIX_SHIFT_EOL (0+2)
172 # define SF_FL_SHIFT_EOL (0+4)
174 # define SF_FIX_SHIFT_EOL (+2)
175 # define SF_FL_SHIFT_EOL (+4)
178 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
179 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
181 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
182 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
183 #define SF_IS_INF 0x40
184 #define SF_HAS_PAR 0x80
185 #define SF_IN_PAR 0x100
186 #define SF_HAS_EVAL 0x200
187 #define SCF_DO_SUBSTR 0x400
188 #define SCF_DO_STCLASS_AND 0x0800
189 #define SCF_DO_STCLASS_OR 0x1000
190 #define SCF_DO_STCLASS (SCF_DO_STCLASS_AND|SCF_DO_STCLASS_OR)
193 #define UTF (PL_reg_flags & RF_utf8)
194 #define LOC (PL_regflags & PMf_LOCALE)
195 #define FOLD (PL_regflags & PMf_FOLD)
197 #define OOB_CHAR8 1234
198 #define OOB_UTF8 123456
199 #define OOB_NAMEDCLASS -1
201 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
202 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
204 /* Allow for side effects in s */
205 #define REGC(c,s) STMT_START { if (!SIZE_ONLY) *(s) = (c); else (s);} STMT_END
207 static void clear_re(pTHXo_ void *r);
209 /* Mark that we cannot extend a found fixed substring at this point.
210 Updata the longest found anchored substring and the longest found
211 floating substrings if needed. */
214 S_scan_commit(pTHX_ scan_data_t *data)
217 STRLEN l = CHR_SVLEN(data->last_found);
218 STRLEN old_l = CHR_SVLEN(*data->longest);
220 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
221 sv_setsv(*data->longest, data->last_found);
222 if (*data->longest == data->longest_fixed) {
223 data->offset_fixed = l ? data->last_start_min : data->pos_min;
224 if (data->flags & SF_BEFORE_EOL)
226 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
228 data->flags &= ~SF_FIX_BEFORE_EOL;
231 data->offset_float_min = l ? data->last_start_min : data->pos_min;
232 data->offset_float_max = (l
233 ? data->last_start_max
234 : data->pos_min + data->pos_delta);
235 if (data->flags & SF_BEFORE_EOL)
237 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
239 data->flags &= ~SF_FL_BEFORE_EOL;
242 SvCUR_set(data->last_found, 0);
244 data->flags &= ~SF_BEFORE_EOL;
247 /* Can match anything (initialization) */
249 S_cl_anything(pTHX_ struct regnode_charclass_class *cl)
253 ANYOF_CLASS_ZERO(cl);
254 for (value = 0; value < 256; ++value)
255 ANYOF_BITMAP_SET(cl, value);
256 cl->flags = ANYOF_EOS;
258 cl->flags |= ANYOF_LOCALE;
261 /* Can match anything (initialization) */
263 S_cl_is_anything(pTHX_ struct regnode_charclass_class *cl)
267 for (value = 0; value < ANYOF_MAX; value += 2)
268 if (ANYOF_CLASS_TEST(cl, value) && ANYOF_CLASS_TEST(cl, value + 1))
270 for (value = 0; value < 256; ++value)
271 if (!ANYOF_BITMAP_TEST(cl, value))
276 /* Can match anything (initialization) */
278 S_cl_init(pTHX_ struct regnode_charclass_class *cl)
285 S_cl_init_zero(pTHX_ struct regnode_charclass_class *cl)
289 ANYOF_CLASS_ZERO(cl);
290 ANYOF_BITMAP_ZERO(cl);
292 cl->flags |= ANYOF_LOCALE;
295 /* 'And' a given class with another one. Can create false positives */
296 /* We assume that cl is not inverted */
298 S_cl_and(pTHX_ struct regnode_charclass_class *cl,
299 struct regnode_charclass_class *and_with)
303 if (!(and_with->flags & ANYOF_CLASS)
304 && !(cl->flags & ANYOF_CLASS)
305 && (and_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
306 && !(and_with->flags & ANYOF_FOLD)
307 && !(cl->flags & ANYOF_FOLD)) {
310 if (and_with->flags & ANYOF_INVERT)
311 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
312 cl->bitmap[i] &= ~and_with->bitmap[i];
314 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
315 cl->bitmap[i] &= and_with->bitmap[i];
316 } /* XXXX: logic is complicated otherwise, leave it along for a moment. */
317 if (!(and_with->flags & ANYOF_EOS))
318 cl->flags &= ~ANYOF_EOS;
321 /* 'OR' a given class with another one. Can create false positives */
322 /* We assume that cl is not inverted */
324 S_cl_or(pTHX_ struct regnode_charclass_class *cl, struct regnode_charclass_class *or_with)
328 if (or_with->flags & ANYOF_INVERT) {
330 * (B1 | CL1) | (!B2 & !CL2) = (B1 | !B2 & !CL2) | (CL1 | (!B2 & !CL2))
331 * <= (B1 | !B2) | (CL1 | !CL2)
332 * which is wasteful if CL2 is small, but we ignore CL2:
333 * (B1 | CL1) | (!B2 & !CL2) <= (B1 | CL1) | !B2 = (B1 | !B2) | CL1
334 * XXXX Can we handle case-fold? Unclear:
335 * (OK1(i) | OK1(i')) | !(OK1(i) | OK1(i')) =
336 * (OK1(i) | OK1(i')) | (!OK1(i) & !OK1(i'))
338 if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
339 && !(or_with->flags & ANYOF_FOLD)
340 && !(cl->flags & ANYOF_FOLD) ) {
343 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
344 cl->bitmap[i] |= ~or_with->bitmap[i];
345 } /* XXXX: logic is complicated otherwise */
350 /* (B1 | CL1) | (B2 | CL2) = (B1 | B2) | (CL1 | CL2)) */
351 if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
352 && (!(or_with->flags & ANYOF_FOLD)
353 || (cl->flags & ANYOF_FOLD)) ) {
356 /* OR char bitmap and class bitmap separately */
357 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
358 cl->bitmap[i] |= or_with->bitmap[i];
359 if (or_with->flags & ANYOF_CLASS) {
360 for (i = 0; i < ANYOF_CLASSBITMAP_SIZE; i++)
361 cl->classflags[i] |= or_with->classflags[i];
362 cl->flags |= ANYOF_CLASS;
365 else { /* XXXX: logic is complicated, leave it along for a moment. */
369 if (or_with->flags & ANYOF_EOS)
370 cl->flags |= ANYOF_EOS;
373 /* REx optimizer. Converts nodes into quickier variants "in place".
374 Finds fixed substrings. */
376 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
377 to the position after last scanned or to NULL. */
380 S_study_chunk(pTHX_ regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
381 /* scanp: Start here (read-write). */
382 /* deltap: Write maxlen-minlen here. */
383 /* last: Stop before this one. */
386 I32 min = 0, pars = 0, code;
387 regnode *scan = *scanp, *next;
389 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
390 int is_inf_internal = 0; /* The studied chunk is infinite */
391 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
392 scan_data_t data_fake;
393 struct regnode_charclass_class and_with; /* Valid if flags & SCF_DO_STCLASS_OR */
395 while (scan && OP(scan) != END && scan < last) {
396 /* Peephole optimizer: */
398 if (PL_regkind[(U8)OP(scan)] == EXACT) {
399 /* Merge several consecutive EXACTish nodes into one. */
400 regnode *n = regnext(scan);
403 regnode *stop = scan;
406 next = scan + NODE_SZ_STR(scan);
407 /* Skip NOTHING, merge EXACT*. */
409 ( PL_regkind[(U8)OP(n)] == NOTHING ||
410 (stringok && (OP(n) == OP(scan))))
412 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
413 if (OP(n) == TAIL || n > next)
415 if (PL_regkind[(U8)OP(n)] == NOTHING) {
416 NEXT_OFF(scan) += NEXT_OFF(n);
417 next = n + NODE_STEP_REGNODE;
425 int oldl = STR_LEN(scan);
426 regnode *nnext = regnext(n);
428 if (oldl + STR_LEN(n) > U8_MAX)
430 NEXT_OFF(scan) += NEXT_OFF(n);
431 STR_LEN(scan) += STR_LEN(n);
432 next = n + NODE_SZ_STR(n);
433 /* Now we can overwrite *n : */
434 Move(STRING(n), STRING(scan) + oldl,
445 n = scan + NODE_SZ_STR(scan);
447 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
455 /* Follow the next-chain of the current node and optimize
456 away all the NOTHINGs from it. */
457 if (OP(scan) != CURLYX) {
458 int max = (reg_off_by_arg[OP(scan)]
460 /* I32 may be smaller than U16 on CRAYs! */
461 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
462 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
466 /* Skip NOTHING and LONGJMP. */
467 while ((n = regnext(n))
468 && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
469 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
472 if (reg_off_by_arg[OP(scan)])
475 NEXT_OFF(scan) = off;
477 /* The principal pseudo-switch. Cannot be a switch, since we
478 look into several different things. */
479 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
480 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
481 next = regnext(scan);
484 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
485 I32 max1 = 0, min1 = I32_MAX, num = 0;
486 struct regnode_charclass_class accum;
488 if (flags & SCF_DO_SUBSTR) /* XXXX Add !SUSPEND? */
489 scan_commit(data); /* Cannot merge strings after this. */
490 if (flags & SCF_DO_STCLASS)
491 cl_init_zero(&accum);
492 while (OP(scan) == code) {
493 I32 deltanext, minnext, f = 0;
494 struct regnode_charclass_class this_class;
499 data_fake.whilem_c = data->whilem_c;
500 next = regnext(scan);
501 scan = NEXTOPER(scan);
503 scan = NEXTOPER(scan);
504 if (flags & SCF_DO_STCLASS) {
505 cl_init(&this_class);
506 data_fake.start_class = &this_class;
507 f = SCF_DO_STCLASS_AND;
509 /* we suppose the run is continuous, last=next...*/
510 minnext = study_chunk(&scan, &deltanext, next,
514 if (max1 < minnext + deltanext)
515 max1 = minnext + deltanext;
516 if (deltanext == I32_MAX)
517 is_inf = is_inf_internal = 1;
519 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
521 if (data && (data_fake.flags & SF_HAS_EVAL))
522 data->flags |= SF_HAS_EVAL;
524 data->whilem_c = data_fake.whilem_c;
525 if (flags & SCF_DO_STCLASS)
526 cl_or(&accum, &this_class);
530 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
532 if (flags & SCF_DO_SUBSTR) {
533 data->pos_min += min1;
534 data->pos_delta += max1 - min1;
535 if (max1 != min1 || is_inf)
536 data->longest = &(data->longest_float);
539 delta += max1 - min1;
540 if (flags & SCF_DO_STCLASS_OR) {
541 cl_or(data->start_class, &accum);
543 cl_and(data->start_class, &and_with);
544 flags &= ~SCF_DO_STCLASS;
547 else if (flags & SCF_DO_STCLASS_AND) {
548 cl_and(data->start_class, &accum);
550 flags &= ~SCF_DO_STCLASS;
553 else if (code == BRANCHJ) /* single branch is optimized. */
554 scan = NEXTOPER(NEXTOPER(scan));
555 else /* single branch is optimized. */
556 scan = NEXTOPER(scan);
559 else if (OP(scan) == EXACT) {
560 I32 l = STR_LEN(scan);
562 unsigned char *s = (unsigned char *)STRING(scan);
563 unsigned char *e = s + l;
572 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
573 /* The code below prefers earlier match for fixed
574 offset, later match for variable offset. */
575 if (data->last_end == -1) { /* Update the start info. */
576 data->last_start_min = data->pos_min;
577 data->last_start_max = is_inf
578 ? I32_MAX : data->pos_min + data->pos_delta;
580 sv_catpvn(data->last_found, STRING(scan), STR_LEN(scan));
581 data->last_end = data->pos_min + l;
582 data->pos_min += l; /* As in the first entry. */
583 data->flags &= ~SF_BEFORE_EOL;
585 if (flags & SCF_DO_STCLASS_AND) {
586 /* Check whether it is compatible with what we know already! */
589 if (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
590 && !ANYOF_BITMAP_TEST(data->start_class, *STRING(scan))
591 && (!(data->start_class->flags & ANYOF_FOLD)
592 || !ANYOF_BITMAP_TEST(data->start_class,
593 PL_fold[*STRING(scan)])))
595 ANYOF_CLASS_ZERO(data->start_class);
596 ANYOF_BITMAP_ZERO(data->start_class);
598 ANYOF_BITMAP_SET(data->start_class, *STRING(scan));
599 data->start_class->flags &= ~ANYOF_EOS;
601 else if (flags & SCF_DO_STCLASS_OR) {
602 /* false positive possible if the class is case-folded */
603 ANYOF_BITMAP_SET(data->start_class, *STRING(scan));
604 data->start_class->flags &= ~ANYOF_EOS;
605 cl_and(data->start_class, &and_with);
607 flags &= ~SCF_DO_STCLASS;
609 else if (PL_regkind[(U8)OP(scan)] == EXACT) { /* But OP != EXACT! */
610 I32 l = STR_LEN(scan);
612 /* Search for fixed substrings supports EXACT only. */
613 if (flags & SCF_DO_SUBSTR)
616 unsigned char *s = (unsigned char *)STRING(scan);
617 unsigned char *e = s + l;
626 if (data && (flags & SCF_DO_SUBSTR))
628 if (flags & SCF_DO_STCLASS_AND) {
629 /* Check whether it is compatible with what we know already! */
632 if (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
633 && !ANYOF_BITMAP_TEST(data->start_class, *STRING(scan))
634 && !ANYOF_BITMAP_TEST(data->start_class,
635 PL_fold[*STRING(scan)]))
637 ANYOF_CLASS_ZERO(data->start_class);
638 ANYOF_BITMAP_ZERO(data->start_class);
640 ANYOF_BITMAP_SET(data->start_class, *STRING(scan));
641 data->start_class->flags &= ~ANYOF_EOS;
642 data->start_class->flags |= ANYOF_FOLD;
643 if (OP(scan) == EXACTFL)
644 data->start_class->flags |= ANYOF_LOCALE;
647 else if (flags & SCF_DO_STCLASS_OR) {
648 if (data->start_class->flags & ANYOF_FOLD) {
649 /* false positive possible if the class is case-folded.
650 Assume that the locale settings are the same... */
651 ANYOF_BITMAP_SET(data->start_class, *STRING(scan));
652 data->start_class->flags &= ~ANYOF_EOS;
654 cl_and(data->start_class, &and_with);
656 flags &= ~SCF_DO_STCLASS;
658 else if (strchr((char*)PL_varies,OP(scan))) {
659 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
661 regnode *oscan = scan;
662 struct regnode_charclass_class this_class;
663 struct regnode_charclass_class *oclass = NULL;
665 switch (PL_regkind[(U8)OP(scan)]) {
666 case WHILEM: /* End of (?:...)* . */
667 scan = NEXTOPER(scan);
670 if (flags & (SCF_DO_SUBSTR | SCF_DO_STCLASS)) {
671 next = NEXTOPER(scan);
672 if (OP(next) == EXACT || (flags & SCF_DO_STCLASS)) {
674 maxcount = REG_INFTY;
675 next = regnext(scan);
676 scan = NEXTOPER(scan);
680 if (flags & SCF_DO_SUBSTR)
685 if (flags & SCF_DO_STCLASS) {
687 maxcount = REG_INFTY;
688 next = regnext(scan);
689 scan = NEXTOPER(scan);
692 is_inf = is_inf_internal = 1;
693 scan = regnext(scan);
694 if (flags & SCF_DO_SUBSTR) {
695 scan_commit(data); /* Cannot extend fixed substrings */
696 data->longest = &(data->longest_float);
698 goto optimize_curly_tail;
700 mincount = ARG1(scan);
701 maxcount = ARG2(scan);
702 next = regnext(scan);
703 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
705 if (flags & SCF_DO_SUBSTR) {
706 if (mincount == 0) scan_commit(data); /* Cannot extend fixed substrings */
707 pos_before = data->pos_min;
711 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
713 data->flags |= SF_IS_INF;
715 if (flags & SCF_DO_STCLASS) {
716 cl_init(&this_class);
717 oclass = data->start_class;
718 data->start_class = &this_class;
719 f |= SCF_DO_STCLASS_AND;
720 f &= ~SCF_DO_STCLASS_OR;
723 /* This will finish on WHILEM, setting scan, or on NULL: */
724 minnext = study_chunk(&scan, &deltanext, last, data,
726 ? (f & ~SCF_DO_SUBSTR) : f);
728 if (flags & SCF_DO_STCLASS)
729 data->start_class = oclass;
730 if (mincount == 0 || minnext == 0) {
731 if (flags & SCF_DO_STCLASS_OR) {
732 cl_or(data->start_class, &this_class);
734 else if (flags & SCF_DO_STCLASS_AND) {
735 /* Switch to OR mode: cache the old value of
736 * data->start_class */
737 StructCopy(data->start_class, &and_with,
738 struct regnode_charclass_class);
739 flags &= ~SCF_DO_STCLASS_AND;
740 StructCopy(&this_class, data->start_class,
741 struct regnode_charclass_class);
742 flags |= SCF_DO_STCLASS_OR;
743 data->start_class->flags |= ANYOF_EOS;
745 } else { /* Non-zero len */
746 if (flags & SCF_DO_STCLASS_OR) {
747 cl_or(data->start_class, &this_class);
748 cl_and(data->start_class, &and_with);
750 else if (flags & SCF_DO_STCLASS_AND)
751 cl_and(data->start_class, &this_class);
752 flags &= ~SCF_DO_STCLASS;
754 if (!scan) /* It was not CURLYX, but CURLY. */
756 if (ckWARN(WARN_UNSAFE) && (minnext + deltanext == 0)
757 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
758 && maxcount <= REG_INFTY/3) /* Complement check for big count */
759 Perl_warner(aTHX_ WARN_UNSAFE,
760 "Strange *+?{} on zero-length expression");
761 min += minnext * mincount;
762 is_inf_internal |= (maxcount == REG_INFTY
763 && (minnext + deltanext) > 0
764 || deltanext == I32_MAX);
765 is_inf |= is_inf_internal;
766 delta += (minnext + deltanext) * maxcount - minnext * mincount;
768 /* Try powerful optimization CURLYX => CURLYN. */
769 if ( OP(oscan) == CURLYX && data
770 && data->flags & SF_IN_PAR
771 && !(data->flags & SF_HAS_EVAL)
772 && !deltanext && minnext == 1 ) {
773 /* Try to optimize to CURLYN. */
774 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
775 regnode *nxt1 = nxt, *nxt2;
779 if (!strchr((char*)PL_simple,OP(nxt))
780 && !(PL_regkind[(U8)OP(nxt)] == EXACT
781 && STR_LEN(nxt) == 1))
785 if (OP(nxt) != CLOSE)
787 /* Now we know that nxt2 is the only contents: */
788 oscan->flags = ARG(nxt);
790 OP(nxt1) = NOTHING; /* was OPEN. */
792 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
793 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
794 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
795 OP(nxt) = OPTIMIZED; /* was CLOSE. */
796 OP(nxt + 1) = OPTIMIZED; /* was count. */
797 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
802 /* Try optimization CURLYX => CURLYM. */
803 if ( OP(oscan) == CURLYX && data
804 && !(data->flags & SF_HAS_PAR)
805 && !(data->flags & SF_HAS_EVAL)
807 /* XXXX How to optimize if data == 0? */
808 /* Optimize to a simpler form. */
809 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
813 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
814 && (OP(nxt2) != WHILEM))
816 OP(nxt2) = SUCCEED; /* Whas WHILEM */
817 /* Need to optimize away parenths. */
818 if (data->flags & SF_IN_PAR) {
819 /* Set the parenth number. */
820 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
822 if (OP(nxt) != CLOSE)
823 FAIL("panic opt close");
824 oscan->flags = ARG(nxt);
825 OP(nxt1) = OPTIMIZED; /* was OPEN. */
826 OP(nxt) = OPTIMIZED; /* was CLOSE. */
828 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
829 OP(nxt + 1) = OPTIMIZED; /* was count. */
830 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
831 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
834 while ( nxt1 && (OP(nxt1) != WHILEM)) {
835 regnode *nnxt = regnext(nxt1);
838 if (reg_off_by_arg[OP(nxt1)])
839 ARG_SET(nxt1, nxt2 - nxt1);
840 else if (nxt2 - nxt1 < U16_MAX)
841 NEXT_OFF(nxt1) = nxt2 - nxt1;
843 OP(nxt) = NOTHING; /* Cannot beautify */
848 /* Optimize again: */
849 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
854 else if (OP(oscan) == CURLYX && data && ++data->whilem_c < 16) {
855 /* This stays as CURLYX, and can put the count/of pair. */
856 /* Find WHILEM (as in regexec.c) */
857 regnode *nxt = oscan + NEXT_OFF(oscan);
859 if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
861 PREVOPER(nxt)->flags = data->whilem_c
862 | (PL_reg_whilem_seen << 4); /* On WHILEM */
864 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
866 if (flags & SCF_DO_SUBSTR) {
867 SV *last_str = Nullsv;
868 int counted = mincount != 0;
870 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
871 I32 b = pos_before >= data->last_start_min
872 ? pos_before : data->last_start_min;
874 char *s = SvPV(data->last_found, l);
875 I32 old = b - data->last_start_min;
878 old = utf8_hop((U8*)s, old) - (U8*)s;
881 /* Get the added string: */
882 last_str = newSVpvn(s + old, l);
883 if (deltanext == 0 && pos_before == b) {
884 /* What was added is a constant string */
886 SvGROW(last_str, (mincount * l) + 1);
887 repeatcpy(SvPVX(last_str) + l,
888 SvPVX(last_str), l, mincount - 1);
889 SvCUR(last_str) *= mincount;
890 /* Add additional parts. */
891 SvCUR_set(data->last_found,
892 SvCUR(data->last_found) - l);
893 sv_catsv(data->last_found, last_str);
894 data->last_end += l * (mincount - 1);
898 /* It is counted once already... */
899 data->pos_min += minnext * (mincount - counted);
900 data->pos_delta += - counted * deltanext +
901 (minnext + deltanext) * maxcount - minnext * mincount;
902 if (mincount != maxcount) {
903 /* Cannot extend fixed substrings found inside
906 if (mincount && last_str) {
907 sv_setsv(data->last_found, last_str);
908 data->last_end = data->pos_min;
909 data->last_start_min =
910 data->pos_min - CHR_SVLEN(last_str);
911 data->last_start_max = is_inf
913 : data->pos_min + data->pos_delta
914 - CHR_SVLEN(last_str);
916 data->longest = &(data->longest_float);
918 SvREFCNT_dec(last_str);
920 if (data && (fl & SF_HAS_EVAL))
921 data->flags |= SF_HAS_EVAL;
923 if (OP(oscan) != CURLYX) {
924 while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
926 NEXT_OFF(oscan) += NEXT_OFF(next);
929 default: /* REF and CLUMP only? */
930 if (flags & SCF_DO_SUBSTR) {
931 scan_commit(data); /* Cannot expect anything... */
932 data->longest = &(data->longest_float);
934 is_inf = is_inf_internal = 1;
935 if (flags & SCF_DO_STCLASS_OR)
936 cl_anything(data->start_class);
937 flags &= ~SCF_DO_STCLASS;
941 else if (strchr((char*)PL_simple,OP(scan)) || PL_regkind[(U8)OP(scan)] == ANYUTF8) {
944 if (flags & SCF_DO_SUBSTR) {
949 if (flags & SCF_DO_STCLASS) {
950 data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */
952 /* Some of the logic below assumes that switching
953 locale on will only add false positives. */
954 switch (PL_regkind[(U8)OP(scan)]) {
971 /* Perl_croak(aTHX_ "panic: unexpected simple REx opcode %d", OP(scan)); */
972 if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
973 cl_anything(data->start_class);
976 if (OP(scan) == SANY)
978 if (flags & SCF_DO_STCLASS_OR) { /* Everything but \n */
979 value = (ANYOF_BITMAP_TEST(data->start_class,'\n')
980 || (data->start_class->flags & ANYOF_CLASS));
981 cl_anything(data->start_class);
983 if (flags & SCF_DO_STCLASS_AND || !value)
984 ANYOF_BITMAP_CLEAR(data->start_class,'\n');
987 if (flags & SCF_DO_STCLASS_AND)
988 cl_and(data->start_class,
989 (struct regnode_charclass_class*)scan);
991 cl_or(data->start_class,
992 (struct regnode_charclass_class*)scan);
995 if (flags & SCF_DO_STCLASS_AND) {
996 if (!(data->start_class->flags & ANYOF_LOCALE)) {
997 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
998 for (value = 0; value < 256; value++)
1000 ANYOF_BITMAP_CLEAR(data->start_class, value);
1004 if (data->start_class->flags & ANYOF_LOCALE)
1005 ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
1007 for (value = 0; value < 256; value++)
1009 ANYOF_BITMAP_SET(data->start_class, value);
1014 if (flags & SCF_DO_STCLASS_AND) {
1015 if (data->start_class->flags & ANYOF_LOCALE)
1016 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
1019 ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
1020 data->start_class->flags |= ANYOF_LOCALE;
1024 if (flags & SCF_DO_STCLASS_AND) {
1025 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1026 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
1027 for (value = 0; value < 256; value++)
1029 ANYOF_BITMAP_CLEAR(data->start_class, value);
1033 if (data->start_class->flags & ANYOF_LOCALE)
1034 ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
1036 for (value = 0; value < 256; value++)
1037 if (!isALNUM(value))
1038 ANYOF_BITMAP_SET(data->start_class, value);
1043 if (flags & SCF_DO_STCLASS_AND) {
1044 if (data->start_class->flags & ANYOF_LOCALE)
1045 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
1048 data->start_class->flags |= ANYOF_LOCALE;
1049 ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
1053 if (flags & SCF_DO_STCLASS_AND) {
1054 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1055 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
1056 for (value = 0; value < 256; value++)
1057 if (!isSPACE(value))
1058 ANYOF_BITMAP_CLEAR(data->start_class, value);
1062 if (data->start_class->flags & ANYOF_LOCALE)
1063 ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
1065 for (value = 0; value < 256; value++)
1067 ANYOF_BITMAP_SET(data->start_class, value);
1072 if (flags & SCF_DO_STCLASS_AND) {
1073 if (data->start_class->flags & ANYOF_LOCALE)
1074 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
1077 data->start_class->flags |= ANYOF_LOCALE;
1078 ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
1082 if (flags & SCF_DO_STCLASS_AND) {
1083 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1084 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
1085 for (value = 0; value < 256; value++)
1087 ANYOF_BITMAP_CLEAR(data->start_class, value);
1091 if (data->start_class->flags & ANYOF_LOCALE)
1092 ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
1094 for (value = 0; value < 256; value++)
1095 if (!isSPACE(value))
1096 ANYOF_BITMAP_SET(data->start_class, value);
1101 if (flags & SCF_DO_STCLASS_AND) {
1102 if (data->start_class->flags & ANYOF_LOCALE) {
1103 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
1104 for (value = 0; value < 256; value++)
1105 if (!isSPACE(value))
1106 ANYOF_BITMAP_CLEAR(data->start_class, value);
1110 data->start_class->flags |= ANYOF_LOCALE;
1111 ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
1115 if (flags & SCF_DO_STCLASS_AND) {
1116 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NDIGIT);
1117 for (value = 0; value < 256; value++)
1118 if (!isDIGIT(value))
1119 ANYOF_BITMAP_CLEAR(data->start_class, value);
1122 if (data->start_class->flags & ANYOF_LOCALE)
1123 ANYOF_CLASS_SET(data->start_class,ANYOF_DIGIT);
1125 for (value = 0; value < 256; value++)
1127 ANYOF_BITMAP_SET(data->start_class, value);
1132 if (flags & SCF_DO_STCLASS_AND) {
1133 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_DIGIT);
1134 for (value = 0; value < 256; value++)
1136 ANYOF_BITMAP_CLEAR(data->start_class, value);
1139 if (data->start_class->flags & ANYOF_LOCALE)
1140 ANYOF_CLASS_SET(data->start_class,ANYOF_NDIGIT);
1142 for (value = 0; value < 256; value++)
1143 if (!isDIGIT(value))
1144 ANYOF_BITMAP_SET(data->start_class, value);
1149 if (flags & SCF_DO_STCLASS_OR)
1150 cl_and(data->start_class, &and_with);
1151 flags &= ~SCF_DO_STCLASS;
1154 else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
1155 data->flags |= (OP(scan) == MEOL
1159 else if ( PL_regkind[(U8)OP(scan)] == BRANCHJ
1160 /* Lookbehind, or need to calculate parens/evals/stclass: */
1161 && (scan->flags || data || (flags & SCF_DO_STCLASS))
1162 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
1163 /* Lookahead/lookbehind */
1164 I32 deltanext, minnext;
1166 struct regnode_charclass_class intrnl;
1169 data_fake.flags = 0;
1171 data_fake.whilem_c = data->whilem_c;
1172 if ( flags & SCF_DO_STCLASS && !scan->flags
1173 && OP(scan) == IFMATCH ) { /* Lookahead */
1175 data_fake.start_class = &intrnl;
1176 f = SCF_DO_STCLASS_AND;
1178 next = regnext(scan);
1179 nscan = NEXTOPER(NEXTOPER(scan));
1180 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, f);
1183 FAIL("variable length lookbehind not implemented");
1185 else if (minnext > U8_MAX) {
1186 FAIL2("lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
1188 scan->flags = minnext;
1190 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
1192 if (data && (data_fake.flags & SF_HAS_EVAL))
1193 data->flags |= SF_HAS_EVAL;
1195 data->whilem_c = data_fake.whilem_c;
1197 int was = (data->start_class->flags & ANYOF_EOS);
1199 cl_and(data->start_class, &intrnl);
1201 data->start_class->flags |= ANYOF_EOS;
1204 else if (OP(scan) == OPEN) {
1207 else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
1208 next = regnext(scan);
1210 if ( next && (OP(next) != WHILEM) && next < last)
1211 is_par = 0; /* Disable optimization */
1213 else if (OP(scan) == EVAL) {
1215 data->flags |= SF_HAS_EVAL;
1217 else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded */
1218 if (flags & SCF_DO_SUBSTR) {
1220 data->longest = &(data->longest_float);
1222 is_inf = is_inf_internal = 1;
1223 if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
1224 cl_anything(data->start_class);
1226 /* Else: zero-length, ignore. */
1227 scan = regnext(scan);
1232 *deltap = is_inf_internal ? I32_MAX : delta;
1233 if (flags & SCF_DO_SUBSTR && is_inf)
1234 data->pos_delta = I32_MAX - data->pos_min;
1235 if (is_par > U8_MAX)
1237 if (is_par && pars==1 && data) {
1238 data->flags |= SF_IN_PAR;
1239 data->flags &= ~SF_HAS_PAR;
1241 else if (pars && data) {
1242 data->flags |= SF_HAS_PAR;
1243 data->flags &= ~SF_IN_PAR;
1245 if (flags & SCF_DO_STCLASS_OR)
1246 cl_and(data->start_class, &and_with);
1251 S_add_data(pTHX_ I32 n, char *s)
1254 if (PL_regcomp_rx->data) {
1255 Renewc(PL_regcomp_rx->data,
1256 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
1257 char, struct reg_data);
1258 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
1259 PL_regcomp_rx->data->count += n;
1262 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
1263 char, struct reg_data);
1264 New(1208, PL_regcomp_rx->data->what, n, U8);
1265 PL_regcomp_rx->data->count = n;
1267 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
1268 return PL_regcomp_rx->data->count - n;
1272 Perl_reginitcolors(pTHX)
1276 char *s = PerlEnv_getenv("PERL_RE_COLORS");
1279 PL_colors[0] = s = savepv(s);
1281 s = strchr(s, '\t');
1287 PL_colors[i] = s = "";
1291 PL_colors[i++] = "";
1297 - pregcomp - compile a regular expression into internal code
1299 * We can't allocate space until we know how big the compiled form will be,
1300 * but we can't compile it (and thus know how big it is) until we've got a
1301 * place to put the code. So we cheat: we compile it twice, once with code
1302 * generation turned off and size counting turned on, and once "for real".
1303 * This also means that we don't allocate space until we are sure that the
1304 * thing really will compile successfully, and we never have to move the
1305 * code and thus invalidate pointers into it. (Note that it has to be in
1306 * one piece because free() must be able to free it all.) [NB: not true in perl]
1308 * Beware that the optimization-preparation code in here knows about some
1309 * of the structure of the compiled regexp. [I'll say.]
1312 Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
1328 FAIL("NULL regexp argument");
1330 if (PL_curcop == &PL_compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
1331 PL_reg_flags |= RF_utf8;
1335 PL_regprecomp = savepvn(exp, xend - exp);
1336 DEBUG_r(if (!PL_colorset) reginitcolors());
1337 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling REx%s `%s%*s%s'\n",
1338 PL_colors[4],PL_colors[5],PL_colors[0],
1339 (int)(xend - exp), PL_regprecomp, PL_colors[1]));
1340 PL_regflags = pm->op_pmflags;
1344 PL_seen_zerolen = *exp == '^' ? -1 : 0;
1348 /* First pass: determine size, legality. */
1349 PL_regcomp_parse = exp;
1354 PL_regcode = &PL_regdummy;
1355 PL_reg_whilem_seen = 0;
1356 REGC((U8)REG_MAGIC, (char*)PL_regcode);
1357 if (reg(0, &flags) == NULL) {
1358 Safefree(PL_regprecomp);
1359 PL_regprecomp = Nullch;
1362 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %"IVdf" ", (IV)PL_regsize));
1364 /* Small enough for pointer-storage convention?
1365 If extralen==0, this means that we will not need long jumps. */
1366 if (PL_regsize >= 0x10000L && PL_extralen)
1367 PL_regsize += PL_extralen;
1370 if (PL_reg_whilem_seen > 15)
1371 PL_reg_whilem_seen = 15;
1373 /* Allocate space and initialize. */
1374 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
1377 FAIL("regexp out of space");
1379 r->prelen = xend - exp;
1380 r->precomp = PL_regprecomp;
1382 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
1383 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
1385 r->substrs = 0; /* Useful during FAIL. */
1386 r->startp = 0; /* Useful during FAIL. */
1387 r->endp = 0; /* Useful during FAIL. */
1391 /* Second pass: emit code. */
1392 PL_regcomp_parse = exp;
1396 PL_regcode = r->program;
1397 /* Store the count of eval-groups for security checks: */
1398 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
1399 REGC((U8)REG_MAGIC, (char*) PL_regcode++);
1401 if (reg(0, &flags) == NULL)
1404 /* Dig out information for optimizations. */
1405 r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
1406 pm->op_pmflags = PL_regflags;
1408 r->reganch |= ROPT_UTF8;
1409 r->regstclass = NULL;
1410 if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
1411 r->reganch |= ROPT_NAUGHTY;
1412 scan = r->program + 1; /* First BRANCH. */
1414 /* XXXX To minimize changes to RE engine we always allocate
1415 3-units-long substrs field. */
1416 Newz(1004, r->substrs, 1, struct reg_substr_data);
1418 StructCopy(&zero_scan_data, &data, scan_data_t);
1419 /* XXXX Should not we check for something else? Usually it is OPEN1... */
1420 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
1422 STRLEN longest_float_length, longest_fixed_length;
1423 struct regnode_charclass_class ch_class;
1427 /* Skip introductions and multiplicators >= 1. */
1428 while ((OP(first) == OPEN && (sawopen = 1)) ||
1429 /* An OR of *one* alternative - should not happen now. */
1430 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
1431 (OP(first) == PLUS) ||
1432 (OP(first) == MINMOD) ||
1433 /* An {n,m} with n>0 */
1434 (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
1435 if (OP(first) == PLUS)
1438 first += regarglen[(U8)OP(first)];
1439 first = NEXTOPER(first);
1442 /* Starting-point info. */
1444 if (PL_regkind[(U8)OP(first)] == EXACT) {
1445 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
1446 else if ((OP(first) == EXACTF || OP(first) == EXACTFL)
1448 r->regstclass = first;
1450 else if (strchr((char*)PL_simple,OP(first)))
1451 r->regstclass = first;
1452 else if (PL_regkind[(U8)OP(first)] == BOUND ||
1453 PL_regkind[(U8)OP(first)] == NBOUND)
1454 r->regstclass = first;
1455 else if (PL_regkind[(U8)OP(first)] == BOL) {
1456 r->reganch |= (OP(first) == MBOL
1458 : (OP(first) == SBOL
1461 first = NEXTOPER(first);
1464 else if (OP(first) == GPOS) {
1465 r->reganch |= ROPT_ANCH_GPOS;
1466 first = NEXTOPER(first);
1469 else if ((OP(first) == STAR &&
1470 PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
1471 !(r->reganch & ROPT_ANCH) )
1473 /* turn .* into ^.* with an implied $*=1 */
1474 int type = OP(NEXTOPER(first));
1476 if (type == REG_ANY || type == ANYUTF8)
1477 type = ROPT_ANCH_MBOL;
1479 type = ROPT_ANCH_SBOL;
1481 r->reganch |= type | ROPT_IMPLICIT;
1482 first = NEXTOPER(first);
1485 if (sawplus && (!sawopen || !PL_regsawback)
1486 && !(PL_regseen & REG_SEEN_EVAL)) /* May examine pos and $& */
1487 /* x+ must match at the 1st pos of run of x's */
1488 r->reganch |= ROPT_SKIP;
1490 /* Scan is after the zeroth branch, first is atomic matcher. */
1491 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n",
1492 (IV)(first - scan + 1)));
1494 * If there's something expensive in the r.e., find the
1495 * longest literal string that must appear and make it the
1496 * regmust. Resolve ties in favor of later strings, since
1497 * the regstart check works with the beginning of the r.e.
1498 * and avoiding duplication strengthens checking. Not a
1499 * strong reason, but sufficient in the absence of others.
1500 * [Now we resolve ties in favor of the earlier string if
1501 * it happens that c_offset_min has been invalidated, since the
1502 * earlier string may buy us something the later one won't.]
1506 data.longest_fixed = newSVpvn("",0);
1507 data.longest_float = newSVpvn("",0);
1508 data.last_found = newSVpvn("",0);
1509 data.longest = &(data.longest_fixed);
1511 if (!r->regstclass) {
1513 data.start_class = &ch_class;
1514 stclass_flag = SCF_DO_STCLASS_AND;
1515 } else /* XXXX Check for BOUND? */
1518 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
1519 &data, SCF_DO_SUBSTR | stclass_flag);
1520 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
1521 && data.last_start_min == 0 && data.last_end > 0
1523 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
1524 r->reganch |= ROPT_CHECK_ALL;
1526 SvREFCNT_dec(data.last_found);
1528 longest_float_length = CHR_SVLEN(data.longest_float);
1529 if (longest_float_length
1530 || (data.flags & SF_FL_BEFORE_EOL
1531 && (!(data.flags & SF_FL_BEFORE_MEOL)
1532 || (PL_regflags & PMf_MULTILINE)))) {
1535 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
1536 && data.offset_fixed == data.offset_float_min
1537 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
1538 goto remove_float; /* As in (a)+. */
1540 r->float_substr = data.longest_float;
1541 r->float_min_offset = data.offset_float_min;
1542 r->float_max_offset = data.offset_float_max;
1543 t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
1544 && (!(data.flags & SF_FL_BEFORE_MEOL)
1545 || (PL_regflags & PMf_MULTILINE)));
1546 fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
1550 r->float_substr = Nullsv;
1551 SvREFCNT_dec(data.longest_float);
1552 longest_float_length = 0;
1555 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
1556 if (longest_fixed_length
1557 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1558 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1559 || (PL_regflags & PMf_MULTILINE)))) {
1562 r->anchored_substr = data.longest_fixed;
1563 r->anchored_offset = data.offset_fixed;
1564 t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
1565 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1566 || (PL_regflags & PMf_MULTILINE)));
1567 fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
1570 r->anchored_substr = Nullsv;
1571 SvREFCNT_dec(data.longest_fixed);
1572 longest_fixed_length = 0;
1575 && (OP(r->regstclass) == REG_ANY || OP(r->regstclass) == ANYUTF8
1576 || OP(r->regstclass) == SANYUTF8 || OP(r->regstclass) == SANY))
1577 r->regstclass = NULL;
1578 if ((!r->anchored_substr || r->anchored_offset) && stclass_flag
1579 && !(data.start_class->flags & ANYOF_EOS)
1580 && !cl_is_anything(data.start_class)) {
1582 I32 n = add_data(1, "f");
1584 New(1006, PL_regcomp_rx->data->data[n], 1,
1585 struct regnode_charclass_class);
1586 StructCopy(data.start_class,
1587 (struct regnode_charclass_class*)PL_regcomp_rx->data->data[n],
1588 struct regnode_charclass_class);
1589 r->regstclass = (regnode*)PL_regcomp_rx->data->data[n];
1590 r->reganch &= ~ROPT_SKIP; /* Used in find_byclass(). */
1591 DEBUG_r((sv = sv_newmortal(),
1592 regprop(sv, (regnode*)data.start_class),
1593 PerlIO_printf(Perl_debug_log, "synthetic stclass.\n",
1597 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1598 if (longest_fixed_length > longest_float_length) {
1599 r->check_substr = r->anchored_substr;
1600 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1601 if (r->reganch & ROPT_ANCH_SINGLE)
1602 r->reganch |= ROPT_NOSCAN;
1605 r->check_substr = r->float_substr;
1606 r->check_offset_min = data.offset_float_min;
1607 r->check_offset_max = data.offset_float_max;
1609 /* XXXX Currently intuiting is not compatible with ANCH_GPOS.
1610 This should be changed ASAP! */
1611 if (r->check_substr && !(r->reganch & ROPT_ANCH_GPOS)) {
1612 r->reganch |= RE_USE_INTUIT;
1613 if (SvTAIL(r->check_substr))
1614 r->reganch |= RE_INTUIT_TAIL;
1618 /* Several toplevels. Best we can is to set minlen. */
1620 struct regnode_charclass_class ch_class;
1622 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1623 scan = r->program + 1;
1625 data.start_class = &ch_class;
1626 minlen = study_chunk(&scan, &fake, scan + PL_regsize, &data, SCF_DO_STCLASS_AND);
1627 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1628 if (!(data.start_class->flags & ANYOF_EOS)
1629 && !cl_is_anything(data.start_class)) {
1631 I32 n = add_data(1, "f");
1633 New(1006, PL_regcomp_rx->data->data[n], 1,
1634 struct regnode_charclass_class);
1635 StructCopy(data.start_class,
1636 (struct regnode_charclass_class*)PL_regcomp_rx->data->data[n],
1637 struct regnode_charclass_class);
1638 r->regstclass = (regnode*)PL_regcomp_rx->data->data[n];
1639 r->reganch &= ~ROPT_SKIP; /* Used in find_byclass(). */
1640 DEBUG_r((sv = sv_newmortal(),
1641 regprop(sv, (regnode*)data.start_class),
1642 PerlIO_printf(Perl_debug_log, "synthetic stclass.\n",
1648 if (PL_regseen & REG_SEEN_GPOS)
1649 r->reganch |= ROPT_GPOS_SEEN;
1650 if (PL_regseen & REG_SEEN_LOOKBEHIND)
1651 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1652 if (PL_regseen & REG_SEEN_EVAL)
1653 r->reganch |= ROPT_EVAL_SEEN;
1654 Newz(1002, r->startp, PL_regnpar, I32);
1655 Newz(1002, r->endp, PL_regnpar, I32);
1656 DEBUG_r(regdump(r));
1661 - reg - regular expression, i.e. main body or parenthesized thing
1663 * Caller must absorb opening parenthesis.
1665 * Combining parenthesis handling with the base level of regular expression
1666 * is a trifle forced, but the need to tie the tails of the branches to what
1667 * follows makes it hard to avoid.
1670 S_reg(pTHX_ I32 paren, I32 *flagp)
1671 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1674 register regnode *ret; /* Will be the head of the group. */
1675 register regnode *br;
1676 register regnode *lastbr;
1677 register regnode *ender = 0;
1678 register I32 parno = 0;
1679 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
1682 *flagp = 0; /* Tentatively. */
1684 /* Make an OPEN node, if parenthesized. */
1686 if (*PL_regcomp_parse == '?') {
1687 U16 posflags = 0, negflags = 0;
1688 U16 *flagsp = &posflags;
1692 paren = *PL_regcomp_parse++;
1693 ret = NULL; /* For look-ahead/behind. */
1696 PL_regseen |= REG_SEEN_LOOKBEHIND;
1697 if (*PL_regcomp_parse == '!')
1699 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
1710 FAIL2("Sequence (?%c...) not implemented", (int)paren);
1713 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1715 if (*PL_regcomp_parse != ')')
1716 FAIL("Sequence (?#... not terminated");
1722 paren = *PL_regcomp_parse++;
1727 I32 count = 1, n = 0;
1729 char *s = PL_regcomp_parse;
1731 OP_4tree *sop, *rop;
1734 PL_regseen |= REG_SEEN_EVAL;
1735 while (count && (c = *PL_regcomp_parse)) {
1736 if (c == '\\' && PL_regcomp_parse[1])
1744 if (*PL_regcomp_parse != ')')
1745 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1749 if (PL_regcomp_parse - 1 - s)
1750 sv = newSVpvn(s, PL_regcomp_parse - 1 - s);
1752 sv = newSVpvn("", 0);
1754 rop = sv_compile_2op(sv, &sop, "re", &av);
1756 n = add_data(3, "nop");
1757 PL_regcomp_rx->data->data[n] = (void*)rop;
1758 PL_regcomp_rx->data->data[n+1] = (void*)sop;
1759 PL_regcomp_rx->data->data[n+2] = (void*)av;
1762 else { /* First pass */
1763 if (PL_reginterp_cnt < ++PL_seen_evals
1764 && PL_curcop != &PL_compiling)
1765 /* No compiled RE interpolated, has runtime
1766 components ===> unsafe. */
1767 FAIL("Eval-group not allowed at runtime, use re 'eval'");
1769 FAIL("Eval-group in insecure regular expression");
1774 ret = reg_node(LOGICAL);
1777 regtail(ret, reganode(EVAL, n));
1780 return reganode(EVAL, n);
1784 if (PL_regcomp_parse[0] == '?') {
1785 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1786 || PL_regcomp_parse[1] == '<'
1787 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
1790 ret = reg_node(LOGICAL);
1793 regtail(ret, reg(1, &flag));
1797 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
1798 parno = atoi(PL_regcomp_parse++);
1800 while (isDIGIT(*PL_regcomp_parse))
1802 ret = reganode(GROUPP, parno);
1803 if ((c = *nextchar()) != ')')
1804 FAIL2("Switch (?(number%c not recognized", c);
1806 regtail(ret, reganode(IFTHEN, 0));
1807 br = regbranch(&flags, 1);
1809 br = reganode(LONGJMP, 0);
1811 regtail(br, reganode(LONGJMP, 0));
1816 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1817 regbranch(&flags, 1);
1818 regtail(ret, lastbr);
1826 FAIL("Switch (?(condition)... contains too many branches");
1827 ender = reg_node(TAIL);
1830 regtail(lastbr, ender);
1831 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1834 regtail(ret, ender);
1838 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
1842 FAIL("Sequence (? incomplete");
1847 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1848 if (*PL_regcomp_parse != 'o')
1849 pmflag(flagsp, *PL_regcomp_parse);
1852 if (*PL_regcomp_parse == '-') {
1857 PL_regflags |= posflags;
1858 PL_regflags &= ~negflags;
1859 if (*PL_regcomp_parse == ':') {
1865 if (*PL_regcomp_parse != ')')
1866 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
1875 ret = reganode(OPEN, parno);
1882 /* Pick up the branches, linking them together. */
1883 br = regbranch(&flags, 1);
1886 if (*PL_regcomp_parse == '|') {
1887 if (!SIZE_ONLY && PL_extralen) {
1888 reginsert(BRANCHJ, br);
1891 reginsert(BRANCH, br);
1894 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
1896 else if (paren == ':') {
1897 *flagp |= flags&SIMPLE;
1899 if (open) { /* Starts with OPEN. */
1900 regtail(ret, br); /* OPEN -> first. */
1902 else if (paren != '?') /* Not Conditional */
1906 *flagp |= flags&SPSTART;
1908 while (*PL_regcomp_parse == '|') {
1909 if (!SIZE_ONLY && PL_extralen) {
1910 ender = reganode(LONGJMP,0);
1911 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1914 PL_extralen += 2; /* Account for LONGJMP. */
1916 br = regbranch(&flags, 0);
1919 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1923 *flagp |= flags&SPSTART;
1926 if (have_branch || paren != ':') {
1927 /* Make a closing node, and hook it on the end. */
1930 ender = reg_node(TAIL);
1933 ender = reganode(CLOSE, parno);
1939 *flagp &= ~HASWIDTH;
1942 ender = reg_node(SUCCEED);
1945 ender = reg_node(END);
1948 regtail(lastbr, ender);
1951 /* Hook the tails of the branches to the closing node. */
1952 for (br = ret; br != NULL; br = regnext(br)) {
1953 regoptail(br, ender);
1960 static char parens[] = "=!<,>";
1962 if (paren && (p = strchr(parens, paren))) {
1963 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1964 int flag = (p - parens) > 1;
1967 node = SUSPEND, flag = 0;
1968 reginsert(node,ret);
1970 regtail(ret, reg_node(TAIL));
1974 /* Check for proper termination. */
1976 PL_regflags = oregflags;
1977 if (PL_regcomp_parse >= PL_regxend || *nextchar() != ')') {
1978 FAIL("unmatched () in regexp");
1981 else if (!paren && PL_regcomp_parse < PL_regxend) {
1982 if (*PL_regcomp_parse == ')') {
1983 FAIL("unmatched () in regexp");
1986 FAIL("junk on end of regexp"); /* "Can't happen". */
1994 - regbranch - one alternative of an | operator
1996 * Implements the concatenation operator.
1999 S_regbranch(pTHX_ I32 *flagp, I32 first)
2002 register regnode *ret;
2003 register regnode *chain = NULL;
2004 register regnode *latest;
2005 I32 flags = 0, c = 0;
2010 if (!SIZE_ONLY && PL_extralen)
2011 ret = reganode(BRANCHJ,0);
2013 ret = reg_node(BRANCH);
2016 if (!first && SIZE_ONLY)
2017 PL_extralen += 1; /* BRANCHJ */
2019 *flagp = WORST; /* Tentatively. */
2023 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
2025 latest = regpiece(&flags);
2026 if (latest == NULL) {
2027 if (flags & TRYAGAIN)
2031 else if (ret == NULL)
2033 *flagp |= flags&HASWIDTH;
2034 if (chain == NULL) /* First piece. */
2035 *flagp |= flags&SPSTART;
2038 regtail(chain, latest);
2043 if (chain == NULL) { /* Loop ran zero times. */
2044 chain = reg_node(NOTHING);
2049 *flagp |= flags&SIMPLE;
2056 - regpiece - something followed by possible [*+?]
2058 * Note that the branching code sequences used for ? and the general cases
2059 * of * and + are somewhat optimized: they use the same NOTHING node as
2060 * both the endmarker for their branch list and the body of the last branch.
2061 * It might seem that this node could be dispensed with entirely, but the
2062 * endmarker role is not redundant.
2065 S_regpiece(pTHX_ I32 *flagp)
2068 register regnode *ret;
2070 register char *next;
2072 char *origparse = PL_regcomp_parse;
2075 I32 max = REG_INFTY;
2077 ret = regatom(&flags);
2079 if (flags & TRYAGAIN)
2084 op = *PL_regcomp_parse;
2086 if (op == '{' && regcurly(PL_regcomp_parse)) {
2087 next = PL_regcomp_parse + 1;
2089 while (isDIGIT(*next) || *next == ',') {
2098 if (*next == '}') { /* got one */
2102 min = atoi(PL_regcomp_parse);
2106 maxpos = PL_regcomp_parse;
2108 if (!max && *maxpos != '0')
2109 max = REG_INFTY; /* meaning "infinity" */
2110 else if (max >= REG_INFTY)
2111 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
2112 PL_regcomp_parse = next;
2116 if ((flags&SIMPLE)) {
2117 PL_regnaughty += 2 + PL_regnaughty / 2;
2118 reginsert(CURLY, ret);
2121 regnode *w = reg_node(WHILEM);
2125 if (!SIZE_ONLY && PL_extralen) {
2126 reginsert(LONGJMP,ret);
2127 reginsert(NOTHING,ret);
2128 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
2130 reginsert(CURLYX,ret);
2131 if (!SIZE_ONLY && PL_extralen)
2132 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
2133 regtail(ret, reg_node(NOTHING));
2135 PL_reg_whilem_seen++, PL_extralen += 3;
2136 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
2144 if (max && max < min)
2145 FAIL("Can't do {n,m} with n > m");
2160 #if 0 /* Now runtime fix should be reliable. */
2161 if (!(flags&HASWIDTH) && op != '?')
2162 FAIL("regexp *+ operand could be empty");
2167 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
2169 if (op == '*' && (flags&SIMPLE)) {
2170 reginsert(STAR, ret);
2174 else if (op == '*') {
2178 else if (op == '+' && (flags&SIMPLE)) {
2179 reginsert(PLUS, ret);
2183 else if (op == '+') {
2187 else if (op == '?') {
2192 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
2193 Perl_warner(aTHX_ WARN_UNSAFE, "%.*s matches null string many times",
2194 PL_regcomp_parse - origparse, origparse);
2197 if (*PL_regcomp_parse == '?') {
2199 reginsert(MINMOD, ret);
2200 regtail(ret, ret + NODE_STEP_REGNODE);
2202 if (ISMULT2(PL_regcomp_parse))
2203 FAIL("nested *?+ in regexp");
2209 - regatom - the lowest level
2211 * Optimization: gobbles an entire sequence of ordinary characters so that
2212 * it can turn them into a single node, which is smaller to store and
2213 * faster to run. Backslashed characters are exceptions, each becoming a
2214 * separate node; the code is simpler that way and it's not worth fixing.
2216 * [Yes, it is worth fixing, some scripts can run twice the speed.]
2219 S_regatom(pTHX_ I32 *flagp)
2222 register regnode *ret = 0;
2225 *flagp = WORST; /* Tentatively. */
2228 switch (*PL_regcomp_parse) {
2232 if (PL_regflags & PMf_MULTILINE)
2233 ret = reg_node(MBOL);
2234 else if (PL_regflags & PMf_SINGLELINE)
2235 ret = reg_node(SBOL);
2237 ret = reg_node(BOL);
2240 if (PL_regcomp_parse[1])
2243 if (PL_regflags & PMf_MULTILINE)
2244 ret = reg_node(MEOL);
2245 else if (PL_regflags & PMf_SINGLELINE)
2246 ret = reg_node(SEOL);
2248 ret = reg_node(EOL);
2253 if (PL_regflags & PMf_SINGLELINE)
2254 ret = reg_node(SANYUTF8);
2256 ret = reg_node(ANYUTF8);
2260 if (PL_regflags & PMf_SINGLELINE)
2261 ret = reg_node(SANY);
2263 ret = reg_node(REG_ANY);
2264 *flagp |= HASWIDTH|SIMPLE;
2270 ret = (UTF ? regclassutf8() : regclass());
2271 if (*PL_regcomp_parse != ']')
2272 FAIL("unmatched [] in regexp");
2274 *flagp |= HASWIDTH|SIMPLE;
2278 ret = reg(1, &flags);
2280 if (flags & TRYAGAIN)
2284 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
2288 if (flags & TRYAGAIN) {
2292 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
2293 /* Supposed to be caught earlier. */
2296 if (!regcurly(PL_regcomp_parse)) {
2304 FAIL("?+*{} follows nothing in regexp");
2307 switch (*++PL_regcomp_parse) {
2310 ret = reg_node(SBOL);
2315 ret = reg_node(GPOS);
2316 PL_regseen |= REG_SEEN_GPOS;
2321 ret = reg_node(SEOL);
2326 ret = reg_node(EOS);
2328 PL_seen_zerolen++; /* Do not optimize RE away */
2332 ret = reg_node(SANY);
2333 *flagp |= HASWIDTH|SIMPLE;
2337 ret = reg_node(CLUMP);
2340 if (UTF && !PL_utf8_mark)
2341 is_utf8_mark((U8*)"~"); /* preload table */
2346 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
2347 : (LOC ? ALNUML : ALNUM));
2348 *flagp |= HASWIDTH|SIMPLE;
2350 if (UTF && !PL_utf8_alnum)
2351 is_utf8_alnum((U8*)"a"); /* preload table */
2356 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
2357 : (LOC ? NALNUML : NALNUM));
2358 *flagp |= HASWIDTH|SIMPLE;
2360 if (UTF && !PL_utf8_alnum)
2361 is_utf8_alnum((U8*)"a"); /* preload table */
2365 PL_regseen |= REG_SEEN_LOOKBEHIND;
2368 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
2369 : (LOC ? BOUNDL : BOUND));
2372 if (UTF && !PL_utf8_alnum)
2373 is_utf8_alnum((U8*)"a"); /* preload table */
2377 PL_regseen |= REG_SEEN_LOOKBEHIND;
2380 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
2381 : (LOC ? NBOUNDL : NBOUND));
2384 if (UTF && !PL_utf8_alnum)
2385 is_utf8_alnum((U8*)"a"); /* preload table */
2390 ? (LOC ? SPACELUTF8 : SPACEUTF8)
2391 : (LOC ? SPACEL : SPACE));
2392 *flagp |= HASWIDTH|SIMPLE;
2394 if (UTF && !PL_utf8_space)
2395 is_utf8_space((U8*)" "); /* preload table */
2400 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
2401 : (LOC ? NSPACEL : NSPACE));
2402 *flagp |= HASWIDTH|SIMPLE;
2404 if (UTF && !PL_utf8_space)
2405 is_utf8_space((U8*)" "); /* preload table */
2408 ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
2409 *flagp |= HASWIDTH|SIMPLE;
2411 if (UTF && !PL_utf8_digit)
2412 is_utf8_digit((U8*)"1"); /* preload table */
2415 ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
2416 *flagp |= HASWIDTH|SIMPLE;
2418 if (UTF && !PL_utf8_digit)
2419 is_utf8_digit((U8*)"1"); /* preload table */
2423 { /* a lovely hack--pretend we saw [\pX] instead */
2424 char* oldregxend = PL_regxend;
2426 if (PL_regcomp_parse[1] == '{') {
2427 PL_regxend = strchr(PL_regcomp_parse, '}');
2429 FAIL("Missing right brace on \\p{}");
2433 PL_regxend = PL_regcomp_parse + 2;
2436 ret = regclassutf8();
2438 PL_regxend = oldregxend;
2441 *flagp |= HASWIDTH|SIMPLE;
2454 case '1': case '2': case '3': case '4':
2455 case '5': case '6': case '7': case '8': case '9':
2457 I32 num = atoi(PL_regcomp_parse);
2459 if (num > 9 && num >= PL_regnpar)
2462 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
2463 FAIL("reference to nonexistent group");
2466 ? (LOC ? REFFL : REFF)
2469 while (isDIGIT(*PL_regcomp_parse))
2477 if (PL_regcomp_parse >= PL_regxend)
2478 FAIL("trailing \\ in regexp");
2481 /* Do not generate `unrecognized' warnings here, we fall
2482 back into the quick-grab loop below */
2488 if (PL_regflags & PMf_EXTENDED) {
2489 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
2490 if (PL_regcomp_parse < PL_regxend)
2506 ? (LOC ? EXACTFL : EXACTF)
2509 for (len = 0, p = PL_regcomp_parse - 1;
2510 len < 127 && p < PL_regxend;
2515 if (PL_regflags & PMf_EXTENDED)
2516 p = regwhite(p, PL_regxend);
2570 char* e = strchr(p, '}');
2573 FAIL("Missing right brace on \\x{}");
2575 ender = (UV)scan_hex(p + 1, e - p, &numlen);
2576 if (numlen + len >= 127) { /* numlen is generous */
2583 FAIL("Can't use \\x{} without 'use utf8' declaration");
2586 ender = (UV)scan_hex(p, 2, &numlen);
2592 ender = UCHARAT(p++);
2593 ender = toCTRL(ender);
2595 case '0': case '1': case '2': case '3':case '4':
2596 case '5': case '6': case '7': case '8':case '9':
2598 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
2599 ender = (UV)scan_oct(p, 3, &numlen);
2608 if (p >= PL_regxend)
2609 FAIL("trailing \\ in regexp");
2612 if (!SIZE_ONLY && ckWARN(WARN_UNSAFE) && isALPHA(*p))
2613 Perl_warner(aTHX_ WARN_UNSAFE,
2614 "/%.127s/: Unrecognized escape \\%c passed through",
2617 goto normal_default;
2622 if ((*p & 0xc0) == 0xc0 && UTF) {
2623 ender = utf8_to_uv((U8*)p, &numlen);
2630 if (PL_regflags & PMf_EXTENDED)
2631 p = regwhite(p, PL_regxend);
2634 ender = toLOWER_LC_uni(ender);
2636 ender = toLOWER_uni(ender);
2638 if (ISMULT2(p)) { /* Back off on ?+*. */
2641 else if (ender >= 0x80 && UTF) {
2642 reguni(ender, s, &numlen);
2652 if (ender >= 0x80 && UTF) {
2653 reguni(ender, s, &numlen);
2661 PL_regcomp_parse = p - 1;
2664 FAIL("internal disaster in regexp");
2672 PL_regsize += STR_SZ(len);
2674 PL_regcode += STR_SZ(len);
2683 S_regwhite(pTHX_ char *p, char *e)
2688 else if (*p == '#') {
2691 } while (p < e && *p != '\n');
2699 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
2700 Character classes ([:foo:]) can also be negated ([:^foo:]).
2701 Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
2702 Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
2703 but trigger warnings because they are currently unimplemented. */
2705 S_regpposixcc(pTHX_ I32 value)
2709 I32 namedclass = OOB_NAMEDCLASS;
2711 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2712 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2713 (*PL_regcomp_parse == ':' ||
2714 *PL_regcomp_parse == '=' ||
2715 *PL_regcomp_parse == '.')) {
2716 char c = *PL_regcomp_parse;
2717 char* s = PL_regcomp_parse++;
2719 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != c)
2721 if (PL_regcomp_parse == PL_regxend)
2722 /* Grandfather lone [:, [=, [. */
2723 PL_regcomp_parse = s;
2725 char* t = PL_regcomp_parse++; /* skip over the c */
2727 if (*PL_regcomp_parse == ']') {
2728 PL_regcomp_parse++; /* skip over the ending ] */
2731 I32 complement = *posixcc == '^' ? *posixcc++ : 0;
2732 I32 skip = 5; /* the most common skip */
2736 if (strnEQ(posixcc, "alnum", 5))
2738 complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
2739 else if (strnEQ(posixcc, "alpha", 5))
2741 complement ? ANYOF_NALPHA : ANYOF_ALPHA;
2742 else if (strnEQ(posixcc, "ascii", 5))
2744 complement ? ANYOF_NASCII : ANYOF_ASCII;
2747 if (strnEQ(posixcc, "cntrl", 5))
2749 complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
2752 if (strnEQ(posixcc, "digit", 5))
2754 complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
2757 if (strnEQ(posixcc, "graph", 5))
2759 complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
2762 if (strnEQ(posixcc, "lower", 5))
2764 complement ? ANYOF_NLOWER : ANYOF_LOWER;
2767 if (strnEQ(posixcc, "print", 5))
2769 complement ? ANYOF_NPRINT : ANYOF_PRINT;
2770 else if (strnEQ(posixcc, "punct", 5))
2772 complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
2775 if (strnEQ(posixcc, "space", 5))
2777 complement ? ANYOF_NSPACE : ANYOF_SPACE;
2779 if (strnEQ(posixcc, "upper", 5))
2781 complement ? ANYOF_NUPPER : ANYOF_UPPER;
2783 case 'w': /* this is not POSIX, this is the Perl \w */
2784 if (strnEQ(posixcc, "word", 4)) {
2786 complement ? ANYOF_NALNUM : ANYOF_ALNUM;
2791 if (strnEQ(posixcc, "xdigit", 6)) {
2793 complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
2798 if ((namedclass == OOB_NAMEDCLASS ||
2799 !(posixcc + skip + 2 < PL_regxend &&
2800 (posixcc[skip] == ':' &&
2801 posixcc[skip + 1] == ']'))))
2802 Perl_croak(aTHX_ "Character class [:%.*s:] unknown",
2804 } else if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
2805 /* [[=foo=]] and [[.foo.]] are still future. */
2806 Perl_warner(aTHX_ WARN_UNSAFE,
2807 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2809 /* Maternal grandfather:
2810 * "[:" ending in ":" but not in ":]" */
2811 PL_regcomp_parse = s;
2820 S_checkposixcc(pTHX)
2822 if (!SIZE_ONLY && ckWARN(WARN_UNSAFE) &&
2823 (*PL_regcomp_parse == ':' ||
2824 *PL_regcomp_parse == '=' ||
2825 *PL_regcomp_parse == '.')) {
2826 char *s = PL_regcomp_parse;
2829 while(*s && isALNUM(*s))
2831 if (*s && c == *s && s[1] == ']') {
2832 Perl_warner(aTHX_ WARN_UNSAFE,
2833 "Character class syntax [%c %c] belongs inside character classes", c, c);
2834 if (c == '=' || c == '.')
2835 Perl_warner(aTHX_ WARN_UNSAFE,
2836 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2846 register I32 lastvalue = OOB_CHAR8;
2847 register I32 range = 0;
2848 register regnode *ret;
2853 bool need_class = 0;
2855 ret = reg_node(ANYOF);
2857 PL_regsize += ANYOF_SKIP;
2860 ANYOF_BITMAP_ZERO(ret);
2861 PL_regcode += ANYOF_SKIP;
2863 ANYOF_FLAGS(ret) |= ANYOF_FOLD;
2865 ANYOF_FLAGS(ret) |= ANYOF_LOCALE;
2867 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2871 ANYOF_FLAGS(ret) |= ANYOF_INVERT;
2874 if (!SIZE_ONLY && ckWARN(WARN_UNSAFE))
2877 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2878 goto skipcond; /* allow 1st char to be ] or - */
2879 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2881 namedclass = OOB_NAMEDCLASS;
2883 rangebegin = PL_regcomp_parse;
2884 value = UCHARAT(PL_regcomp_parse++);
2886 namedclass = regpposixcc(value);
2887 else if (value == '\\') {
2888 value = UCHARAT(PL_regcomp_parse++);
2890 case 'w': namedclass = ANYOF_ALNUM; break;
2891 case 'W': namedclass = ANYOF_NALNUM; break;
2892 case 's': namedclass = ANYOF_SPACE; break;
2893 case 'S': namedclass = ANYOF_NSPACE; break;
2894 case 'd': namedclass = ANYOF_DIGIT; break;
2895 case 'D': namedclass = ANYOF_NDIGIT; break;
2896 case 'n': value = '\n'; break;
2897 case 'r': value = '\r'; break;
2898 case 't': value = '\t'; break;
2899 case 'f': value = '\f'; break;
2900 case 'b': value = '\b'; break;
2901 case 'e': value = '\033'; break;
2902 case 'a': value = '\007'; break;
2904 value = (UV)scan_hex(PL_regcomp_parse, 2, &numlen);
2905 PL_regcomp_parse += numlen;
2908 value = UCHARAT(PL_regcomp_parse++);
2909 value = toCTRL(value);
2911 case '0': case '1': case '2': case '3': case '4':
2912 case '5': case '6': case '7': case '8': case '9':
2913 value = (UV)scan_oct(--PL_regcomp_parse, 3, &numlen);
2914 PL_regcomp_parse += numlen;
2917 if (!SIZE_ONLY && ckWARN(WARN_UNSAFE) && isALPHA(value))
2918 Perl_warner(aTHX_ WARN_UNSAFE,
2919 "/%.127s/: Unrecognized escape \\%c in character class passed through",
2925 if (namedclass > OOB_NAMEDCLASS) {
2926 if (!need_class && !SIZE_ONLY)
2927 ANYOF_CLASS_ZERO(ret);
2929 if (range) { /* a-\d, a-[:digit:] */
2931 if (ckWARN(WARN_UNSAFE))
2932 Perl_warner(aTHX_ WARN_UNSAFE,
2933 "/%.127s/: false [] range \"%*.*s\" in regexp",
2935 PL_regcomp_parse - rangebegin,
2936 PL_regcomp_parse - rangebegin,
2938 ANYOF_BITMAP_SET(ret, lastvalue);
2939 ANYOF_BITMAP_SET(ret, '-');
2941 range = 0; /* this is not a true range */
2944 switch (namedclass) {
2947 ANYOF_CLASS_SET(ret, ANYOF_ALNUM);
2949 for (value = 0; value < 256; value++)
2951 ANYOF_BITMAP_SET(ret, value);
2956 ANYOF_CLASS_SET(ret, ANYOF_NALNUM);
2958 for (value = 0; value < 256; value++)
2959 if (!isALNUM(value))
2960 ANYOF_BITMAP_SET(ret, value);
2965 ANYOF_CLASS_SET(ret, ANYOF_SPACE);
2967 for (value = 0; value < 256; value++)
2969 ANYOF_BITMAP_SET(ret, value);
2974 ANYOF_CLASS_SET(ret, ANYOF_NSPACE);
2976 for (value = 0; value < 256; value++)
2977 if (!isSPACE(value))
2978 ANYOF_BITMAP_SET(ret, value);
2983 ANYOF_CLASS_SET(ret, ANYOF_DIGIT);
2985 for (value = '0'; value <= '9'; value++)
2986 ANYOF_BITMAP_SET(ret, value);
2991 ANYOF_CLASS_SET(ret, ANYOF_NDIGIT);
2993 for (value = 0; value < '0'; value++)
2994 ANYOF_BITMAP_SET(ret, value);
2995 for (value = '9' + 1; value < 256; value++)
2996 ANYOF_BITMAP_SET(ret, value);
3001 ANYOF_CLASS_SET(ret, ANYOF_NALNUMC);
3003 for (value = 0; value < 256; value++)
3004 if (!isALNUMC(value))
3005 ANYOF_BITMAP_SET(ret, value);
3010 ANYOF_CLASS_SET(ret, ANYOF_ALNUMC);
3012 for (value = 0; value < 256; value++)
3013 if (isALNUMC(value))
3014 ANYOF_BITMAP_SET(ret, value);
3019 ANYOF_CLASS_SET(ret, ANYOF_ALPHA);
3021 for (value = 0; value < 256; value++)
3023 ANYOF_BITMAP_SET(ret, value);
3028 ANYOF_CLASS_SET(ret, ANYOF_NALPHA);
3030 for (value = 0; value < 256; value++)
3031 if (!isALPHA(value))
3032 ANYOF_BITMAP_SET(ret, value);
3037 ANYOF_CLASS_SET(ret, ANYOF_ASCII);
3040 for (value = 0; value < 128; value++)
3041 ANYOF_BITMAP_SET(ret, value);
3043 for (value = 0; value < 256; value++)
3045 ANYOF_BITMAP_SET(ret, value);
3051 ANYOF_CLASS_SET(ret, ANYOF_NASCII);
3054 for (value = 128; value < 256; value++)
3055 ANYOF_BITMAP_SET(ret, value);
3057 for (value = 0; value < 256; value++)
3058 if (!isASCII(value))
3059 ANYOF_BITMAP_SET(ret, value);
3065 ANYOF_CLASS_SET(ret, ANYOF_CNTRL);
3067 for (value = 0; value < 256; value++)
3069 ANYOF_BITMAP_SET(ret, value);
3071 lastvalue = OOB_CHAR8;
3075 ANYOF_CLASS_SET(ret, ANYOF_NCNTRL);
3077 for (value = 0; value < 256; value++)
3078 if (!isCNTRL(value))
3079 ANYOF_BITMAP_SET(ret, value);
3084 ANYOF_CLASS_SET(ret, ANYOF_GRAPH);
3086 for (value = 0; value < 256; value++)
3088 ANYOF_BITMAP_SET(ret, value);
3093 ANYOF_CLASS_SET(ret, ANYOF_NGRAPH);
3095 for (value = 0; value < 256; value++)
3096 if (!isGRAPH(value))
3097 ANYOF_BITMAP_SET(ret, value);
3102 ANYOF_CLASS_SET(ret, ANYOF_LOWER);
3104 for (value = 0; value < 256; value++)
3106 ANYOF_BITMAP_SET(ret, value);
3111 ANYOF_CLASS_SET(ret, ANYOF_NLOWER);
3113 for (value = 0; value < 256; value++)
3114 if (!isLOWER(value))
3115 ANYOF_BITMAP_SET(ret, value);
3120 ANYOF_CLASS_SET(ret, ANYOF_PRINT);
3122 for (value = 0; value < 256; value++)
3124 ANYOF_BITMAP_SET(ret, value);
3129 ANYOF_CLASS_SET(ret, ANYOF_NPRINT);
3131 for (value = 0; value < 256; value++)
3132 if (!isPRINT(value))
3133 ANYOF_BITMAP_SET(ret, value);
3138 ANYOF_CLASS_SET(ret, ANYOF_PUNCT);
3140 for (value = 0; value < 256; value++)
3142 ANYOF_BITMAP_SET(ret, value);
3147 ANYOF_CLASS_SET(ret, ANYOF_NPUNCT);
3149 for (value = 0; value < 256; value++)
3150 if (!isPUNCT(value))
3151 ANYOF_BITMAP_SET(ret, value);
3156 ANYOF_CLASS_SET(ret, ANYOF_UPPER);
3158 for (value = 0; value < 256; value++)
3160 ANYOF_BITMAP_SET(ret, value);
3165 ANYOF_CLASS_SET(ret, ANYOF_NUPPER);
3167 for (value = 0; value < 256; value++)
3168 if (!isUPPER(value))
3169 ANYOF_BITMAP_SET(ret, value);
3174 ANYOF_CLASS_SET(ret, ANYOF_XDIGIT);
3176 for (value = 0; value < 256; value++)
3177 if (isXDIGIT(value))
3178 ANYOF_BITMAP_SET(ret, value);
3183 ANYOF_CLASS_SET(ret, ANYOF_NXDIGIT);
3185 for (value = 0; value < 256; value++)
3186 if (!isXDIGIT(value))
3187 ANYOF_BITMAP_SET(ret, value);
3191 FAIL("invalid [::] class in regexp");
3195 ANYOF_FLAGS(ret) |= ANYOF_CLASS;
3200 if (lastvalue > value) /* b-a */ {
3202 "/%.127s/: invalid [] range \"%*.*s\" in regexp",
3204 PL_regcomp_parse - rangebegin,
3205 PL_regcomp_parse - rangebegin,
3212 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
3213 PL_regcomp_parse[1] != ']') {
3215 if (namedclass > OOB_NAMEDCLASS) { /* \w-, [:word:]- */
3216 if (ckWARN(WARN_UNSAFE))
3217 Perl_warner(aTHX_ WARN_UNSAFE,
3218 "/%.127s/: false [] range \"%*.*s\" in regexp",
3220 PL_regcomp_parse - rangebegin,
3221 PL_regcomp_parse - rangebegin,
3224 ANYOF_BITMAP_SET(ret, '-');
3227 continue; /* do it next time */
3230 /* now is the next time */
3232 #ifndef ASCIIish /* EBCDIC, for example. */
3233 if ((isLOWER(lastvalue) && isLOWER(value)) ||
3234 (isUPPER(lastvalue) && isUPPER(value)))
3237 if (isLOWER(lastvalue)) {
3238 for (i = lastvalue; i <= value; i++)
3240 ANYOF_BITMAP_SET(ret, i);
3242 for (i = lastvalue; i <= value; i++)
3244 ANYOF_BITMAP_SET(ret, i);
3249 for ( ; lastvalue <= value; lastvalue++)
3250 ANYOF_BITMAP_SET(ret, lastvalue);
3256 PL_regsize += ANYOF_CLASS_ADD_SKIP;
3258 PL_regcode += ANYOF_CLASS_ADD_SKIP;
3260 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
3262 (ANYOF_FLAGS(ret) & (ANYOF_FLAGS_ALL ^ ANYOF_INVERT)) == ANYOF_FOLD) {
3263 for (value = 0; value < 256; ++value) {
3264 if (ANYOF_BITMAP_TEST(ret, value)) {
3265 I32 cf = PL_fold[value];
3266 ANYOF_BITMAP_SET(ret, cf);
3269 ANYOF_FLAGS(ret) &= ~ANYOF_FOLD;
3271 /* optimize inverted simple patterns (e.g. [^a-z]) */
3272 if (!SIZE_ONLY && (ANYOF_FLAGS(ret) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
3273 for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
3274 ANYOF_BITMAP(ret)[value] ^= ANYOF_FLAGS_ALL;
3275 ANYOF_FLAGS(ret) = 0;
3281 S_regclassutf8(pTHX)
3286 register U32 lastvalue = OOB_UTF8;
3287 register I32 range = 0;
3288 register regnode *ret;
3296 if (*PL_regcomp_parse == '^') { /* Complement of range. */
3300 flags |= ANYOF_INVERT;
3304 flags |= ANYOF_FOLD;
3306 flags |= ANYOF_LOCALE;
3307 listsv = newSVpvn("# comment\n",10);
3310 if (!SIZE_ONLY && ckWARN(WARN_UNSAFE))
3313 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
3314 goto skipcond; /* allow 1st char to be ] or - */
3316 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
3318 namedclass = OOB_NAMEDCLASS;
3320 rangebegin = PL_regcomp_parse;
3321 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
3322 PL_regcomp_parse += numlen;
3324 namedclass = regpposixcc(value);
3325 else if (value == '\\') {
3326 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
3327 PL_regcomp_parse += numlen;
3329 case 'w': namedclass = ANYOF_ALNUM; break;
3330 case 'W': namedclass = ANYOF_NALNUM; break;
3331 case 's': namedclass = ANYOF_SPACE; break;
3332 case 'S': namedclass = ANYOF_NSPACE; break;
3333 case 'd': namedclass = ANYOF_DIGIT; break;
3334 case 'D': namedclass = ANYOF_NDIGIT; break;
3337 if (*PL_regcomp_parse == '{') {
3338 e = strchr(PL_regcomp_parse++, '}');
3340 FAIL("Missing right brace on \\p{}");
3341 n = e - PL_regcomp_parse;
3344 e = PL_regcomp_parse;
3349 Perl_sv_catpvf(aTHX_ listsv,
3350 "+utf8::%.*s\n", n, PL_regcomp_parse);
3352 Perl_sv_catpvf(aTHX_ listsv,
3353 "!utf8::%.*s\n", n, PL_regcomp_parse);
3355 PL_regcomp_parse = e + 1;
3356 lastvalue = OOB_UTF8;
3358 case 'n': value = '\n'; break;
3359 case 'r': value = '\r'; break;
3360 case 't': value = '\t'; break;
3361 case 'f': value = '\f'; break;
3362 case 'b': value = '\b'; break;
3363 case 'e': value = '\033'; break;
3364 case 'a': value = '\007'; break;
3366 if (*PL_regcomp_parse == '{') {
3367 e = strchr(PL_regcomp_parse++, '}');
3369 FAIL("Missing right brace on \\x{}");
3370 value = (UV)scan_hex(PL_regcomp_parse,
3371 e - PL_regcomp_parse,
3373 PL_regcomp_parse = e + 1;
3376 value = (UV)scan_hex(PL_regcomp_parse, 2, &numlen);
3377 PL_regcomp_parse += numlen;
3381 value = UCHARAT(PL_regcomp_parse++);
3382 value = toCTRL(value);
3384 case '0': case '1': case '2': case '3': case '4':
3385 case '5': case '6': case '7': case '8': case '9':
3386 value = (UV)scan_oct(--PL_regcomp_parse, 3, &numlen);
3387 PL_regcomp_parse += numlen;
3390 if (!SIZE_ONLY && ckWARN(WARN_UNSAFE) && isALPHA(value))
3391 Perl_warner(aTHX_ WARN_UNSAFE,
3392 "/%.127s/: Unrecognized escape \\%c in character class passed through",
3398 if (namedclass > OOB_NAMEDCLASS) {
3399 if (range) { /* a-\d, a-[:digit:] */
3401 if (ckWARN(WARN_UNSAFE))
3402 Perl_warner(aTHX_ WARN_UNSAFE,
3403 "/%.127s/: false [] range \"%*.*s\" in regexp",
3405 PL_regcomp_parse - rangebegin,
3406 PL_regcomp_parse - rangebegin,
3408 Perl_sv_catpvf(aTHX_ listsv,
3409 /* 0x002D is Unicode for '-' */
3410 "%04"UVxf"\n002D\n", (UV)lastvalue);
3415 switch (namedclass) {
3417 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n"); break;
3419 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n"); break;
3421 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n"); break;
3423 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n"); break;
3425 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n"); break;
3427 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n"); break;
3429 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n"); break;
3431 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n"); break;
3433 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n"); break;
3435 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n"); break;
3437 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n"); break;
3439 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n"); break;
3441 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n"); break;
3443 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n"); break;
3445 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n"); break;
3447 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n"); break;
3449 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n"); break;
3451 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n"); break;
3453 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n"); break;
3455 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n"); break;
3457 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n"); break;
3459 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n"); break;
3461 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n"); break;
3463 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n"); break;
3465 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n"); break;
3467 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n"); break;
3473 if (lastvalue > value) { /* b-a */
3475 "/%.127s/: invalid [] range \"%*.*s\" in regexp",
3477 PL_regcomp_parse - rangebegin,
3478 PL_regcomp_parse - rangebegin,
3485 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
3486 PL_regcomp_parse[1] != ']') {
3488 if (namedclass > OOB_NAMEDCLASS) { /* \w-, [:word:]- */
3489 if (ckWARN(WARN_UNSAFE))
3490 Perl_warner(aTHX_ WARN_UNSAFE,
3491 "/%.127s/: false [] range \"%*.*s\" in regexp",
3493 PL_regcomp_parse - rangebegin,
3494 PL_regcomp_parse - rangebegin,
3497 Perl_sv_catpvf(aTHX_ listsv,
3498 /* 0x002D is Unicode for '-' */
3502 continue; /* do it next time */
3505 /* now is the next time */
3507 Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\t%04"UVxf"\n",
3508 (UV)lastvalue, (UV)value);
3512 ret = reganode(ANYOFUTF8, 0);
3515 SV *rv = swash_init("utf8", "", listsv, 1, 0);
3516 SvREFCNT_dec(listsv);
3517 n = add_data(1,"s");
3518 PL_regcomp_rx->data->data[n] = (void*)rv;
3519 ARG1_SET(ret, flags);
3530 char* retval = PL_regcomp_parse++;
3533 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
3534 PL_regcomp_parse[2] == '#') {
3535 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
3540 if (PL_regflags & PMf_EXTENDED) {
3541 if (isSPACE(*PL_regcomp_parse)) {
3545 else if (*PL_regcomp_parse == '#') {
3546 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
3557 - reg_node - emit a node
3559 STATIC regnode * /* Location. */
3560 S_reg_node(pTHX_ U8 op)
3563 register regnode *ret;
3564 register regnode *ptr;
3568 SIZE_ALIGN(PL_regsize);
3573 NODE_ALIGN_FILL(ret);
3575 FILL_ADVANCE_NODE(ptr, op);
3582 - reganode - emit a node with an argument
3584 STATIC regnode * /* Location. */
3585 S_reganode(pTHX_ U8 op, U32 arg)
3588 register regnode *ret;
3589 register regnode *ptr;
3593 SIZE_ALIGN(PL_regsize);
3598 NODE_ALIGN_FILL(ret);
3600 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
3607 - reguni - emit (if appropriate) a Unicode character
3610 S_reguni(pTHX_ UV uv, char* s, I32* lenp)
3615 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
3618 *lenp = uv_to_utf8((U8*)s, uv) - (U8*)s;
3623 - reginsert - insert an operator in front of already-emitted operand
3625 * Means relocating the operand.
3628 S_reginsert(pTHX_ U8 op, regnode *opnd)
3631 register regnode *src;
3632 register regnode *dst;
3633 register regnode *place;
3634 register int offset = regarglen[(U8)op];
3636 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
3639 PL_regsize += NODE_STEP_REGNODE + offset;
3644 PL_regcode += NODE_STEP_REGNODE + offset;
3647 StructCopy(--src, --dst, regnode);
3649 place = opnd; /* Op node, where operand used to be. */
3650 src = NEXTOPER(place);
3651 FILL_ADVANCE_NODE(place, op);
3652 Zero(src, offset, regnode);
3656 - regtail - set the next-pointer at the end of a node chain of p to val.
3659 S_regtail(pTHX_ regnode *p, regnode *val)
3662 register regnode *scan;
3663 register regnode *temp;
3664 register I32 offset;
3669 /* Find last node. */
3672 temp = regnext(scan);
3678 if (reg_off_by_arg[OP(scan)]) {
3679 ARG_SET(scan, val - scan);
3682 NEXT_OFF(scan) = val - scan;
3687 - regoptail - regtail on operand of first argument; nop if operandless
3690 S_regoptail(pTHX_ regnode *p, regnode *val)
3693 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
3694 if (p == NULL || SIZE_ONLY)
3696 if (PL_regkind[(U8)OP(p)] == BRANCH) {
3697 regtail(NEXTOPER(p), val);
3699 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
3700 regtail(NEXTOPER(NEXTOPER(p)), val);
3707 - regcurly - a little FSA that accepts {\d+,?\d*}
3710 S_regcurly(pTHX_ register char *s)
3729 S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
3732 register U8 op = EXACT; /* Arbitrary non-END op. */
3733 register regnode *next, *onode;
3735 while (op != END && (!last || node < last)) {
3736 /* While that wasn't END last time... */
3742 next = regnext(node);
3744 if (OP(node) == OPTIMIZED)
3747 PerlIO_printf(Perl_debug_log, "%4"IVdf":%*s%s", (IV)(node - start),
3748 (int)(2*l + 1), "", SvPVX(sv));
3749 if (next == NULL) /* Next ptr. */
3750 PerlIO_printf(Perl_debug_log, "(0)");
3752 PerlIO_printf(Perl_debug_log, "(%"IVdf")", (IV)(next - start));
3753 (void)PerlIO_putc(Perl_debug_log, '\n');
3755 if (PL_regkind[(U8)op] == BRANCHJ) {
3756 register regnode *nnode = (OP(next) == LONGJMP
3759 if (last && nnode > last)
3761 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
3763 else if (PL_regkind[(U8)op] == BRANCH) {
3764 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
3766 else if ( op == CURLY) { /* `next' might be very big: optimizer */
3767 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
3768 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
3770 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
3771 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
3774 else if ( op == PLUS || op == STAR) {
3775 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
3777 else if (op == ANYOF) {
3778 node = NEXTOPER(node);
3781 else if (PL_regkind[(U8)op] == EXACT) {
3782 /* Literal string, where present. */
3783 node += NODE_SZ_STR(node) - 1;
3784 node = NEXTOPER(node);
3787 node = NEXTOPER(node);
3788 node += regarglen[(U8)op];
3790 if (op == CURLYX || op == OPEN)
3792 else if (op == WHILEM)
3795 #endif /* DEBUGGING */
3800 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
3803 Perl_regdump(pTHX_ regexp *r)
3807 SV *sv = sv_newmortal();
3809 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
3811 /* Header fields of interest. */
3812 if (r->anchored_substr)
3813 PerlIO_printf(Perl_debug_log,
3814 "anchored `%s%.*s%s'%s at %"IVdf" ",
3816 (int)(SvCUR(r->anchored_substr) - (SvTAIL(r->anchored_substr)!=0)),
3817 SvPVX(r->anchored_substr),
3819 SvTAIL(r->anchored_substr) ? "$" : "",
3820 (IV)r->anchored_offset);
3821 if (r->float_substr)
3822 PerlIO_printf(Perl_debug_log,
3823 "floating `%s%.*s%s'%s at %"IVdf"..%"UVuf" ",
3825 (int)(SvCUR(r->float_substr) - (SvTAIL(r->float_substr)!=0)),
3826 SvPVX(r->float_substr),
3828 SvTAIL(r->float_substr) ? "$" : "",
3829 (IV)r->float_min_offset, (UV)r->float_max_offset);
3830 if (r->check_substr)
3831 PerlIO_printf(Perl_debug_log,
3832 r->check_substr == r->float_substr
3833 ? "(checking floating" : "(checking anchored");
3834 if (r->reganch & ROPT_NOSCAN)
3835 PerlIO_printf(Perl_debug_log, " noscan");
3836 if (r->reganch & ROPT_CHECK_ALL)
3837 PerlIO_printf(Perl_debug_log, " isall");
3838 if (r->check_substr)
3839 PerlIO_printf(Perl_debug_log, ") ");
3841 if (r->regstclass) {
3842 regprop(sv, r->regstclass);
3843 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
3845 if (r->reganch & ROPT_ANCH) {
3846 PerlIO_printf(Perl_debug_log, "anchored");
3847 if (r->reganch & ROPT_ANCH_BOL)
3848 PerlIO_printf(Perl_debug_log, "(BOL)");
3849 if (r->reganch & ROPT_ANCH_MBOL)
3850 PerlIO_printf(Perl_debug_log, "(MBOL)");
3851 if (r->reganch & ROPT_ANCH_SBOL)
3852 PerlIO_printf(Perl_debug_log, "(SBOL)");
3853 if (r->reganch & ROPT_ANCH_GPOS)
3854 PerlIO_printf(Perl_debug_log, "(GPOS)");
3855 PerlIO_putc(Perl_debug_log, ' ');
3857 if (r->reganch & ROPT_GPOS_SEEN)
3858 PerlIO_printf(Perl_debug_log, "GPOS ");
3859 if (r->reganch & ROPT_SKIP)
3860 PerlIO_printf(Perl_debug_log, "plus ");
3861 if (r->reganch & ROPT_IMPLICIT)
3862 PerlIO_printf(Perl_debug_log, "implicit ");
3863 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
3864 if (r->reganch & ROPT_EVAL_SEEN)
3865 PerlIO_printf(Perl_debug_log, "with eval ");
3866 PerlIO_printf(Perl_debug_log, "\n");
3867 #endif /* DEBUGGING */
3871 S_put_byte(pTHX_ SV *sv, int c)
3873 if (c <= ' ' || c == 127 || c == 255)
3874 Perl_sv_catpvf(aTHX_ sv, "\\%o", c);
3875 else if (c == '-' || c == ']' || c == '\\' || c == '^')
3876 Perl_sv_catpvf(aTHX_ sv, "\\%c", c);
3878 Perl_sv_catpvf(aTHX_ sv, "%c", c);
3882 - regprop - printable representation of opcode
3885 Perl_regprop(pTHX_ SV *sv, regnode *o)
3891 sv_setpvn(sv, "", 0);
3892 if (OP(o) >= reg_num) /* regnode.type is unsigned */
3893 FAIL("corrupted regexp opcode");
3894 sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
3896 k = PL_regkind[(U8)OP(o)];
3899 Perl_sv_catpvf(aTHX_ sv, " <%s%.*s%s>", PL_colors[0],
3900 STR_LEN(o), STRING(o), PL_colors[1]);
3901 else if (k == CURLY) {
3902 if (OP(o) == CURLYM || OP(o) == CURLYN)
3903 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
3904 Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
3906 else if (k == WHILEM && o->flags) /* Ordinal/of */
3907 Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
3908 else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
3909 Perl_sv_catpvf(aTHX_ sv, "%d", ARG(o)); /* Parenth number */
3910 else if (k == LOGICAL)
3911 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* 2: embedded, otherwise 1 */
3912 else if (k == ANYOF) {
3913 int i, rangestart = -1;
3914 const char * const out[] = { /* Should be syncronized with
3915 a table in regcomp.h */
3944 if (o->flags & ANYOF_LOCALE)
3945 sv_catpv(sv, "{loc}");
3946 if (o->flags & ANYOF_FOLD)
3947 sv_catpv(sv, "{i}");
3948 Perl_sv_catpvf(aTHX_ sv, "[%s", PL_colors[0]);
3949 if (o->flags & ANYOF_INVERT)
3951 for (i = 0; i <= 256; i++) {
3952 if (i < 256 && ANYOF_BITMAP_TEST(o,i)) {
3953 if (rangestart == -1)
3955 } else if (rangestart != -1) {
3956 if (i <= rangestart + 3)
3957 for (; rangestart < i; rangestart++)
3958 put_byte(sv, rangestart);
3960 put_byte(sv, rangestart);
3962 put_byte(sv, i - 1);
3967 if (o->flags & ANYOF_CLASS)
3968 for (i = 0; i < sizeof(out)/sizeof(char*); i++)
3969 if (ANYOF_CLASS_TEST(o,i))
3970 sv_catpv(sv, out[i]);
3971 Perl_sv_catpvf(aTHX_ sv, "%s]", PL_colors[1]);
3973 else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
3974 Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
3975 #endif /* DEBUGGING */
3979 Perl_re_intuit_string(pTHX_ regexp *prog)
3980 { /* Assume that RE_INTUIT is set */
3983 char *s = SvPV(prog->check_substr,n_a);
3985 if (!PL_colorset) reginitcolors();
3986 PerlIO_printf(Perl_debug_log,
3987 "%sUsing REx substr:%s `%s%.60s%s%s'\n",
3988 PL_colors[4],PL_colors[5],PL_colors[0],
3991 (strlen(s) > 60 ? "..." : ""));
3994 return prog->check_substr;
3998 Perl_pregfree(pTHX_ struct regexp *r)
4001 DEBUG_r(if (!PL_colorset) reginitcolors());
4003 if (!r || (--r->refcnt > 0))
4005 DEBUG_r(PerlIO_printf(Perl_debug_log,
4006 "%sFreeing REx:%s `%s%.60s%s%s'\n",
4007 PL_colors[4],PL_colors[5],PL_colors[0],
4010 (strlen(r->precomp) > 60 ? "..." : "")));
4013 Safefree(r->precomp);
4014 if (RX_MATCH_COPIED(r))
4015 Safefree(r->subbeg);
4017 if (r->anchored_substr)
4018 SvREFCNT_dec(r->anchored_substr);
4019 if (r->float_substr)
4020 SvREFCNT_dec(r->float_substr);
4021 Safefree(r->substrs);
4024 int n = r->data->count;
4025 AV* new_comppad = NULL;
4030 switch (r->data->what[n]) {
4032 SvREFCNT_dec((SV*)r->data->data[n]);
4035 Safefree(r->data->data[n]);
4038 new_comppad = (AV*)r->data->data[n];
4041 if (new_comppad == NULL)
4042 Perl_croak(aTHX_ "panic: pregfree comppad");
4043 old_comppad = PL_comppad;
4044 old_curpad = PL_curpad;
4045 PL_comppad = new_comppad;
4046 PL_curpad = AvARRAY(new_comppad);
4047 op_free((OP_4tree*)r->data->data[n]);
4048 PL_comppad = old_comppad;
4049 PL_curpad = old_curpad;
4050 SvREFCNT_dec((SV*)new_comppad);
4056 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
4059 Safefree(r->data->what);
4062 Safefree(r->startp);
4068 - regnext - dig the "next" pointer out of a node
4070 * [Note, when REGALIGN is defined there are two places in regmatch()
4071 * that bypass this code for speed.]
4074 Perl_regnext(pTHX_ register regnode *p)
4077 register I32 offset;
4079 if (p == &PL_regdummy)
4082 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
4090 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
4093 STRLEN l1 = strlen(pat1);
4094 STRLEN l2 = strlen(pat2);
4103 Copy(pat1, buf, l1 , char);
4104 Copy(pat2, buf + l1, l2 , char);
4105 buf[l1 + l2] = '\n';
4106 buf[l1 + l2 + 1] = '\0';
4108 /* ANSI variant takes additional second argument */
4109 va_start(args, pat2);
4113 msv = vmess(buf, &args);
4115 message = SvPV(msv,l1);
4118 Copy(message, buf, l1 , char);
4119 buf[l1] = '\0'; /* Overwrite \n */
4120 Perl_croak(aTHX_ "%s", buf);
4123 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
4126 Perl_save_re_context(pTHX)
4130 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
4131 SAVEI32(PL_regnpar); /* () count. */
4132 SAVEI32(PL_regsize); /* Code size. */
4133 SAVEI16(PL_regflags); /* are we folding, multilining? */
4134 SAVEPPTR(PL_reginput); /* String-input pointer. */
4135 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
4136 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
4137 SAVEVPTR(PL_regstartp); /* Pointer to startp array. */
4138 SAVEVPTR(PL_regendp); /* Ditto for endp. */
4139 SAVEVPTR(PL_reglastparen); /* Similarly for lastparen. */
4140 SAVEPPTR(PL_regtill); /* How far we are required to go. */
4141 SAVEI8(PL_regprev); /* char before regbol, \n if none */
4142 SAVEVPTR(PL_reg_start_tmp); /* from regexec.c */
4143 PL_reg_start_tmp = 0;
4144 SAVEFREEPV(PL_reg_start_tmp);
4145 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
4146 PL_reg_start_tmpl = 0;
4147 SAVEVPTR(PL_regdata);
4148 SAVEI32(PL_reg_flags); /* from regexec.c */
4149 SAVEI32(PL_reg_eval_set); /* from regexec.c */
4150 SAVEI32(PL_regnarrate); /* from regexec.c */
4151 SAVEVPTR(PL_regprogram); /* from regexec.c */
4152 SAVEINT(PL_regindent); /* from regexec.c */
4153 SAVEVPTR(PL_regcc); /* from regexec.c */
4154 SAVEVPTR(PL_curcop);
4155 SAVEVPTR(PL_regcomp_rx); /* from regcomp.c */
4156 SAVEI32(PL_regseen); /* from regcomp.c */
4157 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
4158 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
4159 SAVEVPTR(PL_regcode); /* Code-emit pointer; ®dummy = don't */
4160 SAVEPPTR(PL_regxend); /* End of input for compile */
4161 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */
4162 SAVEVPTR(PL_reg_call_cc); /* from regexec.c */
4163 SAVEVPTR(PL_reg_re); /* from regexec.c */
4164 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
4165 SAVESPTR(PL_reg_sv); /* from regexec.c */
4166 SAVEVPTR(PL_reg_magic); /* from regexec.c */
4167 SAVEI32(PL_reg_oldpos); /* from regexec.c */
4168 SAVEVPTR(PL_reg_oldcurpm); /* from regexec.c */
4169 SAVEVPTR(PL_reg_curpm); /* from regexec.c */
4171 SAVEPPTR(PL_reg_starttry); /* from regexec.c */
4182 clear_re(pTHXo_ void *r)
4184 ReREFCNT_dec((regexp *)r);