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;
157 * Forward declarations for pregcomp()'s friends.
160 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
163 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
164 #define SF_BEFORE_SEOL 0x1
165 #define SF_BEFORE_MEOL 0x2
166 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
167 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
170 # define SF_FIX_SHIFT_EOL (0+2)
171 # define SF_FL_SHIFT_EOL (0+4)
173 # define SF_FIX_SHIFT_EOL (+2)
174 # define SF_FL_SHIFT_EOL (+4)
177 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
178 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
180 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
181 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
182 #define SF_IS_INF 0x40
183 #define SF_HAS_PAR 0x80
184 #define SF_IN_PAR 0x100
185 #define SF_HAS_EVAL 0x200
186 #define SCF_DO_SUBSTR 0x400
189 #define UTF (PL_reg_flags & RF_utf8)
190 #define LOC (PL_regflags & PMf_LOCALE)
191 #define FOLD (PL_regflags & PMf_FOLD)
193 #define OOB_CHAR8 1234
194 #define OOB_UTF8 123456
196 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
197 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
199 /* Allow for side effects in s */
200 #define REGC(c,s) STMT_START { if (!SIZE_ONLY) *(s) = (c); else (s);} STMT_END
202 static void clear_re(pTHXo_ void *r);
205 S_scan_commit(pTHX_ scan_data_t *data)
208 STRLEN l = CHR_SVLEN(data->last_found);
209 STRLEN old_l = CHR_SVLEN(*data->longest);
211 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
212 sv_setsv(*data->longest, data->last_found);
213 if (*data->longest == data->longest_fixed) {
214 data->offset_fixed = l ? data->last_start_min : data->pos_min;
215 if (data->flags & SF_BEFORE_EOL)
217 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
219 data->flags &= ~SF_FIX_BEFORE_EOL;
222 data->offset_float_min = l ? data->last_start_min : data->pos_min;
223 data->offset_float_max = (l
224 ? data->last_start_max
225 : data->pos_min + data->pos_delta);
226 if (data->flags & SF_BEFORE_EOL)
228 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
230 data->flags &= ~SF_FL_BEFORE_EOL;
233 SvCUR_set(data->last_found, 0);
235 data->flags &= ~SF_BEFORE_EOL;
238 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
239 to the position after last scanned or to NULL. */
242 S_study_chunk(pTHX_ regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
243 /* scanp: Start here (read-write). */
244 /* deltap: Write maxlen-minlen here. */
245 /* last: Stop before this one. */
248 I32 min = 0, pars = 0, code;
249 regnode *scan = *scanp, *next;
251 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
252 int is_inf_internal = 0; /* The studied chunk is infinite */
253 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
254 scan_data_t data_fake;
256 while (scan && OP(scan) != END && scan < last) {
257 /* Peephole optimizer: */
259 if (PL_regkind[(U8)OP(scan)] == EXACT) {
260 regnode *n = regnext(scan);
263 regnode *stop = scan;
266 next = scan + NODE_SZ_STR(scan);
267 /* Skip NOTHING, merge EXACT*. */
269 ( PL_regkind[(U8)OP(n)] == NOTHING ||
270 (stringok && (OP(n) == OP(scan))))
272 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
273 if (OP(n) == TAIL || n > next)
275 if (PL_regkind[(U8)OP(n)] == NOTHING) {
276 NEXT_OFF(scan) += NEXT_OFF(n);
277 next = n + NODE_STEP_REGNODE;
285 int oldl = STR_LEN(scan);
286 regnode *nnext = regnext(n);
288 if (oldl + STR_LEN(n) > U8_MAX)
290 NEXT_OFF(scan) += NEXT_OFF(n);
291 STR_LEN(scan) += STR_LEN(n);
292 next = n + NODE_SZ_STR(n);
293 /* Now we can overwrite *n : */
294 Move(STRING(n), STRING(scan) + oldl,
305 n = scan + NODE_SZ_STR(scan);
307 /* Purify reports a benign UMR here sometimes, because we
308 * don't initialize the OP() slot of a node when that node
309 * is occupied by just the trailing null of the string in
311 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
320 if (OP(scan) != CURLYX) {
321 int max = (reg_off_by_arg[OP(scan)]
323 /* I32 may be smaller than U16 on CRAYs! */
324 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
325 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
329 /* Skip NOTHING and LONGJMP. */
330 while ((n = regnext(n))
331 && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
332 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
335 if (reg_off_by_arg[OP(scan)])
338 NEXT_OFF(scan) = off;
340 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
341 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
342 next = regnext(scan);
345 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
346 I32 max1 = 0, min1 = I32_MAX, num = 0;
348 if (flags & SCF_DO_SUBSTR)
350 while (OP(scan) == code) {
351 I32 deltanext, minnext;
356 data_fake.whilem_c = data->whilem_c;
357 next = regnext(scan);
358 scan = NEXTOPER(scan);
360 scan = NEXTOPER(scan);
361 /* We suppose the run is continuous, last=next...*/
362 minnext = study_chunk(&scan, &deltanext, next,
366 if (max1 < minnext + deltanext)
367 max1 = minnext + deltanext;
368 if (deltanext == I32_MAX)
369 is_inf = is_inf_internal = 1;
371 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
373 if (data && (data_fake.flags & SF_HAS_EVAL))
374 data->flags |= SF_HAS_EVAL;
376 data->whilem_c = data_fake.whilem_c;
380 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
382 if (flags & SCF_DO_SUBSTR) {
383 data->pos_min += min1;
384 data->pos_delta += max1 - min1;
385 if (max1 != min1 || is_inf)
386 data->longest = &(data->longest_float);
389 delta += max1 - min1;
391 else if (code == BRANCHJ) /* single branch is optimized. */
392 scan = NEXTOPER(NEXTOPER(scan));
393 else /* single branch is optimized. */
394 scan = NEXTOPER(scan);
397 else if (OP(scan) == EXACT) {
398 I32 l = STR_LEN(scan);
400 unsigned char *s = (unsigned char *)STRING(scan);
401 unsigned char *e = s + l;
410 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
411 /* The code below prefers earlier match for fixed
412 offset, later match for variable offset. */
413 if (data->last_end == -1) { /* Update the start info. */
414 data->last_start_min = data->pos_min;
415 data->last_start_max = is_inf
416 ? I32_MAX : data->pos_min + data->pos_delta;
418 sv_catpvn(data->last_found, STRING(scan), STR_LEN(scan));
419 data->last_end = data->pos_min + l;
420 data->pos_min += l; /* As in the first entry. */
421 data->flags &= ~SF_BEFORE_EOL;
424 else if (PL_regkind[(U8)OP(scan)] == EXACT) {
425 I32 l = STR_LEN(scan);
426 if (flags & SCF_DO_SUBSTR)
429 unsigned char *s = (unsigned char *)STRING(scan);
430 unsigned char *e = s + l;
439 if (data && (flags & SCF_DO_SUBSTR))
442 else if (strchr((char*)PL_varies,OP(scan))) {
443 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
444 regnode *oscan = scan;
446 switch (PL_regkind[(U8)OP(scan)]) {
448 scan = NEXTOPER(scan);
451 if (flags & SCF_DO_SUBSTR) {
452 next = NEXTOPER(scan);
453 if (OP(next) == EXACT) {
455 maxcount = REG_INFTY;
456 next = regnext(scan);
457 scan = NEXTOPER(scan);
461 if (flags & SCF_DO_SUBSTR)
466 is_inf = is_inf_internal = 1;
467 scan = regnext(scan);
468 if (flags & SCF_DO_SUBSTR) {
470 data->longest = &(data->longest_float);
472 goto optimize_curly_tail;
474 mincount = ARG1(scan);
475 maxcount = ARG2(scan);
476 next = regnext(scan);
477 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
479 if (flags & SCF_DO_SUBSTR) {
480 if (mincount == 0) scan_commit(data);
481 pos_before = data->pos_min;
485 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
487 data->flags |= SF_IS_INF;
489 /* This will finish on WHILEM, setting scan, or on NULL: */
490 minnext = study_chunk(&scan, &deltanext, last, data,
492 ? (flags & ~SCF_DO_SUBSTR) : flags);
493 if (!scan) /* It was not CURLYX, but CURLY. */
495 if (ckWARN(WARN_UNSAFE) && (minnext + deltanext == 0)
496 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
497 && maxcount <= REG_INFTY/3) /* Complement check for big count */
498 Perl_warner(aTHX_ WARN_UNSAFE,
499 "Strange *+?{} on zero-length expression");
500 min += minnext * mincount;
501 is_inf_internal |= (maxcount == REG_INFTY
502 && (minnext + deltanext) > 0
503 || deltanext == I32_MAX);
504 is_inf |= is_inf_internal;
505 delta += (minnext + deltanext) * maxcount - minnext * mincount;
507 /* Try powerful optimization CURLYX => CURLYN. */
508 if ( OP(oscan) == CURLYX && data
509 && data->flags & SF_IN_PAR
510 && !(data->flags & SF_HAS_EVAL)
511 && !deltanext && minnext == 1 ) {
512 /* Try to optimize to CURLYN. */
513 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
514 regnode *nxt1 = nxt, *nxt2;
518 if (!strchr((char*)PL_simple,OP(nxt))
519 && !(PL_regkind[(U8)OP(nxt)] == EXACT
520 && STR_LEN(nxt) == 1))
524 if (OP(nxt) != CLOSE)
526 /* Now we know that nxt2 is the only contents: */
527 oscan->flags = ARG(nxt);
529 OP(nxt1) = NOTHING; /* was OPEN. */
531 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
532 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
533 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
534 OP(nxt) = OPTIMIZED; /* was CLOSE. */
535 OP(nxt + 1) = OPTIMIZED; /* was count. */
536 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
541 /* Try optimization CURLYX => CURLYM. */
542 if ( OP(oscan) == CURLYX && data
543 && !(data->flags & SF_HAS_PAR)
544 && !(data->flags & SF_HAS_EVAL)
546 /* XXXX How to optimize if data == 0? */
547 /* Optimize to a simpler form. */
548 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
552 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
553 && (OP(nxt2) != WHILEM))
555 OP(nxt2) = SUCCEED; /* Whas WHILEM */
556 /* Need to optimize away parenths. */
557 if (data->flags & SF_IN_PAR) {
558 /* Set the parenth number. */
559 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
561 if (OP(nxt) != CLOSE)
562 FAIL("panic opt close");
563 oscan->flags = ARG(nxt);
564 OP(nxt1) = OPTIMIZED; /* was OPEN. */
565 OP(nxt) = OPTIMIZED; /* was CLOSE. */
567 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
568 OP(nxt + 1) = OPTIMIZED; /* was count. */
569 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
570 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
573 while ( nxt1 && (OP(nxt1) != WHILEM)) {
574 regnode *nnxt = regnext(nxt1);
577 if (reg_off_by_arg[OP(nxt1)])
578 ARG_SET(nxt1, nxt2 - nxt1);
579 else if (nxt2 - nxt1 < U16_MAX)
580 NEXT_OFF(nxt1) = nxt2 - nxt1;
582 OP(nxt) = NOTHING; /* Cannot beautify */
587 /* Optimize again: */
588 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
593 else if (OP(oscan) == CURLYX && data && ++data->whilem_c < 16) {
594 /* This stays as CURLYX, and can put the count/of pair. */
595 /* Find WHILEM (as in regexec.c) */
596 regnode *nxt = oscan + NEXT_OFF(oscan);
598 if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
600 PREVOPER(nxt)->flags = data->whilem_c
601 | (PL_reg_whilem_seen << 4); /* On WHILEM */
603 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
605 if (flags & SCF_DO_SUBSTR) {
606 SV *last_str = Nullsv;
607 int counted = mincount != 0;
609 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
610 I32 b = pos_before >= data->last_start_min
611 ? pos_before : data->last_start_min;
613 char *s = SvPV(data->last_found, l);
614 I32 old = b - data->last_start_min;
617 old = utf8_hop((U8*)s, old) - (U8*)s;
620 /* Get the added string: */
621 last_str = newSVpvn(s + old, l);
622 if (deltanext == 0 && pos_before == b) {
623 /* What was added is a constant string */
625 SvGROW(last_str, (mincount * l) + 1);
626 repeatcpy(SvPVX(last_str) + l,
627 SvPVX(last_str), l, mincount - 1);
628 SvCUR(last_str) *= mincount;
629 /* Add additional parts. */
630 SvCUR_set(data->last_found,
631 SvCUR(data->last_found) - l);
632 sv_catsv(data->last_found, last_str);
633 data->last_end += l * (mincount - 1);
637 /* It is counted once already... */
638 data->pos_min += minnext * (mincount - counted);
639 data->pos_delta += - counted * deltanext +
640 (minnext + deltanext) * maxcount - minnext * mincount;
641 if (mincount != maxcount) {
643 if (mincount && last_str) {
644 sv_setsv(data->last_found, last_str);
645 data->last_end = data->pos_min;
646 data->last_start_min =
647 data->pos_min - CHR_SVLEN(last_str);
648 data->last_start_max = is_inf
650 : data->pos_min + data->pos_delta
651 - CHR_SVLEN(last_str);
653 data->longest = &(data->longest_float);
655 SvREFCNT_dec(last_str);
657 if (data && (fl & SF_HAS_EVAL))
658 data->flags |= SF_HAS_EVAL;
660 if (OP(oscan) != CURLYX) {
661 while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
663 NEXT_OFF(oscan) += NEXT_OFF(next);
666 default: /* REF only? */
667 if (flags & SCF_DO_SUBSTR) {
669 data->longest = &(data->longest_float);
671 is_inf = is_inf_internal = 1;
675 else if (strchr((char*)PL_simple,OP(scan)) || PL_regkind[(U8)OP(scan)] == ANYUTF8) {
676 if (flags & SCF_DO_SUBSTR) {
682 else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
683 data->flags |= (OP(scan) == MEOL
687 else if (PL_regkind[(U8)OP(scan)] == BRANCHJ
688 && (scan->flags || data)
689 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
690 I32 deltanext, minnext;
695 data_fake.whilem_c = data->whilem_c;
696 next = regnext(scan);
697 nscan = NEXTOPER(NEXTOPER(scan));
698 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
701 FAIL("variable length lookbehind not implemented");
703 else if (minnext > U8_MAX) {
705 FAIL2("lookbehind longer than %" PERL_PRIu64 " not implemented", (UV)U8_MAX);
707 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
710 scan->flags = minnext;
712 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
714 if (data && (data_fake.flags & SF_HAS_EVAL))
715 data->flags |= SF_HAS_EVAL;
717 data->whilem_c = data_fake.whilem_c;
719 else if (OP(scan) == OPEN) {
722 else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
723 next = regnext(scan);
725 if ( next && (OP(next) != WHILEM) && next < last)
726 is_par = 0; /* Disable optimization */
728 else if (OP(scan) == EVAL) {
730 data->flags |= SF_HAS_EVAL;
732 else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded */
733 if (flags & SCF_DO_SUBSTR) {
735 data->longest = &(data->longest_float);
737 is_inf = is_inf_internal = 1;
739 /* Else: zero-length, ignore. */
740 scan = regnext(scan);
745 *deltap = is_inf_internal ? I32_MAX : delta;
746 if (flags & SCF_DO_SUBSTR && is_inf)
747 data->pos_delta = I32_MAX - data->pos_min;
750 if (is_par && pars==1 && data) {
751 data->flags |= SF_IN_PAR;
752 data->flags &= ~SF_HAS_PAR;
754 else if (pars && data) {
755 data->flags |= SF_HAS_PAR;
756 data->flags &= ~SF_IN_PAR;
762 S_add_data(pTHX_ I32 n, char *s)
765 if (PL_regcomp_rx->data) {
766 Renewc(PL_regcomp_rx->data,
767 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
768 char, struct reg_data);
769 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
770 PL_regcomp_rx->data->count += n;
773 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
774 char, struct reg_data);
775 New(1208, PL_regcomp_rx->data->what, n, U8);
776 PL_regcomp_rx->data->count = n;
778 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
779 return PL_regcomp_rx->data->count - n;
783 Perl_reginitcolors(pTHX)
787 char *s = PerlEnv_getenv("PERL_RE_COLORS");
790 PL_colors[0] = s = savepv(s);
798 PL_colors[i] = s = "";
808 - pregcomp - compile a regular expression into internal code
810 * We can't allocate space until we know how big the compiled form will be,
811 * but we can't compile it (and thus know how big it is) until we've got a
812 * place to put the code. So we cheat: we compile it twice, once with code
813 * generation turned off and size counting turned on, and once "for real".
814 * This also means that we don't allocate space until we are sure that the
815 * thing really will compile successfully, and we never have to move the
816 * code and thus invalidate pointers into it. (Note that it has to be in
817 * one piece because free() must be able to free it all.) [NB: not true in perl]
819 * Beware that the optimization-preparation code in here knows about some
820 * of the structure of the compiled regexp. [I'll say.]
823 Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
839 FAIL("NULL regexp argument");
841 if (PL_curcop == &PL_compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
842 PL_reg_flags |= RF_utf8;
846 PL_regprecomp = savepvn(exp, xend - exp);
847 DEBUG_r(if (!PL_colorset) reginitcolors());
848 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling REx%s `%s%*s%s'\n",
849 PL_colors[4],PL_colors[5],PL_colors[0],
850 xend - exp, PL_regprecomp, PL_colors[1]));
851 PL_regflags = pm->op_pmflags;
855 PL_seen_zerolen = *exp == '^' ? -1 : 0;
859 /* First pass: determine size, legality. */
860 PL_regcomp_parse = exp;
865 PL_regcode = &PL_regdummy;
866 PL_reg_whilem_seen = 0;
867 REGC((U8)REG_MAGIC, (char*)PL_regcode);
868 if (reg(0, &flags) == NULL) {
869 Safefree(PL_regprecomp);
870 PL_regprecomp = Nullch;
873 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
875 /* Small enough for pointer-storage convention?
876 If extralen==0, this means that we will not need long jumps. */
877 if (PL_regsize >= 0x10000L && PL_extralen)
878 PL_regsize += PL_extralen;
881 if (PL_reg_whilem_seen > 15)
882 PL_reg_whilem_seen = 15;
884 /* Allocate space and initialize. */
885 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
888 FAIL("regexp out of space");
890 r->prelen = xend - exp;
891 r->precomp = PL_regprecomp;
893 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
894 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
896 r->substrs = 0; /* Useful during FAIL. */
897 r->startp = 0; /* Useful during FAIL. */
898 r->endp = 0; /* Useful during FAIL. */
902 /* Second pass: emit code. */
903 PL_regcomp_parse = exp;
907 PL_regcode = r->program;
908 /* Store the count of eval-groups for security checks: */
909 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
910 REGC((U8)REG_MAGIC, (char*) PL_regcode++);
912 if (reg(0, &flags) == NULL)
915 /* Dig out information for optimizations. */
916 r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
917 pm->op_pmflags = PL_regflags;
919 r->reganch |= ROPT_UTF8;
920 r->regstclass = NULL;
921 if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
922 r->reganch |= ROPT_NAUGHTY;
923 scan = r->program + 1; /* First BRANCH. */
925 /* XXXX To minimize changes to RE engine we always allocate
926 3-units-long substrs field. */
927 Newz(1004, r->substrs, 1, struct reg_substr_data);
929 StructCopy(&zero_scan_data, &data, scan_data_t);
930 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
932 STRLEN longest_float_length, longest_fixed_length;
935 /* Skip introductions and multiplicators >= 1. */
936 while ((OP(first) == OPEN && (sawopen = 1)) ||
937 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
938 (OP(first) == PLUS) ||
939 (OP(first) == MINMOD) ||
940 (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
941 if (OP(first) == PLUS)
944 first += regarglen[(U8)OP(first)];
945 first = NEXTOPER(first);
948 /* Starting-point info. */
950 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
951 else if (strchr((char*)PL_simple+4,OP(first)))
952 r->regstclass = first;
953 else if (PL_regkind[(U8)OP(first)] == BOUND ||
954 PL_regkind[(U8)OP(first)] == NBOUND)
955 r->regstclass = first;
956 else if (PL_regkind[(U8)OP(first)] == BOL) {
957 r->reganch |= (OP(first) == MBOL
962 first = NEXTOPER(first);
965 else if (OP(first) == GPOS) {
966 r->reganch |= ROPT_ANCH_GPOS;
967 first = NEXTOPER(first);
970 else if ((OP(first) == STAR &&
971 PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
972 !(r->reganch & ROPT_ANCH) )
974 /* turn .* into ^.* with an implied $*=1 */
975 int type = OP(NEXTOPER(first));
977 if (type == REG_ANY || type == ANYUTF8)
978 type = ROPT_ANCH_MBOL;
980 type = ROPT_ANCH_SBOL;
982 r->reganch |= type | ROPT_IMPLICIT;
983 first = NEXTOPER(first);
986 if (sawplus && (!sawopen || !PL_regsawback)
987 && !(PL_regseen & REG_SEEN_EVAL)) /* May examine pos and $& */
988 /* x+ must match at the 1st pos of run of x's */
989 r->reganch |= ROPT_SKIP;
991 /* Scan is after the zeroth branch, first is atomic matcher. */
992 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
995 * If there's something expensive in the r.e., find the
996 * longest literal string that must appear and make it the
997 * regmust. Resolve ties in favor of later strings, since
998 * the regstart check works with the beginning of the r.e.
999 * and avoiding duplication strengthens checking. Not a
1000 * strong reason, but sufficient in the absence of others.
1001 * [Now we resolve ties in favor of the earlier string if
1002 * it happens that c_offset_min has been invalidated, since the
1003 * earlier string may buy us something the later one won't.]
1007 data.longest_fixed = newSVpvn("",0);
1008 data.longest_float = newSVpvn("",0);
1009 data.last_found = newSVpvn("",0);
1010 data.longest = &(data.longest_fixed);
1013 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
1014 &data, SCF_DO_SUBSTR);
1015 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
1016 && data.last_start_min == 0 && data.last_end > 0
1018 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
1019 r->reganch |= ROPT_CHECK_ALL;
1021 SvREFCNT_dec(data.last_found);
1023 longest_float_length = CHR_SVLEN(data.longest_float);
1024 if (longest_float_length
1025 || (data.flags & SF_FL_BEFORE_EOL
1026 && (!(data.flags & SF_FL_BEFORE_MEOL)
1027 || (PL_regflags & PMf_MULTILINE)))) {
1030 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
1031 && data.offset_fixed == data.offset_float_min
1032 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
1033 goto remove_float; /* As in (a)+. */
1035 r->float_substr = data.longest_float;
1036 r->float_min_offset = data.offset_float_min;
1037 r->float_max_offset = data.offset_float_max;
1038 t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
1039 && (!(data.flags & SF_FL_BEFORE_MEOL)
1040 || (PL_regflags & PMf_MULTILINE)));
1041 fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
1045 r->float_substr = Nullsv;
1046 SvREFCNT_dec(data.longest_float);
1047 longest_float_length = 0;
1050 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
1051 if (longest_fixed_length
1052 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1053 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1054 || (PL_regflags & PMf_MULTILINE)))) {
1057 r->anchored_substr = data.longest_fixed;
1058 r->anchored_offset = data.offset_fixed;
1059 t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
1060 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1061 || (PL_regflags & PMf_MULTILINE)));
1062 fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
1065 r->anchored_substr = Nullsv;
1066 SvREFCNT_dec(data.longest_fixed);
1067 longest_fixed_length = 0;
1070 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1071 if (longest_fixed_length > longest_float_length) {
1072 r->check_substr = r->anchored_substr;
1073 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1074 if (r->reganch & ROPT_ANCH_SINGLE)
1075 r->reganch |= ROPT_NOSCAN;
1078 r->check_substr = r->float_substr;
1079 r->check_offset_min = data.offset_float_min;
1080 r->check_offset_max = data.offset_float_max;
1082 if (r->check_substr) {
1083 r->reganch |= RE_USE_INTUIT;
1084 if (SvTAIL(r->check_substr))
1085 r->reganch |= RE_INTUIT_TAIL;
1089 /* Several toplevels. Best we can is to set minlen. */
1092 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1093 scan = r->program + 1;
1094 minlen = study_chunk(&scan, &fake, scan + PL_regsize, &data, 0);
1095 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1099 if (PL_regseen & REG_SEEN_GPOS)
1100 r->reganch |= ROPT_GPOS_SEEN;
1101 if (PL_regseen & REG_SEEN_LOOKBEHIND)
1102 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1103 if (PL_regseen & REG_SEEN_EVAL)
1104 r->reganch |= ROPT_EVAL_SEEN;
1105 Newz(1002, r->startp, PL_regnpar, I32);
1106 Newz(1002, r->endp, PL_regnpar, I32);
1107 DEBUG_r(regdump(r));
1112 - reg - regular expression, i.e. main body or parenthesized thing
1114 * Caller must absorb opening parenthesis.
1116 * Combining parenthesis handling with the base level of regular expression
1117 * is a trifle forced, but the need to tie the tails of the branches to what
1118 * follows makes it hard to avoid.
1121 S_reg(pTHX_ I32 paren, I32 *flagp)
1122 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1125 register regnode *ret; /* Will be the head of the group. */
1126 register regnode *br;
1127 register regnode *lastbr;
1128 register regnode *ender = 0;
1129 register I32 parno = 0;
1130 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
1133 *flagp = 0; /* Tentatively. */
1135 /* Make an OPEN node, if parenthesized. */
1137 if (*PL_regcomp_parse == '?') {
1138 U16 posflags = 0, negflags = 0;
1139 U16 *flagsp = &posflags;
1143 paren = *PL_regcomp_parse++;
1144 ret = NULL; /* For look-ahead/behind. */
1147 PL_regseen |= REG_SEEN_LOOKBEHIND;
1148 if (*PL_regcomp_parse == '!')
1150 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
1161 FAIL2("Sequence (?%c...) not implemented", (int)paren);
1164 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1166 if (*PL_regcomp_parse != ')')
1167 FAIL("Sequence (?#... not terminated");
1173 paren = *PL_regcomp_parse++;
1178 I32 count = 1, n = 0;
1180 char *s = PL_regcomp_parse;
1182 OP_4tree *sop, *rop;
1185 PL_regseen |= REG_SEEN_EVAL;
1186 while (count && (c = *PL_regcomp_parse)) {
1187 if (c == '\\' && PL_regcomp_parse[1])
1195 if (*PL_regcomp_parse != ')')
1196 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1200 if (PL_regcomp_parse - 1 - s)
1201 sv = newSVpvn(s, PL_regcomp_parse - 1 - s);
1203 sv = newSVpvn("", 0);
1205 rop = sv_compile_2op(sv, &sop, "re", &av);
1207 n = add_data(3, "nop");
1208 PL_regcomp_rx->data->data[n] = (void*)rop;
1209 PL_regcomp_rx->data->data[n+1] = (void*)sop;
1210 PL_regcomp_rx->data->data[n+2] = (void*)av;
1213 else { /* First pass */
1214 if (PL_reginterp_cnt < ++PL_seen_evals
1215 && PL_curcop != &PL_compiling)
1216 /* No compiled RE interpolated, has runtime
1217 components ===> unsafe. */
1218 FAIL("Eval-group not allowed at runtime, use re 'eval'");
1220 FAIL("Eval-group in insecure regular expression");
1225 ret = reg_node(LOGICAL);
1228 regtail(ret, reganode(EVAL, n));
1231 return reganode(EVAL, n);
1235 if (PL_regcomp_parse[0] == '?') {
1236 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1237 || PL_regcomp_parse[1] == '<'
1238 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
1241 ret = reg_node(LOGICAL);
1244 regtail(ret, reg(1, &flag));
1248 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
1249 parno = atoi(PL_regcomp_parse++);
1251 while (isDIGIT(*PL_regcomp_parse))
1253 ret = reganode(GROUPP, parno);
1254 if ((c = *nextchar()) != ')')
1255 FAIL2("Switch (?(number%c not recognized", c);
1257 regtail(ret, reganode(IFTHEN, 0));
1258 br = regbranch(&flags, 1);
1260 br = reganode(LONGJMP, 0);
1262 regtail(br, reganode(LONGJMP, 0));
1267 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1268 regbranch(&flags, 1);
1269 regtail(ret, lastbr);
1277 FAIL("Switch (?(condition)... contains too many branches");
1278 ender = reg_node(TAIL);
1281 regtail(lastbr, ender);
1282 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1285 regtail(ret, ender);
1289 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
1293 FAIL("Sequence (? incomplete");
1298 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1299 if (*PL_regcomp_parse != 'o')
1300 pmflag(flagsp, *PL_regcomp_parse);
1303 if (*PL_regcomp_parse == '-') {
1308 PL_regflags |= posflags;
1309 PL_regflags &= ~negflags;
1310 if (*PL_regcomp_parse == ':') {
1316 if (*PL_regcomp_parse != ')')
1317 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
1326 ret = reganode(OPEN, parno);
1333 /* Pick up the branches, linking them together. */
1334 br = regbranch(&flags, 1);
1337 if (*PL_regcomp_parse == '|') {
1338 if (!SIZE_ONLY && PL_extralen) {
1339 reginsert(BRANCHJ, br);
1342 reginsert(BRANCH, br);
1345 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
1347 else if (paren == ':') {
1348 *flagp |= flags&SIMPLE;
1350 if (open) { /* Starts with OPEN. */
1351 regtail(ret, br); /* OPEN -> first. */
1353 else if (paren != '?') /* Not Conditional */
1357 *flagp |= flags&SPSTART;
1359 while (*PL_regcomp_parse == '|') {
1360 if (!SIZE_ONLY && PL_extralen) {
1361 ender = reganode(LONGJMP,0);
1362 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1365 PL_extralen += 2; /* Account for LONGJMP. */
1367 br = regbranch(&flags, 0);
1370 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1374 *flagp |= flags&SPSTART;
1377 if (have_branch || paren != ':') {
1378 /* Make a closing node, and hook it on the end. */
1381 ender = reg_node(TAIL);
1384 ender = reganode(CLOSE, parno);
1390 *flagp &= ~HASWIDTH;
1393 ender = reg_node(SUCCEED);
1396 ender = reg_node(END);
1399 regtail(lastbr, ender);
1402 /* Hook the tails of the branches to the closing node. */
1403 for (br = ret; br != NULL; br = regnext(br)) {
1404 regoptail(br, ender);
1411 static char parens[] = "=!<,>";
1413 if (paren && (p = strchr(parens, paren))) {
1414 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1415 int flag = (p - parens) > 1;
1418 node = SUSPEND, flag = 0;
1419 reginsert(node,ret);
1421 regtail(ret, reg_node(TAIL));
1425 /* Check for proper termination. */
1427 PL_regflags = oregflags;
1428 if (PL_regcomp_parse >= PL_regxend || *nextchar() != ')') {
1429 FAIL("unmatched () in regexp");
1432 else if (!paren && PL_regcomp_parse < PL_regxend) {
1433 if (*PL_regcomp_parse == ')') {
1434 FAIL("unmatched () in regexp");
1437 FAIL("junk on end of regexp"); /* "Can't happen". */
1445 - regbranch - one alternative of an | operator
1447 * Implements the concatenation operator.
1450 S_regbranch(pTHX_ I32 *flagp, I32 first)
1453 register regnode *ret;
1454 register regnode *chain = NULL;
1455 register regnode *latest;
1456 I32 flags = 0, c = 0;
1461 if (!SIZE_ONLY && PL_extralen)
1462 ret = reganode(BRANCHJ,0);
1464 ret = reg_node(BRANCH);
1467 if (!first && SIZE_ONLY)
1468 PL_extralen += 1; /* BRANCHJ */
1470 *flagp = WORST; /* Tentatively. */
1474 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
1476 latest = regpiece(&flags);
1477 if (latest == NULL) {
1478 if (flags & TRYAGAIN)
1482 else if (ret == NULL)
1484 *flagp |= flags&HASWIDTH;
1485 if (chain == NULL) /* First piece. */
1486 *flagp |= flags&SPSTART;
1489 regtail(chain, latest);
1494 if (chain == NULL) { /* Loop ran zero times. */
1495 chain = reg_node(NOTHING);
1500 *flagp |= flags&SIMPLE;
1507 - regpiece - something followed by possible [*+?]
1509 * Note that the branching code sequences used for ? and the general cases
1510 * of * and + are somewhat optimized: they use the same NOTHING node as
1511 * both the endmarker for their branch list and the body of the last branch.
1512 * It might seem that this node could be dispensed with entirely, but the
1513 * endmarker role is not redundant.
1516 S_regpiece(pTHX_ I32 *flagp)
1519 register regnode *ret;
1521 register char *next;
1523 char *origparse = PL_regcomp_parse;
1526 I32 max = REG_INFTY;
1528 ret = regatom(&flags);
1530 if (flags & TRYAGAIN)
1535 op = *PL_regcomp_parse;
1537 if (op == '{' && regcurly(PL_regcomp_parse)) {
1538 next = PL_regcomp_parse + 1;
1540 while (isDIGIT(*next) || *next == ',') {
1549 if (*next == '}') { /* got one */
1553 min = atoi(PL_regcomp_parse);
1557 maxpos = PL_regcomp_parse;
1559 if (!max && *maxpos != '0')
1560 max = REG_INFTY; /* meaning "infinity" */
1561 else if (max >= REG_INFTY)
1562 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
1563 PL_regcomp_parse = next;
1567 if ((flags&SIMPLE)) {
1568 PL_regnaughty += 2 + PL_regnaughty / 2;
1569 reginsert(CURLY, ret);
1572 regnode *w = reg_node(WHILEM);
1576 if (!SIZE_ONLY && PL_extralen) {
1577 reginsert(LONGJMP,ret);
1578 reginsert(NOTHING,ret);
1579 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1581 reginsert(CURLYX,ret);
1582 if (!SIZE_ONLY && PL_extralen)
1583 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1584 regtail(ret, reg_node(NOTHING));
1586 PL_reg_whilem_seen++, PL_extralen += 3;
1587 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
1595 if (max && max < min)
1596 FAIL("Can't do {n,m} with n > m");
1611 #if 0 /* Now runtime fix should be reliable. */
1612 if (!(flags&HASWIDTH) && op != '?')
1613 FAIL("regexp *+ operand could be empty");
1618 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
1620 if (op == '*' && (flags&SIMPLE)) {
1621 reginsert(STAR, ret);
1625 else if (op == '*') {
1629 else if (op == '+' && (flags&SIMPLE)) {
1630 reginsert(PLUS, ret);
1634 else if (op == '+') {
1638 else if (op == '?') {
1643 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
1644 Perl_warner(aTHX_ WARN_UNSAFE, "%.*s matches null string many times",
1645 PL_regcomp_parse - origparse, origparse);
1648 if (*PL_regcomp_parse == '?') {
1650 reginsert(MINMOD, ret);
1651 regtail(ret, ret + NODE_STEP_REGNODE);
1653 if (ISMULT2(PL_regcomp_parse))
1654 FAIL("nested *?+ in regexp");
1660 - regatom - the lowest level
1662 * Optimization: gobbles an entire sequence of ordinary characters so that
1663 * it can turn them into a single node, which is smaller to store and
1664 * faster to run. Backslashed characters are exceptions, each becoming a
1665 * separate node; the code is simpler that way and it's not worth fixing.
1667 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1670 S_regatom(pTHX_ I32 *flagp)
1673 register regnode *ret = 0;
1676 *flagp = WORST; /* Tentatively. */
1679 switch (*PL_regcomp_parse) {
1683 if (PL_regflags & PMf_MULTILINE)
1684 ret = reg_node(MBOL);
1685 else if (PL_regflags & PMf_SINGLELINE)
1686 ret = reg_node(SBOL);
1688 ret = reg_node(BOL);
1691 if (PL_regcomp_parse[1])
1694 if (PL_regflags & PMf_MULTILINE)
1695 ret = reg_node(MEOL);
1696 else if (PL_regflags & PMf_SINGLELINE)
1697 ret = reg_node(SEOL);
1699 ret = reg_node(EOL);
1704 if (PL_regflags & PMf_SINGLELINE)
1705 ret = reg_node(SANYUTF8);
1707 ret = reg_node(ANYUTF8);
1711 if (PL_regflags & PMf_SINGLELINE)
1712 ret = reg_node(SANY);
1714 ret = reg_node(REG_ANY);
1715 *flagp |= HASWIDTH|SIMPLE;
1721 ret = (UTF ? regclassutf8() : regclass());
1722 if (*PL_regcomp_parse != ']')
1723 FAIL("unmatched [] in regexp");
1725 *flagp |= HASWIDTH|SIMPLE;
1729 ret = reg(1, &flags);
1731 if (flags & TRYAGAIN)
1735 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
1739 if (flags & TRYAGAIN) {
1743 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
1744 /* Supposed to be caught earlier. */
1747 if (!regcurly(PL_regcomp_parse)) {
1755 FAIL("?+*{} follows nothing in regexp");
1758 switch (*++PL_regcomp_parse) {
1761 ret = reg_node(SBOL);
1766 ret = reg_node(GPOS);
1767 PL_regseen |= REG_SEEN_GPOS;
1772 ret = reg_node(SEOL);
1777 ret = reg_node(EOS);
1779 PL_seen_zerolen++; /* Do not optimize RE away */
1783 ret = reg_node(SANY);
1784 *flagp |= HASWIDTH|SIMPLE;
1788 ret = reg_node(CLUMP);
1791 if (UTF && !PL_utf8_mark)
1792 is_utf8_mark((U8*)"~"); /* preload table */
1797 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
1798 : (LOC ? ALNUML : ALNUM));
1799 *flagp |= HASWIDTH|SIMPLE;
1801 if (UTF && !PL_utf8_alnum)
1802 is_utf8_alnum((U8*)"a"); /* preload table */
1807 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
1808 : (LOC ? NALNUML : NALNUM));
1809 *flagp |= HASWIDTH|SIMPLE;
1811 if (UTF && !PL_utf8_alnum)
1812 is_utf8_alnum((U8*)"a"); /* preload table */
1816 PL_regseen |= REG_SEEN_LOOKBEHIND;
1819 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
1820 : (LOC ? BOUNDL : BOUND));
1823 if (UTF && !PL_utf8_alnum)
1824 is_utf8_alnum((U8*)"a"); /* preload table */
1828 PL_regseen |= REG_SEEN_LOOKBEHIND;
1831 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
1832 : (LOC ? NBOUNDL : NBOUND));
1835 if (UTF && !PL_utf8_alnum)
1836 is_utf8_alnum((U8*)"a"); /* preload table */
1841 ? (LOC ? SPACELUTF8 : SPACEUTF8)
1842 : (LOC ? SPACEL : SPACE));
1843 *flagp |= HASWIDTH|SIMPLE;
1845 if (UTF && !PL_utf8_space)
1846 is_utf8_space((U8*)" "); /* preload table */
1851 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
1852 : (LOC ? NSPACEL : NSPACE));
1853 *flagp |= HASWIDTH|SIMPLE;
1855 if (UTF && !PL_utf8_space)
1856 is_utf8_space((U8*)" "); /* preload table */
1859 ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
1860 *flagp |= HASWIDTH|SIMPLE;
1862 if (UTF && !PL_utf8_digit)
1863 is_utf8_digit((U8*)"1"); /* preload table */
1866 ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
1867 *flagp |= HASWIDTH|SIMPLE;
1869 if (UTF && !PL_utf8_digit)
1870 is_utf8_digit((U8*)"1"); /* preload table */
1874 { /* a lovely hack--pretend we saw [\pX] instead */
1875 char* oldregxend = PL_regxend;
1877 if (PL_regcomp_parse[1] == '{') {
1878 PL_regxend = strchr(PL_regcomp_parse, '}');
1880 FAIL("Missing right brace on \\p{}");
1884 PL_regxend = PL_regcomp_parse + 2;
1887 ret = regclassutf8();
1889 PL_regxend = oldregxend;
1892 *flagp |= HASWIDTH|SIMPLE;
1905 case '1': case '2': case '3': case '4':
1906 case '5': case '6': case '7': case '8': case '9':
1908 I32 num = atoi(PL_regcomp_parse);
1910 if (num > 9 && num >= PL_regnpar)
1913 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
1914 FAIL("reference to nonexistent group");
1917 ? (LOC ? REFFL : REFF)
1920 while (isDIGIT(*PL_regcomp_parse))
1928 if (PL_regcomp_parse >= PL_regxend)
1929 FAIL("trailing \\ in regexp");
1932 /* Do not generate `unrecognized' warnings here, we fall
1933 back into the quick-grab loop below */
1939 if (PL_regflags & PMf_EXTENDED) {
1940 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1941 if (PL_regcomp_parse < PL_regxend)
1957 ? (LOC ? EXACTFL : EXACTF)
1960 for (len = 0, p = PL_regcomp_parse - 1;
1961 len < 127 && p < PL_regxend;
1966 if (PL_regflags & PMf_EXTENDED)
1967 p = regwhite(p, PL_regxend);
2021 char* e = strchr(p, '}');
2024 FAIL("Missing right brace on \\x{}");
2026 ender = scan_hex(p + 1, e - p, &numlen);
2027 if (numlen + len >= 127) { /* numlen is generous */
2034 FAIL("Can't use \\x{} without 'use utf8' declaration");
2037 ender = scan_hex(p, 2, &numlen);
2043 ender = UCHARAT(p++);
2044 ender = toCTRL(ender);
2046 case '0': case '1': case '2': case '3':case '4':
2047 case '5': case '6': case '7': case '8':case '9':
2049 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
2050 ender = scan_oct(p, 3, &numlen);
2059 if (p >= PL_regxend)
2060 FAIL("trailing \\ in regexp");
2063 if (!SIZE_ONLY && ckWARN(WARN_UNSAFE) && isALPHA(*p))
2064 Perl_warner(aTHX_ WARN_UNSAFE,
2065 "/%.127s/: Unrecognized escape \\%c passed through",
2068 goto normal_default;
2073 if ((*p & 0xc0) == 0xc0 && UTF) {
2074 ender = utf8_to_uv((U8*)p, &numlen);
2081 if (PL_regflags & PMf_EXTENDED)
2082 p = regwhite(p, PL_regxend);
2085 ender = toLOWER_LC_uni(ender);
2087 ender = toLOWER_uni(ender);
2089 if (ISMULT2(p)) { /* Back off on ?+*. */
2092 else if (ender >= 0x80 && UTF) {
2093 reguni(ender, s, &numlen);
2103 if (ender >= 0x80 && UTF) {
2104 reguni(ender, s, &numlen);
2112 PL_regcomp_parse = p - 1;
2115 FAIL("internal disaster in regexp");
2123 PL_regsize += STR_SZ(len);
2125 PL_regcode += STR_SZ(len);
2134 S_regwhite(pTHX_ char *p, char *e)
2139 else if (*p == '#') {
2142 } while (p < e && *p != '\n');
2150 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
2151 Character classes ([:foo:]) can also be negated ([:^foo:]).
2152 Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
2153 Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
2154 but trigger warnings because they are currently unimplemented. */
2156 S_regpposixcc(pTHX_ I32 value)
2160 I32 namedclass = -1;
2162 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2163 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2164 (*PL_regcomp_parse == ':' ||
2165 *PL_regcomp_parse == '=' ||
2166 *PL_regcomp_parse == '.')) {
2167 char c = *PL_regcomp_parse;
2168 char* s = PL_regcomp_parse++;
2170 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != c)
2172 if (PL_regcomp_parse == PL_regxend)
2173 /* Grandfather lone [:, [=, [. */
2174 PL_regcomp_parse = s;
2176 char* t = PL_regcomp_parse++; /* skip over the c */
2178 if (*PL_regcomp_parse == ']') {
2179 PL_regcomp_parse++; /* skip over the ending ] */
2182 I32 complement = *posixcc == '^' ? *posixcc++ : 0;
2183 I32 skip = 5; /* the most common skip */
2187 if (strnEQ(posixcc, "alnum", 5))
2189 complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
2190 else if (strnEQ(posixcc, "alpha", 5))
2192 complement ? ANYOF_NALPHA : ANYOF_ALPHA;
2193 else if (strnEQ(posixcc, "ascii", 5))
2195 complement ? ANYOF_NASCII : ANYOF_ASCII;
2198 if (strnEQ(posixcc, "cntrl", 5))
2200 complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
2203 if (strnEQ(posixcc, "digit", 5))
2205 complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
2208 if (strnEQ(posixcc, "graph", 5))
2210 complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
2213 if (strnEQ(posixcc, "lower", 5))
2215 complement ? ANYOF_NLOWER : ANYOF_LOWER;
2218 if (strnEQ(posixcc, "print", 5))
2220 complement ? ANYOF_NPRINT : ANYOF_PRINT;
2221 else if (strnEQ(posixcc, "punct", 5))
2223 complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
2226 if (strnEQ(posixcc, "space", 5))
2228 complement ? ANYOF_NSPACE : ANYOF_SPACE;
2230 if (strnEQ(posixcc, "upper", 5))
2232 complement ? ANYOF_NUPPER : ANYOF_UPPER;
2234 case 'w': /* this is not POSIX, this is the Perl \w */
2235 if (strnEQ(posixcc, "word", 4)) {
2237 complement ? ANYOF_NALNUM : ANYOF_ALNUM;
2242 if (strnEQ(posixcc, "xdigit", 6)) {
2244 complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
2249 if ((namedclass == -1 ||
2250 !(posixcc + skip + 2 < PL_regxend &&
2251 (posixcc[skip] == ':' &&
2252 posixcc[skip + 1] == ']'))))
2253 Perl_croak(aTHX_ "Character class [:%.*s:] unknown",
2255 } else if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
2256 /* [[=foo=]] and [[.foo.]] are still future. */
2257 Perl_warner(aTHX_ WARN_UNSAFE,
2258 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2260 /* Maternal grandfather:
2261 * "[:" ending in ":" but not in ":]" */
2262 PL_regcomp_parse = s;
2271 S_checkposixcc(pTHX)
2273 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY &&
2274 (*PL_regcomp_parse == ':' ||
2275 *PL_regcomp_parse == '=' ||
2276 *PL_regcomp_parse == '.')) {
2277 char *s = PL_regcomp_parse;
2280 while(*s && isALNUM(*s))
2282 if (*s && c == *s && s[1] == ']') {
2283 Perl_warner(aTHX_ WARN_UNSAFE,
2284 "Character class syntax [%c %c] belongs inside character classes", c, c);
2285 if (c == '=' || c == '.')
2286 Perl_warner(aTHX_ WARN_UNSAFE,
2287 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2296 register char *opnd, *s;
2298 register I32 lastvalue = OOB_CHAR8;
2299 register I32 range = 0;
2300 register regnode *ret;
2305 s = opnd = MASK(PL_regcode);
2306 ret = reg_node(ANYOF);
2307 for (value = 0; value < ANYOF_SIZE; value++)
2309 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2313 ANYOF_FLAGS(opnd) |= ANYOF_INVERT;
2316 PL_regcode += ANY_SKIP;
2318 ANYOF_FLAGS(opnd) |= ANYOF_FOLD;
2320 ANYOF_FLAGS(opnd) |= ANYOF_LOCALE;
2323 PL_regsize += ANY_SKIP;
2328 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2329 goto skipcond; /* allow 1st char to be ] or - */
2330 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2333 value = UCHARAT(PL_regcomp_parse++);
2335 namedclass = regpposixcc(value);
2336 else if (value == '\\') {
2337 value = UCHARAT(PL_regcomp_parse++);
2339 case 'w': namedclass = ANYOF_ALNUM; break;
2340 case 'W': namedclass = ANYOF_NALNUM; break;
2341 case 's': namedclass = ANYOF_SPACE; break;
2342 case 'S': namedclass = ANYOF_NSPACE; break;
2343 case 'd': namedclass = ANYOF_DIGIT; break;
2344 case 'D': namedclass = ANYOF_NDIGIT; break;
2345 case 'n': value = '\n'; break;
2346 case 'r': value = '\r'; break;
2347 case 't': value = '\t'; break;
2348 case 'f': value = '\f'; break;
2349 case 'b': value = '\b'; break;
2350 case 'e': value = '\033'; break;
2351 case 'a': value = '\007'; break;
2353 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2354 PL_regcomp_parse += numlen;
2357 value = UCHARAT(PL_regcomp_parse++);
2358 value = toCTRL(value);
2360 case '0': case '1': case '2': case '3': case '4':
2361 case '5': case '6': case '7': case '8': case '9':
2362 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2363 PL_regcomp_parse += numlen;
2367 if (!SIZE_ONLY && namedclass > -1) {
2368 switch (namedclass) {
2371 ANYOF_CLASS_SET(opnd, ANYOF_ALNUM);
2373 for (value = 0; value < 256; value++)
2375 ANYOF_BITMAP_SET(opnd, value);
2380 ANYOF_CLASS_SET(opnd, ANYOF_NALNUM);
2382 for (value = 0; value < 256; value++)
2383 if (!isALNUM(value))
2384 ANYOF_BITMAP_SET(opnd, value);
2389 ANYOF_CLASS_SET(opnd, ANYOF_SPACE);
2391 for (value = 0; value < 256; value++)
2393 ANYOF_BITMAP_SET(opnd, value);
2398 ANYOF_CLASS_SET(opnd, ANYOF_NSPACE);
2400 for (value = 0; value < 256; value++)
2401 if (!isSPACE(value))
2402 ANYOF_BITMAP_SET(opnd, value);
2407 ANYOF_CLASS_SET(opnd, ANYOF_DIGIT);
2409 for (value = '0'; value <= '9'; value++)
2410 ANYOF_BITMAP_SET(opnd, value);
2415 ANYOF_CLASS_SET(opnd, ANYOF_NDIGIT);
2417 for (value = 0; value < '0'; value++)
2418 ANYOF_BITMAP_SET(opnd, value);
2419 for (value = '9' + 1; value < 256; value++)
2420 ANYOF_BITMAP_SET(opnd, value);
2425 ANYOF_CLASS_SET(opnd, ANYOF_NALNUMC);
2427 for (value = 0; value < 256; value++)
2428 if (!isALNUMC(value))
2429 ANYOF_BITMAP_SET(opnd, value);
2434 ANYOF_CLASS_SET(opnd, ANYOF_ALNUMC);
2436 for (value = 0; value < 256; value++)
2437 if (isALNUMC(value))
2438 ANYOF_BITMAP_SET(opnd, value);
2443 ANYOF_CLASS_SET(opnd, ANYOF_ALPHA);
2445 for (value = 0; value < 256; value++)
2447 ANYOF_BITMAP_SET(opnd, value);
2452 ANYOF_CLASS_SET(opnd, ANYOF_NALPHA);
2454 for (value = 0; value < 256; value++)
2455 if (!isALPHA(value))
2456 ANYOF_BITMAP_SET(opnd, value);
2461 ANYOF_CLASS_SET(opnd, ANYOF_ASCII);
2463 for (value = 0; value < 128; value++)
2464 ANYOF_BITMAP_SET(opnd, value);
2469 ANYOF_CLASS_SET(opnd, ANYOF_NASCII);
2471 for (value = 128; value < 256; value++)
2472 ANYOF_BITMAP_SET(opnd, value);
2477 ANYOF_CLASS_SET(opnd, ANYOF_CNTRL);
2479 for (value = 0; value < 256; value++)
2481 ANYOF_BITMAP_SET(opnd, value);
2483 lastvalue = OOB_CHAR8;
2487 ANYOF_CLASS_SET(opnd, ANYOF_NCNTRL);
2489 for (value = 0; value < 256; value++)
2490 if (!isCNTRL(value))
2491 ANYOF_BITMAP_SET(opnd, value);
2496 ANYOF_CLASS_SET(opnd, ANYOF_GRAPH);
2498 for (value = 0; value < 256; value++)
2500 ANYOF_BITMAP_SET(opnd, value);
2505 ANYOF_CLASS_SET(opnd, ANYOF_NGRAPH);
2507 for (value = 0; value < 256; value++)
2508 if (!isGRAPH(value))
2509 ANYOF_BITMAP_SET(opnd, value);
2514 ANYOF_CLASS_SET(opnd, ANYOF_LOWER);
2516 for (value = 0; value < 256; value++)
2518 ANYOF_BITMAP_SET(opnd, value);
2523 ANYOF_CLASS_SET(opnd, ANYOF_NLOWER);
2525 for (value = 0; value < 256; value++)
2526 if (!isLOWER(value))
2527 ANYOF_BITMAP_SET(opnd, value);
2532 ANYOF_CLASS_SET(opnd, ANYOF_PRINT);
2534 for (value = 0; value < 256; value++)
2536 ANYOF_BITMAP_SET(opnd, value);
2541 ANYOF_CLASS_SET(opnd, ANYOF_NPRINT);
2543 for (value = 0; value < 256; value++)
2544 if (!isPRINT(value))
2545 ANYOF_BITMAP_SET(opnd, value);
2550 ANYOF_CLASS_SET(opnd, ANYOF_PUNCT);
2552 for (value = 0; value < 256; value++)
2554 ANYOF_BITMAP_SET(opnd, value);
2559 ANYOF_CLASS_SET(opnd, ANYOF_NPUNCT);
2561 for (value = 0; value < 256; value++)
2562 if (!isPUNCT(value))
2563 ANYOF_BITMAP_SET(opnd, value);
2568 ANYOF_CLASS_SET(opnd, ANYOF_UPPER);
2570 for (value = 0; value < 256; value++)
2572 ANYOF_BITMAP_SET(opnd, value);
2577 ANYOF_CLASS_SET(opnd, ANYOF_NUPPER);
2579 for (value = 0; value < 256; value++)
2580 if (!isUPPER(value))
2581 ANYOF_BITMAP_SET(opnd, value);
2586 ANYOF_CLASS_SET(opnd, ANYOF_XDIGIT);
2588 for (value = 0; value < 256; value++)
2589 if (isXDIGIT(value))
2590 ANYOF_BITMAP_SET(opnd, value);
2595 ANYOF_CLASS_SET(opnd, ANYOF_NXDIGIT);
2597 for (value = 0; value < 256; value++)
2598 if (!isXDIGIT(value))
2599 ANYOF_BITMAP_SET(opnd, value);
2603 FAIL("invalid [::] class in regexp");
2607 ANYOF_FLAGS(opnd) |= ANYOF_CLASS;
2608 lastvalue = OOB_CHAR8;
2612 if (lastvalue > value)
2613 FAIL("invalid [] range in regexp");
2618 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2619 PL_regcomp_parse[1] != ']') {
2622 continue; /* do it next time */
2627 if ((isLOWER(lastvalue) && isLOWER(value)) ||
2628 (isUPPER(lastvalue) && isUPPER(value)))
2631 if (isLOWER(lastvalue)) {
2632 for (i = lastvalue; i <= value; i++)
2634 ANYOF_BITMAP_SET(opnd, i);
2636 for (i = lastvalue; i <= value; i++)
2638 ANYOF_BITMAP_SET(opnd, i);
2643 for ( ; lastvalue <= value; lastvalue++)
2644 ANYOF_BITMAP_SET(opnd, lastvalue);
2648 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2650 (ANYOF_FLAGS(opnd) & (ANYOF_FLAGS_ALL ^ ANYOF_INVERT)) == ANYOF_FOLD) {
2651 for (value = 0; value < 256; ++value) {
2652 if (ANYOF_BITMAP_TEST(opnd, value)) {
2653 I32 cf = PL_fold[value];
2654 ANYOF_BITMAP_SET(opnd, cf);
2657 ANYOF_FLAGS(opnd) &= ~ANYOF_FOLD;
2659 /* optimize inverted simple patterns (e.g. [^a-z]) */
2660 if (!SIZE_ONLY && (ANYOF_FLAGS(opnd) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
2661 for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
2662 opnd[ANYOF_BITMAP_OFFSET + value] ^= ANYOF_FLAGS_ALL;
2663 ANYOF_FLAGS(opnd) = 0;
2669 S_regclassutf8(pTHX)
2672 register char *opnd, *e;
2674 register U32 lastvalue = OOB_UTF8;
2675 register I32 range = 0;
2676 register regnode *ret;
2683 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2687 flags |= ANYOF_INVERT;
2691 flags |= ANYOF_FOLD;
2693 flags |= ANYOF_LOCALE;
2694 listsv = newSVpvn("# comment\n",10);
2699 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2700 goto skipcond; /* allow 1st char to be ] or - */
2702 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2705 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2706 PL_regcomp_parse += numlen;
2709 namedclass = regpposixcc(value);
2710 else if (value == '\\') {
2711 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2712 PL_regcomp_parse += numlen;
2714 case 'w': namedclass = ANYOF_ALNUM; break;
2715 case 'W': namedclass = ANYOF_NALNUM; break;
2716 case 's': namedclass = ANYOF_SPACE; break;
2717 case 'S': namedclass = ANYOF_NSPACE; break;
2718 case 'd': namedclass = ANYOF_DIGIT; break;
2719 case 'D': namedclass = ANYOF_NDIGIT; break;
2722 if (*PL_regcomp_parse == '{') {
2723 e = strchr(PL_regcomp_parse++, '}');
2725 FAIL("Missing right brace on \\p{}");
2726 n = e - PL_regcomp_parse;
2729 e = PL_regcomp_parse;
2734 Perl_sv_catpvf(aTHX_ listsv,
2735 "+utf8::%.*s\n", n, PL_regcomp_parse);
2737 Perl_sv_catpvf(aTHX_ listsv,
2738 "!utf8::%.*s\n", n, PL_regcomp_parse);
2740 PL_regcomp_parse = e + 1;
2741 lastvalue = OOB_UTF8;
2743 case 'n': value = '\n'; break;
2744 case 'r': value = '\r'; break;
2745 case 't': value = '\t'; break;
2746 case 'f': value = '\f'; break;
2747 case 'b': value = '\b'; break;
2748 case 'e': value = '\033'; break;
2749 case 'a': value = '\007'; break;
2751 if (*PL_regcomp_parse == '{') {
2752 e = strchr(PL_regcomp_parse++, '}');
2754 FAIL("Missing right brace on \\x{}");
2755 value = scan_hex(PL_regcomp_parse,
2756 e - PL_regcomp_parse,
2758 PL_regcomp_parse = e + 1;
2761 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2762 PL_regcomp_parse += numlen;
2766 value = UCHARAT(PL_regcomp_parse++);
2767 value = toCTRL(value);
2769 case '0': case '1': case '2': case '3': case '4':
2770 case '5': case '6': case '7': case '8': case '9':
2771 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2772 PL_regcomp_parse += numlen;
2776 if (!SIZE_ONLY && namedclass > -1) {
2777 switch (namedclass) {
2779 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n"); break;
2781 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n"); break;
2783 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n"); break;
2785 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n"); break;
2787 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n"); break;
2789 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n"); break;
2791 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n"); break;
2793 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n"); break;
2795 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n"); break;
2797 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n"); break;
2799 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n"); break;
2801 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n"); break;
2803 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n"); break;
2805 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n"); break;
2807 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n"); break;
2809 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n"); break;
2811 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n"); break;
2813 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n"); break;
2815 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n"); break;
2817 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n"); break;
2819 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n"); break;
2821 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n"); break;
2823 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n"); break;
2825 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n"); break;
2827 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n"); break;
2829 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n"); break;
2834 if (lastvalue > value)
2835 FAIL("invalid [] range in regexp");
2838 Perl_sv_catpvf(aTHX_ listsv, "%04" PERL_PRIx64 "\t%04" PERL_PRIx64 "\n", (UV)lastvalue, (UV)value);
2841 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\n", lastvalue, value);
2848 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2849 PL_regcomp_parse[1] != ']') {
2852 continue; /* do it next time */
2856 Perl_sv_catpvf(aTHX_ listsv, "%04" PERL_PRIx64 "\n", (UV)value);
2859 Perl_sv_catpvf(aTHX_ listsv, "%04x\n", value);
2864 ret = reganode(ANYOFUTF8, 0);
2867 SV *rv = swash_init("utf8", "", listsv, 1, 0);
2868 SvREFCNT_dec(listsv);
2869 n = add_data(1,"s");
2870 PL_regcomp_rx->data->data[n] = (void*)rv;
2871 ARG1_SET(ret, flags);
2882 char* retval = PL_regcomp_parse++;
2885 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2886 PL_regcomp_parse[2] == '#') {
2887 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2892 if (PL_regflags & PMf_EXTENDED) {
2893 if (isSPACE(*PL_regcomp_parse)) {
2897 else if (*PL_regcomp_parse == '#') {
2898 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2909 - reg_node - emit a node
2911 STATIC regnode * /* Location. */
2912 S_reg_node(pTHX_ U8 op)
2915 register regnode *ret;
2916 register regnode *ptr;
2920 SIZE_ALIGN(PL_regsize);
2925 NODE_ALIGN_FILL(ret);
2927 FILL_ADVANCE_NODE(ptr, op);
2934 - reganode - emit a node with an argument
2936 STATIC regnode * /* Location. */
2937 S_reganode(pTHX_ U8 op, U32 arg)
2940 register regnode *ret;
2941 register regnode *ptr;
2945 SIZE_ALIGN(PL_regsize);
2950 NODE_ALIGN_FILL(ret);
2952 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
2959 - reguni - emit (if appropriate) a Unicode character
2962 S_reguni(pTHX_ UV uv, char* s, I32* lenp)
2967 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
2970 *lenp = uv_to_utf8((U8*)s, uv) - (U8*)s;
2975 - reginsert - insert an operator in front of already-emitted operand
2977 * Means relocating the operand.
2980 S_reginsert(pTHX_ U8 op, regnode *opnd)
2983 register regnode *src;
2984 register regnode *dst;
2985 register regnode *place;
2986 register int offset = regarglen[(U8)op];
2988 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2991 PL_regsize += NODE_STEP_REGNODE + offset;
2996 PL_regcode += NODE_STEP_REGNODE + offset;
2999 StructCopy(--src, --dst, regnode);
3001 place = opnd; /* Op node, where operand used to be. */
3002 src = NEXTOPER(place);
3003 FILL_ADVANCE_NODE(place, op);
3004 Zero(src, offset, regnode);
3008 - regtail - set the next-pointer at the end of a node chain of p to val.
3011 S_regtail(pTHX_ regnode *p, regnode *val)
3014 register regnode *scan;
3015 register regnode *temp;
3016 register I32 offset;
3021 /* Find last node. */
3024 temp = regnext(scan);
3030 if (reg_off_by_arg[OP(scan)]) {
3031 ARG_SET(scan, val - scan);
3034 NEXT_OFF(scan) = val - scan;
3039 - regoptail - regtail on operand of first argument; nop if operandless
3042 S_regoptail(pTHX_ regnode *p, regnode *val)
3045 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
3046 if (p == NULL || SIZE_ONLY)
3048 if (PL_regkind[(U8)OP(p)] == BRANCH) {
3049 regtail(NEXTOPER(p), val);
3051 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
3052 regtail(NEXTOPER(NEXTOPER(p)), val);
3059 - regcurly - a little FSA that accepts {\d+,?\d*}
3062 S_regcurly(pTHX_ register char *s)
3081 S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
3084 register U8 op = EXACT; /* Arbitrary non-END op. */
3085 register regnode *next, *onode;
3087 while (op != END && (!last || node < last)) {
3088 /* While that wasn't END last time... */
3094 next = regnext(node);
3096 if (OP(node) == OPTIMIZED)
3099 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
3100 2*l + 1, "", SvPVX(sv));
3101 if (next == NULL) /* Next ptr. */
3102 PerlIO_printf(Perl_debug_log, "(0)");
3104 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
3105 (void)PerlIO_putc(Perl_debug_log, '\n');
3107 if (PL_regkind[(U8)op] == BRANCHJ) {
3108 register regnode *nnode = (OP(next) == LONGJMP
3111 if (last && nnode > last)
3113 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
3115 else if (PL_regkind[(U8)op] == BRANCH) {
3116 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
3118 else if ( op == CURLY) { /* `next' might be very big: optimizer */
3119 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
3120 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
3122 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
3123 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
3126 else if ( op == PLUS || op == STAR) {
3127 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
3129 else if (op == ANYOF) {
3130 node = NEXTOPER(node);
3133 else if (PL_regkind[(U8)op] == EXACT) {
3134 /* Literal string, where present. */
3135 node += NODE_SZ_STR(node) - 1;
3136 node = NEXTOPER(node);
3139 node = NEXTOPER(node);
3140 node += regarglen[(U8)op];
3142 if (op == CURLYX || op == OPEN)
3144 else if (op == WHILEM)
3147 #endif /* DEBUGGING */
3152 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
3155 Perl_regdump(pTHX_ regexp *r)
3159 SV *sv = sv_newmortal();
3161 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
3163 /* Header fields of interest. */
3164 if (r->anchored_substr)
3165 PerlIO_printf(Perl_debug_log, "anchored `%s%.*s%s'%s at %d ",
3167 SvCUR(r->anchored_substr) - (SvTAIL(r->anchored_substr)!=0),
3168 SvPVX(r->anchored_substr),
3170 SvTAIL(r->anchored_substr) ? "$" : "",
3171 r->anchored_offset);
3172 if (r->float_substr)
3173 PerlIO_printf(Perl_debug_log, "floating `%s%.*s%s'%s at %d..%u ",
3175 SvCUR(r->float_substr) - (SvTAIL(r->float_substr)!=0),
3176 SvPVX(r->float_substr),
3178 SvTAIL(r->float_substr) ? "$" : "",
3179 r->float_min_offset, r->float_max_offset);
3180 if (r->check_substr)
3181 PerlIO_printf(Perl_debug_log,
3182 r->check_substr == r->float_substr
3183 ? "(checking floating" : "(checking anchored");
3184 if (r->reganch & ROPT_NOSCAN)
3185 PerlIO_printf(Perl_debug_log, " noscan");
3186 if (r->reganch & ROPT_CHECK_ALL)
3187 PerlIO_printf(Perl_debug_log, " isall");
3188 if (r->check_substr)
3189 PerlIO_printf(Perl_debug_log, ") ");
3191 if (r->regstclass) {
3192 regprop(sv, r->regstclass);
3193 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
3195 if (r->reganch & ROPT_ANCH) {
3196 PerlIO_printf(Perl_debug_log, "anchored");
3197 if (r->reganch & ROPT_ANCH_BOL)
3198 PerlIO_printf(Perl_debug_log, "(BOL)");
3199 if (r->reganch & ROPT_ANCH_MBOL)
3200 PerlIO_printf(Perl_debug_log, "(MBOL)");
3201 if (r->reganch & ROPT_ANCH_SBOL)
3202 PerlIO_printf(Perl_debug_log, "(SBOL)");
3203 if (r->reganch & ROPT_ANCH_GPOS)
3204 PerlIO_printf(Perl_debug_log, "(GPOS)");
3205 PerlIO_putc(Perl_debug_log, ' ');
3207 if (r->reganch & ROPT_GPOS_SEEN)
3208 PerlIO_printf(Perl_debug_log, "GPOS ");
3209 if (r->reganch & ROPT_SKIP)
3210 PerlIO_printf(Perl_debug_log, "plus ");
3211 if (r->reganch & ROPT_IMPLICIT)
3212 PerlIO_printf(Perl_debug_log, "implicit ");
3213 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
3214 if (r->reganch & ROPT_EVAL_SEEN)
3215 PerlIO_printf(Perl_debug_log, "with eval ");
3216 PerlIO_printf(Perl_debug_log, "\n");
3217 #endif /* DEBUGGING */
3221 - regprop - printable representation of opcode
3224 Perl_regprop(pTHX_ SV *sv, regnode *o)
3230 sv_setpvn(sv, "", 0);
3231 if (OP(o) >= reg_num) /* regnode.type is unsigned */
3232 FAIL("corrupted regexp opcode");
3233 sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
3235 k = PL_regkind[(U8)OP(o)];
3238 Perl_sv_catpvf(aTHX_ sv, " <%s%*s%s>", PL_colors[0],
3239 STR_LEN(o), STRING(o), PL_colors[1]);
3240 else if (k == CURLY) {
3241 if (OP(o) == CURLYM || OP(o) == CURLYN)
3242 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
3243 Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
3245 else if (k == WHILEM && o->flags) /* Ordinal/of */
3246 Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
3247 else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
3248 Perl_sv_catpvf(aTHX_ sv, "%d", ARG(o)); /* Parenth number */
3249 else if (k == LOGICAL)
3250 Perl_sv_catpvf(aTHX_ sv, "[%d]", ARG(o)); /* 2: embedded, otherwise 1 */
3251 else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
3252 Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
3253 #endif /* DEBUGGING */
3257 Perl_re_intuit_string(pTHX_ regexp *prog)
3258 { /* Assume that RE_INTUIT is set */
3261 char *s = SvPV(prog->check_substr,n_a);
3263 if (!PL_colorset) reginitcolors();
3264 PerlIO_printf(Perl_debug_log,
3265 "%sUsing REx substr:%s `%s%.60s%s%s'\n",
3266 PL_colors[4],PL_colors[5],PL_colors[0],
3269 (strlen(s) > 60 ? "..." : ""));
3272 return prog->check_substr;
3276 Perl_pregfree(pTHX_ struct regexp *r)
3279 DEBUG_r(if (!PL_colorset) reginitcolors());
3280 DEBUG_r(PerlIO_printf(Perl_debug_log,
3281 "%sFreeing REx:%s `%s%.60s%s%s'\n",
3282 PL_colors[4],PL_colors[5],PL_colors[0],
3285 (strlen(r->precomp) > 60 ? "..." : "")));
3288 if (!r || (--r->refcnt > 0))
3291 Safefree(r->precomp);
3292 if (RX_MATCH_COPIED(r))
3293 Safefree(r->subbeg);
3295 if (r->anchored_substr)
3296 SvREFCNT_dec(r->anchored_substr);
3297 if (r->float_substr)
3298 SvREFCNT_dec(r->float_substr);
3299 Safefree(r->substrs);
3302 int n = r->data->count;
3303 AV* new_comppad = NULL;
3308 switch (r->data->what[n]) {
3310 SvREFCNT_dec((SV*)r->data->data[n]);
3313 new_comppad = (AV*)r->data->data[n];
3316 if (new_comppad == NULL)
3317 Perl_croak(aTHX_ "panic: pregfree comppad");
3318 old_comppad = PL_comppad;
3319 old_curpad = PL_curpad;
3320 PL_comppad = new_comppad;
3321 PL_curpad = AvARRAY(new_comppad);
3322 op_free((OP_4tree*)r->data->data[n]);
3323 PL_comppad = old_comppad;
3324 PL_curpad = old_curpad;
3325 SvREFCNT_dec((SV*)new_comppad);
3331 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
3334 Safefree(r->data->what);
3337 Safefree(r->startp);
3343 - regnext - dig the "next" pointer out of a node
3345 * [Note, when REGALIGN is defined there are two places in regmatch()
3346 * that bypass this code for speed.]
3349 Perl_regnext(pTHX_ register regnode *p)
3352 register I32 offset;
3354 if (p == &PL_regdummy)
3357 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
3365 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
3368 STRLEN l1 = strlen(pat1);
3369 STRLEN l2 = strlen(pat2);
3378 Copy(pat1, buf, l1 , char);
3379 Copy(pat2, buf + l1, l2 , char);
3380 buf[l1 + l2] = '\n';
3381 buf[l1 + l2 + 1] = '\0';
3383 /* ANSI variant takes additional second argument */
3384 va_start(args, pat2);
3388 msv = mess(buf, &args);
3390 message = SvPV(msv,l1);
3393 Copy(message, buf, l1 , char);
3394 buf[l1] = '\0'; /* Overwrite \n */
3395 Perl_croak(aTHX_ "%s", buf);
3398 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
3401 Perl_save_re_context(pTHX)
3405 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
3406 SAVEI32(PL_regnpar); /* () count. */
3407 SAVEI32(PL_regsize); /* Code size. */
3408 SAVEI16(PL_regflags); /* are we folding, multilining? */
3409 SAVEPPTR(PL_reginput); /* String-input pointer. */
3410 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
3411 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
3412 SAVESPTR(PL_regstartp); /* Pointer to startp array. */
3413 SAVESPTR(PL_regendp); /* Ditto for endp. */
3414 SAVESPTR(PL_reglastparen); /* Similarly for lastparen. */
3415 SAVEPPTR(PL_regtill); /* How far we are required to go. */
3416 SAVEI32(PL_regprev); /* char before regbol, \n if none */
3417 SAVESPTR(PL_reg_start_tmp); /* from regexec.c */
3418 PL_reg_start_tmp = 0;
3419 SAVEFREEPV(PL_reg_start_tmp);
3420 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
3421 PL_reg_start_tmpl = 0;
3422 SAVESPTR(PL_regdata);
3423 SAVEI32(PL_reg_flags); /* from regexec.c */
3424 SAVEI32(PL_reg_eval_set); /* from regexec.c */
3425 SAVEI32(PL_regnarrate); /* from regexec.c */
3426 SAVESPTR(PL_regprogram); /* from regexec.c */
3427 SAVEINT(PL_regindent); /* from regexec.c */
3428 SAVESPTR(PL_regcc); /* from regexec.c */
3429 SAVESPTR(PL_curcop);
3430 SAVESPTR(PL_regcomp_rx); /* from regcomp.c */
3431 SAVEI32(PL_regseen); /* from regcomp.c */
3432 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
3433 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
3434 SAVESPTR(PL_regcode); /* Code-emit pointer; ®dummy = don't */
3435 SAVEPPTR(PL_regxend); /* End of input for compile */
3436 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */
3437 SAVESPTR(PL_reg_call_cc); /* from regexec.c */
3438 SAVESPTR(PL_reg_re); /* from regexec.c */
3439 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
3440 SAVESPTR(PL_reg_sv); /* from regexec.c */
3441 SAVESPTR(PL_reg_magic); /* from regexec.c */
3442 SAVEI32(PL_reg_oldpos); /* from regexec.c */
3443 SAVESPTR(PL_reg_oldcurpm); /* from regexec.c */
3444 SAVESPTR(PL_reg_curpm); /* from regexec.c */
3446 SAVEPPTR(PL_reg_starttry); /* from regexec.c */
3458 clear_re(pTHXo_ void *r)
3460 ReREFCNT_dec((regexp *)r);