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.
24 * pregcomp and pregexec -- regsub and regerror are not used in perl
26 * Copyright (c) 1986 by University of Toronto.
27 * Written by Henry Spencer. Not derived from licensed software.
29 * Permission is granted to anyone to use this software for any
30 * purpose on any computer system, and to redistribute it freely,
31 * subject to the following restrictions:
33 * 1. The author is not responsible for the consequences of use of
34 * this software, no matter how awful, even if they arise
37 * 2. The origin of this software must not be misrepresented, either
38 * by explicit claim or by omission.
40 * 3. Altered versions must be plainly marked as such, and must not
41 * be misrepresented as being the original software.
44 **** Alterations to Henry's code are...
46 **** Copyright (c) 1991-1997, Larry Wall
48 **** You may distribute under the terms of either the GNU General Public
49 **** License or the Artistic License, as specified in the README file.
52 * Beware that some of this code is subtly aware of the way operator
53 * precedence is structured in regular expressions. Serious changes in
54 * regular-expression syntax might require a total rethink.
67 static regnode regdummy;
68 static char * regparse; /* Input-scan pointer. */
69 static char * regxend; /* End of input for compile */
70 static regnode * regcode; /* Code-emit pointer; ®dummy = don't. */
71 static I32 regnaughty; /* How bad is this pattern? */
72 static I32 regsawback; /* Did we see \1, ...? */
74 /* This guys appear both in regcomp.c and regexec.c, but there is no
75 other reason to have them global. */
76 static char * regprecomp; /* uncompiled string. */
77 static I32 regnpar; /* () count. */
78 static I32 regsize; /* Code size. */
79 static U16 regflags; /* are we folding, multilining? */
82 # if defined(BUGGY_MSC6)
83 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
84 # pragma optimize("a",off)
85 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
86 # pragma optimize("w",on )
87 # endif /* BUGGY_MSC6 */
94 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
95 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
96 ((*s) == '{' && regcurly(s)))
98 #define PERL_META "^$.[()|?+*\\"
100 #define META "^$.[()|?+*\\"
104 #undef SPSTART /* dratted cpp namespace... */
107 * Flags to be passed up and down.
109 #define WORST 0 /* Worst case. */
110 #define HASWIDTH 0x1 /* Known never to match null string. */
111 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
112 #define SPSTART 0x4 /* Starts with * or +. */
113 #define TRYAGAIN 0x8 /* Weeded out a declaration. */
116 * Forward declarations for pregcomp()'s friends.
119 static regnode *reg _((I32, I32 *));
120 static regnode *reganode _((U8, U32));
121 static regnode *regatom _((I32 *));
122 static regnode *regbranch _((I32 *, I32));
123 static void regc _((U8, char *));
124 static regnode *regclass _((void));
125 STATIC I32 regcurly _((char *));
126 static regnode *reg_node _((U8));
127 static regnode *regpiece _((I32 *));
128 static void reginsert _((U8, regnode *));
129 static void regoptail _((regnode *, regnode *));
130 static void regset _((char *, I32));
131 static void regtail _((regnode *, regnode *));
132 static char* regwhite _((char *, char *));
133 static char* nextchar _((void));
136 static I32 seen_zerolen;
145 /* Length of a variant. */
153 I32 last_end; /* min value, <0 unless valid. */
156 SV **longest; /* Either &l_fixed, or &l_float. */
160 I32 offset_float_min;
161 I32 offset_float_max;
165 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
167 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
168 #define SF_BEFORE_SEOL 0x1
169 #define SF_BEFORE_MEOL 0x2
170 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
171 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
173 #define SF_FIX_SHIFT_EOL (+2)
174 #define SF_FL_SHIFT_EOL (+4)
176 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
177 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
179 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
180 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
181 #define SF_IS_INF 0x40
182 #define SF_HAS_PAR 0x80
183 #define SF_IN_PAR 0x100
184 #define SF_HAS_EVAL 0x200
185 #define SCF_DO_SUBSTR 0x400
188 scan_commit(scan_data_t *data)
190 STRLEN l = SvCUR(data->last_found);
191 STRLEN old_l = SvCUR(*data->longest);
193 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
194 sv_setsv(*data->longest, data->last_found);
195 if (*data->longest == data->longest_fixed) {
196 data->offset_fixed = l ? data->last_start_min : data->pos_min;
197 if (data->flags & SF_BEFORE_EOL)
199 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
201 data->flags &= ~SF_FIX_BEFORE_EOL;
203 data->offset_float_min = l ? data->last_start_min : data->pos_min;
204 data->offset_float_max = (l
205 ? data->last_start_max
206 : data->pos_min + data->pos_delta);
207 if (data->flags & SF_BEFORE_EOL)
209 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
211 data->flags &= ~SF_FL_BEFORE_EOL;
214 SvCUR_set(data->last_found, 0);
216 data->flags &= ~SF_BEFORE_EOL;
219 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
220 to the position after last scanned or to NULL. */
223 study_chunk(regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
224 /* scanp: Start here (read-write). */
225 /* deltap: Write maxlen-minlen here. */
226 /* last: Stop before this one. */
228 I32 min = 0, pars = 0, code;
229 regnode *scan = *scanp, *next;
231 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
232 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
233 scan_data_t data_fake;
235 while (scan && OP(scan) != END && scan < last) {
236 /* Peephole optimizer: */
238 if (regkind[(U8)OP(scan)] == EXACT) {
239 regnode *n = regnext(scan);
242 regnode *stop = scan;
245 next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
246 /* Skip NOTHING, merge EXACT*. */
248 ( regkind[(U8)OP(n)] == NOTHING ||
249 (stringok && (OP(n) == OP(scan))))
251 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
252 if (OP(n) == TAIL || n > next)
254 if (regkind[(U8)OP(n)] == NOTHING) {
255 NEXT_OFF(scan) += NEXT_OFF(n);
256 next = n + NODE_STEP_REGNODE;
263 int oldl = *OPERAND(scan);
264 regnode *nnext = regnext(n);
266 if (oldl + *OPERAND(n) > U8_MAX)
268 NEXT_OFF(scan) += NEXT_OFF(n);
269 *OPERAND(scan) += *OPERAND(n);
270 next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2;
271 /* Now we can overwrite *n : */
272 Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1,
273 *OPERAND(n) + 1, char);
283 n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
285 if (regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
294 if (OP(scan) != CURLYX) {
295 int max = (reg_off_by_arg[OP(scan)] ? I32_MAX : U16_MAX);
296 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
300 /* Skip NOTHING and LONGJMP. */
301 while ((n = regnext(n))
302 && ((regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
303 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
306 if (reg_off_by_arg[OP(scan)])
309 NEXT_OFF(scan) = off;
311 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
312 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
313 next = regnext(scan);
316 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
317 I32 max1 = 0, min1 = I32_MAX, num = 0;
319 if (flags & SCF_DO_SUBSTR)
321 while (OP(scan) == code) {
322 I32 deltanext, minnext;
326 next = regnext(scan);
327 scan = NEXTOPER(scan);
329 scan = NEXTOPER(scan);
330 /* We suppose the run is continuous, last=next...*/
331 minnext = study_chunk(&scan, &deltanext, next,
335 if (max1 < minnext + deltanext)
336 max1 = minnext + deltanext;
337 if (deltanext == I32_MAX)
340 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
342 if (data && (data_fake.flags & SF_HAS_EVAL))
343 data->flags |= SF_HAS_EVAL;
347 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
349 if (flags & SCF_DO_SUBSTR) {
350 data->pos_min += min1;
351 data->pos_delta += max1 - min1;
352 if (max1 != min1 || is_inf)
353 data->longest = &(data->longest_float);
356 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);
362 } else if (OP(scan) == EXACT) {
363 min += *OPERAND(scan);
364 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
365 I32 l = *OPERAND(scan);
367 /* The code below prefers earlier match for fixed
368 offset, later match for variable offset. */
369 if (data->last_end == -1) { /* Update the start info. */
370 data->last_start_min = data->pos_min;
371 data->last_start_max = is_inf
372 ? I32_MAX : data->pos_min + data->pos_delta;
374 sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), l);
375 data->last_end = data->pos_min + l;
376 data->pos_min += l; /* As in the first entry. */
377 data->flags &= ~SF_BEFORE_EOL;
379 } else if (regkind[(U8)OP(scan)] == EXACT) {
380 if (flags & SCF_DO_SUBSTR)
382 min += *OPERAND(scan);
383 if (data && (flags & SCF_DO_SUBSTR))
384 data->pos_min += *OPERAND(scan);
385 } else if (strchr(varies,OP(scan))) {
386 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
387 regnode *oscan = scan;
389 switch (regkind[(U8)OP(scan)]) {
391 scan = NEXTOPER(scan);
394 if (flags & SCF_DO_SUBSTR) {
395 next = NEXTOPER(scan);
396 if (OP(next) == EXACT) {
398 maxcount = REG_INFTY;
399 next = regnext(scan);
400 scan = NEXTOPER(scan);
404 if (flags & SCF_DO_SUBSTR)
410 scan = regnext(scan);
411 if (flags & SCF_DO_SUBSTR) {
413 data->longest = &(data->longest_float);
415 goto optimize_curly_tail;
417 mincount = ARG1(scan);
418 maxcount = ARG2(scan);
419 next = regnext(scan);
420 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
422 if (flags & SCF_DO_SUBSTR) {
423 if (mincount == 0) scan_commit(data);
424 pos_before = data->pos_min;
428 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
430 data->flags |= SF_IS_INF;
432 /* This will finish on WHILEM, setting scan, or on NULL: */
433 minnext = study_chunk(&scan, &deltanext, last, data,
435 ? (flags & ~SCF_DO_SUBSTR) : flags);
436 if (!scan) /* It was not CURLYX, but CURLY. */
438 if (dowarn && (minnext + deltanext == 0)
439 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR)))
440 warn("Strange *+?{} on zero-length expression");
441 min += minnext * mincount;
442 is_inf |= (maxcount == REG_INFTY && (minnext + deltanext) > 0
443 || deltanext == I32_MAX);
444 delta += (minnext + deltanext) * maxcount - minnext * mincount;
446 /* Try powerful optimization CURLYX => CURLYN. */
447 #ifdef REGALIGN_STRUCT
448 if ( OP(oscan) == CURLYX && data
449 && data->flags & SF_IN_PAR
450 && !(data->flags & SF_HAS_EVAL)
451 && !deltanext && minnext == 1 ) {
452 /* Try to optimize to CURLYN. */
453 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
454 regnode *nxt1 = nxt, *nxt2;
458 if (!strchr(simple,OP(nxt))
459 && !(regkind[(U8)OP(nxt)] == EXACT
460 && *OPERAND(nxt) == 1))
464 if (OP(nxt) != CLOSE)
466 /* Now we know that nxt2 is the only contents: */
467 oscan->flags = ARG(nxt);
469 OP(nxt1) = NOTHING; /* was OPEN. */
471 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
472 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
473 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
474 OP(nxt) = OPTIMIZED; /* was CLOSE. */
475 OP(nxt + 1) = OPTIMIZED; /* was count. */
476 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
482 /* Try optimization CURLYX => CURLYM. */
483 if ( OP(oscan) == CURLYX && data
484 #ifdef REGALIGN_STRUCT
485 && !(data->flags & SF_HAS_PAR)
487 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
489 && !(data->flags & SF_HAS_EVAL)
491 /* XXXX How to optimize if data == 0? */
492 /* Optimize to a simpler form. */
493 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
497 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
498 && (OP(nxt2) != WHILEM))
500 OP(nxt2) = SUCCEED; /* Whas WHILEM */
501 #ifdef REGALIGN_STRUCT
502 /* Need to optimize away parenths. */
503 if (data->flags & SF_IN_PAR) {
504 /* Set the parenth number. */
505 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
507 if (OP(nxt) != CLOSE)
508 FAIL("panic opt close");
509 oscan->flags = ARG(nxt);
510 OP(nxt1) = OPTIMIZED; /* was OPEN. */
511 OP(nxt) = OPTIMIZED; /* was CLOSE. */
513 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
514 OP(nxt + 1) = OPTIMIZED; /* was count. */
515 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
516 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
519 while ( nxt1 && (OP(nxt1) != WHILEM)) {
520 regnode *nnxt = regnext(nxt1);
523 if (reg_off_by_arg[OP(nxt1)])
524 ARG_SET(nxt1, nxt2 - nxt1);
525 else if (nxt2 - nxt1 < U16_MAX)
526 NEXT_OFF(nxt1) = nxt2 - nxt1;
528 OP(nxt) = NOTHING; /* Cannot beautify */
533 /* Optimize again: */
534 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
539 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
541 if (flags & SCF_DO_SUBSTR) {
542 SV *last_str = Nullsv;
543 int counted = mincount != 0;
545 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
546 I32 b = pos_before >= data->last_start_min
547 ? pos_before : data->last_start_min;
549 char *s = SvPV(data->last_found, l);
551 l -= b - data->last_start_min;
552 /* Get the added string: */
553 last_str = newSVpv(s + b - data->last_start_min, l);
554 if (deltanext == 0 && pos_before == b) {
555 /* What was added is a constant string */
557 SvGROW(last_str, (mincount * l) + 1);
558 repeatcpy(SvPVX(last_str) + l,
559 SvPVX(last_str), l, mincount - 1);
560 SvCUR(last_str) *= mincount;
561 /* Add additional parts. */
562 SvCUR_set(data->last_found,
563 SvCUR(data->last_found) - l);
564 sv_catsv(data->last_found, last_str);
565 data->last_end += l * (mincount - 1);
569 /* It is counted once already... */
570 data->pos_min += minnext * (mincount - counted);
571 data->pos_delta += - counted * deltanext +
572 (minnext + deltanext) * maxcount - minnext * mincount;
573 if (mincount != maxcount) {
575 if (mincount && last_str) {
576 sv_setsv(data->last_found, last_str);
577 data->last_end = data->pos_min;
578 data->last_start_min =
579 data->pos_min - SvCUR(last_str);
580 data->last_start_max = is_inf
582 : data->pos_min + data->pos_delta
585 data->longest = &(data->longest_float);
588 if (data && (fl & SF_HAS_EVAL))
589 data->flags |= SF_HAS_EVAL;
592 if (OP(oscan) != CURLYX) {
593 while (regkind[(U8)OP(next = regnext(oscan))] == NOTHING
595 NEXT_OFF(oscan) += NEXT_OFF(next);
599 default: /* REF only? */
600 if (flags & SCF_DO_SUBSTR) {
602 data->longest = &(data->longest_float);
607 } else if (strchr(simple,OP(scan))) {
608 if (flags & SCF_DO_SUBSTR) {
613 } else if (regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
614 data->flags |= (OP(scan) == MEOL
617 } else if (regkind[(U8)OP(scan)] == BRANCHJ
618 && (scan->flags || data)
619 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
620 I32 deltanext, minnext;
624 next = regnext(scan);
625 nscan = NEXTOPER(NEXTOPER(scan));
626 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
629 FAIL("variable length lookbehind not implemented");
630 } else if (minnext > U8_MAX) {
631 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
633 scan->flags = minnext;
635 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
637 if (data && (data_fake.flags & SF_HAS_EVAL))
638 data->flags |= SF_HAS_EVAL;
639 } else if (OP(scan) == OPEN) {
641 } else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
642 #ifdef REGALIGN_STRUCT
643 next = regnext(scan);
645 if ( next && (OP(next) != WHILEM) && next < last)
647 is_par = 0; /* Disable optimization */
648 } else if (OP(scan) == EVAL) {
650 data->flags |= SF_HAS_EVAL;
652 /* Else: zero-length, ignore. */
653 scan = regnext(scan);
658 *deltap = is_inf ? I32_MAX : delta;
659 if (flags & SCF_DO_SUBSTR && is_inf)
660 data->pos_delta = I32_MAX - data->pos_min;
663 if (is_par && pars==1 && data) {
664 data->flags |= SF_IN_PAR;
665 data->flags &= ~SF_HAS_PAR;
666 } else if (pars && data) {
667 data->flags |= SF_HAS_PAR;
668 data->flags &= ~SF_IN_PAR;
674 add_data(I32 n, char *s)
678 sizeof(*rx->data) + sizeof(void*) * (rx->data->count + n - 1),
679 char, struct reg_data);
680 Renew(rx->data->what, rx->data->count + n, U8);
681 rx->data->count += n;
683 Newc(1207, rx->data, sizeof(*rx->data) + sizeof(void*) * (n - 1),
684 char, struct reg_data);
685 New(1208, rx->data->what, n, U8);
688 Copy(s, rx->data->what + rx->data->count - n, n, U8);
689 return rx->data->count - n;
693 - pregcomp - compile a regular expression into internal code
695 * We can't allocate space until we know how big the compiled form will be,
696 * but we can't compile it (and thus know how big it is) until we've got a
697 * place to put the code. So we cheat: we compile it twice, once with code
698 * generation turned off and size counting turned on, and once "for real".
699 * This also means that we don't allocate space until we are sure that the
700 * thing really will compile successfully, and we never have to move the
701 * code and thus invalidate pointers into it. (Note that it has to be in
702 * one piece because free() must be able to free it all.) [NB: not true in perl]
704 * Beware that the optimization-preparation code in here knows about some
705 * of the structure of the compiled regexp. [I'll say.]
708 pregcomp(char *exp, char *xend, PMOP *pm)
722 FAIL("NULL regexp argument");
724 regprecomp = savepvn(exp, xend - exp);
725 DEBUG_r(PerlIO_printf(Perl_debug_log, "compiling RE `%*s'\n",
726 xend - exp, regprecomp));
727 regflags = pm->op_pmflags;
731 seen_zerolen = *exp == '^' ? -1 : 0;
734 /* First pass: determine size, legality. */
741 regc((U8)MAGIC, (char*)regcode);
742 if (reg(0, &flags) == NULL) {
743 Safefree(regprecomp);
747 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", regsize));
752 char *s = PerlEnv_getenv("TERMCAP_COLORS");
756 colors[0] = s = savepv(s);
760 FAIL("Not enough TABs in TERMCAP_COLORS");
769 PerlIO_printf(Perl_debug_log, "%s%s%s%s",
770 colors[0],colors[1],colors[2],colors[3]);
774 /* Small enough for pointer-storage convention?
775 If extralen==0, this means that we will not need long jumps. */
776 #ifndef REGALIGN_STRUCT
777 if (regsize >= 0x10000L && extralen)
778 FAIL("regexp too big");
780 if (regsize >= 0x10000L && extralen)
786 /* Allocate space and initialize. */
787 Newc(1001, r, sizeof(regexp) + (unsigned)regsize * sizeof(regnode),
790 FAIL("regexp out of space");
792 r->prelen = xend - exp;
793 r->precomp = regprecomp;
794 r->subbeg = r->subbase = NULL;
797 /* Second pass: emit code. */
802 regcode = r->program;
803 regc((U8)MAGIC, (char*) regcode++);
805 if (reg(0, &flags) == NULL)
808 /* Dig out information for optimizations. */
809 pm->op_pmflags = regflags;
811 r->regstclass = NULL;
812 r->naughty = regnaughty >= 10; /* Probably an expensive pattern. */
813 scan = r->program + 1; /* First BRANCH. */
814 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
817 STRLEN longest_float_length, longest_fixed_length;
819 StructCopy(&zero_scan_data, &data, scan_data_t);
821 /* Skip introductions and multiplicators >= 1. */
822 while ((OP(first) == OPEN && (sawopen = 1)) ||
823 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
824 (OP(first) == PLUS) ||
825 (OP(first) == MINMOD) ||
826 (regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
827 if (OP(first) == PLUS)
830 first += regarglen[(U8)OP(first)];
831 first = NEXTOPER(first);
834 /* Starting-point info. */
836 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
837 else if (strchr(simple+2,OP(first)))
838 r->regstclass = first;
839 else if (regkind[(U8)OP(first)] == BOUND ||
840 regkind[(U8)OP(first)] == NBOUND)
841 r->regstclass = first;
842 else if (regkind[(U8)OP(first)] == BOL) {
843 r->reganch |= (OP(first) == MBOL ? ROPT_ANCH_MBOL: ROPT_ANCH_BOL);
844 first = NEXTOPER(first);
847 else if (OP(first) == GPOS) {
848 r->reganch |= ROPT_ANCH_GPOS;
849 first = NEXTOPER(first);
852 else if ((OP(first) == STAR &&
853 regkind[(U8)OP(NEXTOPER(first))] == ANY) &&
854 !(r->reganch & ROPT_ANCH) )
856 /* turn .* into ^.* with an implied $*=1 */
857 r->reganch |= ROPT_ANCH_BOL | ROPT_IMPLICIT;
858 first = NEXTOPER(first);
861 if (sawplus && (!sawopen || !regsawback))
862 r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */
864 /* Scan is after the zeroth branch, first is atomic matcher. */
865 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
868 * If there's something expensive in the r.e., find the
869 * longest literal string that must appear and make it the
870 * regmust. Resolve ties in favor of later strings, since
871 * the regstart check works with the beginning of the r.e.
872 * and avoiding duplication strengthens checking. Not a
873 * strong reason, but sufficient in the absence of others.
874 * [Now we resolve ties in favor of the earlier string if
875 * it happens that c_offset_min has been invalidated, since the
876 * earlier string may buy us something the later one won't.]
880 data.longest_fixed = newSVpv("",0);
881 data.longest_float = newSVpv("",0);
882 data.last_found = newSVpv("",0);
883 data.longest = &(data.longest_fixed);
886 minlen = study_chunk(&first, &fake, scan + regsize, /* Up to end */
887 &data, SCF_DO_SUBSTR);
888 if ( regnpar == 1 && data.longest == &(data.longest_fixed)
889 && data.last_start_min == 0 && data.last_end > 0
891 && (!(regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
892 r->reganch |= ROPT_CHECK_ALL;
894 SvREFCNT_dec(data.last_found);
896 longest_float_length = SvCUR(data.longest_float);
897 if (longest_float_length
898 || (data.flags & SF_FL_BEFORE_EOL
899 && (!(data.flags & SF_FL_BEFORE_MEOL)
900 || (regflags & PMf_MULTILINE)))) {
901 if (SvCUR(data.longest_fixed)
902 && data.offset_fixed == data.offset_float_min)
903 goto remove; /* Like in (a)+. */
905 r->float_substr = data.longest_float;
906 r->float_min_offset = data.offset_float_min;
907 r->float_max_offset = data.offset_float_max;
908 fbm_compile(r->float_substr);
909 BmUSEFUL(r->float_substr) = 100;
910 if (data.flags & SF_FL_BEFORE_EOL /* Cannot have SEOL and MULTI */
911 && (!(data.flags & SF_FL_BEFORE_MEOL)
912 || (regflags & PMf_MULTILINE)))
913 SvTAIL_on(r->float_substr);
916 r->float_substr = Nullsv;
917 SvREFCNT_dec(data.longest_float);
918 longest_float_length = 0;
921 longest_fixed_length = SvCUR(data.longest_fixed);
922 if (longest_fixed_length
923 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
924 && (!(data.flags & SF_FIX_BEFORE_MEOL)
925 || (regflags & PMf_MULTILINE)))) {
926 r->anchored_substr = data.longest_fixed;
927 r->anchored_offset = data.offset_fixed;
928 fbm_compile(r->anchored_substr);
929 BmUSEFUL(r->anchored_substr) = 100;
930 if (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
931 && (!(data.flags & SF_FIX_BEFORE_MEOL)
932 || (regflags & PMf_MULTILINE)))
933 SvTAIL_on(r->anchored_substr);
935 r->anchored_substr = Nullsv;
936 SvREFCNT_dec(data.longest_fixed);
937 longest_fixed_length = 0;
940 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
941 if (longest_fixed_length > longest_float_length) {
942 r->check_substr = r->anchored_substr;
943 r->check_offset_min = r->check_offset_max = r->anchored_offset;
944 if (r->reganch & ROPT_ANCH_SINGLE)
945 r->reganch |= ROPT_NOSCAN;
947 r->check_substr = r->float_substr;
948 r->check_offset_min = data.offset_float_min;
949 r->check_offset_max = data.offset_float_max;
952 /* Several toplevels. Best we can is to set minlen. */
955 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
956 scan = r->program + 1;
957 minlen = study_chunk(&scan, &fake, scan + regsize, NULL, 0);
958 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
961 r->nparens = regnpar - 1;
963 if (regseen & REG_SEEN_GPOS)
964 r->reganch |= ROPT_GPOS_SEEN;
965 if (regseen & REG_SEEN_LOOKBEHIND)
966 r->reganch |= ROPT_LOOKBEHIND_SEEN;
967 Newz(1002, r->startp, regnpar, char*);
968 Newz(1002, r->endp, regnpar, char*);
974 - reg - regular expression, i.e. main body or parenthesized thing
976 * Caller must absorb opening parenthesis.
978 * Combining parenthesis handling with the base level of regular expression
979 * is a trifle forced, but the need to tie the tails of the branches to what
980 * follows makes it hard to avoid.
983 reg(I32 paren, I32 *flagp)
984 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
986 register regnode *ret; /* Will be the head of the group. */
987 register regnode *br;
988 register regnode *lastbr;
989 register regnode *ender = 0;
990 register I32 parno = 0;
991 I32 flags, oregflags = regflags, have_branch = 0, open = 0;
994 *flagp = HASWIDTH; /* Tentatively. */
996 /* Make an OPEN node, if parenthesized. */
998 if (*regparse == '?') {
1000 paren = *regparse++;
1001 ret = NULL; /* For look-ahead/behind. */
1004 #ifndef REGALIGN_STRUCT
1005 FAIL("lookbehind non-implemented without REGALIGN_STRUCT");
1007 regseen |= REG_SEEN_LOOKBEHIND;
1008 if (*regparse == '!')
1010 if (*regparse != '=' && *regparse != '!')
1021 FAIL2("Sequence (?%c...) not implemented", (int)paren);
1024 while (*regparse && *regparse != ')')
1026 if (*regparse != ')')
1027 FAIL("Sequence (?#... not terminated");
1034 I32 count = 1, n = 0;
1038 OP_4tree *sop, *rop;
1041 while (count && (c = *regparse)) {
1042 if (c == '\\' && regparse[1])
1050 if (*regparse != ')')
1051 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1055 if (regparse - 1 - s)
1056 sv = newSVpv(s, regparse - 1 - s);
1058 sv = newSVpv("", 0);
1060 rop = sv_compile_2op(sv, &sop, "re", &av);
1062 n = add_data(3, "nso");
1063 rx->data->data[n] = (void*)rop;
1064 rx->data->data[n+1] = (void*)av;
1065 rx->data->data[n+2] = (void*)sop;
1067 } else { /* First pass */
1069 FAIL("Eval-group in insecure regular expression");
1073 return reganode(EVAL, n);
1077 if (regparse[0] == '?') {
1078 if (regparse[1] == '=' || regparse[1] == '!'
1079 || regparse[1] == '<'
1080 || regparse[1] == '{') { /* Lookahead or eval. */
1083 ret = reg_node(LOGICAL);
1084 regtail(ret, reg(1, &flag));
1087 } else if (regparse[0] >= '1' && regparse[0] <= '9' ) {
1088 parno = atoi(regparse++);
1090 while (isDIGIT(*regparse))
1092 ret = reganode(GROUPP, parno);
1093 if ((c = *nextchar()) != ')')
1094 FAIL2("Switch (?(number%c not recognized", c);
1096 regtail(ret, reganode(IFTHEN, 0));
1097 br = regbranch(&flags, 1);
1099 br = reganode(LONGJMP, 0);
1101 regtail(br, reganode(LONGJMP, 0));
1104 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1105 regbranch(&flags, 1);
1106 regtail(ret, lastbr);
1111 FAIL("Switch (?(condition)... contains too many branches");
1112 ender = reg_node(TAIL);
1115 regtail(lastbr, ender);
1116 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1118 regtail(ret, ender);
1121 FAIL2("Unknown condition for (?(%.2s", regparse);
1125 FAIL("Sequence (? incomplete");
1129 while (*regparse && strchr("iogcmsx", *regparse))
1130 pmflag(®flags, *regparse++);
1132 if (*regparse != ')')
1133 FAIL2("Sequence (?%c...) not recognized", *regparse);
1142 ret = reganode(OPEN, parno);
1148 /* Pick up the branches, linking them together. */
1149 br = regbranch(&flags, 1);
1152 if (*regparse == '|') {
1153 if (!SIZE_ONLY && extralen) {
1154 reginsert(BRANCHJ, br);
1156 reginsert(BRANCH, br);
1159 extralen += 1; /* For BRANCHJ-BRANCH. */
1160 } else if (paren == ':') {
1161 *flagp |= flags&SIMPLE;
1163 if (open) { /* Starts with OPEN. */
1164 regtail(ret, br); /* OPEN -> first. */
1165 } else if (paren != '?') /* Not Conditional */
1167 if (!(flags&HASWIDTH))
1168 *flagp &= ~HASWIDTH;
1169 *flagp |= flags&SPSTART;
1171 while (*regparse == '|') {
1172 if (!SIZE_ONLY && extralen) {
1173 ender = reganode(LONGJMP,0);
1174 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1177 extralen += 2; /* Account for LONGJMP. */
1179 br = regbranch(&flags, 0);
1182 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1184 if (!(flags&HASWIDTH))
1185 *flagp &= ~HASWIDTH;
1186 *flagp |= flags&SPSTART;
1189 if (have_branch || paren != ':') {
1190 /* Make a closing node, and hook it on the end. */
1193 ender = reg_node(TAIL);
1196 ender = reganode(CLOSE, parno);
1203 ender = reg_node(SUCCEED);
1204 *flagp &= ~HASWIDTH;
1207 ender = reg_node(END);
1210 regtail(lastbr, ender);
1213 /* Hook the tails of the branches to the closing node. */
1214 for (br = ret; br != NULL; br = regnext(br)) {
1215 regoptail(br, ender);
1222 static char parens[] = "=!<,>";
1224 if (paren && (p = strchr(parens, paren))) {
1225 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1226 int flag = (p - parens) > 1;
1229 node = SUSPEND, flag = 0;
1230 reginsert(node,ret);
1231 #ifdef REGALIGN_STRUCT
1234 regtail(ret, reg_node(TAIL));
1238 /* Check for proper termination. */
1239 if (paren && (regparse >= regxend || *nextchar() != ')')) {
1240 FAIL("unmatched () in regexp");
1241 } else if (!paren && regparse < regxend) {
1242 if (*regparse == ')') {
1243 FAIL("unmatched () in regexp");
1245 FAIL("junk on end of regexp"); /* "Can't happen". */
1249 regflags = oregflags;
1256 - regbranch - one alternative of an | operator
1258 * Implements the concatenation operator.
1261 regbranch(I32 *flagp, I32 first)
1263 register regnode *ret;
1264 register regnode *chain = NULL;
1265 register regnode *latest;
1266 I32 flags = 0, c = 0;
1271 if (!SIZE_ONLY && extralen)
1272 ret = reganode(BRANCHJ,0);
1274 ret = reg_node(BRANCH);
1277 if (!first && SIZE_ONLY)
1278 extralen += 1; /* BRANCHJ */
1280 *flagp = WORST; /* Tentatively. */
1284 while (regparse < regxend && *regparse != '|' && *regparse != ')') {
1286 latest = regpiece(&flags);
1287 if (latest == NULL) {
1288 if (flags & TRYAGAIN)
1291 } else if (ret == NULL)
1293 *flagp |= flags&HASWIDTH;
1294 if (chain == NULL) /* First piece. */
1295 *flagp |= flags&SPSTART;
1298 regtail(chain, latest);
1303 if (chain == NULL) { /* Loop ran zero times. */
1304 chain = reg_node(NOTHING);
1309 *flagp |= flags&SIMPLE;
1316 - regpiece - something followed by possible [*+?]
1318 * Note that the branching code sequences used for ? and the general cases
1319 * of * and + are somewhat optimized: they use the same NOTHING node as
1320 * both the endmarker for their branch list and the body of the last branch.
1321 * It might seem that this node could be dispensed with entirely, but the
1322 * endmarker role is not redundant.
1325 regpiece(I32 *flagp)
1327 register regnode *ret;
1329 register char *next;
1331 char *origparse = regparse;
1334 I32 max = REG_INFTY;
1336 ret = regatom(&flags);
1338 if (flags & TRYAGAIN)
1345 if (op == '{' && regcurly(regparse)) {
1346 next = regparse + 1;
1348 while (isDIGIT(*next) || *next == ',') {
1357 if (*next == '}') { /* got one */
1361 min = atoi(regparse);
1367 if (!max && *maxpos != '0')
1368 max = REG_INFTY; /* meaning "infinity" */
1369 else if (max >= REG_INFTY)
1370 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
1375 if ((flags&SIMPLE)) {
1376 regnaughty += 2 + regnaughty / 2;
1377 reginsert(CURLY, ret);
1380 regnaughty += 4 + regnaughty; /* compound interest */
1381 regtail(ret, reg_node(WHILEM));
1382 if (!SIZE_ONLY && extralen) {
1383 reginsert(LONGJMP,ret);
1384 reginsert(NOTHING,ret);
1385 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1387 reginsert(CURLYX,ret);
1388 if (!SIZE_ONLY && extralen)
1389 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1390 regtail(ret, reg_node(NOTHING));
1394 #ifdef REGALIGN_STRUCT
1399 *flagp = (WORST|HASWIDTH);
1400 if (max && max < min)
1401 FAIL("Can't do {n,m} with n > m");
1416 #if 0 /* Now runtime fix should be reliable. */
1417 if (!(flags&HASWIDTH) && op != '?')
1418 FAIL("regexp *+ operand could be empty");
1423 *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1425 if (op == '*' && (flags&SIMPLE)) {
1426 reginsert(STAR, ret);
1427 #ifdef REGALIGN_STRUCT
1432 else if (op == '*') {
1435 } else if (op == '+' && (flags&SIMPLE)) {
1436 reginsert(PLUS, ret);
1437 #ifdef REGALIGN_STRUCT
1442 else if (op == '+') {
1445 } else if (op == '?') {
1450 if (dowarn && !SIZE_ONLY && !(flags&HASWIDTH) && max > 10000) {
1451 warn("%.*s matches null string many times",
1452 regparse - origparse, origparse);
1455 if (*regparse == '?') {
1457 reginsert(MINMOD, ret);
1459 regtail(ret, ret + NODE_STEP_REGNODE);
1461 regtail(ret, ret + 3);
1464 if (ISMULT2(regparse))
1465 FAIL("nested *?+ in regexp");
1471 - regatom - the lowest level
1473 * Optimization: gobbles an entire sequence of ordinary characters so that
1474 * it can turn them into a single node, which is smaller to store and
1475 * faster to run. Backslashed characters are exceptions, each becoming a
1476 * separate node; the code is simpler that way and it's not worth fixing.
1478 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1483 register regnode *ret = 0;
1486 *flagp = WORST; /* Tentatively. */
1489 switch (*regparse) {
1493 if (regflags & PMf_MULTILINE)
1494 ret = reg_node(MBOL);
1495 else if (regflags & PMf_SINGLELINE)
1496 ret = reg_node(SBOL);
1498 ret = reg_node(BOL);
1504 if (regflags & PMf_MULTILINE)
1505 ret = reg_node(MEOL);
1506 else if (regflags & PMf_SINGLELINE)
1507 ret = reg_node(SEOL);
1509 ret = reg_node(EOL);
1513 if (regflags & PMf_SINGLELINE)
1514 ret = reg_node(SANY);
1516 ret = reg_node(ANY);
1518 *flagp |= HASWIDTH|SIMPLE;
1523 *flagp |= HASWIDTH|SIMPLE;
1527 ret = reg(1, &flags);
1529 if (flags & TRYAGAIN)
1533 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
1537 if (flags & TRYAGAIN) {
1541 FAIL2("internal urp in regexp at /%s/", regparse);
1542 /* Supposed to be caught earlier. */
1545 if (!regcurly(regparse)) {
1553 FAIL("?+*{} follows nothing in regexp");
1556 switch (*++regparse) {
1559 ret = reg_node(SBOL);
1564 ret = reg_node(GPOS);
1565 regseen |= REG_SEEN_GPOS;
1570 ret = reg_node(SEOL);
1575 ret = reg_node((regflags & PMf_LOCALE) ? ALNUML : ALNUM);
1576 *flagp |= HASWIDTH|SIMPLE;
1580 ret = reg_node((regflags & PMf_LOCALE) ? NALNUML : NALNUM);
1581 *flagp |= HASWIDTH|SIMPLE;
1586 ret = reg_node((regflags & PMf_LOCALE) ? BOUNDL : BOUND);
1592 ret = reg_node((regflags & PMf_LOCALE) ? NBOUNDL : NBOUND);
1597 ret = reg_node((regflags & PMf_LOCALE) ? SPACEL : SPACE);
1598 *flagp |= HASWIDTH|SIMPLE;
1602 ret = reg_node((regflags & PMf_LOCALE) ? NSPACEL : NSPACE);
1603 *flagp |= HASWIDTH|SIMPLE;
1607 ret = reg_node(DIGIT);
1608 *flagp |= HASWIDTH|SIMPLE;
1612 ret = reg_node(NDIGIT);
1613 *flagp |= HASWIDTH|SIMPLE;
1626 case '1': case '2': case '3': case '4':
1627 case '5': case '6': case '7': case '8': case '9':
1629 I32 num = atoi(regparse);
1631 if (num > 9 && num >= regnpar)
1635 ret = reganode((regflags & PMf_FOLD)
1636 ? ((regflags & PMf_LOCALE) ? REFFL : REFF)
1639 while (isDIGIT(*regparse))
1647 if (regparse >= regxend)
1648 FAIL("trailing \\ in regexp");
1656 if (regflags & PMf_EXTENDED) {
1657 while (regparse < regxend && *regparse != '\n') regparse++;
1658 if (regparse < regxend)
1673 ret = reg_node((regflags & PMf_FOLD)
1674 ? ((regflags & PMf_LOCALE) ? EXACTFL : EXACTF)
1676 s = (char *) OPERAND(ret);
1677 regc(0, s++); /* save spot for len */
1678 for (len = 0, p = regparse - 1;
1679 len < 127 && p < regxend;
1684 if (regflags & PMf_EXTENDED)
1685 p = regwhite(p, regxend);
1735 ender = scan_hex(++p, 2, &numlen);
1740 ender = UCHARAT(p++);
1741 ender = toCTRL(ender);
1743 case '0': case '1': case '2': case '3':case '4':
1744 case '5': case '6': case '7': case '8':case '9':
1746 (isDIGIT(p[1]) && atoi(p) >= regnpar) ) {
1747 ender = scan_oct(p, 3, &numlen);
1757 FAIL("trailing \\ in regexp");
1768 if (regflags & PMf_EXTENDED)
1769 p = regwhite(p, regxend);
1770 if (ISMULT2(p)) { /* Back off on ?+*. */
1785 FAIL("internal disaster in regexp");
1791 *OPERAND(ret) = len;
1794 #ifdef REGALIGN_STRUCT
1795 regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
1798 regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
1808 regwhite(char *p, char *e)
1813 else if (*p == '#') {
1816 } while (p < e && *p != '\n');
1825 regset(char *opnd, register I32 c)
1830 opnd[1 + (c >> 3)] |= (1 << (c & 7));
1836 register char *opnd, *s;
1838 register I32 lastclass = 1234;
1839 register I32 range = 0;
1840 register regnode *ret;
1844 s = opnd = (char *) OPERAND(regcode);
1845 ret = reg_node(ANYOF);
1846 for (Class = 0; Class < 33; Class++)
1848 if (*regparse == '^') { /* Complement of range. */
1852 *opnd |= ANYOF_INVERT;
1855 regcode += ANY_SKIP;
1856 if (regflags & PMf_FOLD)
1857 *opnd |= ANYOF_FOLD;
1858 if (regflags & PMf_LOCALE)
1859 *opnd |= ANYOF_LOCALE;
1861 regsize += ANY_SKIP;
1863 if (*regparse == ']' || *regparse == '-')
1864 goto skipcond; /* allow 1st char to be ] or - */
1865 while (regparse < regxend && *regparse != ']') {
1867 Class = UCHARAT(regparse++);
1868 if (Class == '[' && regparse + 1 < regxend &&
1869 /* I smell either [: or [= or [. -- POSIX has been here, right? */
1870 (*regparse == ':' || *regparse == '=' || *regparse == '.')) {
1871 char posixccc = *regparse;
1872 char* posixccs = regparse++;
1874 while (regparse < regxend && *regparse != posixccc)
1876 if (regparse == regxend)
1877 /* Grandfather lone [:, [=, [. */
1878 regparse = posixccs;
1880 regparse++; /* skip over the posixccc */
1881 if (*regparse == ']') {
1882 /* Not Implemented Yet.
1883 * (POSIX Extended Character Classes, that is)
1884 * The text between e.g. [: and :] would start
1885 * at posixccs + 1 and stop at regparse - 2. */
1886 if (dowarn && !SIZE_ONLY)
1887 warn("Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc);
1888 regparse++; /* skip over the ending ] */
1892 if (Class == '\\') {
1893 Class = UCHARAT(regparse++);
1896 if (regflags & PMf_LOCALE) {
1898 *opnd |= ANYOF_ALNUML;
1901 for (Class = 0; Class < 256; Class++)
1903 regset(opnd, Class);
1908 if (regflags & PMf_LOCALE) {
1910 *opnd |= ANYOF_NALNUML;
1913 for (Class = 0; Class < 256; Class++)
1914 if (!isALNUM(Class))
1915 regset(opnd, Class);
1920 if (regflags & PMf_LOCALE) {
1922 *opnd |= ANYOF_SPACEL;
1925 for (Class = 0; Class < 256; Class++)
1927 regset(opnd, Class);
1932 if (regflags & PMf_LOCALE) {
1934 *opnd |= ANYOF_NSPACEL;
1937 for (Class = 0; Class < 256; Class++)
1938 if (!isSPACE(Class))
1939 regset(opnd, Class);
1944 for (Class = '0'; Class <= '9'; Class++)
1945 regset(opnd, Class);
1949 for (Class = 0; Class < '0'; Class++)
1950 regset(opnd, Class);
1951 for (Class = '9' + 1; Class < 256; Class++)
1952 regset(opnd, Class);
1977 Class = scan_hex(regparse, 2, &numlen);
1981 Class = UCHARAT(regparse++);
1982 Class = toCTRL(Class);
1984 case '0': case '1': case '2': case '3': case '4':
1985 case '5': case '6': case '7': case '8': case '9':
1986 Class = scan_oct(--regparse, 3, &numlen);
1992 if (lastclass > Class)
1993 FAIL("invalid [] range in regexp");
1998 if (*regparse == '-' && regparse+1 < regxend &&
1999 regparse[1] != ']') {
2002 continue; /* do it next time */
2005 for ( ; lastclass <= Class; lastclass++)
2006 regset(opnd, lastclass);
2009 if (*regparse != ']')
2010 FAIL("unmatched [] in regexp");
2018 char* retval = regparse++;
2021 if (*regparse == '(' && regparse[1] == '?' &&
2022 regparse[2] == '#') {
2023 while (*regparse && *regparse != ')')
2028 if (regflags & PMf_EXTENDED) {
2029 if (isSPACE(*regparse)) {
2033 else if (*regparse == '#') {
2034 while (*regparse && *regparse != '\n')
2045 - reg_node - emit a node
2047 static regnode * /* Location. */
2050 register regnode *ret;
2051 register regnode *ptr;
2055 SIZE_ALIGN(regsize);
2056 #ifdef REGALIGN_STRUCT
2064 NODE_ALIGN_FILL(ret);
2066 FILL_ADVANCE_NODE(ptr, op);
2073 - reganode - emit a node with an argument
2075 static regnode * /* Location. */
2076 reganode(U8 op, U32 arg)
2078 register regnode *ret;
2079 register regnode *ptr;
2083 SIZE_ALIGN(regsize);
2092 NODE_ALIGN_FILL(ret);
2094 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
2101 - regc - emit (if appropriate) a byte of code
2111 - reginsert - insert an operator in front of already-emitted operand
2113 * Means relocating the operand.
2116 reginsert(U8 op, regnode *opnd)
2118 register regnode *src;
2119 register regnode *dst;
2120 register regnode *place;
2121 register int offset = regarglen[(U8)op];
2123 /* (regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2126 regsize += NODE_STEP_REGNODE + offset;
2131 regcode += NODE_STEP_REGNODE + offset;
2134 StructCopy(--src, --dst, regnode);
2136 place = opnd; /* Op node, where operand used to be. */
2137 src = NEXTOPER(place);
2138 FILL_ADVANCE_NODE(place, op);
2139 Zero(src, offset, regnode);
2140 #if defined(REGALIGN) && !defined(REGALIGN_STRUCT)
2141 src[offset + 1] = '\177';
2146 - regtail - set the next-pointer at the end of a node chain of p to val.
2149 regtail(regnode *p, regnode *val)
2151 register regnode *scan;
2152 register regnode *temp;
2153 register I32 offset;
2158 /* Find last node. */
2161 temp = regnext(scan);
2168 # ifdef REGALIGN_STRUCT
2169 if (reg_off_by_arg[OP(scan)]) {
2170 ARG_SET(scan, val - scan);
2172 NEXT_OFF(scan) = val - scan;
2175 offset = val - scan;
2177 *(short*)(scan+1) = offset;
2181 if (OP(scan) == BACK)
2182 offset = scan - val;
2184 offset = val - scan;
2185 *(scan+1) = (offset>>8)&0377;
2186 *(scan+2) = offset&0377;
2191 - regoptail - regtail on operand of first argument; nop if operandless
2194 regoptail(regnode *p, regnode *val)
2196 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
2197 if (p == NULL || SIZE_ONLY)
2199 if (regkind[(U8)OP(p)] == BRANCH) {
2200 regtail(NEXTOPER(p), val);
2201 } else if ( regkind[(U8)OP(p)] == BRANCHJ) {
2202 regtail(NEXTOPER(NEXTOPER(p)), val);
2208 - regcurly - a little FSA that accepts {\d+,?\d*}
2211 regcurly(register char *s)
2231 dumpuntil(regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
2233 register char op = EXACT; /* Arbitrary non-END op. */
2234 register regnode *next, *onode;
2236 while (op != END && (!last || node < last)) {
2237 /* While that wasn't END last time... */
2243 next = regnext(node);
2245 if (OP(node) == OPTIMIZED)
2248 PerlIO_printf(Perl_debug_log, "%4d%*s%s", node - start,
2249 2*l + 1, "", SvPVX(sv));
2250 if (next == NULL) /* Next ptr. */
2251 PerlIO_printf(Perl_debug_log, "(0)");
2253 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
2254 (void)PerlIO_putc(Perl_debug_log, '\n');
2256 if (regkind[(U8)op] == BRANCHJ) {
2257 register regnode *nnode = (OP(next) == LONGJMP
2260 if (last && nnode > last)
2262 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
2263 } else if (regkind[(U8)op] == BRANCH) {
2264 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
2265 } else if ( op == CURLY) { /* `next' might be very big: optimizer */
2266 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2267 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
2268 } else if (regkind[(U8)op] == CURLY && op != CURLYX) {
2269 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2271 } else if ( op == PLUS || op == STAR) {
2272 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
2273 } else if (op == ANYOF) {
2274 node = NEXTOPER(node);
2276 } else if (regkind[(U8)op] == EXACT) {
2277 /* Literal string, where present. */
2278 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2279 node = NEXTOPER(node);
2281 node = NEXTOPER(node);
2282 node += regarglen[(U8)op];
2284 if (op == CURLYX || op == OPEN)
2286 else if (op == WHILEM)
2293 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
2298 SV *sv = sv_newmortal();
2300 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
2302 /* Header fields of interest. */
2303 if (r->anchored_substr)
2304 PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
2306 SvPVX(r->anchored_substr),
2308 SvTAIL(r->anchored_substr) ? "$" : "",
2309 r->anchored_offset);
2310 if (r->float_substr)
2311 PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
2313 SvPVX(r->float_substr),
2315 SvTAIL(r->float_substr) ? "$" : "",
2316 r->float_min_offset, r->float_max_offset);
2317 if (r->check_substr)
2318 PerlIO_printf(Perl_debug_log,
2319 r->check_substr == r->float_substr
2320 ? "(checking floating" : "(checking anchored");
2321 if (r->reganch & ROPT_NOSCAN)
2322 PerlIO_printf(Perl_debug_log, " noscan");
2323 if (r->reganch & ROPT_CHECK_ALL)
2324 PerlIO_printf(Perl_debug_log, " isall");
2325 if (r->check_substr)
2326 PerlIO_printf(Perl_debug_log, ") ");
2328 if (r->regstclass) {
2329 regprop(sv, r->regstclass);
2330 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
2332 if (r->reganch & ROPT_ANCH) {
2333 PerlIO_printf(Perl_debug_log, "anchored");
2334 if (r->reganch & ROPT_ANCH_BOL)
2335 PerlIO_printf(Perl_debug_log, "(BOL)");
2336 if (r->reganch & ROPT_ANCH_MBOL)
2337 PerlIO_printf(Perl_debug_log, "(MBOL)");
2338 if (r->reganch & ROPT_ANCH_GPOS)
2339 PerlIO_printf(Perl_debug_log, "(GPOS)");
2340 PerlIO_putc(Perl_debug_log, ' ');
2342 if (r->reganch & ROPT_GPOS_SEEN)
2343 PerlIO_printf(Perl_debug_log, "GPOS ");
2344 if (r->reganch & ROPT_SKIP)
2345 PerlIO_printf(Perl_debug_log, "plus ");
2346 if (r->reganch & ROPT_IMPLICIT)
2347 PerlIO_printf(Perl_debug_log, "implicit ");
2348 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
2349 PerlIO_printf(Perl_debug_log, "\n");
2353 - regprop - printable representation of opcode
2356 regprop(SV *sv, regnode *o)
2358 register char *p = 0;
2393 sv_catpvf(sv, "EXACT <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]);
2396 sv_catpvf(sv, "EXACTF <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]);
2399 sv_catpvf(sv, "EXACTFL <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]);
2426 sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o));
2429 #ifdef REGALIGN_STRUCT
2430 sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
2432 sv_catpvf(sv, "CURLYM {%d,%d}", ARG1(o), ARG2(o));
2436 #ifdef REGALIGN_STRUCT
2437 sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
2439 sv_catpvf(sv, "CURLYN {%d,%d}", ARG1(o), ARG2(o));
2443 sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o));
2446 sv_catpvf(sv, "REF%d", ARG(o));
2449 sv_catpvf(sv, "REFF%d", ARG(o));
2452 sv_catpvf(sv, "REFFL%d", ARG(o));
2455 sv_catpvf(sv, "OPEN%d", ARG(o));
2458 sv_catpvf(sv, "CLOSE%d", ARG(o));
2474 #ifdef REGALIGN_STRUCT
2475 sv_catpvf(sv, "UNLESSM[-%d]", o->flags);
2481 #ifdef REGALIGN_STRUCT
2482 sv_catpvf(sv, "IFMATCH[-%d]", o->flags);
2536 sv_catpvf(sv, "GROUPP%d", ARG(o));
2551 FAIL("corrupted regexp opcode");
2556 #endif /* DEBUGGING */
2559 pregfree(struct regexp *r)
2561 if (!r || (--r->refcnt > 0))
2564 Safefree(r->precomp);
2566 Safefree(r->subbase);
2567 if (r->anchored_substr)
2568 SvREFCNT_dec(r->anchored_substr);
2569 if (r->float_substr)
2570 SvREFCNT_dec(r->float_substr);
2572 int n = r->data->count;
2574 switch (r->data->what[n]) {
2576 SvREFCNT_dec((SV*)r->data->data[n]);
2579 op_free((OP_4tree*)r->data->data[n]);
2584 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
2587 Safefree(r->data->what);
2590 Safefree(r->startp);
2596 - regnext - dig the "next" pointer out of a node
2598 * [Note, when REGALIGN is defined there are two places in regmatch()
2599 * that bypass this code for speed.]
2602 regnext(register regnode *p)
2604 register I32 offset;
2609 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
2625 re_croak2(const char* pat1,const char* pat2,...)
2629 re_croak2(const char* pat1,const char* pat2, va_alist)
2636 STRLEN l1 = strlen(pat1);
2637 STRLEN l2 = strlen(pat2);
2645 Copy(pat1, buf, l1 , char);
2646 Copy(pat2, buf + l1, l2 , char);
2647 buf[l1 + l2 + 1] = '\n';
2648 buf[l1 + l2 + 2] = '\0';
2650 va_start(args, pat2);
2654 message = mess(buf, &args);
2656 l1 = strlen(message);
2659 Copy(message, buf, l1 , char);
2660 buf[l1] = '\0'; /* Overwrite \n */