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(REF, num);
908 while (isDIGIT(*regparse))
916 if (regparse >= regxend)
917 FAIL("trailing \\ in regexp");
925 if (regflags & PMf_EXTENDED) {
926 while (regparse < regxend && *regparse != '\n') regparse++;
927 if (regparse < regxend)
942 ret = regnode((regflags & PMf_FOLD)
943 ? ((regflags & PMf_LOCALE) ? EXACTFL : EXACTF)
945 regc(0); /* save spot for len */
946 for (len = 0, p = regparse - 1;
947 len < 127 && p < regxend;
952 if (regflags & PMf_EXTENDED)
953 p = regwhite(p, regxend);
1003 ender = scan_hex(++p, 2, &numlen);
1008 ender = UCHARAT(p++);
1009 ender = toCTRL(ender);
1011 case '0': case '1': case '2': case '3':case '4':
1012 case '5': case '6': case '7': case '8':case '9':
1014 (isDIGIT(p[1]) && atoi(p) >= regnpar) ) {
1015 ender = scan_oct(p, 3, &numlen);
1025 FAIL("trailing \\ in regexp");
1036 if (regflags & PMf_EXTENDED)
1037 p = regwhite(p, regxend);
1038 if (ISMULT2(p)) { /* Back off on ?+*. */
1053 FAIL("internal disaster in regexp");
1058 if (regcode != ®dummy)
1059 *OPERAND(ret) = len;
1076 else if (*p == '#') {
1079 } while (p < e && *p != '\n');
1092 if (opnd == ®dummy)
1095 opnd[1 + (c >> 3)] |= (1 << (c & 7));
1101 register char *opnd;
1103 register I32 lastclass = 1234;
1104 register I32 range = 0;
1109 ret = regnode(ANYOF);
1111 for (class = 0; class < 33; class++)
1113 if (*regparse == '^') { /* Complement of range. */
1116 if (opnd != ®dummy)
1117 *opnd |= ANYOF_INVERT;
1119 if (opnd != ®dummy) {
1120 if (regflags & PMf_FOLD)
1121 *opnd |= ANYOF_FOLD;
1122 if (regflags & PMf_LOCALE)
1123 *opnd |= ANYOF_LOCALE;
1125 if (*regparse == ']' || *regparse == '-')
1126 goto skipcond; /* allow 1st char to be ] or - */
1127 while (regparse < regxend && *regparse != ']') {
1129 class = UCHARAT(regparse++);
1130 if (class == '\\') {
1131 class = UCHARAT(regparse++);
1134 if (regflags & PMf_LOCALE) {
1135 if (opnd != ®dummy)
1136 *opnd |= ANYOF_ALNUML;
1139 for (class = 0; class < 256; class++)
1141 regset(opnd, class);
1146 if (regflags & PMf_LOCALE) {
1147 if (opnd != ®dummy)
1148 *opnd |= ANYOF_NALNUML;
1151 for (class = 0; class < 256; class++)
1152 if (!isALNUM(class))
1153 regset(opnd, class);
1158 if (regflags & PMf_LOCALE) {
1159 if (opnd != ®dummy)
1160 *opnd |= ANYOF_SPACEL;
1163 for (class = 0; class < 256; class++)
1165 regset(opnd, class);
1170 if (regflags & PMf_LOCALE) {
1171 if (opnd != ®dummy)
1172 *opnd |= ANYOF_NSPACEL;
1175 for (class = 0; class < 256; class++)
1176 if (!isSPACE(class))
1177 regset(opnd, class);
1182 for (class = '0'; class <= '9'; class++)
1183 regset(opnd, class);
1187 for (class = 0; class < '0'; class++)
1188 regset(opnd, class);
1189 for (class = '9' + 1; class < 256; class++)
1190 regset(opnd, class);
1215 class = scan_hex(regparse, 2, &numlen);
1219 class = UCHARAT(regparse++);
1220 class = toCTRL(class);
1222 case '0': case '1': case '2': case '3': case '4':
1223 case '5': case '6': case '7': case '8': case '9':
1224 class = scan_oct(--regparse, 3, &numlen);
1230 if (lastclass > class)
1231 FAIL("invalid [] range in regexp");
1236 if (*regparse == '-' && regparse+1 < regxend &&
1237 regparse[1] != ']') {
1240 continue; /* do it next time */
1243 for ( ; lastclass <= class; lastclass++)
1244 regset(opnd, lastclass);
1247 if (*regparse != ']')
1248 FAIL("unmatched [] in regexp");
1256 char* retval = regparse++;
1259 if (*regparse == '(' && regparse[1] == '?' &&
1260 regparse[2] == '#') {
1261 while (*regparse && *regparse != ')')
1266 if (regflags & PMf_EXTENDED) {
1267 if (isSPACE(*regparse)) {
1271 else if (*regparse == '#') {
1272 while (*regparse && *regparse != '\n')
1283 - regnode - emit a node
1285 #ifdef CAN_PROTOTYPE
1286 static char * /* Location. */
1289 static char * /* Location. */
1298 if (ret == ®dummy) {
1309 if (!((long)ret & 1))
1315 *ptr++ = '\0'; /* Null "next" pointer. */
1323 - reganode - emit a node with an argument
1325 #ifdef CAN_PROTOTYPE
1326 static char * /* Location. */
1327 reganode(char op, unsigned short arg)
1329 static char * /* Location. */
1339 if (ret == ®dummy) {
1350 if (!((long)ret & 1))
1356 *ptr++ = '\0'; /* Null "next" pointer. */
1359 *(unsigned short *)(ret+3) = arg;
1361 ret[3] = arg >> 8; ret[4] = arg & 0377;
1370 - regc - emit (if appropriate) a byte of code
1372 #ifdef CAN_PROTOTYPE
1381 if (regcode != ®dummy)
1388 - reginsert - insert an operator in front of already-emitted operand
1390 * Means relocating the operand.
1392 #ifdef CAN_PROTOTYPE
1394 reginsert(char op, char *opnd)
1404 register char *place;
1405 register int offset = (regkind[(U8)op] == CURLY ? 4 : 0);
1407 if (regcode == ®dummy) {
1409 regsize += 4 + offset;
1411 regsize += 3 + offset;
1418 regcode += 4 + offset;
1420 regcode += 3 + offset;
1426 place = opnd; /* Op node, where operand used to be. */
1430 while (offset-- > 0)
1438 - regtail - set the next-pointer at the end of a node chain
1445 register char *scan;
1446 register char *temp;
1447 register I32 offset;
1452 /* Find last node. */
1455 temp = regnext(scan);
1462 offset = val - scan;
1464 *(short*)(scan+1) = offset;
1469 if (OP(scan) == BACK)
1470 offset = scan - val;
1472 offset = val - scan;
1473 *(scan+1) = (offset>>8)&0377;
1474 *(scan+2) = offset&0377;
1479 - regoptail - regtail on operand of first argument; nop if operandless
1486 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1487 if (p == NULL || p == ®dummy || regkind[(U8)OP(p)] != BRANCH)
1489 regtail(NEXTOPER(p), val);
1493 - regcurly - a little FSA that accepts {\d+,?\d*}
1517 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
1524 register char op = EXACT; /* Arbitrary non-END op. */
1525 register char *next;
1526 SV *sv = sv_newmortal();
1529 while (op != END) { /* While that wasn't END last time... */
1537 PerlIO_printf(Perl_debug_log, "%2d%s", s - r->program, SvPVX(sv));
1539 s += regarglen[(U8)op];
1540 if (next == NULL) /* Next ptr. */
1541 PerlIO_printf(Perl_debug_log, "(0)");
1543 PerlIO_printf(Perl_debug_log, "(%d)", (s-r->program)+(next-s));
1548 if (regkind[(U8)op] == EXACT) {
1549 /* Literal string, where present. */
1551 (void)PerlIO_putc(Perl_debug_log, ' ');
1552 (void)PerlIO_putc(Perl_debug_log, '<');
1553 while (*s != '\0') {
1554 (void)PerlIO_putc(Perl_debug_log,*s);
1557 (void)PerlIO_putc(Perl_debug_log, '>');
1560 (void)PerlIO_putc(Perl_debug_log, '\n');
1563 /* Header fields of interest. */
1565 PerlIO_printf(Perl_debug_log, "start `%s' ", SvPVX(r->regstart));
1566 if (r->regstclass) {
1567 regprop(sv, r->regstclass);
1568 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
1570 if (r->reganch & ROPT_ANCH) {
1571 PerlIO_printf(Perl_debug_log, "anchored");
1572 if (r->reganch & ROPT_ANCH_BOL)
1573 PerlIO_printf(Perl_debug_log, "(BOL)");
1574 if (r->reganch & ROPT_ANCH_GPOS)
1575 PerlIO_printf(Perl_debug_log, "(GPOS)");
1576 PerlIO_putc(Perl_debug_log, ' ');
1578 if (r->reganch & ROPT_SKIP)
1579 PerlIO_printf(Perl_debug_log, "plus ");
1580 if (r->reganch & ROPT_IMPLICIT)
1581 PerlIO_printf(Perl_debug_log, "implicit ");
1582 if (r->regmust != NULL)
1583 PerlIO_printf(Perl_debug_log, "must have \"%s\" back %ld ", SvPVX(r->regmust),
1585 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
1586 PerlIO_printf(Perl_debug_log, "\n");
1590 - regprop - printable representation of opcode
1597 register char *p = 0;
1662 sv_catpvf(sv, "CURLY {%d,%d}", ARG1(op), ARG2(op));
1665 sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(op), ARG2(op));
1668 sv_catpvf(sv, "REF%d", ARG1(op));
1671 sv_catpvf(sv, "OPEN%d", ARG1(op));
1674 sv_catpvf(sv, "CLOSE%d", ARG1(op));
1732 FAIL("corrupted regexp opcode");
1737 #endif /* DEBUGGING */
1746 Safefree(r->precomp);
1747 r->precomp = Nullch;
1750 Safefree(r->subbase);
1751 r->subbase = Nullch;
1754 SvREFCNT_dec(r->regmust);
1755 r->regmust = Nullsv;
1758 SvREFCNT_dec(r->regstart);
1759 r->regstart = Nullsv;
1761 Safefree(r->startp);