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 */
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 /* *These* symbols are masked to allow static link. */
39 # define Perl_pregfree my_regfree
40 # define Perl_regnext my_regnext
41 # define Perl_save_re_context my_save_re_context
42 # define Perl_reginitcolors my_reginitcolors
47 * pregcomp and pregexec -- regsub and regerror are not used in perl
49 * Copyright (c) 1986 by University of Toronto.
50 * Written by Henry Spencer. Not derived from licensed software.
52 * Permission is granted to anyone to use this software for any
53 * purpose on any computer system, and to redistribute it freely,
54 * subject to the following restrictions:
56 * 1. The author is not responsible for the consequences of use of
57 * this software, no matter how awful, even if they arise
60 * 2. The origin of this software must not be misrepresented, either
61 * by explicit claim or by omission.
63 * 3. Altered versions must be plainly marked as such, and must not
64 * be misrepresented as being the original software.
67 **** Alterations to Henry's code are...
69 **** Copyright (c) 1991-1998, Larry Wall
71 **** You may distribute under the terms of either the GNU General Public
72 **** License or the Artistic License, as specified in the README file.
75 * Beware that some of this code is subtly aware of the way operator
76 * precedence is structured in regular expressions. Serious changes in
77 * regular-expression syntax might require a total rethink.
82 #ifndef PERL_IN_XSUB_RE
94 # if defined(BUGGY_MSC6)
95 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
96 # pragma optimize("a",off)
97 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
98 # pragma optimize("w",on )
99 # endif /* BUGGY_MSC6 */
103 #define STATIC static
106 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
107 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
108 ((*s) == '{' && regcurly(s)))
110 #define PERL_META "^$.[()|?+*\\"
112 #define META "^$.[()|?+*\\"
116 #undef SPSTART /* dratted cpp namespace... */
119 * Flags to be passed up and down.
121 #define WORST 0 /* Worst case. */
122 #define HASWIDTH 0x1 /* Known to match non-null strings. */
123 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
124 #define SPSTART 0x4 /* Starts with * or +. */
125 #define TRYAGAIN 0x8 /* Weeded out a declaration. */
128 * Forward declarations for pregcomp()'s friends.
132 static regnode *reg _((I32, I32 *));
133 static regnode *reganode _((U8, U32));
134 static regnode *regatom _((I32 *));
135 static regnode *regbranch _((I32 *, I32));
136 static void regc _((U8, char *));
137 static void reguni _((UV, char *, I32*));
138 static regnode *regclass _((void));
139 static regnode *regclassutf8 _((void));
140 STATIC I32 regcurly _((char *));
141 static regnode *reg_node _((U8));
142 static regnode *regpiece _((I32 *));
143 static void reginsert _((U8, regnode *));
144 static void regoptail _((regnode *, regnode *));
145 static void regtail _((regnode *, regnode *));
146 static char* regwhite _((char *, char *));
147 static char* nextchar _((void));
148 static void re_croak2 _((const char* pat1,const char* pat2,...)) __attribute__((noreturn));
149 static char* regpposixcc _((I32 value));
150 static void clear_re _((void *r));
153 /* Length of a variant. */
159 I32 pos_min; /* CC */
160 I32 pos_delta; /* CC */
162 I32 last_end; /* min value, <0 unless valid. */
163 I32 last_start_min; /* CC */
164 I32 last_start_max; /* CC */
165 SV **longest; /* Either &l_fixed, or &l_float. */
167 I32 offset_fixed; /* CC */
169 I32 offset_float_min; /* CC */
170 I32 offset_float_max; /* CC */
175 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
177 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
178 #define SF_BEFORE_SEOL 0x1
179 #define SF_BEFORE_MEOL 0x2
180 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
181 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
184 # define SF_FIX_SHIFT_EOL (0+2)
185 # define SF_FL_SHIFT_EOL (0+4)
187 # define SF_FIX_SHIFT_EOL (+2)
188 # define SF_FL_SHIFT_EOL (+4)
191 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
192 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
194 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
195 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
196 #define SF_IS_INF 0x40
197 #define SF_HAS_PAR 0x80
198 #define SF_IN_PAR 0x100
199 #define SF_HAS_EVAL 0x200
200 #define SCF_DO_SUBSTR 0x400
203 #define UTF (PL_reg_flags & RF_utf8)
204 #define LOC (PL_regflags & PMf_LOCALE)
205 #define FOLD (PL_regflags & PMf_FOLD)
207 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
208 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
213 ReREFCNT_dec((regexp *)r);
217 scan_commit(scan_data_t *data)
220 STRLEN l = CHR_SVLEN(data->last_found);
221 STRLEN old_l = CHR_SVLEN(*data->longest);
223 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
224 sv_setsv(*data->longest, data->last_found);
225 if (*data->longest == data->longest_fixed) {
226 data->offset_fixed = l ? data->last_start_min : data->pos_min;
227 if (data->flags & SF_BEFORE_EOL)
229 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
231 data->flags &= ~SF_FIX_BEFORE_EOL;
234 data->offset_float_min = l ? data->last_start_min : data->pos_min;
235 data->offset_float_max = (l
236 ? data->last_start_max
237 : data->pos_min + data->pos_delta);
238 if (data->flags & SF_BEFORE_EOL)
240 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
242 data->flags &= ~SF_FL_BEFORE_EOL;
245 SvCUR_set(data->last_found, 0);
247 data->flags &= ~SF_BEFORE_EOL;
250 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
251 to the position after last scanned or to NULL. */
254 study_chunk(regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
255 /* scanp: Start here (read-write). */
256 /* deltap: Write maxlen-minlen here. */
257 /* last: Stop before this one. */
260 I32 min = 0, pars = 0, code;
261 regnode *scan = *scanp, *next;
263 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
264 int is_inf_internal = 0; /* The studied chunk is infinite */
265 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
266 scan_data_t data_fake;
268 while (scan && OP(scan) != END && scan < last) {
269 /* Peephole optimizer: */
271 if (PL_regkind[(U8)OP(scan)] == EXACT) {
272 regnode *n = regnext(scan);
275 regnode *stop = scan;
278 next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
279 /* Skip NOTHING, merge EXACT*. */
281 ( PL_regkind[(U8)OP(n)] == NOTHING ||
282 (stringok && (OP(n) == OP(scan))))
284 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
285 if (OP(n) == TAIL || n > next)
287 if (PL_regkind[(U8)OP(n)] == NOTHING) {
288 NEXT_OFF(scan) += NEXT_OFF(n);
289 next = n + NODE_STEP_REGNODE;
297 int oldl = *OPERAND(scan);
298 regnode *nnext = regnext(n);
300 if (oldl + *OPERAND(n) > U8_MAX)
302 NEXT_OFF(scan) += NEXT_OFF(n);
303 *OPERAND(scan) += *OPERAND(n);
304 next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2;
305 /* Now we can overwrite *n : */
306 Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1,
307 *OPERAND(n) + 1, char);
317 n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
319 /* Purify reports a benign UMR here sometimes, because we
320 * don't initialize the OP() slot of a node when that node
321 * is occupied by just the trailing null of the string in
323 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
332 if (OP(scan) != CURLYX) {
333 int max = (reg_off_by_arg[OP(scan)]
335 /* I32 may be smaller than U16 on CRAYs! */
336 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
337 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
341 /* Skip NOTHING and LONGJMP. */
342 while ((n = regnext(n))
343 && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
344 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
347 if (reg_off_by_arg[OP(scan)])
350 NEXT_OFF(scan) = off;
352 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
353 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
354 next = regnext(scan);
357 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
358 I32 max1 = 0, min1 = I32_MAX, num = 0;
360 if (flags & SCF_DO_SUBSTR)
362 while (OP(scan) == code) {
363 I32 deltanext, minnext;
367 next = regnext(scan);
368 scan = NEXTOPER(scan);
370 scan = NEXTOPER(scan);
371 /* We suppose the run is continuous, last=next...*/
372 minnext = study_chunk(&scan, &deltanext, next,
376 if (max1 < minnext + deltanext)
377 max1 = minnext + deltanext;
378 if (deltanext == I32_MAX)
379 is_inf = is_inf_internal = 1;
381 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
383 if (data && (data_fake.flags & SF_HAS_EVAL))
384 data->flags |= SF_HAS_EVAL;
388 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
390 if (flags & SCF_DO_SUBSTR) {
391 data->pos_min += min1;
392 data->pos_delta += max1 - min1;
393 if (max1 != min1 || is_inf)
394 data->longest = &(data->longest_float);
397 delta += max1 - min1;
399 else if (code == BRANCHJ) /* single branch is optimized. */
400 scan = NEXTOPER(NEXTOPER(scan));
401 else /* single branch is optimized. */
402 scan = NEXTOPER(scan);
405 else if (OP(scan) == EXACT) {
406 I32 l = *OPERAND(scan);
408 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
409 unsigned char *e = s + l;
418 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
419 /* The code below prefers earlier match for fixed
420 offset, later match for variable offset. */
421 if (data->last_end == -1) { /* Update the start info. */
422 data->last_start_min = data->pos_min;
423 data->last_start_max = is_inf
424 ? I32_MAX : data->pos_min + data->pos_delta;
426 sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), *OPERAND(scan));
427 data->last_end = data->pos_min + l;
428 data->pos_min += l; /* As in the first entry. */
429 data->flags &= ~SF_BEFORE_EOL;
432 else if (PL_regkind[(U8)OP(scan)] == EXACT) {
433 I32 l = *OPERAND(scan);
434 if (flags & SCF_DO_SUBSTR)
437 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
438 unsigned char *e = s + l;
447 if (data && (flags & SCF_DO_SUBSTR))
450 else if (strchr(PL_varies,OP(scan))) {
451 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
452 regnode *oscan = scan;
454 switch (PL_regkind[(U8)OP(scan)]) {
456 scan = NEXTOPER(scan);
459 if (flags & SCF_DO_SUBSTR) {
460 next = NEXTOPER(scan);
461 if (OP(next) == EXACT) {
463 maxcount = REG_INFTY;
464 next = regnext(scan);
465 scan = NEXTOPER(scan);
469 if (flags & SCF_DO_SUBSTR)
474 is_inf = is_inf_internal = 1;
475 scan = regnext(scan);
476 if (flags & SCF_DO_SUBSTR) {
478 data->longest = &(data->longest_float);
480 goto optimize_curly_tail;
482 mincount = ARG1(scan);
483 maxcount = ARG2(scan);
484 next = regnext(scan);
485 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
487 if (flags & SCF_DO_SUBSTR) {
488 if (mincount == 0) scan_commit(data);
489 pos_before = data->pos_min;
493 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
495 data->flags |= SF_IS_INF;
497 /* This will finish on WHILEM, setting scan, or on NULL: */
498 minnext = study_chunk(&scan, &deltanext, last, data,
500 ? (flags & ~SCF_DO_SUBSTR) : flags);
501 if (!scan) /* It was not CURLYX, but CURLY. */
503 if (ckWARN(WARN_UNSAFE) && (minnext + deltanext == 0)
504 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
505 && maxcount <= REG_INFTY/3) /* Complement check for big count */
506 warner(WARN_UNSAFE, "Strange *+?{} on zero-length expression");
507 min += minnext * mincount;
508 is_inf_internal |= (maxcount == REG_INFTY
509 && (minnext + deltanext) > 0
510 || deltanext == I32_MAX);
511 is_inf |= is_inf_internal;
512 delta += (minnext + deltanext) * maxcount - minnext * mincount;
514 /* Try powerful optimization CURLYX => CURLYN. */
515 if ( OP(oscan) == CURLYX && data
516 && data->flags & SF_IN_PAR
517 && !(data->flags & SF_HAS_EVAL)
518 && !deltanext && minnext == 1 ) {
519 /* Try to optimize to CURLYN. */
520 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
521 regnode *nxt1 = nxt, *nxt2;
525 if (!strchr(PL_simple,OP(nxt))
526 && !(PL_regkind[(U8)OP(nxt)] == EXACT
527 && *OPERAND(nxt) == 1))
531 if (OP(nxt) != CLOSE)
533 /* Now we know that nxt2 is the only contents: */
534 oscan->flags = ARG(nxt);
536 OP(nxt1) = NOTHING; /* was OPEN. */
538 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
539 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
540 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
541 OP(nxt) = OPTIMIZED; /* was CLOSE. */
542 OP(nxt + 1) = OPTIMIZED; /* was count. */
543 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
548 /* Try optimization CURLYX => CURLYM. */
549 if ( OP(oscan) == CURLYX && data
550 && !(data->flags & SF_HAS_PAR)
551 && !(data->flags & SF_HAS_EVAL)
553 /* XXXX How to optimize if data == 0? */
554 /* Optimize to a simpler form. */
555 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
559 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
560 && (OP(nxt2) != WHILEM))
562 OP(nxt2) = SUCCEED; /* Whas WHILEM */
563 /* Need to optimize away parenths. */
564 if (data->flags & SF_IN_PAR) {
565 /* Set the parenth number. */
566 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
568 if (OP(nxt) != CLOSE)
569 FAIL("panic opt close");
570 oscan->flags = ARG(nxt);
571 OP(nxt1) = OPTIMIZED; /* was OPEN. */
572 OP(nxt) = OPTIMIZED; /* was CLOSE. */
574 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
575 OP(nxt + 1) = OPTIMIZED; /* was count. */
576 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
577 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
580 while ( nxt1 && (OP(nxt1) != WHILEM)) {
581 regnode *nnxt = regnext(nxt1);
584 if (reg_off_by_arg[OP(nxt1)])
585 ARG_SET(nxt1, nxt2 - nxt1);
586 else if (nxt2 - nxt1 < U16_MAX)
587 NEXT_OFF(nxt1) = nxt2 - nxt1;
589 OP(nxt) = NOTHING; /* Cannot beautify */
594 /* Optimize again: */
595 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
600 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
602 if (flags & SCF_DO_SUBSTR) {
603 SV *last_str = Nullsv;
604 int counted = mincount != 0;
606 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
607 I32 b = pos_before >= data->last_start_min
608 ? pos_before : data->last_start_min;
610 char *s = SvPV(data->last_found, l);
611 I32 old = b - data->last_start_min;
614 old = utf8_hop((U8*)s, old) - (U8*)s;
617 /* Get the added string: */
618 last_str = newSVpv(s + old, l);
619 if (deltanext == 0 && pos_before == b) {
620 /* What was added is a constant string */
622 SvGROW(last_str, (mincount * l) + 1);
623 repeatcpy(SvPVX(last_str) + l,
624 SvPVX(last_str), l, mincount - 1);
625 SvCUR(last_str) *= mincount;
626 /* Add additional parts. */
627 SvCUR_set(data->last_found,
628 SvCUR(data->last_found) - l);
629 sv_catsv(data->last_found, last_str);
630 data->last_end += l * (mincount - 1);
634 /* It is counted once already... */
635 data->pos_min += minnext * (mincount - counted);
636 data->pos_delta += - counted * deltanext +
637 (minnext + deltanext) * maxcount - minnext * mincount;
638 if (mincount != maxcount) {
640 if (mincount && last_str) {
641 sv_setsv(data->last_found, last_str);
642 data->last_end = data->pos_min;
643 data->last_start_min =
644 data->pos_min - CHR_SVLEN(last_str);
645 data->last_start_max = is_inf
647 : data->pos_min + data->pos_delta
648 - CHR_SVLEN(last_str);
650 data->longest = &(data->longest_float);
652 SvREFCNT_dec(last_str);
654 if (data && (fl & SF_HAS_EVAL))
655 data->flags |= SF_HAS_EVAL;
657 if (OP(oscan) != CURLYX) {
658 while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
660 NEXT_OFF(oscan) += NEXT_OFF(next);
663 default: /* REF only? */
664 if (flags & SCF_DO_SUBSTR) {
666 data->longest = &(data->longest_float);
668 is_inf = is_inf_internal = 1;
672 else if (strchr(PL_simple,OP(scan)) || PL_regkind[(U8)OP(scan)] == ANYUTF8) {
673 if (flags & SCF_DO_SUBSTR) {
679 else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
680 data->flags |= (OP(scan) == MEOL
684 else if (PL_regkind[(U8)OP(scan)] == BRANCHJ
685 && (scan->flags || data)
686 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
687 I32 deltanext, minnext;
691 next = regnext(scan);
692 nscan = NEXTOPER(NEXTOPER(scan));
693 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
696 FAIL("variable length lookbehind not implemented");
698 else if (minnext > U8_MAX) {
699 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
701 scan->flags = minnext;
703 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
705 if (data && (data_fake.flags & SF_HAS_EVAL))
706 data->flags |= SF_HAS_EVAL;
708 else if (OP(scan) == OPEN) {
711 else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
712 next = regnext(scan);
714 if ( next && (OP(next) != WHILEM) && next < last)
715 is_par = 0; /* Disable optimization */
717 else if (OP(scan) == EVAL) {
719 data->flags |= SF_HAS_EVAL;
721 else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded */
722 if (flags & SCF_DO_SUBSTR) {
724 data->longest = &(data->longest_float);
726 is_inf = is_inf_internal = 1;
728 /* Else: zero-length, ignore. */
729 scan = regnext(scan);
734 *deltap = is_inf_internal ? I32_MAX : delta;
735 if (flags & SCF_DO_SUBSTR && is_inf)
736 data->pos_delta = I32_MAX - data->pos_min;
739 if (is_par && pars==1 && data) {
740 data->flags |= SF_IN_PAR;
741 data->flags &= ~SF_HAS_PAR;
743 else if (pars && data) {
744 data->flags |= SF_HAS_PAR;
745 data->flags &= ~SF_IN_PAR;
751 add_data(I32 n, char *s)
754 if (PL_regcomp_rx->data) {
755 Renewc(PL_regcomp_rx->data,
756 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
757 char, struct reg_data);
758 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
759 PL_regcomp_rx->data->count += n;
762 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
763 char, struct reg_data);
764 New(1208, PL_regcomp_rx->data->what, n, U8);
765 PL_regcomp_rx->data->count = n;
767 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
768 return PL_regcomp_rx->data->count - n;
776 char *s = PerlEnv_getenv("PERL_RE_COLORS");
779 PL_colors[0] = s = savepv(s);
787 PL_colors[i] = s = "";
797 - pregcomp - compile a regular expression into internal code
799 * We can't allocate space until we know how big the compiled form will be,
800 * but we can't compile it (and thus know how big it is) until we've got a
801 * place to put the code. So we cheat: we compile it twice, once with code
802 * generation turned off and size counting turned on, and once "for real".
803 * This also means that we don't allocate space until we are sure that the
804 * thing really will compile successfully, and we never have to move the
805 * code and thus invalidate pointers into it. (Note that it has to be in
806 * one piece because free() must be able to free it all.) [NB: not true in perl]
808 * Beware that the optimization-preparation code in here knows about some
809 * of the structure of the compiled regexp. [I'll say.]
812 pregcomp(char *exp, char *xend, PMOP *pm)
827 FAIL("NULL regexp argument");
829 if (PL_curcop == &PL_compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
830 PL_reg_flags |= RF_utf8;
834 PL_regprecomp = savepvn(exp, xend - exp);
835 DEBUG_r(if (!PL_colorset) reginitcolors());
836 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling%s RE `%s%*s%s'\n",
837 PL_colors[4],PL_colors[5],PL_colors[0],
838 xend - exp, PL_regprecomp, PL_colors[1]));
839 PL_regflags = pm->op_pmflags;
843 PL_seen_zerolen = *exp == '^' ? -1 : 0;
847 /* First pass: determine size, legality. */
848 PL_regcomp_parse = exp;
853 PL_regcode = &PL_regdummy;
854 regc((U8)REG_MAGIC, (char*)PL_regcode);
855 if (reg(0, &flags) == NULL) {
856 Safefree(PL_regprecomp);
857 PL_regprecomp = Nullch;
860 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
862 /* Small enough for pointer-storage convention?
863 If extralen==0, this means that we will not need long jumps. */
864 if (PL_regsize >= 0x10000L && PL_extralen)
865 PL_regsize += PL_extralen;
869 /* Allocate space and initialize. */
870 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
873 FAIL("regexp out of space");
875 r->prelen = xend - exp;
876 r->precomp = PL_regprecomp;
877 r->subbeg = r->subbase = NULL;
878 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
880 r->substrs = 0; /* Useful during FAIL. */
881 r->startp = 0; /* Useful during FAIL. */
882 r->endp = 0; /* Useful during FAIL. */
886 /* Second pass: emit code. */
887 PL_regcomp_parse = exp;
891 PL_regcode = r->program;
892 /* Store the count of eval-groups for security checks: */
893 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
894 regc((U8)REG_MAGIC, (char*) PL_regcode++);
896 if (reg(0, &flags) == NULL)
899 /* Dig out information for optimizations. */
900 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
901 pm->op_pmflags = PL_regflags;
903 r->reganch |= ROPT_UTF8;
904 r->regstclass = NULL;
905 if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
906 r->reganch |= ROPT_NAUGHTY;
907 scan = r->program + 1; /* First BRANCH. */
909 /* XXXX To minimize changes to RE engine we always allocate
910 3-units-long substrs field. */
911 Newz(1004, r->substrs, 1, struct reg_substr_data);
913 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
916 STRLEN longest_float_length, longest_fixed_length;
918 StructCopy(&zero_scan_data, &data, scan_data_t);
920 /* Skip introductions and multiplicators >= 1. */
921 while ((OP(first) == OPEN && (sawopen = 1)) ||
922 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
923 (OP(first) == PLUS) ||
924 (OP(first) == MINMOD) ||
925 (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
926 if (OP(first) == PLUS)
929 first += regarglen[(U8)OP(first)];
930 first = NEXTOPER(first);
933 /* Starting-point info. */
935 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
936 else if (strchr(PL_simple+4,OP(first)))
937 r->regstclass = first;
938 else if (PL_regkind[(U8)OP(first)] == BOUND ||
939 PL_regkind[(U8)OP(first)] == NBOUND)
940 r->regstclass = first;
941 else if (PL_regkind[(U8)OP(first)] == BOL) {
942 r->reganch |= (OP(first) == MBOL ? ROPT_ANCH_MBOL: ROPT_ANCH_BOL);
943 first = NEXTOPER(first);
946 else if (OP(first) == GPOS) {
947 r->reganch |= ROPT_ANCH_GPOS;
948 first = NEXTOPER(first);
951 else if ((OP(first) == STAR &&
952 PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
953 !(r->reganch & ROPT_ANCH) )
955 /* turn .* into ^.* with an implied $*=1 */
956 r->reganch |= ROPT_ANCH_BOL | ROPT_IMPLICIT;
957 first = NEXTOPER(first);
960 if (sawplus && (!sawopen || !PL_regsawback))
961 r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */
963 /* Scan is after the zeroth branch, first is atomic matcher. */
964 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
967 * If there's something expensive in the r.e., find the
968 * longest literal string that must appear and make it the
969 * regmust. Resolve ties in favor of later strings, since
970 * the regstart check works with the beginning of the r.e.
971 * and avoiding duplication strengthens checking. Not a
972 * strong reason, but sufficient in the absence of others.
973 * [Now we resolve ties in favor of the earlier string if
974 * it happens that c_offset_min has been invalidated, since the
975 * earlier string may buy us something the later one won't.]
979 data.longest_fixed = newSVpv("",0);
980 data.longest_float = newSVpv("",0);
981 data.last_found = newSVpv("",0);
982 data.longest = &(data.longest_fixed);
985 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
986 &data, SCF_DO_SUBSTR);
987 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
988 && data.last_start_min == 0 && data.last_end > 0
990 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
991 r->reganch |= ROPT_CHECK_ALL;
993 SvREFCNT_dec(data.last_found);
995 longest_float_length = CHR_SVLEN(data.longest_float);
996 if (longest_float_length
997 || (data.flags & SF_FL_BEFORE_EOL
998 && (!(data.flags & SF_FL_BEFORE_MEOL)
999 || (PL_regflags & PMf_MULTILINE)))) {
1000 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
1001 && data.offset_fixed == data.offset_float_min
1002 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
1003 goto remove_float; /* As in (a)+. */
1005 r->float_substr = data.longest_float;
1006 r->float_min_offset = data.offset_float_min;
1007 r->float_max_offset = data.offset_float_max;
1008 fbm_compile(r->float_substr, 0);
1009 BmUSEFUL(r->float_substr) = 100;
1010 if (data.flags & SF_FL_BEFORE_EOL /* Cannot have SEOL and MULTI */
1011 && (!(data.flags & SF_FL_BEFORE_MEOL)
1012 || (PL_regflags & PMf_MULTILINE)))
1013 SvTAIL_on(r->float_substr);
1017 r->float_substr = Nullsv;
1018 SvREFCNT_dec(data.longest_float);
1019 longest_float_length = 0;
1022 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
1023 if (longest_fixed_length
1024 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1025 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1026 || (PL_regflags & PMf_MULTILINE)))) {
1027 r->anchored_substr = data.longest_fixed;
1028 r->anchored_offset = data.offset_fixed;
1029 fbm_compile(r->anchored_substr, 0);
1030 BmUSEFUL(r->anchored_substr) = 100;
1031 if (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1032 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1033 || (PL_regflags & PMf_MULTILINE)))
1034 SvTAIL_on(r->anchored_substr);
1037 r->anchored_substr = Nullsv;
1038 SvREFCNT_dec(data.longest_fixed);
1039 longest_fixed_length = 0;
1042 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1043 if (longest_fixed_length > longest_float_length) {
1044 r->check_substr = r->anchored_substr;
1045 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1046 if (r->reganch & ROPT_ANCH_SINGLE)
1047 r->reganch |= ROPT_NOSCAN;
1050 r->check_substr = r->float_substr;
1051 r->check_offset_min = data.offset_float_min;
1052 r->check_offset_max = data.offset_float_max;
1056 /* Several toplevels. Best we can is to set minlen. */
1059 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1060 scan = r->program + 1;
1061 minlen = study_chunk(&scan, &fake, scan + PL_regsize, NULL, 0);
1062 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1066 if (PL_regseen & REG_SEEN_GPOS)
1067 r->reganch |= ROPT_GPOS_SEEN;
1068 if (PL_regseen & REG_SEEN_LOOKBEHIND)
1069 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1070 if (PL_regseen & REG_SEEN_EVAL)
1071 r->reganch |= ROPT_EVAL_SEEN;
1072 Newz(1002, r->startp, PL_regnpar, char*);
1073 Newz(1002, r->endp, PL_regnpar, char*);
1074 DEBUG_r(regdump(r));
1079 - reg - regular expression, i.e. main body or parenthesized thing
1081 * Caller must absorb opening parenthesis.
1083 * Combining parenthesis handling with the base level of regular expression
1084 * is a trifle forced, but the need to tie the tails of the branches to what
1085 * follows makes it hard to avoid.
1088 reg(I32 paren, I32 *flagp)
1089 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1092 register regnode *ret; /* Will be the head of the group. */
1093 register regnode *br;
1094 register regnode *lastbr;
1095 register regnode *ender = 0;
1096 register I32 parno = 0;
1097 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
1100 *flagp = 0; /* Tentatively. */
1102 /* Make an OPEN node, if parenthesized. */
1104 if (*PL_regcomp_parse == '?') {
1105 U16 posflags = 0, negflags = 0;
1106 U16 *flagsp = &posflags;
1110 paren = *PL_regcomp_parse++;
1111 ret = NULL; /* For look-ahead/behind. */
1114 PL_regseen |= REG_SEEN_LOOKBEHIND;
1115 if (*PL_regcomp_parse == '!')
1117 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
1128 FAIL2("Sequence (?%c...) not implemented", (int)paren);
1131 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1133 if (*PL_regcomp_parse != ')')
1134 FAIL("Sequence (?#... not terminated");
1140 paren = *PL_regcomp_parse++;
1145 I32 count = 1, n = 0;
1147 char *s = PL_regcomp_parse;
1149 OP_4tree *sop, *rop;
1152 PL_regseen |= REG_SEEN_EVAL;
1153 while (count && (c = *PL_regcomp_parse)) {
1154 if (c == '\\' && PL_regcomp_parse[1])
1162 if (*PL_regcomp_parse != ')')
1163 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1167 if (PL_regcomp_parse - 1 - s)
1168 sv = newSVpv(s, PL_regcomp_parse - 1 - s);
1170 sv = newSVpv("", 0);
1172 rop = sv_compile_2op(sv, &sop, "re", &av);
1174 n = add_data(3, "nso");
1175 PL_regcomp_rx->data->data[n] = (void*)rop;
1176 PL_regcomp_rx->data->data[n+1] = (void*)av;
1177 PL_regcomp_rx->data->data[n+2] = (void*)sop;
1180 else { /* First pass */
1181 if (PL_reginterp_cnt < ++PL_seen_evals
1182 && PL_curcop != &PL_compiling)
1183 /* No compiled RE interpolated, has runtime
1184 components ===> unsafe. */
1185 FAIL("Eval-group not allowed at runtime, use re 'eval'");
1187 FAIL("Eval-group in insecure regular expression");
1192 ret = reg_node(LOGICAL);
1195 regtail(ret, reganode(EVAL, n));
1198 return reganode(EVAL, n);
1202 if (PL_regcomp_parse[0] == '?') {
1203 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1204 || PL_regcomp_parse[1] == '<'
1205 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
1208 ret = reg_node(LOGICAL);
1211 regtail(ret, reg(1, &flag));
1215 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
1216 parno = atoi(PL_regcomp_parse++);
1218 while (isDIGIT(*PL_regcomp_parse))
1220 ret = reganode(GROUPP, parno);
1221 if ((c = *nextchar()) != ')')
1222 FAIL2("Switch (?(number%c not recognized", c);
1224 regtail(ret, reganode(IFTHEN, 0));
1225 br = regbranch(&flags, 1);
1227 br = reganode(LONGJMP, 0);
1229 regtail(br, reganode(LONGJMP, 0));
1234 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1235 regbranch(&flags, 1);
1236 regtail(ret, lastbr);
1244 FAIL("Switch (?(condition)... contains too many branches");
1245 ender = reg_node(TAIL);
1248 regtail(lastbr, ender);
1249 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1252 regtail(ret, ender);
1256 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
1260 FAIL("Sequence (? incomplete");
1265 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1266 if (*PL_regcomp_parse != 'o')
1267 pmflag(flagsp, *PL_regcomp_parse);
1270 if (*PL_regcomp_parse == '-') {
1275 PL_regflags |= posflags;
1276 PL_regflags &= ~negflags;
1277 if (*PL_regcomp_parse == ':') {
1283 if (*PL_regcomp_parse != ')')
1284 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
1293 ret = reganode(OPEN, parno);
1300 /* Pick up the branches, linking them together. */
1301 br = regbranch(&flags, 1);
1304 if (*PL_regcomp_parse == '|') {
1305 if (!SIZE_ONLY && PL_extralen) {
1306 reginsert(BRANCHJ, br);
1309 reginsert(BRANCH, br);
1312 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
1314 else if (paren == ':') {
1315 *flagp |= flags&SIMPLE;
1317 if (open) { /* Starts with OPEN. */
1318 regtail(ret, br); /* OPEN -> first. */
1320 else if (paren != '?') /* Not Conditional */
1324 *flagp |= flags&SPSTART;
1326 while (*PL_regcomp_parse == '|') {
1327 if (!SIZE_ONLY && PL_extralen) {
1328 ender = reganode(LONGJMP,0);
1329 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1332 PL_extralen += 2; /* Account for LONGJMP. */
1334 br = regbranch(&flags, 0);
1337 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1341 *flagp |= flags&SPSTART;
1344 if (have_branch || paren != ':') {
1345 /* Make a closing node, and hook it on the end. */
1348 ender = reg_node(TAIL);
1351 ender = reganode(CLOSE, parno);
1357 *flagp &= ~HASWIDTH;
1360 ender = reg_node(SUCCEED);
1363 ender = reg_node(END);
1366 regtail(lastbr, ender);
1369 /* Hook the tails of the branches to the closing node. */
1370 for (br = ret; br != NULL; br = regnext(br)) {
1371 regoptail(br, ender);
1378 static char parens[] = "=!<,>";
1380 if (paren && (p = strchr(parens, paren))) {
1381 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1382 int flag = (p - parens) > 1;
1385 node = SUSPEND, flag = 0;
1386 reginsert(node,ret);
1388 regtail(ret, reg_node(TAIL));
1392 /* Check for proper termination. */
1394 PL_regflags = oregflags;
1395 if (PL_regcomp_parse >= PL_regxend || *nextchar() != ')') {
1396 FAIL("unmatched () in regexp");
1399 else if (!paren && PL_regcomp_parse < PL_regxend) {
1400 if (*PL_regcomp_parse == ')') {
1401 FAIL("unmatched () in regexp");
1404 FAIL("junk on end of regexp"); /* "Can't happen". */
1412 - regbranch - one alternative of an | operator
1414 * Implements the concatenation operator.
1417 regbranch(I32 *flagp, I32 first)
1420 register regnode *ret;
1421 register regnode *chain = NULL;
1422 register regnode *latest;
1423 I32 flags = 0, c = 0;
1428 if (!SIZE_ONLY && PL_extralen)
1429 ret = reganode(BRANCHJ,0);
1431 ret = reg_node(BRANCH);
1434 if (!first && SIZE_ONLY)
1435 PL_extralen += 1; /* BRANCHJ */
1437 *flagp = WORST; /* Tentatively. */
1441 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
1443 latest = regpiece(&flags);
1444 if (latest == NULL) {
1445 if (flags & TRYAGAIN)
1449 else if (ret == NULL)
1451 *flagp |= flags&HASWIDTH;
1452 if (chain == NULL) /* First piece. */
1453 *flagp |= flags&SPSTART;
1456 regtail(chain, latest);
1461 if (chain == NULL) { /* Loop ran zero times. */
1462 chain = reg_node(NOTHING);
1467 *flagp |= flags&SIMPLE;
1474 - regpiece - something followed by possible [*+?]
1476 * Note that the branching code sequences used for ? and the general cases
1477 * of * and + are somewhat optimized: they use the same NOTHING node as
1478 * both the endmarker for their branch list and the body of the last branch.
1479 * It might seem that this node could be dispensed with entirely, but the
1480 * endmarker role is not redundant.
1483 regpiece(I32 *flagp)
1486 register regnode *ret;
1488 register char *next;
1490 char *origparse = PL_regcomp_parse;
1493 I32 max = REG_INFTY;
1495 ret = regatom(&flags);
1497 if (flags & TRYAGAIN)
1502 op = *PL_regcomp_parse;
1504 if (op == '{' && regcurly(PL_regcomp_parse)) {
1505 next = PL_regcomp_parse + 1;
1507 while (isDIGIT(*next) || *next == ',') {
1516 if (*next == '}') { /* got one */
1520 min = atoi(PL_regcomp_parse);
1524 maxpos = PL_regcomp_parse;
1526 if (!max && *maxpos != '0')
1527 max = REG_INFTY; /* meaning "infinity" */
1528 else if (max >= REG_INFTY)
1529 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
1530 PL_regcomp_parse = next;
1534 if ((flags&SIMPLE)) {
1535 PL_regnaughty += 2 + PL_regnaughty / 2;
1536 reginsert(CURLY, ret);
1539 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
1540 regtail(ret, reg_node(WHILEM));
1541 if (!SIZE_ONLY && PL_extralen) {
1542 reginsert(LONGJMP,ret);
1543 reginsert(NOTHING,ret);
1544 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1546 reginsert(CURLYX,ret);
1547 if (!SIZE_ONLY && PL_extralen)
1548 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1549 regtail(ret, reg_node(NOTHING));
1559 if (max && max < min)
1560 FAIL("Can't do {n,m} with n > m");
1575 #if 0 /* Now runtime fix should be reliable. */
1576 if (!(flags&HASWIDTH) && op != '?')
1577 FAIL("regexp *+ operand could be empty");
1582 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
1584 if (op == '*' && (flags&SIMPLE)) {
1585 reginsert(STAR, ret);
1589 else if (op == '*') {
1593 else if (op == '+' && (flags&SIMPLE)) {
1594 reginsert(PLUS, ret);
1598 else if (op == '+') {
1602 else if (op == '?') {
1607 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
1608 warner(WARN_UNSAFE, "%.*s matches null string many times",
1609 PL_regcomp_parse - origparse, origparse);
1612 if (*PL_regcomp_parse == '?') {
1614 reginsert(MINMOD, ret);
1615 regtail(ret, ret + NODE_STEP_REGNODE);
1617 if (ISMULT2(PL_regcomp_parse))
1618 FAIL("nested *?+ in regexp");
1624 - regatom - the lowest level
1626 * Optimization: gobbles an entire sequence of ordinary characters so that
1627 * it can turn them into a single node, which is smaller to store and
1628 * faster to run. Backslashed characters are exceptions, each becoming a
1629 * separate node; the code is simpler that way and it's not worth fixing.
1631 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1637 register regnode *ret = 0;
1640 *flagp = WORST; /* Tentatively. */
1643 switch (*PL_regcomp_parse) {
1647 if (PL_regflags & PMf_MULTILINE)
1648 ret = reg_node(MBOL);
1649 else if (PL_regflags & PMf_SINGLELINE)
1650 ret = reg_node(SBOL);
1652 ret = reg_node(BOL);
1655 if (PL_regcomp_parse[1])
1658 if (PL_regflags & PMf_MULTILINE)
1659 ret = reg_node(MEOL);
1660 else if (PL_regflags & PMf_SINGLELINE)
1661 ret = reg_node(SEOL);
1663 ret = reg_node(EOL);
1668 if (PL_regflags & PMf_SINGLELINE)
1669 ret = reg_node(SANYUTF8);
1671 ret = reg_node(ANYUTF8);
1675 if (PL_regflags & PMf_SINGLELINE)
1676 ret = reg_node(SANY);
1678 ret = reg_node(REG_ANY);
1679 *flagp |= HASWIDTH|SIMPLE;
1685 ret = (UTF ? regclassutf8() : regclass());
1686 if (*PL_regcomp_parse != ']')
1687 FAIL("unmatched [] in regexp");
1689 *flagp |= HASWIDTH|SIMPLE;
1693 ret = reg(1, &flags);
1695 if (flags & TRYAGAIN)
1699 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
1703 if (flags & TRYAGAIN) {
1707 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
1708 /* Supposed to be caught earlier. */
1711 if (!regcurly(PL_regcomp_parse)) {
1719 FAIL("?+*{} follows nothing in regexp");
1722 switch (*++PL_regcomp_parse) {
1725 ret = reg_node(SBOL);
1730 ret = reg_node(GPOS);
1731 PL_regseen |= REG_SEEN_GPOS;
1736 ret = reg_node(SEOL);
1741 ret = reg_node(EOS);
1743 PL_seen_zerolen++; /* Do not optimize RE away */
1747 ret = reg_node(SANY);
1748 *flagp |= HASWIDTH|SIMPLE;
1752 ret = reg_node(CLUMP);
1755 if (UTF && !PL_utf8_mark)
1756 is_utf8_mark((U8*)"~"); /* preload table */
1761 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
1762 : (LOC ? ALNUML : ALNUM));
1763 *flagp |= HASWIDTH|SIMPLE;
1765 if (UTF && !PL_utf8_alnum)
1766 is_utf8_alnum((U8*)"a"); /* preload table */
1771 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
1772 : (LOC ? NALNUML : NALNUM));
1773 *flagp |= HASWIDTH|SIMPLE;
1775 if (UTF && !PL_utf8_alnum)
1776 is_utf8_alnum((U8*)"a"); /* preload table */
1780 PL_regseen |= REG_SEEN_LOOKBEHIND;
1783 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
1784 : (LOC ? BOUNDL : BOUND));
1787 if (UTF && !PL_utf8_alnum)
1788 is_utf8_alnum((U8*)"a"); /* preload table */
1792 PL_regseen |= REG_SEEN_LOOKBEHIND;
1795 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
1796 : (LOC ? NBOUNDL : NBOUND));
1799 if (UTF && !PL_utf8_alnum)
1800 is_utf8_alnum((U8*)"a"); /* preload table */
1805 ? (LOC ? SPACELUTF8 : SPACEUTF8)
1806 : (LOC ? SPACEL : SPACE));
1807 *flagp |= HASWIDTH|SIMPLE;
1809 if (UTF && !PL_utf8_space)
1810 is_utf8_space((U8*)" "); /* preload table */
1815 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
1816 : (LOC ? NSPACEL : NSPACE));
1817 *flagp |= HASWIDTH|SIMPLE;
1819 if (UTF && !PL_utf8_space)
1820 is_utf8_space((U8*)" "); /* preload table */
1823 ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
1824 *flagp |= HASWIDTH|SIMPLE;
1826 if (UTF && !PL_utf8_digit)
1827 is_utf8_digit((U8*)"1"); /* preload table */
1830 ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
1831 *flagp |= HASWIDTH|SIMPLE;
1833 if (UTF && !PL_utf8_digit)
1834 is_utf8_digit((U8*)"1"); /* preload table */
1838 { /* a lovely hack--pretend we saw [\pX] instead */
1839 char* oldregxend = PL_regxend;
1841 if (PL_regcomp_parse[1] == '{') {
1842 PL_regxend = strchr(PL_regcomp_parse, '}');
1844 FAIL("Missing right brace on \\p{}");
1848 PL_regxend = PL_regcomp_parse + 2;
1851 ret = regclassutf8();
1853 PL_regxend = oldregxend;
1856 *flagp |= HASWIDTH|SIMPLE;
1869 case '1': case '2': case '3': case '4':
1870 case '5': case '6': case '7': case '8': case '9':
1872 I32 num = atoi(PL_regcomp_parse);
1874 if (num > 9 && num >= PL_regnpar)
1877 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
1878 FAIL("reference to nonexistent group");
1881 ? (LOC ? REFFL : REFF)
1884 while (isDIGIT(*PL_regcomp_parse))
1892 if (PL_regcomp_parse >= PL_regxend)
1893 FAIL("trailing \\ in regexp");
1896 /* Do not generate `unrecognized' warnings here, we fall
1897 back into the quick-grab loop below */
1903 if (PL_regflags & PMf_EXTENDED) {
1904 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1905 if (PL_regcomp_parse < PL_regxend)
1921 ? (LOC ? EXACTFL : EXACTF)
1923 s = (char *) OPERAND(ret);
1924 regc(0, s++); /* save spot for len */
1925 for (len = 0, p = PL_regcomp_parse - 1;
1926 len < 127 && p < PL_regxend;
1931 if (PL_regflags & PMf_EXTENDED)
1932 p = regwhite(p, PL_regxend);
1986 char* e = strchr(p, '}');
1989 FAIL("Missing right brace on \\x{}");
1991 ender = scan_hex(p + 1, e - p, &numlen);
1992 if (numlen + len >= 127) { /* numlen is generous */
1999 FAIL("Can't use \\x{} without 'use utf8' declaration");
2002 ender = scan_hex(p, 2, &numlen);
2008 ender = UCHARAT(p++);
2009 ender = toCTRL(ender);
2011 case '0': case '1': case '2': case '3':case '4':
2012 case '5': case '6': case '7': case '8':case '9':
2014 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
2015 ender = scan_oct(p, 3, &numlen);
2024 if (p >= PL_regxend)
2025 FAIL("trailing \\ in regexp");
2028 if (!SIZE_ONLY && ckWARN(WARN_UNSAFE) && isALPHA(*p))
2030 "/%.127s/: Unrecognized escape \\%c passed through",
2033 goto normal_default;
2038 if ((*p & 0xc0) == 0xc0 && UTF) {
2039 ender = utf8_to_uv((U8*)p, &numlen);
2046 if (PL_regflags & PMf_EXTENDED)
2047 p = regwhite(p, PL_regxend);
2050 ender = toLOWER_LC_uni(ender);
2052 ender = toLOWER_uni(ender);
2054 if (ISMULT2(p)) { /* Back off on ?+*. */
2057 else if (ender >= 0x80 && UTF) {
2058 reguni(ender, s, &numlen);
2068 if (ender >= 0x80 && UTF) {
2069 reguni(ender, s, &numlen);
2077 PL_regcomp_parse = p - 1;
2080 FAIL("internal disaster in regexp");
2086 *OPERAND(ret) = len;
2089 PL_regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2092 PL_regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2102 regwhite(char *p, char *e)
2107 else if (*p == '#') {
2110 } while (p < e && *p != '\n');
2118 /* parse POSIX character classes like [[:foo:]] */
2120 regpposixcc(I32 value)
2125 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2126 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2127 (*PL_regcomp_parse == ':' ||
2128 *PL_regcomp_parse == '=' ||
2129 *PL_regcomp_parse == '.')) {
2130 char c = *PL_regcomp_parse;
2131 char* s = PL_regcomp_parse++;
2133 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != c)
2135 if (PL_regcomp_parse == PL_regxend)
2136 /* Grandfather lone [:, [=, [. */
2137 PL_regcomp_parse = s;
2139 PL_regcomp_parse++; /* skip over the c */
2140 if (*PL_regcomp_parse == ']') {
2141 /* Not Implemented Yet.
2142 * (POSIX Extended Character Classes, that is)
2143 * The text between e.g. [: and :] would start
2144 * at s + 1 and stop at regcomp_parse - 2. */
2145 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
2147 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2148 PL_regcomp_parse++; /* skip over the ending ] */
2161 register char *opnd, *s;
2163 register I32 lastvalue = 1234;
2164 register I32 range = 0;
2165 register regnode *ret;
2169 s = opnd = (char *) OPERAND(PL_regcode);
2170 ret = reg_node(ANYOF);
2171 for (value = 0; value < 33; value++)
2173 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2177 *opnd |= ANYOF_INVERT;
2180 PL_regcode += ANY_SKIP;
2182 *opnd |= ANYOF_FOLD;
2184 *opnd |= ANYOF_LOCALE;
2187 PL_regsize += ANY_SKIP;
2189 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2190 goto skipcond; /* allow 1st char to be ] or - */
2191 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2193 value = UCHARAT(PL_regcomp_parse++);
2195 (void)regpposixcc(value); /* ignore the return value for now */
2196 else if (value == '\\') {
2197 value = UCHARAT(PL_regcomp_parse++);
2202 *opnd |= ANYOF_ALNUML;
2204 for (value = 0; value < 256; value++)
2206 ANYOF_SET(opnd, value);
2214 *opnd |= ANYOF_NALNUML;
2216 for (value = 0; value < 256; value++)
2217 if (!isALNUM(value))
2218 ANYOF_SET(opnd, value);
2226 *opnd |= ANYOF_SPACEL;
2228 for (value = 0; value < 256; value++)
2230 ANYOF_SET(opnd, value);
2238 *opnd |= ANYOF_NSPACEL;
2240 for (value = 0; value < 256; value++)
2241 if (!isSPACE(value))
2242 ANYOF_SET(opnd, value);
2249 for (value = '0'; value <= '9'; value++)
2250 ANYOF_SET(opnd, value);
2256 for (value = 0; value < '0'; value++)
2257 ANYOF_SET(opnd, value);
2258 for (value = '9' + 1; value < 256; value++)
2259 ANYOF_SET(opnd, value);
2285 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2286 PL_regcomp_parse += numlen;
2289 value = UCHARAT(PL_regcomp_parse++);
2290 value = toCTRL(value);
2292 case '0': case '1': case '2': case '3': case '4':
2293 case '5': case '6': case '7': case '8': case '9':
2294 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2295 PL_regcomp_parse += numlen;
2300 if (lastvalue > value)
2301 FAIL("invalid [] range in regexp");
2306 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2307 PL_regcomp_parse[1] != ']') {
2310 continue; /* do it next time */
2315 if ((isLOWER(lastvalue) && isLOWER(value)) ||
2316 (isUPPER(lastvalue) && isUPPER(value)))
2319 if (isLOWER(lastvalue)) {
2320 for (i = lastvalue; i <= value; i++)
2324 for (i = lastvalue; i <= value; i++)
2331 for ( ; lastvalue <= value; lastvalue++)
2332 ANYOF_SET(opnd, lastvalue);
2336 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2337 if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) {
2338 for (value = 0; value < 256; ++value) {
2339 if (ANYOF_TEST(opnd, value)) {
2340 I32 cf = PL_fold[value];
2341 ANYOF_SET(opnd, cf);
2344 *opnd &= ~ANYOF_FOLD;
2346 /* optimize inverted simple patterns (e.g. [^a-z]) */
2347 if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) {
2348 for (value = 0; value < 32; ++value)
2349 opnd[1 + value] ^= 0xFF;
2358 register char *opnd, *e;
2360 register U32 lastvalue = 123456;
2361 register I32 range = 0;
2362 register regnode *ret;
2369 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2373 flags |= ANYOF_INVERT;
2377 flags |= ANYOF_FOLD;
2379 flags |= ANYOF_LOCALE;
2380 listsv = newSVpv("# comment\n",0);
2383 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2384 goto skipcond; /* allow 1st char to be ] or - */
2386 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2388 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2389 PL_regcomp_parse += numlen;
2392 (void)regpposixcc(value); /* ignore the return value for now */
2393 else if (value == '\\') {
2394 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2395 PL_regcomp_parse += numlen;
2400 flags |= ANYOF_ALNUML;
2402 sv_catpvf(listsv, "+utf8::IsAlnum\n");
2409 flags |= ANYOF_NALNUML;
2412 "-utf8::IsAlpha\n-utf8::IsDigit\n0000\t%04x\n%04x\tffff\n",
2421 flags |= ANYOF_SPACEL;
2422 sv_catpvf(listsv, "+utf8::IsSpace\n");
2424 is_utf8_space((U8*)" ");
2431 flags |= ANYOF_NSPACEL;
2433 "!utf8::IsSpace\n");
2435 is_utf8_space((U8*)" ");
2441 sv_catpvf(listsv, "+utf8::IsDigit\n");
2448 "!utf8::IsDigit\n");
2454 if (*PL_regcomp_parse == '{') {
2455 e = strchr(PL_regcomp_parse++, '}');
2457 FAIL("Missing right brace on \\p{}");
2458 n = e - PL_regcomp_parse;
2461 e = PL_regcomp_parse;
2466 sv_catpvf(listsv, "+utf8::%.*s\n", n, PL_regcomp_parse);
2469 "!utf8::%.*s\n", n, PL_regcomp_parse);
2471 PL_regcomp_parse = e + 1;
2496 if (*PL_regcomp_parse == '{') {
2497 e = strchr(PL_regcomp_parse++, '}');
2499 FAIL("Missing right brace on \\x{}");
2500 value = scan_hex(PL_regcomp_parse + 1, e - PL_regcomp_parse, &numlen);
2501 PL_regcomp_parse = e + 1;
2504 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2505 PL_regcomp_parse += numlen;
2509 value = UCHARAT(PL_regcomp_parse++);
2510 value = toCTRL(value);
2512 case '0': case '1': case '2': case '3': case '4':
2513 case '5': case '6': case '7': case '8': case '9':
2514 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2515 PL_regcomp_parse += numlen;
2520 if (lastvalue > value)
2521 FAIL("invalid [] range in regexp");
2523 sv_catpvf(listsv, "%04x\t%04x\n", lastvalue, value);
2529 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2530 PL_regcomp_parse[1] != ']') {
2533 continue; /* do it next time */
2536 sv_catpvf(listsv, "%04x\n", value);
2540 ret = reganode(ANYOFUTF8, 0);
2543 SV *rv = swash_init("utf8", "", listsv, 1, 0);
2544 SvREFCNT_dec(listsv);
2545 n = add_data(1,"s");
2546 PL_regcomp_rx->data->data[n] = (void*)rv;
2547 ARG1_SET(ret, flags);
2558 char* retval = PL_regcomp_parse++;
2561 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2562 PL_regcomp_parse[2] == '#') {
2563 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2568 if (PL_regflags & PMf_EXTENDED) {
2569 if (isSPACE(*PL_regcomp_parse)) {
2573 else if (*PL_regcomp_parse == '#') {
2574 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2585 - reg_node - emit a node
2587 STATIC regnode * /* Location. */
2591 register regnode *ret;
2592 register regnode *ptr;
2596 SIZE_ALIGN(PL_regsize);
2601 NODE_ALIGN_FILL(ret);
2603 FILL_ADVANCE_NODE(ptr, op);
2610 - reganode - emit a node with an argument
2612 STATIC regnode * /* Location. */
2613 reganode(U8 op, U32 arg)
2616 register regnode *ret;
2617 register regnode *ptr;
2621 SIZE_ALIGN(PL_regsize);
2626 NODE_ALIGN_FILL(ret);
2628 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
2635 - regc - emit (if appropriate) a Unicode character
2638 reguni(UV uv, char* s, I32* lenp)
2643 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
2646 *lenp = uv_to_utf8((U8*)s, uv) - (U8*)s;
2651 - regc - emit (if appropriate) a byte of code
2662 - reginsert - insert an operator in front of already-emitted operand
2664 * Means relocating the operand.
2667 reginsert(U8 op, regnode *opnd)
2670 register regnode *src;
2671 register regnode *dst;
2672 register regnode *place;
2673 register int offset = regarglen[(U8)op];
2675 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2678 PL_regsize += NODE_STEP_REGNODE + offset;
2683 PL_regcode += NODE_STEP_REGNODE + offset;
2686 StructCopy(--src, --dst, regnode);
2688 place = opnd; /* Op node, where operand used to be. */
2689 src = NEXTOPER(place);
2690 FILL_ADVANCE_NODE(place, op);
2691 Zero(src, offset, regnode);
2695 - regtail - set the next-pointer at the end of a node chain of p to val.
2698 regtail(regnode *p, regnode *val)
2701 register regnode *scan;
2702 register regnode *temp;
2703 register I32 offset;
2708 /* Find last node. */
2711 temp = regnext(scan);
2717 if (reg_off_by_arg[OP(scan)]) {
2718 ARG_SET(scan, val - scan);
2721 NEXT_OFF(scan) = val - scan;
2726 - regoptail - regtail on operand of first argument; nop if operandless
2729 regoptail(regnode *p, regnode *val)
2732 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
2733 if (p == NULL || SIZE_ONLY)
2735 if (PL_regkind[(U8)OP(p)] == BRANCH) {
2736 regtail(NEXTOPER(p), val);
2738 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
2739 regtail(NEXTOPER(NEXTOPER(p)), val);
2746 - regcurly - a little FSA that accepts {\d+,?\d*}
2749 regcurly(register char *s)
2768 dumpuntil(regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
2771 register char op = EXACT; /* Arbitrary non-END op. */
2772 register regnode *next, *onode;
2774 while (op != END && (!last || node < last)) {
2775 /* While that wasn't END last time... */
2781 next = regnext(node);
2783 if (OP(node) == OPTIMIZED)
2786 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
2787 2*l + 1, "", SvPVX(sv));
2788 if (next == NULL) /* Next ptr. */
2789 PerlIO_printf(Perl_debug_log, "(0)");
2791 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
2792 (void)PerlIO_putc(Perl_debug_log, '\n');
2794 if (PL_regkind[(U8)op] == BRANCHJ) {
2795 register regnode *nnode = (OP(next) == LONGJMP
2798 if (last && nnode > last)
2800 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
2802 else if (PL_regkind[(U8)op] == BRANCH) {
2803 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
2805 else if ( op == CURLY) { /* `next' might be very big: optimizer */
2806 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2807 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
2809 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
2810 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2813 else if ( op == PLUS || op == STAR) {
2814 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
2816 else if (op == ANYOF) {
2817 node = NEXTOPER(node);
2820 else if (PL_regkind[(U8)op] == EXACT) {
2821 /* Literal string, where present. */
2822 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2823 node = NEXTOPER(node);
2826 node = NEXTOPER(node);
2827 node += regarglen[(U8)op];
2829 if (op == CURLYX || op == OPEN)
2831 else if (op == WHILEM)
2834 #endif /* DEBUGGING */
2839 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
2846 SV *sv = sv_newmortal();
2848 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
2850 /* Header fields of interest. */
2851 if (r->anchored_substr)
2852 PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
2854 SvPVX(r->anchored_substr),
2856 SvTAIL(r->anchored_substr) ? "$" : "",
2857 r->anchored_offset);
2858 if (r->float_substr)
2859 PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
2861 SvPVX(r->float_substr),
2863 SvTAIL(r->float_substr) ? "$" : "",
2864 r->float_min_offset, r->float_max_offset);
2865 if (r->check_substr)
2866 PerlIO_printf(Perl_debug_log,
2867 r->check_substr == r->float_substr
2868 ? "(checking floating" : "(checking anchored");
2869 if (r->reganch & ROPT_NOSCAN)
2870 PerlIO_printf(Perl_debug_log, " noscan");
2871 if (r->reganch & ROPT_CHECK_ALL)
2872 PerlIO_printf(Perl_debug_log, " isall");
2873 if (r->check_substr)
2874 PerlIO_printf(Perl_debug_log, ") ");
2876 if (r->regstclass) {
2877 regprop(sv, r->regstclass);
2878 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
2880 if (r->reganch & ROPT_ANCH) {
2881 PerlIO_printf(Perl_debug_log, "anchored");
2882 if (r->reganch & ROPT_ANCH_BOL)
2883 PerlIO_printf(Perl_debug_log, "(BOL)");
2884 if (r->reganch & ROPT_ANCH_MBOL)
2885 PerlIO_printf(Perl_debug_log, "(MBOL)");
2886 if (r->reganch & ROPT_ANCH_GPOS)
2887 PerlIO_printf(Perl_debug_log, "(GPOS)");
2888 PerlIO_putc(Perl_debug_log, ' ');
2890 if (r->reganch & ROPT_GPOS_SEEN)
2891 PerlIO_printf(Perl_debug_log, "GPOS ");
2892 if (r->reganch & ROPT_SKIP)
2893 PerlIO_printf(Perl_debug_log, "plus ");
2894 if (r->reganch & ROPT_IMPLICIT)
2895 PerlIO_printf(Perl_debug_log, "implicit ");
2896 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
2897 if (r->reganch & ROPT_EVAL_SEEN)
2898 PerlIO_printf(Perl_debug_log, "with eval ");
2899 PerlIO_printf(Perl_debug_log, "\n");
2900 #endif /* DEBUGGING */
2904 - regprop - printable representation of opcode
2907 regprop(SV *sv, regnode *o)
2911 register char *p = 0;
2913 sv_setpvn(sv, "", 0);
2958 sv_catpvf(sv, "EXACT <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2961 sv_catpvf(sv, "EXACTF <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2964 sv_catpvf(sv, "EXACTFL <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2991 sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o));
2994 sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
2997 sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
3000 sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o));
3003 sv_catpvf(sv, "REF%d", ARG(o));
3006 sv_catpvf(sv, "REFF%d", ARG(o));
3009 sv_catpvf(sv, "REFFL%d", ARG(o));
3012 sv_catpvf(sv, "OPEN%d", ARG(o));
3015 sv_catpvf(sv, "CLOSE%d", ARG(o));
3031 sv_catpvf(sv, "UNLESSM[-%d]", o->flags);
3034 sv_catpvf(sv, "IFMATCH[-%d]", o->flags);
3085 sv_catpvf(sv, "GROUPP%d", ARG(o));
3088 sv_catpvf(sv, "LOGICAL[%d]", o->flags);
3100 FAIL("corrupted regexp opcode");
3104 #endif /* DEBUGGING */
3108 pregfree(struct regexp *r)
3111 if (!r || (--r->refcnt > 0))
3114 Safefree(r->precomp);
3116 Safefree(r->subbase);
3118 if (r->anchored_substr)
3119 SvREFCNT_dec(r->anchored_substr);
3120 if (r->float_substr)
3121 SvREFCNT_dec(r->float_substr);
3122 Safefree(r->substrs);
3125 int n = r->data->count;
3127 switch (r->data->what[n]) {
3129 SvREFCNT_dec((SV*)r->data->data[n]);
3132 op_free((OP_4tree*)r->data->data[n]);
3137 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
3140 Safefree(r->data->what);
3143 Safefree(r->startp);
3149 - regnext - dig the "next" pointer out of a node
3151 * [Note, when REGALIGN is defined there are two places in regmatch()
3152 * that bypass this code for speed.]
3155 regnext(register regnode *p)
3158 register I32 offset;
3160 if (p == &PL_regdummy)
3163 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
3171 re_croak2(const char* pat1,const char* pat2,...)
3174 STRLEN l1 = strlen(pat1);
3175 STRLEN l2 = strlen(pat2);
3183 Copy(pat1, buf, l1 , char);
3184 Copy(pat2, buf + l1, l2 , char);
3185 buf[l1 + l2] = '\n';
3186 buf[l1 + l2 + 1] = '\0';
3187 va_start(args, pat2);
3188 message = mess(buf, &args);
3190 l1 = strlen(message);
3193 Copy(message, buf, l1 , char);
3194 buf[l1] = '\0'; /* Overwrite \n */
3198 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
3201 save_re_context(void)
3205 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
3206 SAVEI32(PL_regnpar); /* () count. */
3207 SAVEI32(PL_regsize); /* Code size. */
3208 SAVEI16(PL_regflags); /* are we folding, multilining? */
3209 SAVEPPTR(PL_reginput); /* String-input pointer. */
3210 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
3211 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
3212 SAVESPTR(PL_regstartp); /* Pointer to startp array. */
3213 SAVESPTR(PL_regendp); /* Ditto for endp. */
3214 SAVESPTR(PL_reglastparen); /* Similarly for lastparen. */
3215 SAVEPPTR(PL_regtill); /* How far we are required to go. */
3216 SAVEI32(PL_regprev); /* char before regbol, \n if none */
3217 SAVESPTR(PL_reg_start_tmp); /* from regexec.c */
3218 PL_reg_start_tmp = 0;
3219 SAVEFREEPV(PL_reg_start_tmp);
3220 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
3221 PL_reg_start_tmpl = 0;
3222 SAVESPTR(PL_regdata);
3223 SAVEI32(PL_reg_flags); /* from regexec.c */
3224 SAVEI32(PL_reg_eval_set); /* from regexec.c */
3225 SAVEI32(PL_regnarrate); /* from regexec.c */
3226 SAVESPTR(PL_regprogram); /* from regexec.c */
3227 SAVEINT(PL_regindent); /* from regexec.c */
3228 SAVESPTR(PL_regcc); /* from regexec.c */
3229 SAVESPTR(PL_curcop);
3230 SAVESPTR(PL_regcomp_rx); /* from regcomp.c */
3231 SAVEI32(PL_regseen); /* from regcomp.c */
3232 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
3233 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
3234 SAVESPTR(PL_regcode); /* Code-emit pointer; ®dummy = don't */
3235 SAVEPPTR(PL_regxend); /* End of input for compile */
3236 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */
3237 SAVESPTR(PL_reg_call_cc); /* from regexec.c */
3238 SAVESPTR(PL_reg_re); /* from regexec.c */
3239 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
3240 SAVESPTR(PL_reg_sv); /* from regexec.c */
3241 SAVESPTR(PL_reg_magic); /* from regexec.c */
3242 SAVEI32(PL_reg_oldpos); /* from regexec.c */
3243 SAVESPTR(PL_reg_oldcurpm); /* from regexec.c */
3244 SAVESPTR(PL_reg_curpm); /* from regexec.c */
3246 SAVEPPTR(PL_reg_starttry); /* from regexec.c */