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 regset _((char *, I32));
111 static void regtail _((char *, char *));
112 static char* regwhite _((char *, char *));
113 static char* nextchar _((void));
116 - pregcomp - compile a regular expression into internal code
118 * We can't allocate space until we know how big the compiled form will be,
119 * but we can't compile it (and thus know how big it is) until we've got a
120 * place to put the code. So we cheat: we compile it twice, once with code
121 * generation turned off and size counting turned on, and once "for real".
122 * This also means that we don't allocate space until we are sure that the
123 * thing really will compile successfully, and we never have to move the
124 * code and thus invalidate pointers into it. (Note that it has to be in
125 * one piece because free() must be able to free it all.) [NB: not true in perl]
127 * Beware that the optimization-preparation code in here knows about some
128 * of the structure of the compiled regexp. [I'll say.]
131 pregcomp(exp,xend,pm)
138 register SV *longish;
141 register char *first;
149 #define MAX_REPEAT_DEPTH 12
153 } repeat_stack[MAX_REPEAT_DEPTH];
154 I32 repeat_depth = 0;
155 I32 repeat_count = 1; /* We start unmultiplied. */
158 croak("NULL regexp argument");
160 regprecomp = savepvn(exp, xend - exp);
161 regflags = pm->op_pmflags;
164 /* First pass: determine size, legality. */
172 if (reg(0, &flags) == NULL) {
173 Safefree(regprecomp);
178 /* Small enough for pointer-storage convention? */
179 if (regsize >= 32767L) /* Probably could be 65535L. */
180 FAIL("regexp too big");
182 /* Allocate space and initialize. */
183 Newc(1001, r, sizeof(regexp) + (unsigned)regsize, char, regexp);
185 FAIL("regexp out of space");
186 r->prelen = xend - exp;
187 r->precomp = regprecomp;
188 r->subbeg = r->subbase = NULL;
190 /* Second pass: emit code. */
195 regcode = r->program;
197 if (reg(0, &flags) == NULL)
200 /* Dig out information for optimizations. */
201 pm->op_pmflags = regflags;
202 r->regstart = Nullsv; /* Worst-case defaults. */
206 r->regstclass = Nullch;
207 r->naughty = regnaughty >= 10; /* Probably an expensive pattern. */
208 scan = r->program+1; /* First BRANCH. */
209 if (OP(regnext(scan)) == END) {/* Only one top-level choice. */
210 scan = NEXTOPER(scan);
213 while ((OP(first) == OPEN && (sawopen = 1)) ||
214 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
215 (OP(first) == PLUS) ||
216 (OP(first) == MINMOD) ||
217 (regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
218 if (OP(first) == PLUS)
221 first += regarglen[(U8)OP(first)];
222 first = NEXTOPER(first);
225 /* Starting-point info. */
227 if (OP(first) == EXACT) {
228 r->regstart = newSVpv(OPERAND(first)+1,*OPERAND(first));
229 if (SvCUR(r->regstart) > !sawstudy)
230 fbm_compile(r->regstart);
231 (void)SvUPGRADE(r->regstart, SVt_PVBM);
233 else if (strchr(simple+2,OP(first)))
234 r->regstclass = first;
235 else if (regkind[(U8)OP(first)] == BOUND ||
236 regkind[(U8)OP(first)] == NBOUND)
237 r->regstclass = first;
238 else if (regkind[(U8)OP(first)] == BOL) {
239 r->reganch |= ROPT_ANCH_BOL;
240 first = NEXTOPER(first);
243 else if (OP(first) == GPOS) {
244 r->reganch |= ROPT_ANCH_GPOS;
245 first = NEXTOPER(first);
248 else if ((OP(first) == STAR &&
249 regkind[(U8)OP(NEXTOPER(first))] == ANY) &&
250 !(r->reganch & ROPT_ANCH) )
252 /* turn .* into ^.* with an implied $*=1 */
253 r->reganch |= ROPT_ANCH_BOL | ROPT_IMPLICIT;
254 first = NEXTOPER(first);
257 if (sawplus && (!sawopen || !regsawback))
258 r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */
260 DEBUG_r(PerlIO_printf(Perl_debug_log, "first %d next %d offset %d\n",
261 OP(first), OP(NEXTOPER(first)), first - scan));
263 * If there's something expensive in the r.e., find the
264 * longest literal string that must appear and make it the
265 * regmust. Resolve ties in favor of later strings, since
266 * the regstart check works with the beginning of the r.e.
267 * and avoiding duplication strengthens checking. Not a
268 * strong reason, but sufficient in the absence of others.
269 * [Now we resolve ties in favor of the earlier string if
270 * it happens that curback has been invalidated, since the
271 * earlier string may buy us something the later one won't.]
273 longish = newSVpv("",0);
274 longest = newSVpv("",0);
280 while (OP(scan) != END) {
281 if (OP(scan) == BRANCH) {
282 if (OP(regnext(scan)) == BRANCH) {
284 while (OP(scan) == BRANCH)
285 scan = regnext(scan);
287 else /* single branch is ok */
288 scan = NEXTOPER(scan);
291 if (OP(scan) == UNLESSM) {
293 scan = regnext(scan);
296 if (OP(scan) == EXACT) {
300 while ((t = regnext(scan)) && OP(t) == CLOSE)
302 minlen += *OPERAND(first) * repeat_count;
303 if (curback - backish == len) {
304 sv_catpvn(longish, OPERAND(first)+1,
306 len += *OPERAND(first);
307 curback += *OPERAND(first);
308 first = regnext(scan);
310 else if (*OPERAND(first) >= len + (curback >= 0)) {
311 len = *OPERAND(first);
312 sv_setpvn(longish, OPERAND(first)+1,len);
315 first = regnext(scan);
318 curback += *OPERAND(first);
320 else if (strchr(varies,OP(scan))) {
324 if (repeat_depth < MAX_REPEAT_DEPTH
325 && ((OP(scan) == PLUS
327 && (next = NEXTOPER(scan)))
328 || (regkind[(U8)OP(scan)] == CURLY
329 && (tcount = ARG1(scan))
330 && (next = NEXTOPER(scan)+4))))
332 /* We treat (abc)+ as (abc)(abc)*. */
334 /* Mark the place to return back. */
335 repeat_stack[repeat_depth].opcode = regnext(scan);
336 repeat_stack[repeat_depth].count = repeat_count;
338 repeat_count *= tcount;
347 if (SvCUR(longish) > SvCUR(longest)) {
348 sv_setsv(longest,longish);
351 sv_setpvn(longish,"",0);
354 else if (strchr(simple,OP(scan))) {
356 minlen += repeat_count;
358 if (SvCUR(longish) > SvCUR(longest)) {
359 sv_setsv(longest,longish);
362 sv_setpvn(longish,"",0);
364 scan = regnext(scan);
365 if (!scan) { /* Go up PLUS or CURLY. */
367 croak("panic: re scan");
368 scan = repeat_stack[repeat_depth].opcode;
369 repeat_count = repeat_stack[repeat_depth].count;
370 /* Need to submit the longest string found: */
373 if (SvCUR(longish) > SvCUR(longest)) {
374 sv_setsv(longest,longish);
377 sv_setpvn(longish,"",0);
381 /* Prefer earlier on tie, unless we can tail match latter */
383 if (SvCUR(longish) + (first && regkind[(U8)OP(first)] == EOL)
386 sv_setsv(longest,longish);
390 sv_setpvn(longish,"",0);
393 || !fbm_instr((unsigned char*) SvPVX(r->regstart),
394 (unsigned char *) (SvPVX(r->regstart)
395 + SvCUR(r->regstart)),
398 r->regmust = longest;
401 r->regback = backest;
402 if (SvCUR(longest) > !(sawstudy ||
403 (first && regkind[(U8)OP(first)] == EOL)))
404 fbm_compile(r->regmust);
405 (void)SvUPGRADE(r->regmust, SVt_PVBM);
406 BmUSEFUL(r->regmust) = 100;
407 if (first && regkind[(U8)OP(first)] == EOL && SvCUR(longish))
408 SvTAIL_on(r->regmust);
411 SvREFCNT_dec(longest);
414 SvREFCNT_dec(longish);
417 r->nparens = regnpar - 1;
419 Newz(1002, r->startp, regnpar, char*);
420 Newz(1002, r->endp, regnpar, char*);
426 - reg - regular expression, i.e. main body or parenthesized thing
428 * Caller must absorb opening parenthesis.
430 * Combining parenthesis handling with the base level of regular expression
431 * is a trifle forced, but the need to tie the tails of the branches to what
432 * follows makes it hard to avoid.
436 I32 paren; /* Parenthesized? */
441 register char *ender = 0;
442 register I32 parno = 0;
445 *flagp = HASWIDTH; /* Tentatively. */
447 /* Make an OPEN node, if parenthesized. */
449 if (*regparse == '?') {
460 croak("Sequence (?%c...) not implemented", (int)paren);
463 while (*regparse && *regparse != ')')
465 if (*regparse != ')')
466 croak("Sequence (?#... not terminated");
472 while (*regparse && strchr("iogcmsx", *regparse))
473 pmflag(®flags, *regparse++);
474 if (*regparse != ')')
475 croak("Sequence (?%c...) not recognized", *regparse);
484 ret = reganode(OPEN, parno);
489 /* Pick up the branches, linking them together. */
490 br = regbranch(&flags);
494 regtail(ret, br); /* OPEN -> first. */
497 if (!(flags&HASWIDTH))
499 *flagp |= flags&SPSTART;
500 while (*regparse == '|') {
502 br = regbranch(&flags);
505 regtail(ret, br); /* BRANCH -> BRANCH. */
506 if (!(flags&HASWIDTH))
508 *flagp |= flags&SPSTART;
511 /* Make a closing node, and hook it on the end. */
514 ender = regnode(NOTHING);
517 ender = reganode(CLOSE, parno);
521 ender = regnode(SUCCEED);
525 ender = regnode(END);
530 /* Hook the tails of the branches to the closing node. */
531 for (br = ret; br != NULL; br = regnext(br))
532 regoptail(br, ender);
535 reginsert(IFMATCH,ret);
536 regtail(ret, regnode(NOTHING));
538 else if (paren == '!') {
539 reginsert(UNLESSM,ret);
540 regtail(ret, regnode(NOTHING));
543 /* Check for proper termination. */
544 if (paren && (regparse >= regxend || *nextchar() != ')')) {
545 FAIL("unmatched () in regexp");
546 } else if (!paren && regparse < regxend) {
547 if (*regparse == ')') {
548 FAIL("unmatched () in regexp");
550 FAIL("junk on end of regexp"); /* "Can't happen". */
558 - regbranch - one alternative of an | operator
560 * Implements the concatenation operator.
567 register char *chain;
568 register char *latest;
571 *flagp = WORST; /* Tentatively. */
573 ret = regnode(BRANCH);
577 while (regparse < regxend && *regparse != '|' && *regparse != ')') {
579 latest = regpiece(&flags);
580 if (latest == NULL) {
581 if (flags & TRYAGAIN)
585 *flagp |= flags&HASWIDTH;
586 if (chain == NULL) /* First piece. */
587 *flagp |= flags&SPSTART;
590 regtail(chain, latest);
594 if (chain == NULL) /* Loop ran zero times. */
595 (void) regnode(NOTHING);
601 - regpiece - something followed by possible [*+?]
603 * Note that the branching code sequences used for ? and the general cases
604 * of * and + are somewhat optimized: they use the same NOTHING node as
605 * both the endmarker for their branch list and the body of the last branch.
606 * It might seem that this node could be dispensed with entirely, but the
607 * endmarker role is not redundant.
617 char *origparse = regparse;
622 ret = regatom(&flags);
624 if (flags & TRYAGAIN)
630 if (op == '(' && regparse[1] == '?' && regparse[2] == '#') {
631 while (op && op != ')')
639 if (op == '{' && regcurly(regparse)) {
642 while (isDIGIT(*next) || *next == ',') {
651 if (*next == '}') { /* got one */
655 min = atoi(regparse);
661 if (!max && *maxpos != '0')
662 max = 32767; /* meaning "infinity" */
667 if ((flags&SIMPLE)) {
668 regnaughty += 2 + regnaughty / 2;
669 reginsert(CURLY, ret);
672 regnaughty += 4 + regnaughty; /* compound interest */
673 regtail(ret, regnode(WHILEM));
674 reginsert(CURLYX,ret);
675 regtail(ret, regnode(NOTHING));
679 *flagp = (WORST|HASWIDTH);
680 if (max && max < min)
681 croak("Can't do {n,m} with n > m");
682 if (regcode != ®dummy) {
684 *(unsigned short *)(ret+3) = min;
685 *(unsigned short *)(ret+5) = max;
687 ret[3] = min >> 8; ret[4] = min & 0377;
688 ret[5] = max >> 8; ret[6] = max & 0377;
701 if (!(flags&HASWIDTH) && op != '?')
702 FAIL("regexp *+ operand could be empty");
706 *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
708 if (op == '*' && (flags&SIMPLE)) {
709 reginsert(STAR, ret);
712 else if (op == '*') {
715 } else if (op == '+' && (flags&SIMPLE)) {
716 reginsert(PLUS, ret);
719 else if (op == '+') {
722 } else if (op == '?') {
727 if (dowarn && regcode != ®dummy && !(flags&HASWIDTH) && max > 10000) {
728 warn("%.*s matches null string many times",
729 regparse - origparse, origparse);
732 if (*regparse == '?') {
734 reginsert(MINMOD, ret);
736 regtail(ret, ret + 4);
738 regtail(ret, ret + 3);
741 if (ISMULT2(regparse))
742 FAIL("nested *?+ in regexp");
748 - regatom - the lowest level
750 * Optimization: gobbles an entire sequence of ordinary characters so that
751 * it can turn them into a single node, which is smaller to store and
752 * faster to run. Backslashed characters are exceptions, each becoming a
753 * separate node; the code is simpler that way and it's not worth fixing.
755 * [Yes, it is worth fixing, some scripts can run twice the speed.]
761 register char *ret = 0;
764 *flagp = WORST; /* Tentatively. */
770 if (regflags & PMf_MULTILINE)
772 else if (regflags & PMf_SINGLELINE)
779 if (regflags & PMf_MULTILINE)
781 else if (regflags & PMf_SINGLELINE)
788 if (regflags & PMf_SINGLELINE)
793 *flagp |= HASWIDTH|SIMPLE;
798 *flagp |= HASWIDTH|SIMPLE;
802 ret = reg(1, &flags);
804 if (flags & TRYAGAIN)
808 *flagp |= flags&(HASWIDTH|SPSTART);
812 if (flags & TRYAGAIN) {
816 croak("internal urp in regexp at /%s/", regparse);
817 /* Supposed to be caught earlier. */
820 if (!regcurly(regparse)) {
828 FAIL("?+*{} follows nothing in regexp");
831 switch (*++regparse) {
848 ret = regnode((regflags & PMf_LOCALE) ? ALNUML : ALNUM);
849 *flagp |= HASWIDTH|SIMPLE;
853 ret = regnode((regflags & PMf_LOCALE) ? NALNUML : NALNUM);
854 *flagp |= HASWIDTH|SIMPLE;
858 ret = regnode((regflags & PMf_LOCALE) ? BOUNDL : BOUND);
863 ret = regnode((regflags & PMf_LOCALE) ? NBOUNDL : NBOUND);
868 ret = regnode((regflags & PMf_LOCALE) ? SPACEL : SPACE);
869 *flagp |= HASWIDTH|SIMPLE;
873 ret = regnode((regflags & PMf_LOCALE) ? NSPACEL : NSPACE);
874 *flagp |= HASWIDTH|SIMPLE;
878 ret = regnode(DIGIT);
879 *flagp |= HASWIDTH|SIMPLE;
883 ret = regnode(NDIGIT);
884 *flagp |= HASWIDTH|SIMPLE;
897 case '1': case '2': case '3': case '4':
898 case '5': case '6': case '7': case '8': case '9':
900 I32 num = atoi(regparse);
902 if (num > 9 && num >= regnpar)
906 ret = reganode((regflags & PMf_FOLD)
907 ? ((regflags & PMf_LOCALE) ? REFFL : REFF)
910 while (isDIGIT(*regparse))
918 if (regparse >= regxend)
919 FAIL("trailing \\ in regexp");
927 if (regflags & PMf_EXTENDED) {
928 while (regparse < regxend && *regparse != '\n') regparse++;
929 if (regparse < regxend)
944 ret = regnode((regflags & PMf_FOLD)
945 ? ((regflags & PMf_LOCALE) ? EXACTFL : EXACTF)
947 regc(0); /* save spot for len */
948 for (len = 0, p = regparse - 1;
949 len < 127 && p < regxend;
954 if (regflags & PMf_EXTENDED)
955 p = regwhite(p, regxend);
1005 ender = scan_hex(++p, 2, &numlen);
1010 ender = UCHARAT(p++);
1011 ender = toCTRL(ender);
1013 case '0': case '1': case '2': case '3':case '4':
1014 case '5': case '6': case '7': case '8':case '9':
1016 (isDIGIT(p[1]) && atoi(p) >= regnpar) ) {
1017 ender = scan_oct(p, 3, &numlen);
1027 FAIL("trailing \\ in regexp");
1038 if (regflags & PMf_EXTENDED)
1039 p = regwhite(p, regxend);
1040 if (ISMULT2(p)) { /* Back off on ?+*. */
1055 FAIL("internal disaster in regexp");
1060 if (regcode != ®dummy)
1061 *OPERAND(ret) = len;
1078 else if (*p == '#') {
1081 } while (p < e && *p != '\n');
1094 if (opnd == ®dummy)
1097 opnd[1 + (c >> 3)] |= (1 << (c & 7));
1103 register char *opnd;
1105 register I32 lastclass = 1234;
1106 register I32 range = 0;
1111 ret = regnode(ANYOF);
1113 for (class = 0; class < 33; class++)
1115 if (*regparse == '^') { /* Complement of range. */
1118 if (opnd != ®dummy)
1119 *opnd |= ANYOF_INVERT;
1121 if (opnd != ®dummy) {
1122 if (regflags & PMf_FOLD)
1123 *opnd |= ANYOF_FOLD;
1124 if (regflags & PMf_LOCALE)
1125 *opnd |= ANYOF_LOCALE;
1127 if (*regparse == ']' || *regparse == '-')
1128 goto skipcond; /* allow 1st char to be ] or - */
1129 while (regparse < regxend && *regparse != ']') {
1131 class = UCHARAT(regparse++);
1132 if (class == '\\') {
1133 class = UCHARAT(regparse++);
1136 if (regflags & PMf_LOCALE) {
1137 if (opnd != ®dummy)
1138 *opnd |= ANYOF_ALNUML;
1141 for (class = 0; class < 256; class++)
1143 regset(opnd, class);
1148 if (regflags & PMf_LOCALE) {
1149 if (opnd != ®dummy)
1150 *opnd |= ANYOF_NALNUML;
1153 for (class = 0; class < 256; class++)
1154 if (!isALNUM(class))
1155 regset(opnd, class);
1160 if (regflags & PMf_LOCALE) {
1161 if (opnd != ®dummy)
1162 *opnd |= ANYOF_SPACEL;
1165 for (class = 0; class < 256; class++)
1167 regset(opnd, class);
1172 if (regflags & PMf_LOCALE) {
1173 if (opnd != ®dummy)
1174 *opnd |= ANYOF_NSPACEL;
1177 for (class = 0; class < 256; class++)
1178 if (!isSPACE(class))
1179 regset(opnd, class);
1184 for (class = '0'; class <= '9'; class++)
1185 regset(opnd, class);
1189 for (class = 0; class < '0'; class++)
1190 regset(opnd, class);
1191 for (class = '9' + 1; class < 256; class++)
1192 regset(opnd, class);
1217 class = scan_hex(regparse, 2, &numlen);
1221 class = UCHARAT(regparse++);
1222 class = toCTRL(class);
1224 case '0': case '1': case '2': case '3': case '4':
1225 case '5': case '6': case '7': case '8': case '9':
1226 class = scan_oct(--regparse, 3, &numlen);
1232 if (lastclass > class)
1233 FAIL("invalid [] range in regexp");
1238 if (*regparse == '-' && regparse+1 < regxend &&
1239 regparse[1] != ']') {
1242 continue; /* do it next time */
1245 for ( ; lastclass <= class; lastclass++)
1246 regset(opnd, lastclass);
1249 if (*regparse != ']')
1250 FAIL("unmatched [] in regexp");
1258 char* retval = regparse++;
1261 if (*regparse == '(' && regparse[1] == '?' &&
1262 regparse[2] == '#') {
1263 while (*regparse && *regparse != ')')
1268 if (regflags & PMf_EXTENDED) {
1269 if (isSPACE(*regparse)) {
1273 else if (*regparse == '#') {
1274 while (*regparse && *regparse != '\n')
1285 - regnode - emit a node
1287 #ifdef CAN_PROTOTYPE
1288 static char * /* Location. */
1291 static char * /* Location. */
1300 if (ret == ®dummy) {
1311 if (!((long)ret & 1))
1317 *ptr++ = '\0'; /* Null "next" pointer. */
1325 - reganode - emit a node with an argument
1327 #ifdef CAN_PROTOTYPE
1328 static char * /* Location. */
1329 reganode(char op, unsigned short arg)
1331 static char * /* Location. */
1341 if (ret == ®dummy) {
1352 if (!((long)ret & 1))
1358 *ptr++ = '\0'; /* Null "next" pointer. */
1361 *(unsigned short *)(ret+3) = arg;
1363 ret[3] = arg >> 8; ret[4] = arg & 0377;
1372 - regc - emit (if appropriate) a byte of code
1374 #ifdef CAN_PROTOTYPE
1383 if (regcode != ®dummy)
1390 - reginsert - insert an operator in front of already-emitted operand
1392 * Means relocating the operand.
1394 #ifdef CAN_PROTOTYPE
1396 reginsert(char op, char *opnd)
1406 register char *place;
1407 register int offset = (regkind[(U8)op] == CURLY ? 4 : 0);
1409 if (regcode == ®dummy) {
1411 regsize += 4 + offset;
1413 regsize += 3 + offset;
1420 regcode += 4 + offset;
1422 regcode += 3 + offset;
1428 place = opnd; /* Op node, where operand used to be. */
1432 while (offset-- > 0)
1440 - regtail - set the next-pointer at the end of a node chain
1447 register char *scan;
1448 register char *temp;
1449 register I32 offset;
1454 /* Find last node. */
1457 temp = regnext(scan);
1464 offset = val - scan;
1466 *(short*)(scan+1) = offset;
1471 if (OP(scan) == BACK)
1472 offset = scan - val;
1474 offset = val - scan;
1475 *(scan+1) = (offset>>8)&0377;
1476 *(scan+2) = offset&0377;
1481 - regoptail - regtail on operand of first argument; nop if operandless
1488 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1489 if (p == NULL || p == ®dummy || regkind[(U8)OP(p)] != BRANCH)
1491 regtail(NEXTOPER(p), val);
1495 - regcurly - a little FSA that accepts {\d+,?\d*}
1519 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
1526 register char op = EXACT; /* Arbitrary non-END op. */
1527 register char *next;
1528 SV *sv = sv_newmortal();
1531 while (op != END) { /* While that wasn't END last time... */
1539 PerlIO_printf(Perl_debug_log, "%2d%s", s - r->program, SvPVX(sv));
1541 s += regarglen[(U8)op];
1542 if (next == NULL) /* Next ptr. */
1543 PerlIO_printf(Perl_debug_log, "(0)");
1545 PerlIO_printf(Perl_debug_log, "(%d)", (s-r->program)+(next-s));
1550 if (regkind[(U8)op] == EXACT) {
1551 /* Literal string, where present. */
1553 (void)PerlIO_putc(Perl_debug_log, ' ');
1554 (void)PerlIO_putc(Perl_debug_log, '<');
1555 while (*s != '\0') {
1556 (void)PerlIO_putc(Perl_debug_log,*s);
1559 (void)PerlIO_putc(Perl_debug_log, '>');
1562 (void)PerlIO_putc(Perl_debug_log, '\n');
1565 /* Header fields of interest. */
1567 PerlIO_printf(Perl_debug_log, "start `%s' ", SvPVX(r->regstart));
1568 if (r->regstclass) {
1569 regprop(sv, r->regstclass);
1570 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
1572 if (r->reganch & ROPT_ANCH) {
1573 PerlIO_printf(Perl_debug_log, "anchored");
1574 if (r->reganch & ROPT_ANCH_BOL)
1575 PerlIO_printf(Perl_debug_log, "(BOL)");
1576 if (r->reganch & ROPT_ANCH_GPOS)
1577 PerlIO_printf(Perl_debug_log, "(GPOS)");
1578 PerlIO_putc(Perl_debug_log, ' ');
1580 if (r->reganch & ROPT_SKIP)
1581 PerlIO_printf(Perl_debug_log, "plus ");
1582 if (r->reganch & ROPT_IMPLICIT)
1583 PerlIO_printf(Perl_debug_log, "implicit ");
1584 if (r->regmust != NULL)
1585 PerlIO_printf(Perl_debug_log, "must have \"%s\" back %ld ", SvPVX(r->regmust),
1587 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
1588 PerlIO_printf(Perl_debug_log, "\n");
1592 - regprop - printable representation of opcode
1599 register char *p = 0;
1664 sv_catpvf(sv, "CURLY {%d,%d}", ARG1(op), ARG2(op));
1667 sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(op), ARG2(op));
1670 sv_catpvf(sv, "REF%d", ARG1(op));
1673 sv_catpvf(sv, "REFF%d", ARG1(op));
1676 sv_catpvf(sv, "REFFL%d", ARG1(op));
1679 sv_catpvf(sv, "OPEN%d", ARG1(op));
1682 sv_catpvf(sv, "CLOSE%d", ARG1(op));
1740 FAIL("corrupted regexp opcode");
1745 #endif /* DEBUGGING */
1754 Safefree(r->precomp);
1755 r->precomp = Nullch;
1758 Safefree(r->subbase);
1759 r->subbase = Nullch;
1762 SvREFCNT_dec(r->regmust);
1763 r->regmust = Nullsv;
1766 SvREFCNT_dec(r->regstart);
1767 r->regstart = Nullsv;
1769 Safefree(r->startp);