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.
62 # if defined(BUGGY_MSC6)
63 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
64 # pragma optimize("a",off)
65 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
66 # pragma optimize("w",on )
67 # endif /* BUGGY_MSC6 */
74 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
75 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
76 ((*s) == '{' && regcurly(s)))
78 #define PERL_META "^$.[()|?+*\\"
80 #define META "^$.[()|?+*\\"
84 #undef SPSTART /* dratted cpp namespace... */
87 * Flags to be passed up and down.
89 #define WORST 0 /* Worst case. */
90 #define HASWIDTH 0x1 /* Known never to match null string. */
91 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
92 #define SPSTART 0x4 /* Starts with * or +. */
93 #define TRYAGAIN 0x8 /* Weeded out a declaration. */
96 * Forward declarations for pregcomp()'s friends.
99 static char *reg _((I32, I32 *));
100 static char *reganode _((char, unsigned short));
101 static char *regatom _((I32 *));
102 static char *regbranch _((I32 *));
103 static void regc _((char));
104 static char *regclass _((void));
105 STATIC I32 regcurly _((char *));
106 static char *regnode _((char));
107 static char *regpiece _((I32 *));
108 static void reginsert _((char, char *));
109 static void regoptail _((char *, char *));
110 static void regtail _((char *, char *));
111 static char* regwhite _((char *, char *));
112 static char* nextchar _((void));
115 - pregcomp - compile a regular expression into internal code
117 * We can't allocate space until we know how big the compiled form will be,
118 * but we can't compile it (and thus know how big it is) until we've got a
119 * place to put the code. So we cheat: we compile it twice, once with code
120 * generation turned off and size counting turned on, and once "for real".
121 * This also means that we don't allocate space until we are sure that the
122 * thing really will compile successfully, and we never have to move the
123 * code and thus invalidate pointers into it. (Note that it has to be in
124 * one piece because free() must be able to free it all.) [NB: not true in perl]
126 * Beware that the optimization-preparation code in here knows about some
127 * of the structure of the compiled regexp. [I'll say.]
130 pregcomp(exp,xend,pm)
137 register SV *longish;
140 register char *first;
148 #define MAX_REPEAT_DEPTH 12
152 } repeat_stack[MAX_REPEAT_DEPTH];
153 I32 repeat_depth = 0;
154 I32 repeat_count = 1; /* We start unmultiplied. */
157 croak("NULL regexp argument");
159 regprecomp = savepvn(exp, xend - exp);
160 regflags = pm->op_pmflags;
163 /* First pass: determine size, legality. */
171 if (reg(0, &flags) == NULL) {
172 Safefree(regprecomp);
177 /* Small enough for pointer-storage convention? */
178 if (regsize >= 32767L) /* Probably could be 65535L. */
179 FAIL("regexp too big");
181 /* Allocate space and initialize. */
182 Newc(1001, r, sizeof(regexp) + (unsigned)regsize, char, regexp);
184 FAIL("regexp out of space");
185 r->prelen = xend - exp;
186 r->precomp = regprecomp;
187 r->subbeg = r->subbase = NULL;
189 /* Second pass: emit code. */
194 regcode = r->program;
196 if (reg(0, &flags) == NULL)
199 /* Dig out information for optimizations. */
200 pm->op_pmflags = regflags;
201 r->regstart = Nullsv; /* Worst-case defaults. */
205 r->regstclass = Nullch;
206 r->naughty = regnaughty >= 10; /* Probably an expensive pattern. */
207 scan = r->program+1; /* First BRANCH. */
208 if (OP(regnext(scan)) == END) {/* Only one top-level choice. */
209 scan = NEXTOPER(scan);
212 while ((OP(first) == OPEN && (sawopen = 1)) ||
213 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
214 (OP(first) == PLUS) ||
215 (OP(first) == MINMOD) ||
216 (regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
217 if (OP(first) == PLUS)
220 first += regarglen[(U8)OP(first)];
221 first = NEXTOPER(first);
224 /* Starting-point info. */
226 if (OP(first) == EXACT) {
227 r->regstart = newSVpv(OPERAND(first)+1,*OPERAND(first));
228 if (SvCUR(r->regstart) > !sawstudy)
229 fbm_compile(r->regstart);
230 (void)SvUPGRADE(r->regstart, SVt_PVBM);
232 else if (strchr(simple+2,OP(first)))
233 r->regstclass = first;
234 else if (regkind[(U8)OP(first)] == BOUND ||
235 regkind[(U8)OP(first)] == NBOUND)
236 r->regstclass = first;
237 else if (regkind[(U8)OP(first)] == BOL) {
238 r->reganch |= ROPT_ANCH_BOL;
239 first = NEXTOPER(first);
242 else if (OP(first) == GPOS) {
243 r->reganch |= ROPT_ANCH_GPOS;
244 first = NEXTOPER(first);
247 else if ((OP(first) == STAR &&
248 regkind[(U8)OP(NEXTOPER(first))] == ANY) &&
249 !(r->reganch & ROPT_ANCH) )
251 /* turn .* into ^.* with an implied $*=1 */
252 r->reganch |= ROPT_ANCH_BOL | ROPT_IMPLICIT;
253 first = NEXTOPER(first);
256 if (sawplus && (!sawopen || !regsawback))
257 r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */
259 DEBUG_r(PerlIO_printf(Perl_debug_log, "first %d next %d offset %ld\n",
260 OP(first), OP(NEXTOPER(first)), (long)(first - scan)));
262 * If there's something expensive in the r.e., find the
263 * longest literal string that must appear and make it the
264 * regmust. Resolve ties in favor of later strings, since
265 * the regstart check works with the beginning of the r.e.
266 * and avoiding duplication strengthens checking. Not a
267 * strong reason, but sufficient in the absence of others.
268 * [Now we resolve ties in favor of the earlier string if
269 * it happens that curback has been invalidated, since the
270 * earlier string may buy us something the later one won't.]
272 longish = newSVpv("",0);
273 longest = newSVpv("",0);
279 while (OP(scan) != END) {
280 if (OP(scan) == BRANCH) {
281 if (OP(regnext(scan)) == BRANCH) {
283 while (OP(scan) == BRANCH)
284 scan = regnext(scan);
286 else /* single branch is ok */
287 scan = NEXTOPER(scan);
290 if (OP(scan) == UNLESSM) {
292 scan = regnext(scan);
295 if (OP(scan) == EXACT) {
299 while ((t = regnext(scan)) && OP(t) == CLOSE)
301 minlen += *OPERAND(first) * repeat_count;
302 if (curback - backish == len) {
303 sv_catpvn(longish, OPERAND(first)+1,
305 len += *OPERAND(first);
306 curback += *OPERAND(first);
307 first = regnext(scan);
309 else if (*OPERAND(first) >= len + (curback >= 0)) {
310 len = *OPERAND(first);
311 sv_setpvn(longish, OPERAND(first)+1,len);
314 first = regnext(scan);
317 curback += *OPERAND(first);
319 else if (strchr(varies,OP(scan))) {
323 if (repeat_depth < MAX_REPEAT_DEPTH
324 && ((OP(scan) == PLUS
326 && (next = NEXTOPER(scan)))
327 || (regkind[(U8)OP(scan)] == CURLY
328 && (tcount = ARG1(scan))
329 && (next = NEXTOPER(scan)+4))))
331 /* We treat (abc)+ as (abc)(abc)*. */
333 /* Mark the place to return back. */
334 repeat_stack[repeat_depth].opcode = regnext(scan);
335 repeat_stack[repeat_depth].count = repeat_count;
337 repeat_count *= tcount;
346 if (SvCUR(longish) > SvCUR(longest)) {
347 sv_setsv(longest,longish);
350 sv_setpvn(longish,"",0);
353 else if (strchr(simple,OP(scan))) {
355 minlen += repeat_count;
357 if (SvCUR(longish) > SvCUR(longest)) {
358 sv_setsv(longest,longish);
361 sv_setpvn(longish,"",0);
363 scan = regnext(scan);
364 if (!scan) { /* Go up PLUS or CURLY. */
366 croak("panic: re scan");
367 scan = repeat_stack[repeat_depth].opcode;
368 repeat_count = repeat_stack[repeat_depth].count;
369 /* Need to submit the longest string found: */
372 if (SvCUR(longish) > SvCUR(longest)) {
373 sv_setsv(longest,longish);
376 sv_setpvn(longish,"",0);
380 /* Prefer earlier on tie, unless we can tail match latter */
382 if (SvCUR(longish) + (first && regkind[(U8)OP(first)] == EOL)
385 sv_setsv(longest,longish);
389 sv_setpvn(longish,"",0);
392 || !fbm_instr((unsigned char*) SvPVX(r->regstart),
393 (unsigned char *) (SvPVX(r->regstart)
394 + SvCUR(r->regstart)),
397 r->regmust = longest;
400 r->regback = backest;
401 if (SvCUR(longest) > !(sawstudy ||
402 (first && regkind[(U8)OP(first)] == EOL)))
403 fbm_compile(r->regmust);
404 (void)SvUPGRADE(r->regmust, SVt_PVBM);
405 BmUSEFUL(r->regmust) = 100;
406 if (first && regkind[(U8)OP(first)] == EOL && SvCUR(longish))
407 SvTAIL_on(r->regmust);
410 SvREFCNT_dec(longest);
413 SvREFCNT_dec(longish);
416 r->nparens = regnpar - 1;
418 Newz(1002, r->startp, regnpar, char*);
419 Newz(1002, r->endp, regnpar, char*);
425 - reg - regular expression, i.e. main body or parenthesized thing
427 * Caller must absorb opening parenthesis.
429 * Combining parenthesis handling with the base level of regular expression
430 * is a trifle forced, but the need to tie the tails of the branches to what
431 * follows makes it hard to avoid.
435 I32 paren; /* Parenthesized? */
440 register char *ender = 0;
441 register I32 parno = 0;
444 *flagp = HASWIDTH; /* Tentatively. */
446 /* Make an OPEN node, if parenthesized. */
448 if (*regparse == '?') {
459 croak("Sequence (?%c...) not implemented", (int)paren);
462 while (*regparse && *regparse != ')')
464 if (*regparse != ')')
465 croak("Sequence (?#... not terminated");
470 croak("Sequence (? incomplete");
474 while (*regparse && strchr("iogcmsx", *regparse))
475 pmflag(®flags, *regparse++);
476 if (*regparse != ')')
477 croak("Sequence (?%c...) not recognized", *regparse);
486 ret = reganode(OPEN, parno);
491 /* Pick up the branches, linking them together. */
492 br = regbranch(&flags);
496 regtail(ret, br); /* OPEN -> first. */
499 if (!(flags&HASWIDTH))
501 *flagp |= flags&SPSTART;
502 while (*regparse == '|') {
504 br = regbranch(&flags);
507 regtail(ret, br); /* BRANCH -> BRANCH. */
508 if (!(flags&HASWIDTH))
510 *flagp |= flags&SPSTART;
513 /* Make a closing node, and hook it on the end. */
516 ender = regnode(NOTHING);
519 ender = reganode(CLOSE, parno);
523 ender = regnode(SUCCEED);
527 ender = regnode(END);
532 /* Hook the tails of the branches to the closing node. */
533 for (br = ret; br != NULL; br = regnext(br))
534 regoptail(br, ender);
537 reginsert(IFMATCH,ret);
538 regtail(ret, regnode(NOTHING));
540 else if (paren == '!') {
541 reginsert(UNLESSM,ret);
542 regtail(ret, regnode(NOTHING));
545 /* Check for proper termination. */
546 if (paren && (regparse >= regxend || *nextchar() != ')')) {
547 FAIL("unmatched () in regexp");
548 } else if (!paren && regparse < regxend) {
549 if (*regparse == ')') {
550 FAIL("unmatched () in regexp");
552 FAIL("junk on end of regexp"); /* "Can't happen". */
560 - regbranch - one alternative of an | operator
562 * Implements the concatenation operator.
569 register char *chain;
570 register char *latest;
573 *flagp = WORST; /* Tentatively. */
575 ret = regnode(BRANCH);
579 while (regparse < regxend && *regparse != '|' && *regparse != ')') {
581 latest = regpiece(&flags);
582 if (latest == NULL) {
583 if (flags & TRYAGAIN)
587 *flagp |= flags&HASWIDTH;
588 if (chain == NULL) /* First piece. */
589 *flagp |= flags&SPSTART;
592 regtail(chain, latest);
596 if (chain == NULL) /* Loop ran zero times. */
597 (void) regnode(NOTHING);
603 - regpiece - something followed by possible [*+?]
605 * Note that the branching code sequences used for ? and the general cases
606 * of * and + are somewhat optimized: they use the same NOTHING node as
607 * both the endmarker for their branch list and the body of the last branch.
608 * It might seem that this node could be dispensed with entirely, but the
609 * endmarker role is not redundant.
619 char *origparse = regparse;
624 ret = regatom(&flags);
626 if (flags & TRYAGAIN)
632 if (op == '(' && regparse[1] == '?' && regparse[2] == '#') {
633 while (op && op != ')')
641 if (op == '{' && regcurly(regparse)) {
644 while (isDIGIT(*next) || *next == ',') {
653 if (*next == '}') { /* got one */
657 min = atoi(regparse);
663 if (!max && *maxpos != '0')
664 max = 32767; /* meaning "infinity" */
669 if ((flags&SIMPLE)) {
670 regnaughty += 2 + regnaughty / 2;
671 reginsert(CURLY, ret);
674 regnaughty += 4 + regnaughty; /* compound interest */
675 regtail(ret, regnode(WHILEM));
676 reginsert(CURLYX,ret);
677 regtail(ret, regnode(NOTHING));
681 *flagp = (WORST|HASWIDTH);
682 if (max && max < min)
683 croak("Can't do {n,m} with n > m");
684 if (regcode != ®dummy) {
686 *(unsigned short *)(ret+3) = min;
687 *(unsigned short *)(ret+5) = max;
689 ret[3] = min >> 8; ret[4] = min & 0377;
690 ret[5] = max >> 8; ret[6] = max & 0377;
703 if (!(flags&HASWIDTH) && op != '?')
704 FAIL("regexp *+ operand could be empty"); /* else may core dump */
708 *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
710 if (op == '*' && (flags&SIMPLE)) {
711 reginsert(STAR, ret);
714 else if (op == '*') {
717 } else if (op == '+' && (flags&SIMPLE)) {
718 reginsert(PLUS, ret);
721 else if (op == '+') {
724 } else if (op == '?') {
729 if (dowarn && regcode != ®dummy && !(flags&HASWIDTH) && max > 10000) {
730 warn("%.*s matches null string many times",
731 regparse - origparse, origparse);
734 if (*regparse == '?') {
736 reginsert(MINMOD, ret);
738 regtail(ret, ret + 4);
740 regtail(ret, ret + 3);
743 if (ISMULT2(regparse))
744 FAIL("nested *?+ in regexp");
750 - regatom - the lowest level
752 * Optimization: gobbles an entire sequence of ordinary characters so that
753 * it can turn them into a single node, which is smaller to store and
754 * faster to run. Backslashed characters are exceptions, each becoming a
755 * separate node; the code is simpler that way and it's not worth fixing.
757 * [Yes, it is worth fixing, some scripts can run twice the speed.]
763 register char *ret = 0;
766 *flagp = WORST; /* Tentatively. */
772 if (regflags & PMf_MULTILINE)
774 else if (regflags & PMf_SINGLELINE)
781 if (regflags & PMf_MULTILINE)
783 else if (regflags & PMf_SINGLELINE)
790 if (regflags & PMf_SINGLELINE)
795 *flagp |= HASWIDTH|SIMPLE;
800 *flagp |= HASWIDTH|SIMPLE;
804 ret = reg(1, &flags);
806 if (flags & TRYAGAIN)
810 *flagp |= flags&(HASWIDTH|SPSTART);
814 if (flags & TRYAGAIN) {
818 croak("internal urp in regexp at /%s/", regparse);
819 /* Supposed to be caught earlier. */
822 if (!regcurly(regparse)) {
830 FAIL("?+*{} follows nothing in regexp");
833 switch (*++regparse) {
850 ret = regnode((regflags & PMf_LOCALE) ? ALNUML : ALNUM);
851 *flagp |= HASWIDTH|SIMPLE;
855 ret = regnode((regflags & PMf_LOCALE) ? NALNUML : NALNUM);
856 *flagp |= HASWIDTH|SIMPLE;
860 ret = regnode((regflags & PMf_LOCALE) ? BOUNDL : BOUND);
865 ret = regnode((regflags & PMf_LOCALE) ? NBOUNDL : NBOUND);
870 ret = regnode((regflags & PMf_LOCALE) ? SPACEL : SPACE);
871 *flagp |= HASWIDTH|SIMPLE;
875 ret = regnode((regflags & PMf_LOCALE) ? NSPACEL : NSPACE);
876 *flagp |= HASWIDTH|SIMPLE;
880 ret = regnode(DIGIT);
881 *flagp |= HASWIDTH|SIMPLE;
885 ret = regnode(NDIGIT);
886 *flagp |= HASWIDTH|SIMPLE;
899 case '1': case '2': case '3': case '4':
900 case '5': case '6': case '7': case '8': case '9':
902 I32 num = atoi(regparse);
904 if (num > 9 && num >= regnpar)
908 ret = reganode((regflags & PMf_FOLD)
909 ? ((regflags & PMf_LOCALE) ? REFFL : REFF)
912 while (isDIGIT(*regparse))
920 if (regparse >= regxend)
921 FAIL("trailing \\ in regexp");
929 if (regflags & PMf_EXTENDED) {
930 while (regparse < regxend && *regparse != '\n') regparse++;
931 if (regparse < regxend)
946 ret = regnode((regflags & PMf_FOLD)
947 ? ((regflags & PMf_LOCALE) ? EXACTFL : EXACTF)
949 regc(0); /* save spot for len */
950 for (len = 0, p = regparse - 1;
951 len < 127 && p < regxend;
956 if (regflags & PMf_EXTENDED)
957 p = regwhite(p, regxend);
1007 ender = scan_hex(++p, 2, &numlen);
1012 ender = UCHARAT(p++);
1013 ender = toCTRL(ender);
1015 case '0': case '1': case '2': case '3':case '4':
1016 case '5': case '6': case '7': case '8':case '9':
1018 (isDIGIT(p[1]) && atoi(p) >= regnpar) ) {
1019 ender = scan_oct(p, 3, &numlen);
1029 FAIL("trailing \\ in regexp");
1040 if (regflags & PMf_EXTENDED)
1041 p = regwhite(p, regxend);
1042 if (ISMULT2(p)) { /* Back off on ?+*. */
1057 FAIL("internal disaster in regexp");
1062 if (regcode != ®dummy)
1063 *OPERAND(ret) = len;
1080 else if (*p == '#') {
1083 } while (p < e && *p != '\n');
1094 register char *opnd;
1096 register I32 lastclass = 1234;
1097 register I32 range = 0;
1102 ret = regnode(ANYOF);
1104 for (class = 0; class < 33; class++)
1106 if (*regparse == '^') { /* Complement of range. */
1109 if (opnd != ®dummy)
1110 *opnd |= ANYOF_INVERT;
1112 if (opnd != ®dummy) {
1113 if (regflags & PMf_FOLD)
1114 *opnd |= ANYOF_FOLD;
1115 if (regflags & PMf_LOCALE)
1116 *opnd |= ANYOF_LOCALE;
1118 if (*regparse == ']' || *regparse == '-')
1119 goto skipcond; /* allow 1st char to be ] or - */
1120 while (regparse < regxend && *regparse != ']') {
1122 class = UCHARAT(regparse++);
1123 if (class == '\\') {
1124 class = UCHARAT(regparse++);
1127 if (opnd != ®dummy) {
1128 if (regflags & PMf_LOCALE)
1129 *opnd |= ANYOF_ALNUML;
1131 for (class = 0; class < 256; class++)
1133 ANYOF_SET(opnd, class);
1139 if (opnd != ®dummy) {
1140 if (regflags & PMf_LOCALE)
1141 *opnd |= ANYOF_NALNUML;
1143 for (class = 0; class < 256; class++)
1144 if (!isALNUM(class))
1145 ANYOF_SET(opnd, class);
1151 if (opnd != ®dummy) {
1152 if (regflags & PMf_LOCALE)
1153 *opnd |= ANYOF_SPACEL;
1155 for (class = 0; class < 256; class++)
1157 ANYOF_SET(opnd, class);
1163 if (opnd != ®dummy) {
1164 if (regflags & PMf_LOCALE)
1165 *opnd |= ANYOF_NSPACEL;
1167 for (class = 0; class < 256; class++)
1168 if (!isSPACE(class))
1169 ANYOF_SET(opnd, class);
1175 if (opnd != ®dummy) {
1176 for (class = '0'; class <= '9'; class++)
1177 ANYOF_SET(opnd, class);
1182 if (opnd != ®dummy) {
1183 for (class = 0; class < '0'; class++)
1184 ANYOF_SET(opnd, class);
1185 for (class = '9' + 1; class < 256; class++)
1186 ANYOF_SET(opnd, class);
1212 class = scan_hex(regparse, 2, &numlen);
1216 class = UCHARAT(regparse++);
1217 class = toCTRL(class);
1219 case '0': case '1': case '2': case '3': case '4':
1220 case '5': case '6': case '7': case '8': case '9':
1221 class = scan_oct(--regparse, 3, &numlen);
1227 if (lastclass > class)
1228 FAIL("invalid [] range in regexp");
1233 if (*regparse == '-' && regparse+1 < regxend &&
1234 regparse[1] != ']') {
1237 continue; /* do it next time */
1240 if (opnd != ®dummy) {
1241 for ( ; lastclass <= class; lastclass++)
1242 ANYOF_SET(opnd, lastclass);
1246 if (*regparse != ']')
1247 FAIL("unmatched [] in regexp");
1249 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
1250 if (opnd != ®dummy && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) {
1251 for (class = 0; class < 256; ++class) {
1252 if (ANYOF_TEST(opnd, class)) {
1253 I32 cf = fold[class];
1254 ANYOF_SET(opnd, cf);
1257 *opnd &= ~ANYOF_FOLD;
1259 /* optimize inverted simple patterns (e.g. [^a-z]) */
1260 if (opnd != ®dummy && (*opnd & 0xFF) == ANYOF_INVERT) {
1261 for (class = 0; class < 32; ++class)
1262 opnd[1 + class] ^= 0xFF;
1271 char* retval = regparse++;
1274 if (*regparse == '(' && regparse[1] == '?' &&
1275 regparse[2] == '#') {
1276 while (*regparse && *regparse != ')')
1281 if (regflags & PMf_EXTENDED) {
1282 if (isSPACE(*regparse)) {
1286 else if (*regparse == '#') {
1287 while (*regparse && *regparse != '\n')
1298 - regnode - emit a node
1300 #ifdef CAN_PROTOTYPE
1301 static char * /* Location. */
1304 static char * /* Location. */
1313 if (ret == ®dummy) {
1324 if (!((long)ret & 1))
1330 *ptr++ = '\0'; /* Null "next" pointer. */
1338 - reganode - emit a node with an argument
1340 #ifdef CAN_PROTOTYPE
1341 static char * /* Location. */
1342 reganode(char op, unsigned short arg)
1344 static char * /* Location. */
1354 if (ret == ®dummy) {
1365 if (!((long)ret & 1))
1371 *ptr++ = '\0'; /* Null "next" pointer. */
1374 *(unsigned short *)(ret+3) = arg;
1376 ret[3] = arg >> 8; ret[4] = arg & 0377;
1385 - regc - emit (if appropriate) a byte of code
1387 #ifdef CAN_PROTOTYPE
1396 if (regcode != ®dummy)
1403 - reginsert - insert an operator in front of already-emitted operand
1405 * Means relocating the operand.
1407 #ifdef CAN_PROTOTYPE
1409 reginsert(char op, char *opnd)
1419 register char *place;
1420 register int offset = (regkind[(U8)op] == CURLY ? 4 : 0);
1422 if (regcode == ®dummy) {
1424 regsize += 4 + offset;
1426 regsize += 3 + offset;
1433 regcode += 4 + offset;
1435 regcode += 3 + offset;
1441 place = opnd; /* Op node, where operand used to be. */
1445 while (offset-- > 0)
1453 - regtail - set the next-pointer at the end of a node chain
1460 register char *scan;
1461 register char *temp;
1462 register I32 offset;
1467 /* Find last node. */
1470 temp = regnext(scan);
1477 offset = val - scan;
1479 *(short*)(scan+1) = offset;
1484 if (OP(scan) == BACK)
1485 offset = scan - val;
1487 offset = val - scan;
1488 *(scan+1) = (offset>>8)&0377;
1489 *(scan+2) = offset&0377;
1494 - regoptail - regtail on operand of first argument; nop if operandless
1501 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1502 if (p == NULL || p == ®dummy || regkind[(U8)OP(p)] != BRANCH)
1504 regtail(NEXTOPER(p), val);
1508 - regcurly - a little FSA that accepts {\d+,?\d*}
1532 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
1539 register char op = EXACT; /* Arbitrary non-END op. */
1540 register char *next;
1541 SV *sv = sv_newmortal();
1544 while (op != END) { /* While that wasn't END last time... */
1552 PerlIO_printf(Perl_debug_log, "%2ld%s", (long)(s - r->program), SvPVX(sv));
1554 s += regarglen[(U8)op];
1555 if (next == NULL) /* Next ptr. */
1556 PerlIO_printf(Perl_debug_log, "(0)");
1558 PerlIO_printf(Perl_debug_log, "(%ld)", (long)(s-r->program)+(next-s));
1563 if (regkind[(U8)op] == EXACT) {
1564 /* Literal string, where present. */
1566 (void)PerlIO_putc(Perl_debug_log, ' ');
1567 (void)PerlIO_putc(Perl_debug_log, '<');
1568 while (*s != '\0') {
1569 (void)PerlIO_putc(Perl_debug_log,*s);
1572 (void)PerlIO_putc(Perl_debug_log, '>');
1575 (void)PerlIO_putc(Perl_debug_log, '\n');
1578 /* Header fields of interest. */
1580 PerlIO_printf(Perl_debug_log, "start `%s' ", SvPVX(r->regstart));
1581 if (r->regstclass) {
1582 regprop(sv, r->regstclass);
1583 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
1585 if (r->reganch & ROPT_ANCH) {
1586 PerlIO_printf(Perl_debug_log, "anchored");
1587 if (r->reganch & ROPT_ANCH_BOL)
1588 PerlIO_printf(Perl_debug_log, "(BOL)");
1589 if (r->reganch & ROPT_ANCH_GPOS)
1590 PerlIO_printf(Perl_debug_log, "(GPOS)");
1591 PerlIO_putc(Perl_debug_log, ' ');
1593 if (r->reganch & ROPT_SKIP)
1594 PerlIO_printf(Perl_debug_log, "plus ");
1595 if (r->reganch & ROPT_IMPLICIT)
1596 PerlIO_printf(Perl_debug_log, "implicit ");
1597 if (r->regmust != NULL)
1598 PerlIO_printf(Perl_debug_log, "must have \"%s\" back %ld ", SvPVX(r->regmust),
1600 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
1601 PerlIO_printf(Perl_debug_log, "\n");
1605 - regprop - printable representation of opcode
1612 register char *p = 0;
1677 sv_catpvf(sv, "CURLY {%d,%d}", ARG1(op), ARG2(op));
1680 sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(op), ARG2(op));
1683 sv_catpvf(sv, "REF%d", ARG1(op));
1686 sv_catpvf(sv, "REFF%d", ARG1(op));
1689 sv_catpvf(sv, "REFFL%d", ARG1(op));
1692 sv_catpvf(sv, "OPEN%d", ARG1(op));
1695 sv_catpvf(sv, "CLOSE%d", ARG1(op));
1753 FAIL("corrupted regexp opcode");
1758 #endif /* DEBUGGING */
1767 Safefree(r->precomp);
1768 r->precomp = Nullch;
1771 Safefree(r->subbase);
1772 r->subbase = Nullch;
1775 SvREFCNT_dec(r->regmust);
1776 r->regmust = Nullsv;
1779 SvREFCNT_dec(r->regstart);
1780 r->regstart = Nullsv;
1782 Safefree(r->startp);