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
48 * pregcomp and pregexec -- regsub and regerror are not used in perl
50 * Copyright (c) 1986 by University of Toronto.
51 * Written by Henry Spencer. Not derived from licensed software.
53 * Permission is granted to anyone to use this software for any
54 * purpose on any computer system, and to redistribute it freely,
55 * subject to the following restrictions:
57 * 1. The author is not responsible for the consequences of use of
58 * this software, no matter how awful, even if they arise
61 * 2. The origin of this software must not be misrepresented, either
62 * by explicit claim or by omission.
64 * 3. Altered versions must be plainly marked as such, and must not
65 * be misrepresented as being the original software.
68 **** Alterations to Henry's code are...
70 **** Copyright (c) 1991-1999, Larry Wall
72 **** You may distribute under the terms of either the GNU General Public
73 **** License or the Artistic License, as specified in the README file.
76 * Beware that some of this code is subtly aware of the way operator
77 * precedence is structured in regular expressions. Serious changes in
78 * regular-expression syntax might require a total rethink.
81 #define PERL_IN_REGCOMP_C
84 #ifndef PERL_IN_XSUB_RE
96 # if defined(BUGGY_MSC6)
97 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
98 # pragma optimize("a",off)
99 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
100 # pragma optimize("w",on )
101 # endif /* BUGGY_MSC6 */
105 #define STATIC static
108 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
109 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
110 ((*s) == '{' && regcurly(s)))
112 #define PERL_META "^$.[()|?+*\\"
114 #define META "^$.[()|?+*\\"
118 #undef SPSTART /* dratted cpp namespace... */
121 * Flags to be passed up and down.
123 #define WORST 0 /* Worst case. */
124 #define HASWIDTH 0x1 /* Known to match non-null strings. */
125 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
126 #define SPSTART 0x4 /* Starts with * or +. */
127 #define TRYAGAIN 0x8 /* Weeded out a declaration. */
130 * Forward declarations for pregcomp()'s friends.
133 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
136 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
137 #define SF_BEFORE_SEOL 0x1
138 #define SF_BEFORE_MEOL 0x2
139 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
140 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
143 # define SF_FIX_SHIFT_EOL (0+2)
144 # define SF_FL_SHIFT_EOL (0+4)
146 # define SF_FIX_SHIFT_EOL (+2)
147 # define SF_FL_SHIFT_EOL (+4)
150 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
151 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
153 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
154 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
155 #define SF_IS_INF 0x40
156 #define SF_HAS_PAR 0x80
157 #define SF_IN_PAR 0x100
158 #define SF_HAS_EVAL 0x200
159 #define SCF_DO_SUBSTR 0x400
162 #define UTF (PL_reg_flags & RF_utf8)
163 #define LOC (PL_regflags & PMf_LOCALE)
164 #define FOLD (PL_regflags & PMf_FOLD)
166 #define OOB_CHAR8 1234
167 #define OOB_UTF8 123456
169 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
170 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
172 static void clear_re(pTHXo_ void *r);
175 S_scan_commit(pTHX_ scan_data_t *data)
178 STRLEN l = CHR_SVLEN(data->last_found);
179 STRLEN old_l = CHR_SVLEN(*data->longest);
181 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
182 sv_setsv(*data->longest, data->last_found);
183 if (*data->longest == data->longest_fixed) {
184 data->offset_fixed = l ? data->last_start_min : data->pos_min;
185 if (data->flags & SF_BEFORE_EOL)
187 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
189 data->flags &= ~SF_FIX_BEFORE_EOL;
192 data->offset_float_min = l ? data->last_start_min : data->pos_min;
193 data->offset_float_max = (l
194 ? data->last_start_max
195 : data->pos_min + data->pos_delta);
196 if (data->flags & SF_BEFORE_EOL)
198 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
200 data->flags &= ~SF_FL_BEFORE_EOL;
203 SvCUR_set(data->last_found, 0);
205 data->flags &= ~SF_BEFORE_EOL;
208 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
209 to the position after last scanned or to NULL. */
212 S_study_chunk(pTHX_ regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
213 /* scanp: Start here (read-write). */
214 /* deltap: Write maxlen-minlen here. */
215 /* last: Stop before this one. */
218 I32 min = 0, pars = 0, code;
219 regnode *scan = *scanp, *next;
221 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
222 int is_inf_internal = 0; /* The studied chunk is infinite */
223 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
224 scan_data_t data_fake;
226 while (scan && OP(scan) != END && scan < last) {
227 /* Peephole optimizer: */
229 if (PL_regkind[(U8)OP(scan)] == EXACT) {
230 regnode *n = regnext(scan);
233 regnode *stop = scan;
236 next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
237 /* Skip NOTHING, merge EXACT*. */
239 ( PL_regkind[(U8)OP(n)] == NOTHING ||
240 (stringok && (OP(n) == OP(scan))))
242 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
243 if (OP(n) == TAIL || n > next)
245 if (PL_regkind[(U8)OP(n)] == NOTHING) {
246 NEXT_OFF(scan) += NEXT_OFF(n);
247 next = n + NODE_STEP_REGNODE;
255 int oldl = *OPERAND(scan);
256 regnode *nnext = regnext(n);
258 if (oldl + *OPERAND(n) > U8_MAX)
260 NEXT_OFF(scan) += NEXT_OFF(n);
261 *OPERAND(scan) += *OPERAND(n);
262 next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2;
263 /* Now we can overwrite *n : */
264 Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1,
265 *OPERAND(n) + 1, char);
275 n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
277 /* Purify reports a benign UMR here sometimes, because we
278 * don't initialize the OP() slot of a node when that node
279 * is occupied by just the trailing null of the string in
281 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
290 if (OP(scan) != CURLYX) {
291 int max = (reg_off_by_arg[OP(scan)]
293 /* I32 may be smaller than U16 on CRAYs! */
294 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
295 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
299 /* Skip NOTHING and LONGJMP. */
300 while ((n = regnext(n))
301 && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
302 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
305 if (reg_off_by_arg[OP(scan)])
308 NEXT_OFF(scan) = off;
310 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
311 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
312 next = regnext(scan);
315 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
316 I32 max1 = 0, min1 = I32_MAX, num = 0;
318 if (flags & SCF_DO_SUBSTR)
320 while (OP(scan) == code) {
321 I32 deltanext, minnext;
325 next = regnext(scan);
326 scan = NEXTOPER(scan);
328 scan = NEXTOPER(scan);
329 /* We suppose the run is continuous, last=next...*/
330 minnext = study_chunk(&scan, &deltanext, next,
334 if (max1 < minnext + deltanext)
335 max1 = minnext + deltanext;
336 if (deltanext == I32_MAX)
337 is_inf = is_inf_internal = 1;
339 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
341 if (data && (data_fake.flags & SF_HAS_EVAL))
342 data->flags |= SF_HAS_EVAL;
346 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
348 if (flags & SCF_DO_SUBSTR) {
349 data->pos_min += min1;
350 data->pos_delta += max1 - min1;
351 if (max1 != min1 || is_inf)
352 data->longest = &(data->longest_float);
355 delta += max1 - min1;
357 else if (code == BRANCHJ) /* single branch is optimized. */
358 scan = NEXTOPER(NEXTOPER(scan));
359 else /* single branch is optimized. */
360 scan = NEXTOPER(scan);
363 else if (OP(scan) == EXACT) {
364 I32 l = *OPERAND(scan);
366 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
367 unsigned char *e = s + l;
376 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
377 /* The code below prefers earlier match for fixed
378 offset, later match for variable offset. */
379 if (data->last_end == -1) { /* Update the start info. */
380 data->last_start_min = data->pos_min;
381 data->last_start_max = is_inf
382 ? I32_MAX : data->pos_min + data->pos_delta;
384 sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), *OPERAND(scan));
385 data->last_end = data->pos_min + l;
386 data->pos_min += l; /* As in the first entry. */
387 data->flags &= ~SF_BEFORE_EOL;
390 else if (PL_regkind[(U8)OP(scan)] == EXACT) {
391 I32 l = *OPERAND(scan);
392 if (flags & SCF_DO_SUBSTR)
395 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
396 unsigned char *e = s + l;
405 if (data && (flags & SCF_DO_SUBSTR))
408 else if (strchr((char*)PL_varies,OP(scan))) {
409 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
410 regnode *oscan = scan;
412 switch (PL_regkind[(U8)OP(scan)]) {
414 scan = NEXTOPER(scan);
417 if (flags & SCF_DO_SUBSTR) {
418 next = NEXTOPER(scan);
419 if (OP(next) == EXACT) {
421 maxcount = REG_INFTY;
422 next = regnext(scan);
423 scan = NEXTOPER(scan);
427 if (flags & SCF_DO_SUBSTR)
432 is_inf = is_inf_internal = 1;
433 scan = regnext(scan);
434 if (flags & SCF_DO_SUBSTR) {
436 data->longest = &(data->longest_float);
438 goto optimize_curly_tail;
440 mincount = ARG1(scan);
441 maxcount = ARG2(scan);
442 next = regnext(scan);
443 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
445 if (flags & SCF_DO_SUBSTR) {
446 if (mincount == 0) scan_commit(data);
447 pos_before = data->pos_min;
451 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
453 data->flags |= SF_IS_INF;
455 /* This will finish on WHILEM, setting scan, or on NULL: */
456 minnext = study_chunk(&scan, &deltanext, last, data,
458 ? (flags & ~SCF_DO_SUBSTR) : flags);
459 if (!scan) /* It was not CURLYX, but CURLY. */
461 if (ckWARN(WARN_UNSAFE) && (minnext + deltanext == 0)
462 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
463 && maxcount <= REG_INFTY/3) /* Complement check for big count */
464 Perl_warner(aTHX_ WARN_UNSAFE, "Strange *+?{} on zero-length expression");
465 min += minnext * mincount;
466 is_inf_internal |= (maxcount == REG_INFTY
467 && (minnext + deltanext) > 0
468 || deltanext == I32_MAX);
469 is_inf |= is_inf_internal;
470 delta += (minnext + deltanext) * maxcount - minnext * mincount;
472 /* Try powerful optimization CURLYX => CURLYN. */
473 if ( OP(oscan) == CURLYX && data
474 && data->flags & SF_IN_PAR
475 && !(data->flags & SF_HAS_EVAL)
476 && !deltanext && minnext == 1 ) {
477 /* Try to optimize to CURLYN. */
478 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
479 regnode *nxt1 = nxt, *nxt2;
483 if (!strchr((char*)PL_simple,OP(nxt))
484 && !(PL_regkind[(U8)OP(nxt)] == EXACT
485 && *OPERAND(nxt) == 1))
489 if (OP(nxt) != CLOSE)
491 /* Now we know that nxt2 is the only contents: */
492 oscan->flags = ARG(nxt);
494 OP(nxt1) = NOTHING; /* was OPEN. */
496 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
497 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
498 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
499 OP(nxt) = OPTIMIZED; /* was CLOSE. */
500 OP(nxt + 1) = OPTIMIZED; /* was count. */
501 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
506 /* Try optimization CURLYX => CURLYM. */
507 if ( OP(oscan) == CURLYX && data
508 && !(data->flags & SF_HAS_PAR)
509 && !(data->flags & SF_HAS_EVAL)
511 /* XXXX How to optimize if data == 0? */
512 /* Optimize to a simpler form. */
513 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
517 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
518 && (OP(nxt2) != WHILEM))
520 OP(nxt2) = SUCCEED; /* Whas WHILEM */
521 /* Need to optimize away parenths. */
522 if (data->flags & SF_IN_PAR) {
523 /* Set the parenth number. */
524 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
526 if (OP(nxt) != CLOSE)
527 FAIL("panic opt close");
528 oscan->flags = ARG(nxt);
529 OP(nxt1) = OPTIMIZED; /* was OPEN. */
530 OP(nxt) = OPTIMIZED; /* was CLOSE. */
532 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
533 OP(nxt + 1) = OPTIMIZED; /* was count. */
534 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
535 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
538 while ( nxt1 && (OP(nxt1) != WHILEM)) {
539 regnode *nnxt = regnext(nxt1);
542 if (reg_off_by_arg[OP(nxt1)])
543 ARG_SET(nxt1, nxt2 - nxt1);
544 else if (nxt2 - nxt1 < U16_MAX)
545 NEXT_OFF(nxt1) = nxt2 - nxt1;
547 OP(nxt) = NOTHING; /* Cannot beautify */
552 /* Optimize again: */
553 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
558 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
560 if (flags & SCF_DO_SUBSTR) {
561 SV *last_str = Nullsv;
562 int counted = mincount != 0;
564 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
565 I32 b = pos_before >= data->last_start_min
566 ? pos_before : data->last_start_min;
568 char *s = SvPV(data->last_found, l);
569 I32 old = b - data->last_start_min;
572 old = utf8_hop((U8*)s, old) - (U8*)s;
575 /* Get the added string: */
576 last_str = newSVpvn(s + old, l);
577 if (deltanext == 0 && pos_before == b) {
578 /* What was added is a constant string */
580 SvGROW(last_str, (mincount * l) + 1);
581 repeatcpy(SvPVX(last_str) + l,
582 SvPVX(last_str), l, mincount - 1);
583 SvCUR(last_str) *= mincount;
584 /* Add additional parts. */
585 SvCUR_set(data->last_found,
586 SvCUR(data->last_found) - l);
587 sv_catsv(data->last_found, last_str);
588 data->last_end += l * (mincount - 1);
592 /* It is counted once already... */
593 data->pos_min += minnext * (mincount - counted);
594 data->pos_delta += - counted * deltanext +
595 (minnext + deltanext) * maxcount - minnext * mincount;
596 if (mincount != maxcount) {
598 if (mincount && last_str) {
599 sv_setsv(data->last_found, last_str);
600 data->last_end = data->pos_min;
601 data->last_start_min =
602 data->pos_min - CHR_SVLEN(last_str);
603 data->last_start_max = is_inf
605 : data->pos_min + data->pos_delta
606 - CHR_SVLEN(last_str);
608 data->longest = &(data->longest_float);
610 SvREFCNT_dec(last_str);
612 if (data && (fl & SF_HAS_EVAL))
613 data->flags |= SF_HAS_EVAL;
615 if (OP(oscan) != CURLYX) {
616 while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
618 NEXT_OFF(oscan) += NEXT_OFF(next);
621 default: /* REF only? */
622 if (flags & SCF_DO_SUBSTR) {
624 data->longest = &(data->longest_float);
626 is_inf = is_inf_internal = 1;
630 else if (strchr((char*)PL_simple,OP(scan)) || PL_regkind[(U8)OP(scan)] == ANYUTF8) {
631 if (flags & SCF_DO_SUBSTR) {
637 else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
638 data->flags |= (OP(scan) == MEOL
642 else if (PL_regkind[(U8)OP(scan)] == BRANCHJ
643 && (scan->flags || data)
644 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
645 I32 deltanext, minnext;
649 next = regnext(scan);
650 nscan = NEXTOPER(NEXTOPER(scan));
651 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
654 FAIL("variable length lookbehind not implemented");
656 else if (minnext > U8_MAX) {
657 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
659 scan->flags = minnext;
661 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
663 if (data && (data_fake.flags & SF_HAS_EVAL))
664 data->flags |= SF_HAS_EVAL;
666 else if (OP(scan) == OPEN) {
669 else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
670 next = regnext(scan);
672 if ( next && (OP(next) != WHILEM) && next < last)
673 is_par = 0; /* Disable optimization */
675 else if (OP(scan) == EVAL) {
677 data->flags |= SF_HAS_EVAL;
679 else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded */
680 if (flags & SCF_DO_SUBSTR) {
682 data->longest = &(data->longest_float);
684 is_inf = is_inf_internal = 1;
686 /* Else: zero-length, ignore. */
687 scan = regnext(scan);
692 *deltap = is_inf_internal ? I32_MAX : delta;
693 if (flags & SCF_DO_SUBSTR && is_inf)
694 data->pos_delta = I32_MAX - data->pos_min;
697 if (is_par && pars==1 && data) {
698 data->flags |= SF_IN_PAR;
699 data->flags &= ~SF_HAS_PAR;
701 else if (pars && data) {
702 data->flags |= SF_HAS_PAR;
703 data->flags &= ~SF_IN_PAR;
709 S_add_data(pTHX_ I32 n, char *s)
712 if (PL_regcomp_rx->data) {
713 Renewc(PL_regcomp_rx->data,
714 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
715 char, struct reg_data);
716 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
717 PL_regcomp_rx->data->count += n;
720 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
721 char, struct reg_data);
722 New(1208, PL_regcomp_rx->data->what, n, U8);
723 PL_regcomp_rx->data->count = n;
725 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
726 return PL_regcomp_rx->data->count - n;
730 Perl_reginitcolors(pTHX)
734 char *s = PerlEnv_getenv("PERL_RE_COLORS");
737 PL_colors[0] = s = savepv(s);
745 PL_colors[i] = s = "";
755 - pregcomp - compile a regular expression into internal code
757 * We can't allocate space until we know how big the compiled form will be,
758 * but we can't compile it (and thus know how big it is) until we've got a
759 * place to put the code. So we cheat: we compile it twice, once with code
760 * generation turned off and size counting turned on, and once "for real".
761 * This also means that we don't allocate space until we are sure that the
762 * thing really will compile successfully, and we never have to move the
763 * code and thus invalidate pointers into it. (Note that it has to be in
764 * one piece because free() must be able to free it all.) [NB: not true in perl]
766 * Beware that the optimization-preparation code in here knows about some
767 * of the structure of the compiled regexp. [I'll say.]
770 Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
785 FAIL("NULL regexp argument");
787 if (PL_curcop == &PL_compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
788 PL_reg_flags |= RF_utf8;
792 PL_regprecomp = savepvn(exp, xend - exp);
793 DEBUG_r(if (!PL_colorset) reginitcolors());
794 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling%s RE `%s%*s%s'\n",
795 PL_colors[4],PL_colors[5],PL_colors[0],
796 xend - exp, PL_regprecomp, PL_colors[1]));
797 PL_regflags = pm->op_pmflags;
801 PL_seen_zerolen = *exp == '^' ? -1 : 0;
805 /* First pass: determine size, legality. */
806 PL_regcomp_parse = exp;
811 PL_regcode = &PL_regdummy;
812 regc((U8)REG_MAGIC, (char*)PL_regcode);
813 if (reg(0, &flags) == NULL) {
814 Safefree(PL_regprecomp);
815 PL_regprecomp = Nullch;
818 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
820 /* Small enough for pointer-storage convention?
821 If extralen==0, this means that we will not need long jumps. */
822 if (PL_regsize >= 0x10000L && PL_extralen)
823 PL_regsize += PL_extralen;
827 /* Allocate space and initialize. */
828 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
831 FAIL("regexp out of space");
833 r->prelen = xend - exp;
834 r->precomp = PL_regprecomp;
836 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
837 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
839 r->substrs = 0; /* Useful during FAIL. */
840 r->startp = 0; /* Useful during FAIL. */
841 r->endp = 0; /* Useful during FAIL. */
845 /* Second pass: emit code. */
846 PL_regcomp_parse = exp;
850 PL_regcode = r->program;
851 /* Store the count of eval-groups for security checks: */
852 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
853 regc((U8)REG_MAGIC, (char*) PL_regcode++);
855 if (reg(0, &flags) == NULL)
858 /* Dig out information for optimizations. */
859 r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
860 pm->op_pmflags = PL_regflags;
862 r->reganch |= ROPT_UTF8;
863 r->regstclass = NULL;
864 if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
865 r->reganch |= ROPT_NAUGHTY;
866 scan = r->program + 1; /* First BRANCH. */
868 /* XXXX To minimize changes to RE engine we always allocate
869 3-units-long substrs field. */
870 Newz(1004, r->substrs, 1, struct reg_substr_data);
872 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
875 STRLEN longest_float_length, longest_fixed_length;
877 StructCopy(&zero_scan_data, &data, scan_data_t);
879 /* Skip introductions and multiplicators >= 1. */
880 while ((OP(first) == OPEN && (sawopen = 1)) ||
881 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
882 (OP(first) == PLUS) ||
883 (OP(first) == MINMOD) ||
884 (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
885 if (OP(first) == PLUS)
888 first += regarglen[(U8)OP(first)];
889 first = NEXTOPER(first);
892 /* Starting-point info. */
894 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
895 else if (strchr((char*)PL_simple+4,OP(first)))
896 r->regstclass = first;
897 else if (PL_regkind[(U8)OP(first)] == BOUND ||
898 PL_regkind[(U8)OP(first)] == NBOUND)
899 r->regstclass = first;
900 else if (PL_regkind[(U8)OP(first)] == BOL) {
901 r->reganch |= (OP(first) == MBOL
906 first = NEXTOPER(first);
909 else if (OP(first) == GPOS) {
910 r->reganch |= ROPT_ANCH_GPOS;
911 first = NEXTOPER(first);
914 else if ((OP(first) == STAR &&
915 PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
916 !(r->reganch & ROPT_ANCH) )
918 /* turn .* into ^.* with an implied $*=1 */
919 int type = OP(NEXTOPER(first));
921 if (type == REG_ANY || type == ANYUTF8)
922 type = ROPT_ANCH_MBOL;
924 type = ROPT_ANCH_SBOL;
926 r->reganch |= type | ROPT_IMPLICIT;
927 first = NEXTOPER(first);
930 if (sawplus && (!sawopen || !PL_regsawback)
931 && !(PL_regseen & REG_SEEN_EVAL)) /* May examine pos and $& */
932 /* x+ must match at the 1st pos of run of x's */
933 r->reganch |= ROPT_SKIP;
935 /* Scan is after the zeroth branch, first is atomic matcher. */
936 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
939 * If there's something expensive in the r.e., find the
940 * longest literal string that must appear and make it the
941 * regmust. Resolve ties in favor of later strings, since
942 * the regstart check works with the beginning of the r.e.
943 * and avoiding duplication strengthens checking. Not a
944 * strong reason, but sufficient in the absence of others.
945 * [Now we resolve ties in favor of the earlier string if
946 * it happens that c_offset_min has been invalidated, since the
947 * earlier string may buy us something the later one won't.]
951 data.longest_fixed = newSVpvn("",0);
952 data.longest_float = newSVpvn("",0);
953 data.last_found = newSVpvn("",0);
954 data.longest = &(data.longest_fixed);
957 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
958 &data, SCF_DO_SUBSTR);
959 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
960 && data.last_start_min == 0 && data.last_end > 0
962 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
963 r->reganch |= ROPT_CHECK_ALL;
965 SvREFCNT_dec(data.last_found);
967 longest_float_length = CHR_SVLEN(data.longest_float);
968 if (longest_float_length
969 || (data.flags & SF_FL_BEFORE_EOL
970 && (!(data.flags & SF_FL_BEFORE_MEOL)
971 || (PL_regflags & PMf_MULTILINE)))) {
974 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
975 && data.offset_fixed == data.offset_float_min
976 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
977 goto remove_float; /* As in (a)+. */
979 r->float_substr = data.longest_float;
980 r->float_min_offset = data.offset_float_min;
981 r->float_max_offset = data.offset_float_max;
982 t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
983 && (!(data.flags & SF_FL_BEFORE_MEOL)
984 || (PL_regflags & PMf_MULTILINE)));
985 fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
989 r->float_substr = Nullsv;
990 SvREFCNT_dec(data.longest_float);
991 longest_float_length = 0;
994 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
995 if (longest_fixed_length
996 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
997 && (!(data.flags & SF_FIX_BEFORE_MEOL)
998 || (PL_regflags & PMf_MULTILINE)))) {
1001 r->anchored_substr = data.longest_fixed;
1002 r->anchored_offset = data.offset_fixed;
1003 t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
1004 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1005 || (PL_regflags & PMf_MULTILINE)));
1006 fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
1009 r->anchored_substr = Nullsv;
1010 SvREFCNT_dec(data.longest_fixed);
1011 longest_fixed_length = 0;
1014 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1015 if (longest_fixed_length > longest_float_length) {
1016 r->check_substr = r->anchored_substr;
1017 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1018 if (r->reganch & ROPT_ANCH_SINGLE)
1019 r->reganch |= ROPT_NOSCAN;
1022 r->check_substr = r->float_substr;
1023 r->check_offset_min = data.offset_float_min;
1024 r->check_offset_max = data.offset_float_max;
1026 if (r->check_substr) {
1027 r->reganch |= RE_USE_INTUIT;
1028 if (SvTAIL(r->check_substr))
1029 r->reganch |= RE_INTUIT_TAIL;
1033 /* Several toplevels. Best we can is to set minlen. */
1036 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1037 scan = r->program + 1;
1038 minlen = study_chunk(&scan, &fake, scan + PL_regsize, NULL, 0);
1039 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1043 if (PL_regseen & REG_SEEN_GPOS)
1044 r->reganch |= ROPT_GPOS_SEEN;
1045 if (PL_regseen & REG_SEEN_LOOKBEHIND)
1046 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1047 if (PL_regseen & REG_SEEN_EVAL)
1048 r->reganch |= ROPT_EVAL_SEEN;
1049 Newz(1002, r->startp, PL_regnpar, I32);
1050 Newz(1002, r->endp, PL_regnpar, I32);
1051 DEBUG_r(regdump(r));
1056 - reg - regular expression, i.e. main body or parenthesized thing
1058 * Caller must absorb opening parenthesis.
1060 * Combining parenthesis handling with the base level of regular expression
1061 * is a trifle forced, but the need to tie the tails of the branches to what
1062 * follows makes it hard to avoid.
1065 S_reg(pTHX_ I32 paren, I32 *flagp)
1066 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1069 register regnode *ret; /* Will be the head of the group. */
1070 register regnode *br;
1071 register regnode *lastbr;
1072 register regnode *ender = 0;
1073 register I32 parno = 0;
1074 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
1077 *flagp = 0; /* Tentatively. */
1079 /* Make an OPEN node, if parenthesized. */
1081 if (*PL_regcomp_parse == '?') {
1082 U16 posflags = 0, negflags = 0;
1083 U16 *flagsp = &posflags;
1087 paren = *PL_regcomp_parse++;
1088 ret = NULL; /* For look-ahead/behind. */
1091 PL_regseen |= REG_SEEN_LOOKBEHIND;
1092 if (*PL_regcomp_parse == '!')
1094 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
1105 FAIL2("Sequence (?%c...) not implemented", (int)paren);
1108 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1110 if (*PL_regcomp_parse != ')')
1111 FAIL("Sequence (?#... not terminated");
1117 paren = *PL_regcomp_parse++;
1122 I32 count = 1, n = 0;
1124 char *s = PL_regcomp_parse;
1126 OP_4tree *sop, *rop;
1129 PL_regseen |= REG_SEEN_EVAL;
1130 while (count && (c = *PL_regcomp_parse)) {
1131 if (c == '\\' && PL_regcomp_parse[1])
1139 if (*PL_regcomp_parse != ')')
1140 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1144 if (PL_regcomp_parse - 1 - s)
1145 sv = newSVpvn(s, PL_regcomp_parse - 1 - s);
1147 sv = newSVpvn("", 0);
1149 rop = sv_compile_2op(sv, &sop, "re", &av);
1151 n = add_data(3, "nop");
1152 PL_regcomp_rx->data->data[n] = (void*)rop;
1153 PL_regcomp_rx->data->data[n+1] = (void*)sop;
1154 PL_regcomp_rx->data->data[n+2] = (void*)av;
1157 else { /* First pass */
1158 if (PL_reginterp_cnt < ++PL_seen_evals
1159 && PL_curcop != &PL_compiling)
1160 /* No compiled RE interpolated, has runtime
1161 components ===> unsafe. */
1162 FAIL("Eval-group not allowed at runtime, use re 'eval'");
1164 FAIL("Eval-group in insecure regular expression");
1169 ret = reg_node(LOGICAL);
1172 regtail(ret, reganode(EVAL, n));
1175 return reganode(EVAL, n);
1179 if (PL_regcomp_parse[0] == '?') {
1180 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1181 || PL_regcomp_parse[1] == '<'
1182 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
1185 ret = reg_node(LOGICAL);
1188 regtail(ret, reg(1, &flag));
1192 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
1193 parno = atoi(PL_regcomp_parse++);
1195 while (isDIGIT(*PL_regcomp_parse))
1197 ret = reganode(GROUPP, parno);
1198 if ((c = *nextchar()) != ')')
1199 FAIL2("Switch (?(number%c not recognized", c);
1201 regtail(ret, reganode(IFTHEN, 0));
1202 br = regbranch(&flags, 1);
1204 br = reganode(LONGJMP, 0);
1206 regtail(br, reganode(LONGJMP, 0));
1211 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1212 regbranch(&flags, 1);
1213 regtail(ret, lastbr);
1221 FAIL("Switch (?(condition)... contains too many branches");
1222 ender = reg_node(TAIL);
1225 regtail(lastbr, ender);
1226 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1229 regtail(ret, ender);
1233 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
1237 FAIL("Sequence (? incomplete");
1242 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1243 if (*PL_regcomp_parse != 'o')
1244 pmflag(flagsp, *PL_regcomp_parse);
1247 if (*PL_regcomp_parse == '-') {
1252 PL_regflags |= posflags;
1253 PL_regflags &= ~negflags;
1254 if (*PL_regcomp_parse == ':') {
1260 if (*PL_regcomp_parse != ')')
1261 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
1270 ret = reganode(OPEN, parno);
1277 /* Pick up the branches, linking them together. */
1278 br = regbranch(&flags, 1);
1281 if (*PL_regcomp_parse == '|') {
1282 if (!SIZE_ONLY && PL_extralen) {
1283 reginsert(BRANCHJ, br);
1286 reginsert(BRANCH, br);
1289 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
1291 else if (paren == ':') {
1292 *flagp |= flags&SIMPLE;
1294 if (open) { /* Starts with OPEN. */
1295 regtail(ret, br); /* OPEN -> first. */
1297 else if (paren != '?') /* Not Conditional */
1301 *flagp |= flags&SPSTART;
1303 while (*PL_regcomp_parse == '|') {
1304 if (!SIZE_ONLY && PL_extralen) {
1305 ender = reganode(LONGJMP,0);
1306 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1309 PL_extralen += 2; /* Account for LONGJMP. */
1311 br = regbranch(&flags, 0);
1314 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1318 *flagp |= flags&SPSTART;
1321 if (have_branch || paren != ':') {
1322 /* Make a closing node, and hook it on the end. */
1325 ender = reg_node(TAIL);
1328 ender = reganode(CLOSE, parno);
1334 *flagp &= ~HASWIDTH;
1337 ender = reg_node(SUCCEED);
1340 ender = reg_node(END);
1343 regtail(lastbr, ender);
1346 /* Hook the tails of the branches to the closing node. */
1347 for (br = ret; br != NULL; br = regnext(br)) {
1348 regoptail(br, ender);
1355 static char parens[] = "=!<,>";
1357 if (paren && (p = strchr(parens, paren))) {
1358 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1359 int flag = (p - parens) > 1;
1362 node = SUSPEND, flag = 0;
1363 reginsert(node,ret);
1365 regtail(ret, reg_node(TAIL));
1369 /* Check for proper termination. */
1371 PL_regflags = oregflags;
1372 if (PL_regcomp_parse >= PL_regxend || *nextchar() != ')') {
1373 FAIL("unmatched () in regexp");
1376 else if (!paren && PL_regcomp_parse < PL_regxend) {
1377 if (*PL_regcomp_parse == ')') {
1378 FAIL("unmatched () in regexp");
1381 FAIL("junk on end of regexp"); /* "Can't happen". */
1389 - regbranch - one alternative of an | operator
1391 * Implements the concatenation operator.
1394 S_regbranch(pTHX_ I32 *flagp, I32 first)
1397 register regnode *ret;
1398 register regnode *chain = NULL;
1399 register regnode *latest;
1400 I32 flags = 0, c = 0;
1405 if (!SIZE_ONLY && PL_extralen)
1406 ret = reganode(BRANCHJ,0);
1408 ret = reg_node(BRANCH);
1411 if (!first && SIZE_ONLY)
1412 PL_extralen += 1; /* BRANCHJ */
1414 *flagp = WORST; /* Tentatively. */
1418 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
1420 latest = regpiece(&flags);
1421 if (latest == NULL) {
1422 if (flags & TRYAGAIN)
1426 else if (ret == NULL)
1428 *flagp |= flags&HASWIDTH;
1429 if (chain == NULL) /* First piece. */
1430 *flagp |= flags&SPSTART;
1433 regtail(chain, latest);
1438 if (chain == NULL) { /* Loop ran zero times. */
1439 chain = reg_node(NOTHING);
1444 *flagp |= flags&SIMPLE;
1451 - regpiece - something followed by possible [*+?]
1453 * Note that the branching code sequences used for ? and the general cases
1454 * of * and + are somewhat optimized: they use the same NOTHING node as
1455 * both the endmarker for their branch list and the body of the last branch.
1456 * It might seem that this node could be dispensed with entirely, but the
1457 * endmarker role is not redundant.
1460 S_regpiece(pTHX_ I32 *flagp)
1463 register regnode *ret;
1465 register char *next;
1467 char *origparse = PL_regcomp_parse;
1470 I32 max = REG_INFTY;
1472 ret = regatom(&flags);
1474 if (flags & TRYAGAIN)
1479 op = *PL_regcomp_parse;
1481 if (op == '{' && regcurly(PL_regcomp_parse)) {
1482 next = PL_regcomp_parse + 1;
1484 while (isDIGIT(*next) || *next == ',') {
1493 if (*next == '}') { /* got one */
1497 min = atoi(PL_regcomp_parse);
1501 maxpos = PL_regcomp_parse;
1503 if (!max && *maxpos != '0')
1504 max = REG_INFTY; /* meaning "infinity" */
1505 else if (max >= REG_INFTY)
1506 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
1507 PL_regcomp_parse = next;
1511 if ((flags&SIMPLE)) {
1512 PL_regnaughty += 2 + PL_regnaughty / 2;
1513 reginsert(CURLY, ret);
1516 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
1517 regtail(ret, reg_node(WHILEM));
1518 if (!SIZE_ONLY && PL_extralen) {
1519 reginsert(LONGJMP,ret);
1520 reginsert(NOTHING,ret);
1521 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1523 reginsert(CURLYX,ret);
1524 if (!SIZE_ONLY && PL_extralen)
1525 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1526 regtail(ret, reg_node(NOTHING));
1536 if (max && max < min)
1537 FAIL("Can't do {n,m} with n > m");
1552 #if 0 /* Now runtime fix should be reliable. */
1553 if (!(flags&HASWIDTH) && op != '?')
1554 FAIL("regexp *+ operand could be empty");
1559 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
1561 if (op == '*' && (flags&SIMPLE)) {
1562 reginsert(STAR, ret);
1566 else if (op == '*') {
1570 else if (op == '+' && (flags&SIMPLE)) {
1571 reginsert(PLUS, ret);
1575 else if (op == '+') {
1579 else if (op == '?') {
1584 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
1585 Perl_warner(aTHX_ WARN_UNSAFE, "%.*s matches null string many times",
1586 PL_regcomp_parse - origparse, origparse);
1589 if (*PL_regcomp_parse == '?') {
1591 reginsert(MINMOD, ret);
1592 regtail(ret, ret + NODE_STEP_REGNODE);
1594 if (ISMULT2(PL_regcomp_parse))
1595 FAIL("nested *?+ in regexp");
1601 - regatom - the lowest level
1603 * Optimization: gobbles an entire sequence of ordinary characters so that
1604 * it can turn them into a single node, which is smaller to store and
1605 * faster to run. Backslashed characters are exceptions, each becoming a
1606 * separate node; the code is simpler that way and it's not worth fixing.
1608 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1611 S_regatom(pTHX_ I32 *flagp)
1614 register regnode *ret = 0;
1617 *flagp = WORST; /* Tentatively. */
1620 switch (*PL_regcomp_parse) {
1624 if (PL_regflags & PMf_MULTILINE)
1625 ret = reg_node(MBOL);
1626 else if (PL_regflags & PMf_SINGLELINE)
1627 ret = reg_node(SBOL);
1629 ret = reg_node(BOL);
1632 if (PL_regcomp_parse[1])
1635 if (PL_regflags & PMf_MULTILINE)
1636 ret = reg_node(MEOL);
1637 else if (PL_regflags & PMf_SINGLELINE)
1638 ret = reg_node(SEOL);
1640 ret = reg_node(EOL);
1645 if (PL_regflags & PMf_SINGLELINE)
1646 ret = reg_node(SANYUTF8);
1648 ret = reg_node(ANYUTF8);
1652 if (PL_regflags & PMf_SINGLELINE)
1653 ret = reg_node(SANY);
1655 ret = reg_node(REG_ANY);
1656 *flagp |= HASWIDTH|SIMPLE;
1662 ret = (UTF ? regclassutf8() : regclass());
1663 if (*PL_regcomp_parse != ']')
1664 FAIL("unmatched [] in regexp");
1666 *flagp |= HASWIDTH|SIMPLE;
1670 ret = reg(1, &flags);
1672 if (flags & TRYAGAIN)
1676 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
1680 if (flags & TRYAGAIN) {
1684 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
1685 /* Supposed to be caught earlier. */
1688 if (!regcurly(PL_regcomp_parse)) {
1696 FAIL("?+*{} follows nothing in regexp");
1699 switch (*++PL_regcomp_parse) {
1702 ret = reg_node(SBOL);
1707 ret = reg_node(GPOS);
1708 PL_regseen |= REG_SEEN_GPOS;
1713 ret = reg_node(SEOL);
1718 ret = reg_node(EOS);
1720 PL_seen_zerolen++; /* Do not optimize RE away */
1724 ret = reg_node(SANY);
1725 *flagp |= HASWIDTH|SIMPLE;
1729 ret = reg_node(CLUMP);
1732 if (UTF && !PL_utf8_mark)
1733 is_utf8_mark((U8*)"~"); /* preload table */
1738 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
1739 : (LOC ? ALNUML : ALNUM));
1740 *flagp |= HASWIDTH|SIMPLE;
1742 if (UTF && !PL_utf8_alnum)
1743 is_utf8_alnum((U8*)"a"); /* preload table */
1748 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
1749 : (LOC ? NALNUML : NALNUM));
1750 *flagp |= HASWIDTH|SIMPLE;
1752 if (UTF && !PL_utf8_alnum)
1753 is_utf8_alnum((U8*)"a"); /* preload table */
1757 PL_regseen |= REG_SEEN_LOOKBEHIND;
1760 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
1761 : (LOC ? BOUNDL : BOUND));
1764 if (UTF && !PL_utf8_alnum)
1765 is_utf8_alnum((U8*)"a"); /* preload table */
1769 PL_regseen |= REG_SEEN_LOOKBEHIND;
1772 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
1773 : (LOC ? NBOUNDL : NBOUND));
1776 if (UTF && !PL_utf8_alnum)
1777 is_utf8_alnum((U8*)"a"); /* preload table */
1782 ? (LOC ? SPACELUTF8 : SPACEUTF8)
1783 : (LOC ? SPACEL : SPACE));
1784 *flagp |= HASWIDTH|SIMPLE;
1786 if (UTF && !PL_utf8_space)
1787 is_utf8_space((U8*)" "); /* preload table */
1792 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
1793 : (LOC ? NSPACEL : NSPACE));
1794 *flagp |= HASWIDTH|SIMPLE;
1796 if (UTF && !PL_utf8_space)
1797 is_utf8_space((U8*)" "); /* preload table */
1800 ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
1801 *flagp |= HASWIDTH|SIMPLE;
1803 if (UTF && !PL_utf8_digit)
1804 is_utf8_digit((U8*)"1"); /* preload table */
1807 ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
1808 *flagp |= HASWIDTH|SIMPLE;
1810 if (UTF && !PL_utf8_digit)
1811 is_utf8_digit((U8*)"1"); /* preload table */
1815 { /* a lovely hack--pretend we saw [\pX] instead */
1816 char* oldregxend = PL_regxend;
1818 if (PL_regcomp_parse[1] == '{') {
1819 PL_regxend = strchr(PL_regcomp_parse, '}');
1821 FAIL("Missing right brace on \\p{}");
1825 PL_regxend = PL_regcomp_parse + 2;
1828 ret = regclassutf8();
1830 PL_regxend = oldregxend;
1833 *flagp |= HASWIDTH|SIMPLE;
1846 case '1': case '2': case '3': case '4':
1847 case '5': case '6': case '7': case '8': case '9':
1849 I32 num = atoi(PL_regcomp_parse);
1851 if (num > 9 && num >= PL_regnpar)
1854 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
1855 FAIL("reference to nonexistent group");
1858 ? (LOC ? REFFL : REFF)
1861 while (isDIGIT(*PL_regcomp_parse))
1869 if (PL_regcomp_parse >= PL_regxend)
1870 FAIL("trailing \\ in regexp");
1873 /* Do not generate `unrecognized' warnings here, we fall
1874 back into the quick-grab loop below */
1880 if (PL_regflags & PMf_EXTENDED) {
1881 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1882 if (PL_regcomp_parse < PL_regxend)
1898 ? (LOC ? EXACTFL : EXACTF)
1900 s = (char *) OPERAND(ret);
1901 regc(0, s++); /* save spot for len */
1902 for (len = 0, p = PL_regcomp_parse - 1;
1903 len < 127 && p < PL_regxend;
1908 if (PL_regflags & PMf_EXTENDED)
1909 p = regwhite(p, PL_regxend);
1963 char* e = strchr(p, '}');
1966 FAIL("Missing right brace on \\x{}");
1968 ender = scan_hex(p + 1, e - p, &numlen);
1969 if (numlen + len >= 127) { /* numlen is generous */
1976 FAIL("Can't use \\x{} without 'use utf8' declaration");
1979 ender = scan_hex(p, 2, &numlen);
1985 ender = UCHARAT(p++);
1986 ender = toCTRL(ender);
1988 case '0': case '1': case '2': case '3':case '4':
1989 case '5': case '6': case '7': case '8':case '9':
1991 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
1992 ender = scan_oct(p, 3, &numlen);
2001 if (p >= PL_regxend)
2002 FAIL("trailing \\ in regexp");
2005 if (!SIZE_ONLY && ckWARN(WARN_UNSAFE) && isALPHA(*p))
2006 Perl_warner(aTHX_ WARN_UNSAFE,
2007 "/%.127s/: Unrecognized escape \\%c passed through",
2010 goto normal_default;
2015 if ((*p & 0xc0) == 0xc0 && UTF) {
2016 ender = utf8_to_uv((U8*)p, &numlen);
2023 if (PL_regflags & PMf_EXTENDED)
2024 p = regwhite(p, PL_regxend);
2027 ender = toLOWER_LC_uni(ender);
2029 ender = toLOWER_uni(ender);
2031 if (ISMULT2(p)) { /* Back off on ?+*. */
2034 else if (ender >= 0x80 && UTF) {
2035 reguni(ender, s, &numlen);
2045 if (ender >= 0x80 && UTF) {
2046 reguni(ender, s, &numlen);
2054 PL_regcomp_parse = p - 1;
2057 FAIL("internal disaster in regexp");
2063 *OPERAND(ret) = len;
2066 PL_regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2069 PL_regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2079 S_regwhite(pTHX_ char *p, char *e)
2084 else if (*p == '#') {
2087 } while (p < e && *p != '\n');
2095 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
2096 Character classes ([:foo:]) can also be negated ([:^foo:]).
2097 Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
2098 Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
2099 but trigger warnings because they are currently unimplemented. */
2101 S_regpposixcc(pTHX_ I32 value)
2105 I32 namedclass = -1;
2107 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2108 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2109 (*PL_regcomp_parse == ':' ||
2110 *PL_regcomp_parse == '=' ||
2111 *PL_regcomp_parse == '.')) {
2112 char c = *PL_regcomp_parse;
2113 char* s = PL_regcomp_parse++;
2115 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != c)
2117 if (PL_regcomp_parse == PL_regxend)
2118 /* Grandfather lone [:, [=, [. */
2119 PL_regcomp_parse = s;
2121 char* t = PL_regcomp_parse++; /* skip over the c */
2123 if (*PL_regcomp_parse == ']') {
2124 PL_regcomp_parse++; /* skip over the ending ] */
2127 I32 complement = *posixcc == '^' ? *posixcc++ : 0;
2128 I32 skip = 5; /* the most common skip */
2132 if (strnEQ(posixcc, "alnum", 5))
2134 complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
2135 else if (strnEQ(posixcc, "alpha", 5))
2137 complement ? ANYOF_NALPHA : ANYOF_ALPHA;
2138 else if (strnEQ(posixcc, "ascii", 5))
2140 complement ? ANYOF_NASCII : ANYOF_ASCII;
2143 if (strnEQ(posixcc, "cntrl", 5))
2145 complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
2148 if (strnEQ(posixcc, "digit", 5))
2150 complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
2153 if (strnEQ(posixcc, "graph", 5))
2155 complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
2158 if (strnEQ(posixcc, "lower", 5))
2160 complement ? ANYOF_NLOWER : ANYOF_LOWER;
2163 if (strnEQ(posixcc, "print", 5))
2165 complement ? ANYOF_NPRINT : ANYOF_PRINT;
2166 else if (strnEQ(posixcc, "punct", 5))
2168 complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
2171 if (strnEQ(posixcc, "space", 5))
2173 complement ? ANYOF_NSPACE : ANYOF_SPACE;
2175 if (strnEQ(posixcc, "upper", 5))
2177 complement ? ANYOF_NUPPER : ANYOF_UPPER;
2179 case 'w': /* this is not POSIX, this is the Perl \w */
2180 if (strnEQ(posixcc, "word", 4)) {
2182 complement ? ANYOF_NALNUM : ANYOF_ALNUM;
2187 if (strnEQ(posixcc, "xdigit", 6)) {
2189 complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
2194 if ((namedclass == -1 ||
2195 !(posixcc + skip + 2 < PL_regxend &&
2196 (posixcc[skip] == ':' &&
2197 posixcc[skip + 1] == ']'))))
2198 Perl_croak(aTHX_ "Character class [:%.*s:] unknown",
2200 } else if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
2201 /* [[=foo=]] and [[.foo.]] are still future. */
2202 Perl_warner(aTHX_ WARN_UNSAFE,
2203 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2205 /* Maternal grandfather:
2206 * "[:" ending in ":" but not in ":]" */
2207 PL_regcomp_parse = s;
2216 S_checkposixcc(pTHX)
2218 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY &&
2219 (*PL_regcomp_parse == ':' ||
2220 *PL_regcomp_parse == '=' ||
2221 *PL_regcomp_parse == '.')) {
2222 char *s = PL_regcomp_parse;
2225 while(*s && isALNUM(*s))
2227 if (*s && c == *s && s[1] == ']') {
2228 Perl_warner(aTHX_ WARN_UNSAFE,
2229 "Character class syntax [%c %c] belongs inside character classes", c, c);
2230 if (c == '=' || c == '.')
2231 Perl_warner(aTHX_ WARN_UNSAFE,
2232 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2241 register char *opnd, *s;
2243 register I32 lastvalue = OOB_CHAR8;
2244 register I32 range = 0;
2245 register regnode *ret;
2250 s = opnd = (char *) OPERAND(PL_regcode);
2251 ret = reg_node(ANYOF);
2252 for (value = 0; value < ANYOF_SIZE; value++)
2254 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2258 ANYOF_FLAGS(opnd) |= ANYOF_INVERT;
2261 PL_regcode += ANY_SKIP;
2263 ANYOF_FLAGS(opnd) |= ANYOF_FOLD;
2265 ANYOF_FLAGS(opnd) |= ANYOF_LOCALE;
2268 PL_regsize += ANY_SKIP;
2273 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2274 goto skipcond; /* allow 1st char to be ] or - */
2275 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2278 value = UCHARAT(PL_regcomp_parse++);
2280 namedclass = regpposixcc(value);
2281 else if (value == '\\') {
2282 value = UCHARAT(PL_regcomp_parse++);
2284 case 'w': namedclass = ANYOF_ALNUM; break;
2285 case 'W': namedclass = ANYOF_NALNUM; break;
2286 case 's': namedclass = ANYOF_SPACE; break;
2287 case 'S': namedclass = ANYOF_NSPACE; break;
2288 case 'd': namedclass = ANYOF_DIGIT; break;
2289 case 'D': namedclass = ANYOF_NDIGIT; break;
2290 case 'n': value = '\n'; break;
2291 case 'r': value = '\r'; break;
2292 case 't': value = '\t'; break;
2293 case 'f': value = '\f'; break;
2294 case 'b': value = '\b'; break;
2295 case 'e': value = '\033'; break;
2296 case 'a': value = '\007'; break;
2298 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2299 PL_regcomp_parse += numlen;
2302 value = UCHARAT(PL_regcomp_parse++);
2303 value = toCTRL(value);
2305 case '0': case '1': case '2': case '3': case '4':
2306 case '5': case '6': case '7': case '8': case '9':
2307 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2308 PL_regcomp_parse += numlen;
2312 if (!SIZE_ONLY && namedclass > -1) {
2313 switch (namedclass) {
2316 ANYOF_CLASS_SET(opnd, ANYOF_ALNUM);
2318 for (value = 0; value < 256; value++)
2320 ANYOF_BITMAP_SET(opnd, value);
2325 ANYOF_CLASS_SET(opnd, ANYOF_NALNUM);
2327 for (value = 0; value < 256; value++)
2328 if (!isALNUM(value))
2329 ANYOF_BITMAP_SET(opnd, value);
2334 ANYOF_CLASS_SET(opnd, ANYOF_SPACE);
2336 for (value = 0; value < 256; value++)
2338 ANYOF_BITMAP_SET(opnd, value);
2343 ANYOF_CLASS_SET(opnd, ANYOF_NSPACE);
2345 for (value = 0; value < 256; value++)
2346 if (!isSPACE(value))
2347 ANYOF_BITMAP_SET(opnd, value);
2352 ANYOF_CLASS_SET(opnd, ANYOF_DIGIT);
2354 for (value = '0'; value <= '9'; value++)
2355 ANYOF_BITMAP_SET(opnd, value);
2360 ANYOF_CLASS_SET(opnd, ANYOF_NDIGIT);
2362 for (value = 0; value < '0'; value++)
2363 ANYOF_BITMAP_SET(opnd, value);
2364 for (value = '9' + 1; value < 256; value++)
2365 ANYOF_BITMAP_SET(opnd, value);
2370 ANYOF_CLASS_SET(opnd, ANYOF_NALNUMC);
2372 for (value = 0; value < 256; value++)
2373 if (!isALNUMC(value))
2374 ANYOF_BITMAP_SET(opnd, value);
2379 ANYOF_CLASS_SET(opnd, ANYOF_ALNUMC);
2381 for (value = 0; value < 256; value++)
2382 if (isALNUMC(value))
2383 ANYOF_BITMAP_SET(opnd, value);
2388 ANYOF_CLASS_SET(opnd, ANYOF_ALPHA);
2390 for (value = 0; value < 256; value++)
2392 ANYOF_BITMAP_SET(opnd, value);
2397 ANYOF_CLASS_SET(opnd, ANYOF_NALPHA);
2399 for (value = 0; value < 256; value++)
2400 if (!isALPHA(value))
2401 ANYOF_BITMAP_SET(opnd, value);
2406 ANYOF_CLASS_SET(opnd, ANYOF_ASCII);
2408 for (value = 0; value < 128; value++)
2409 ANYOF_BITMAP_SET(opnd, value);
2414 ANYOF_CLASS_SET(opnd, ANYOF_NASCII);
2416 for (value = 128; value < 256; value++)
2417 ANYOF_BITMAP_SET(opnd, value);
2422 ANYOF_CLASS_SET(opnd, ANYOF_CNTRL);
2424 for (value = 0; value < 256; value++)
2426 ANYOF_BITMAP_SET(opnd, value);
2428 lastvalue = OOB_CHAR8;
2432 ANYOF_CLASS_SET(opnd, ANYOF_NCNTRL);
2434 for (value = 0; value < 256; value++)
2435 if (!isCNTRL(value))
2436 ANYOF_BITMAP_SET(opnd, value);
2441 ANYOF_CLASS_SET(opnd, ANYOF_GRAPH);
2443 for (value = 0; value < 256; value++)
2445 ANYOF_BITMAP_SET(opnd, value);
2450 ANYOF_CLASS_SET(opnd, ANYOF_NGRAPH);
2452 for (value = 0; value < 256; value++)
2453 if (!isGRAPH(value))
2454 ANYOF_BITMAP_SET(opnd, value);
2459 ANYOF_CLASS_SET(opnd, ANYOF_LOWER);
2461 for (value = 0; value < 256; value++)
2463 ANYOF_BITMAP_SET(opnd, value);
2468 ANYOF_CLASS_SET(opnd, ANYOF_NLOWER);
2470 for (value = 0; value < 256; value++)
2471 if (!isLOWER(value))
2472 ANYOF_BITMAP_SET(opnd, value);
2477 ANYOF_CLASS_SET(opnd, ANYOF_PRINT);
2479 for (value = 0; value < 256; value++)
2481 ANYOF_BITMAP_SET(opnd, value);
2486 ANYOF_CLASS_SET(opnd, ANYOF_NPRINT);
2488 for (value = 0; value < 256; value++)
2489 if (!isPRINT(value))
2490 ANYOF_BITMAP_SET(opnd, value);
2495 ANYOF_CLASS_SET(opnd, ANYOF_PUNCT);
2497 for (value = 0; value < 256; value++)
2499 ANYOF_BITMAP_SET(opnd, value);
2504 ANYOF_CLASS_SET(opnd, ANYOF_NPUNCT);
2506 for (value = 0; value < 256; value++)
2507 if (!isPUNCT(value))
2508 ANYOF_BITMAP_SET(opnd, value);
2513 ANYOF_CLASS_SET(opnd, ANYOF_UPPER);
2515 for (value = 0; value < 256; value++)
2517 ANYOF_BITMAP_SET(opnd, value);
2522 ANYOF_CLASS_SET(opnd, ANYOF_NUPPER);
2524 for (value = 0; value < 256; value++)
2525 if (!isUPPER(value))
2526 ANYOF_BITMAP_SET(opnd, value);
2531 ANYOF_CLASS_SET(opnd, ANYOF_XDIGIT);
2533 for (value = 0; value < 256; value++)
2534 if (isXDIGIT(value))
2535 ANYOF_BITMAP_SET(opnd, value);
2540 ANYOF_CLASS_SET(opnd, ANYOF_NXDIGIT);
2542 for (value = 0; value < 256; value++)
2543 if (!isXDIGIT(value))
2544 ANYOF_BITMAP_SET(opnd, value);
2548 FAIL("invalid [::] class in regexp");
2552 ANYOF_FLAGS(opnd) |= ANYOF_CLASS;
2553 lastvalue = OOB_CHAR8;
2557 if (lastvalue > value)
2558 FAIL("invalid [] range in regexp");
2563 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2564 PL_regcomp_parse[1] != ']') {
2567 continue; /* do it next time */
2572 if ((isLOWER(lastvalue) && isLOWER(value)) ||
2573 (isUPPER(lastvalue) && isUPPER(value)))
2576 if (isLOWER(lastvalue)) {
2577 for (i = lastvalue; i <= value; i++)
2579 ANYOF_BITMAP_SET(opnd, i);
2581 for (i = lastvalue; i <= value; i++)
2583 ANYOF_BITMAP_SET(opnd, i);
2588 for ( ; lastvalue <= value; lastvalue++)
2589 ANYOF_BITMAP_SET(opnd, lastvalue);
2593 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2595 (ANYOF_FLAGS(opnd) & (ANYOF_FLAGS_ALL ^ ANYOF_INVERT)) == ANYOF_FOLD) {
2596 for (value = 0; value < 256; ++value) {
2597 if (ANYOF_BITMAP_TEST(opnd, value)) {
2598 I32 cf = PL_fold[value];
2599 ANYOF_BITMAP_SET(opnd, cf);
2602 ANYOF_FLAGS(opnd) &= ~ANYOF_FOLD;
2604 /* optimize inverted simple patterns (e.g. [^a-z]) */
2605 if (!SIZE_ONLY && (ANYOF_FLAGS(opnd) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
2606 for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
2607 opnd[ANYOF_BITMAP_OFFSET + value] ^= ANYOF_FLAGS_ALL;
2608 ANYOF_FLAGS(opnd) = 0;
2614 S_regclassutf8(pTHX)
2617 register char *opnd, *e;
2619 register U32 lastvalue = OOB_UTF8;
2620 register I32 range = 0;
2621 register regnode *ret;
2628 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2632 flags |= ANYOF_INVERT;
2636 flags |= ANYOF_FOLD;
2638 flags |= ANYOF_LOCALE;
2639 listsv = newSVpvn("# comment\n",10);
2644 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2645 goto skipcond; /* allow 1st char to be ] or - */
2647 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2650 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2651 PL_regcomp_parse += numlen;
2654 namedclass = regpposixcc(value);
2655 else if (value == '\\') {
2656 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2657 PL_regcomp_parse += numlen;
2659 case 'w': namedclass = ANYOF_ALNUM; break;
2660 case 'W': namedclass = ANYOF_NALNUM; break;
2661 case 's': namedclass = ANYOF_SPACE; break;
2662 case 'S': namedclass = ANYOF_NSPACE; break;
2663 case 'd': namedclass = ANYOF_DIGIT; break;
2664 case 'D': namedclass = ANYOF_NDIGIT; break;
2667 if (*PL_regcomp_parse == '{') {
2668 e = strchr(PL_regcomp_parse++, '}');
2670 FAIL("Missing right brace on \\p{}");
2671 n = e - PL_regcomp_parse;
2674 e = PL_regcomp_parse;
2679 Perl_sv_catpvf(aTHX_ listsv,
2680 "+utf8::%.*s\n", n, PL_regcomp_parse);
2682 Perl_sv_catpvf(aTHX_ listsv,
2683 "!utf8::%.*s\n", n, PL_regcomp_parse);
2685 PL_regcomp_parse = e + 1;
2686 lastvalue = OOB_UTF8;
2688 case 'n': value = '\n'; break;
2689 case 'r': value = '\r'; break;
2690 case 't': value = '\t'; break;
2691 case 'f': value = '\f'; break;
2692 case 'b': value = '\b'; break;
2693 case 'e': value = '\033'; break;
2694 case 'a': value = '\007'; break;
2696 if (*PL_regcomp_parse == '{') {
2697 e = strchr(PL_regcomp_parse++, '}');
2699 FAIL("Missing right brace on \\x{}");
2700 value = scan_hex(PL_regcomp_parse,
2701 e - PL_regcomp_parse,
2703 PL_regcomp_parse = e + 1;
2706 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2707 PL_regcomp_parse += numlen;
2711 value = UCHARAT(PL_regcomp_parse++);
2712 value = toCTRL(value);
2714 case '0': case '1': case '2': case '3': case '4':
2715 case '5': case '6': case '7': case '8': case '9':
2716 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2717 PL_regcomp_parse += numlen;
2721 if (!SIZE_ONLY && namedclass > -1) {
2722 switch (namedclass) {
2724 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n"); break;
2726 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n"); break;
2728 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n"); break;
2730 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n"); break;
2732 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n"); break;
2734 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n"); break;
2736 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n"); break;
2738 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n"); break;
2740 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n"); break;
2742 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n"); break;
2744 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n"); break;
2746 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n"); break;
2748 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n"); break;
2750 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n"); break;
2752 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n"); break;
2754 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n"); break;
2756 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n"); break;
2758 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n"); break;
2760 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n"); break;
2762 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n"); break;
2764 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n"); break;
2766 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n"); break;
2768 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n"); break;
2770 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n"); break;
2772 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n"); break;
2774 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n"); break;
2779 if (lastvalue > value)
2780 FAIL("invalid [] range in regexp");
2782 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\n", lastvalue, value);
2788 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2789 PL_regcomp_parse[1] != ']') {
2792 continue; /* do it next time */
2795 Perl_sv_catpvf(aTHX_ listsv, "%04x\n", value);
2799 ret = reganode(ANYOFUTF8, 0);
2802 SV *rv = swash_init("utf8", "", listsv, 1, 0);
2803 SvREFCNT_dec(listsv);
2804 n = add_data(1,"s");
2805 PL_regcomp_rx->data->data[n] = (void*)rv;
2806 ARG1_SET(ret, flags);
2817 char* retval = PL_regcomp_parse++;
2820 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2821 PL_regcomp_parse[2] == '#') {
2822 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2827 if (PL_regflags & PMf_EXTENDED) {
2828 if (isSPACE(*PL_regcomp_parse)) {
2832 else if (*PL_regcomp_parse == '#') {
2833 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2844 - reg_node - emit a node
2846 STATIC regnode * /* Location. */
2847 S_reg_node(pTHX_ U8 op)
2850 register regnode *ret;
2851 register regnode *ptr;
2855 SIZE_ALIGN(PL_regsize);
2860 NODE_ALIGN_FILL(ret);
2862 FILL_ADVANCE_NODE(ptr, op);
2869 - reganode - emit a node with an argument
2871 STATIC regnode * /* Location. */
2872 S_reganode(pTHX_ U8 op, U32 arg)
2875 register regnode *ret;
2876 register regnode *ptr;
2880 SIZE_ALIGN(PL_regsize);
2885 NODE_ALIGN_FILL(ret);
2887 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
2894 - regc - emit (if appropriate) a Unicode character
2897 S_reguni(pTHX_ UV uv, char* s, I32* lenp)
2902 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
2905 *lenp = uv_to_utf8((U8*)s, uv) - (U8*)s;
2910 - regc - emit (if appropriate) a byte of code
2913 S_regc(pTHX_ U8 b, char* s)
2921 - reginsert - insert an operator in front of already-emitted operand
2923 * Means relocating the operand.
2926 S_reginsert(pTHX_ U8 op, regnode *opnd)
2929 register regnode *src;
2930 register regnode *dst;
2931 register regnode *place;
2932 register int offset = regarglen[(U8)op];
2934 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2937 PL_regsize += NODE_STEP_REGNODE + offset;
2942 PL_regcode += NODE_STEP_REGNODE + offset;
2945 StructCopy(--src, --dst, regnode);
2947 place = opnd; /* Op node, where operand used to be. */
2948 src = NEXTOPER(place);
2949 FILL_ADVANCE_NODE(place, op);
2950 Zero(src, offset, regnode);
2954 - regtail - set the next-pointer at the end of a node chain of p to val.
2957 S_regtail(pTHX_ regnode *p, regnode *val)
2960 register regnode *scan;
2961 register regnode *temp;
2962 register I32 offset;
2967 /* Find last node. */
2970 temp = regnext(scan);
2976 if (reg_off_by_arg[OP(scan)]) {
2977 ARG_SET(scan, val - scan);
2980 NEXT_OFF(scan) = val - scan;
2985 - regoptail - regtail on operand of first argument; nop if operandless
2988 S_regoptail(pTHX_ regnode *p, regnode *val)
2991 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
2992 if (p == NULL || SIZE_ONLY)
2994 if (PL_regkind[(U8)OP(p)] == BRANCH) {
2995 regtail(NEXTOPER(p), val);
2997 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
2998 regtail(NEXTOPER(NEXTOPER(p)), val);
3005 - regcurly - a little FSA that accepts {\d+,?\d*}
3008 S_regcurly(pTHX_ register char *s)
3027 S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
3030 register U8 op = EXACT; /* Arbitrary non-END op. */
3031 register regnode *next, *onode;
3033 while (op != END && (!last || node < last)) {
3034 /* While that wasn't END last time... */
3040 next = regnext(node);
3042 if (OP(node) == OPTIMIZED)
3045 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
3046 2*l + 1, "", SvPVX(sv));
3047 if (next == NULL) /* Next ptr. */
3048 PerlIO_printf(Perl_debug_log, "(0)");
3050 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
3051 (void)PerlIO_putc(Perl_debug_log, '\n');
3053 if (PL_regkind[(U8)op] == BRANCHJ) {
3054 register regnode *nnode = (OP(next) == LONGJMP
3057 if (last && nnode > last)
3059 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
3061 else if (PL_regkind[(U8)op] == BRANCH) {
3062 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
3064 else if ( op == CURLY) { /* `next' might be very big: optimizer */
3065 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
3066 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
3068 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
3069 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
3072 else if ( op == PLUS || op == STAR) {
3073 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
3075 else if (op == ANYOF) {
3076 node = NEXTOPER(node);
3079 else if (PL_regkind[(U8)op] == EXACT) {
3080 /* Literal string, where present. */
3081 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
3082 node = NEXTOPER(node);
3085 node = NEXTOPER(node);
3086 node += regarglen[(U8)op];
3088 if (op == CURLYX || op == OPEN)
3090 else if (op == WHILEM)
3093 #endif /* DEBUGGING */
3098 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
3101 Perl_regdump(pTHX_ regexp *r)
3105 SV *sv = sv_newmortal();
3107 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
3109 /* Header fields of interest. */
3110 if (r->anchored_substr)
3111 PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
3113 SvPVX(r->anchored_substr),
3115 SvTAIL(r->anchored_substr) ? "$" : "",
3116 r->anchored_offset);
3117 if (r->float_substr)
3118 PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
3120 SvPVX(r->float_substr),
3122 SvTAIL(r->float_substr) ? "$" : "",
3123 r->float_min_offset, r->float_max_offset);
3124 if (r->check_substr)
3125 PerlIO_printf(Perl_debug_log,
3126 r->check_substr == r->float_substr
3127 ? "(checking floating" : "(checking anchored");
3128 if (r->reganch & ROPT_NOSCAN)
3129 PerlIO_printf(Perl_debug_log, " noscan");
3130 if (r->reganch & ROPT_CHECK_ALL)
3131 PerlIO_printf(Perl_debug_log, " isall");
3132 if (r->check_substr)
3133 PerlIO_printf(Perl_debug_log, ") ");
3135 if (r->regstclass) {
3136 regprop(sv, r->regstclass);
3137 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
3139 if (r->reganch & ROPT_ANCH) {
3140 PerlIO_printf(Perl_debug_log, "anchored");
3141 if (r->reganch & ROPT_ANCH_BOL)
3142 PerlIO_printf(Perl_debug_log, "(BOL)");
3143 if (r->reganch & ROPT_ANCH_MBOL)
3144 PerlIO_printf(Perl_debug_log, "(MBOL)");
3145 if (r->reganch & ROPT_ANCH_SBOL)
3146 PerlIO_printf(Perl_debug_log, "(SBOL)");
3147 if (r->reganch & ROPT_ANCH_GPOS)
3148 PerlIO_printf(Perl_debug_log, "(GPOS)");
3149 PerlIO_putc(Perl_debug_log, ' ');
3151 if (r->reganch & ROPT_GPOS_SEEN)
3152 PerlIO_printf(Perl_debug_log, "GPOS ");
3153 if (r->reganch & ROPT_SKIP)
3154 PerlIO_printf(Perl_debug_log, "plus ");
3155 if (r->reganch & ROPT_IMPLICIT)
3156 PerlIO_printf(Perl_debug_log, "implicit ");
3157 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
3158 if (r->reganch & ROPT_EVAL_SEEN)
3159 PerlIO_printf(Perl_debug_log, "with eval ");
3160 PerlIO_printf(Perl_debug_log, "\n");
3161 #endif /* DEBUGGING */
3165 - regprop - printable representation of opcode
3168 Perl_regprop(pTHX_ SV *sv, regnode *o)
3174 sv_setpvn(sv, "", 0);
3175 if (OP(o) >= reg_num) /* regnode.type is unsigned */
3176 FAIL("corrupted regexp opcode");
3177 sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
3179 k = PL_regkind[(U8)OP(o)];
3182 Perl_sv_catpvf(aTHX_ sv, " <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
3183 else if (k == CURLY) {
3184 if (OP(o) == CURLYM || OP(o) == CURLYN)
3185 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
3186 Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
3188 else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
3189 Perl_sv_catpvf(aTHX_ sv, "%d", ARG(o)); /* Parenth number */
3190 else if (k == LOGICAL)
3191 Perl_sv_catpvf(aTHX_ sv, "[%d]", ARG(o)); /* 2: embedded, otherwise 1 */
3192 else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
3193 Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
3194 #endif /* DEBUGGING */
3198 Perl_re_intuit_string(pTHX_ regexp *prog)
3199 { /* Assume that RE_INTUIT is set */
3202 char *s = SvPV(prog->check_substr,n_a);
3204 if (!PL_colorset) reginitcolors();
3205 PerlIO_printf(Perl_debug_log,
3206 "%sUsing REx substr:%s `%s%.60s%s%s'\n",
3207 PL_colors[4],PL_colors[5],PL_colors[0],
3210 (strlen(s) > 60 ? "..." : ""));
3213 return prog->check_substr;
3217 Perl_pregfree(pTHX_ struct regexp *r)
3220 DEBUG_r(PerlIO_printf(Perl_debug_log,
3221 "%sFreeing REx:%s `%s%.60s%s%s'\n",
3222 PL_colors[4],PL_colors[5],PL_colors[0],
3225 (strlen(r->precomp) > 60 ? "..." : "")));
3228 if (!r || (--r->refcnt > 0))
3231 Safefree(r->precomp);
3232 if (RX_MATCH_COPIED(r))
3233 Safefree(r->subbeg);
3235 if (r->anchored_substr)
3236 SvREFCNT_dec(r->anchored_substr);
3237 if (r->float_substr)
3238 SvREFCNT_dec(r->float_substr);
3239 Safefree(r->substrs);
3242 int n = r->data->count;
3243 AV* new_comppad = NULL;
3248 switch (r->data->what[n]) {
3250 SvREFCNT_dec((SV*)r->data->data[n]);
3253 new_comppad = (AV*)r->data->data[n];
3256 if (new_comppad == NULL)
3257 Perl_croak(aTHX_ "panic: pregfree comppad");
3258 old_comppad = PL_comppad;
3259 old_curpad = PL_curpad;
3260 PL_comppad = new_comppad;
3261 PL_curpad = AvARRAY(new_comppad);
3262 op_free((OP_4tree*)r->data->data[n]);
3263 PL_comppad = old_comppad;
3264 PL_curpad = old_curpad;
3265 SvREFCNT_dec((SV*)new_comppad);
3271 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
3274 Safefree(r->data->what);
3277 Safefree(r->startp);
3283 - regnext - dig the "next" pointer out of a node
3285 * [Note, when REGALIGN is defined there are two places in regmatch()
3286 * that bypass this code for speed.]
3289 Perl_regnext(pTHX_ register regnode *p)
3292 register I32 offset;
3294 if (p == &PL_regdummy)
3297 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
3305 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
3308 STRLEN l1 = strlen(pat1);
3309 STRLEN l2 = strlen(pat2);
3318 Copy(pat1, buf, l1 , char);
3319 Copy(pat2, buf + l1, l2 , char);
3320 buf[l1 + l2] = '\n';
3321 buf[l1 + l2 + 1] = '\0';
3323 /* ANSI variant takes additional second argument */
3324 va_start(args, pat2);
3328 msv = mess(buf, &args);
3330 message = SvPV(msv,l1);
3333 Copy(message, buf, l1 , char);
3334 buf[l1] = '\0'; /* Overwrite \n */
3335 Perl_croak(aTHX_ "%s", buf);
3338 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
3341 Perl_save_re_context(pTHX)
3345 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
3346 SAVEI32(PL_regnpar); /* () count. */
3347 SAVEI32(PL_regsize); /* Code size. */
3348 SAVEI16(PL_regflags); /* are we folding, multilining? */
3349 SAVEPPTR(PL_reginput); /* String-input pointer. */
3350 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
3351 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
3352 SAVESPTR(PL_regstartp); /* Pointer to startp array. */
3353 SAVESPTR(PL_regendp); /* Ditto for endp. */
3354 SAVESPTR(PL_reglastparen); /* Similarly for lastparen. */
3355 SAVEPPTR(PL_regtill); /* How far we are required to go. */
3356 SAVEI32(PL_regprev); /* char before regbol, \n if none */
3357 SAVESPTR(PL_reg_start_tmp); /* from regexec.c */
3358 PL_reg_start_tmp = 0;
3359 SAVEFREEPV(PL_reg_start_tmp);
3360 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
3361 PL_reg_start_tmpl = 0;
3362 SAVESPTR(PL_regdata);
3363 SAVEI32(PL_reg_flags); /* from regexec.c */
3364 SAVEI32(PL_reg_eval_set); /* from regexec.c */
3365 SAVEI32(PL_regnarrate); /* from regexec.c */
3366 SAVESPTR(PL_regprogram); /* from regexec.c */
3367 SAVEINT(PL_regindent); /* from regexec.c */
3368 SAVESPTR(PL_regcc); /* from regexec.c */
3369 SAVESPTR(PL_curcop);
3370 SAVESPTR(PL_regcomp_rx); /* from regcomp.c */
3371 SAVEI32(PL_regseen); /* from regcomp.c */
3372 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
3373 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
3374 SAVESPTR(PL_regcode); /* Code-emit pointer; ®dummy = don't */
3375 SAVEPPTR(PL_regxend); /* End of input for compile */
3376 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */
3377 SAVESPTR(PL_reg_call_cc); /* from regexec.c */
3378 SAVESPTR(PL_reg_re); /* from regexec.c */
3379 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
3380 SAVESPTR(PL_reg_sv); /* from regexec.c */
3381 SAVESPTR(PL_reg_magic); /* from regexec.c */
3382 SAVEI32(PL_reg_oldpos); /* from regexec.c */
3383 SAVESPTR(PL_reg_oldcurpm); /* from regexec.c */
3384 SAVESPTR(PL_reg_curpm); /* from regexec.c */
3386 SAVEPPTR(PL_reg_starttry); /* from regexec.c */
3398 clear_re(pTHXo_ void *r)
3400 ReREFCNT_dec((regexp *)r);