2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
5 * Copyright (c) 1992, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
8 * Copyright (c) 2011 The FreeBSD Foundation
10 * Portions of this software were developed by David Chisnall
11 * under sponsorship from the FreeBSD Foundation.
13 * This code is derived from software contributed to Berkeley by
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * @(#)regcomp.c 8.5 (Berkeley) 3/20/94
43 #if defined(LIBC_SCCS) && !defined(lint)
44 static char sccsid[] = "@(#)regcomp.c 8.5 (Berkeley) 3/20/94";
45 #endif /* LIBC_SCCS and not lint */
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/types.h>
70 * Branching context, used to keep track of branch state for all of the branch-
71 * aware functions. In addition to keeping track of branch positions for the
72 * p_branch_* functions, we use this to simplify some clumsiness in BREs for
73 * detection of whether ^ is acting as an anchor or being used erroneously and
74 * also for whether we're in a sub-expression or not.
88 * parse structure, passed up and down to avoid global variables and
92 const char *next; /* next character in RE */
93 const char *end; /* end of string (-> NUL normally) */
94 int error; /* has an error been seen? */
96 sop *strip; /* malloced strip */
97 sopno ssize; /* malloced strip size (allocated) */
98 sopno slen; /* malloced strip length (used) */
99 int ncsalloc; /* number of csets allocated */
101 # define NPAREN 10 /* we need to remember () 1-9 for back refs */
102 sopno pbegin[NPAREN]; /* -> ( ([0] unused) */
103 sopno pend[NPAREN]; /* -> ) ([0] unused) */
104 bool allowbranch; /* can this expression branch? */
105 bool bre; /* convenience; is this a BRE? */
106 int pflags; /* other parsing flags -- legacy escapes? */
107 bool (*parse_expr)(struct parse *, struct branchc *);
108 void (*pre_parse)(struct parse *, struct branchc *);
109 void (*post_parse)(struct parse *, struct branchc *);
112 #define PFLAG_LEGACY_ESC 0x00000001
114 /* ========= begin header generated by ./mkh ========= */
119 /* === regcomp.c === */
120 static bool p_ere_exp(struct parse *p, struct branchc *bc);
121 static void p_str(struct parse *p);
122 static int p_branch_eat_delim(struct parse *p, struct branchc *bc);
123 static void p_branch_ins_offset(struct parse *p, struct branchc *bc);
124 static void p_branch_fix_tail(struct parse *p, struct branchc *bc);
125 static bool p_branch_empty(struct parse *p, struct branchc *bc);
126 static bool p_branch_do(struct parse *p, struct branchc *bc);
127 static void p_bre_pre_parse(struct parse *p, struct branchc *bc);
128 static void p_bre_post_parse(struct parse *p, struct branchc *bc);
129 static void p_re(struct parse *p, int end1, int end2);
130 static bool p_simp_re(struct parse *p, struct branchc *bc);
131 static int p_count(struct parse *p);
132 static void p_bracket(struct parse *p);
133 static int p_range_cmp(wchar_t c1, wchar_t c2);
134 static void p_b_term(struct parse *p, cset *cs);
135 static int p_b_pseudoclass(struct parse *p, char c);
136 static void p_b_cclass(struct parse *p, cset *cs);
137 static void p_b_cclass_named(struct parse *p, cset *cs, const char[]);
138 static void p_b_eclass(struct parse *p, cset *cs);
139 static wint_t p_b_symbol(struct parse *p);
140 static wint_t p_b_coll_elem(struct parse *p, wint_t endc);
141 static bool may_escape(struct parse *p, const wint_t ch);
142 static wint_t othercase(wint_t ch);
143 static void bothcases(struct parse *p, wint_t ch);
144 static void ordinary(struct parse *p, wint_t ch);
145 static void nonnewline(struct parse *p);
146 static void repeat(struct parse *p, sopno start, int from, int to);
147 static int seterr(struct parse *p, int e);
148 static cset *allocset(struct parse *p);
149 static void freeset(struct parse *p, cset *cs);
150 static void CHadd(struct parse *p, cset *cs, wint_t ch);
151 static void CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max);
152 static void CHaddtype(struct parse *p, cset *cs, wctype_t wct);
153 static wint_t singleton(cset *cs);
154 static sopno dupl(struct parse *p, sopno start, sopno finish);
155 static void doemit(struct parse *p, sop op, size_t opnd);
156 static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
157 static void dofwd(struct parse *p, sopno pos, sop value);
158 static int enlarge(struct parse *p, sopno size);
159 static void stripsnug(struct parse *p, struct re_guts *g);
160 static void findmust(struct parse *p, struct re_guts *g);
161 static int altoffset(sop *scan, int offset);
162 static void computejumps(struct parse *p, struct re_guts *g);
163 static void computematchjumps(struct parse *p, struct re_guts *g);
164 static sopno pluscount(struct parse *p, struct re_guts *g);
165 static wint_t wgetnext(struct parse *p);
170 /* ========= end header generated by ./mkh ========= */
172 static char nuls[10]; /* place to point scanner in event of error */
175 * macros for use with parse structure
176 * BEWARE: these know that the parse structure is named `p' !!!
178 #define PEEK() (*p->next)
179 #define PEEK2() (*(p->next+1))
180 #define MORE() (p->next < p->end)
181 #define MORE2() (p->next+1 < p->end)
182 #define SEE(c) (MORE() && PEEK() == (c))
183 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
184 #define SEESPEC(a) (p->bre ? SEETWO('\\', a) : SEE(a))
185 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
186 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
187 #define EATSPEC(a) (p->bre ? EATTWO('\\', a) : EAT(a))
188 #define NEXT() (p->next++)
189 #define NEXT2() (p->next += 2)
190 #define NEXTn(n) (p->next += (n))
191 #define GETNEXT() (*p->next++)
192 #define WGETNEXT() wgetnext(p)
193 #define SETERROR(e) seterr(p, (e))
194 #define REQUIRE(co, e) ((co) || SETERROR(e))
195 #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
196 #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
197 #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
198 #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd))
199 #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
200 #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
201 #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
202 #define HERE() (p->slen)
203 #define THERE() (p->slen - 1)
204 #define THERETHERE() (p->slen - 2)
205 #define DROP(n) (p->slen -= (n))
207 /* Macro used by computejump()/computematchjump() */
208 #define MIN(a,b) ((a)<(b)?(a):(b))
210 static int /* 0 success, otherwise REG_something */
211 regcomp_internal(regex_t * __restrict preg,
212 const char * __restrict pattern,
213 int cflags, int pflags)
217 struct parse *p = &pa;
222 # define GOODFLAGS(f) (f)
224 # define GOODFLAGS(f) ((f)&~REG_DUMP)
227 cflags = GOODFLAGS(cflags);
228 if ((cflags®_EXTENDED) && (cflags®_NOSPEC))
231 if (cflags®_PEND) {
232 if (preg->re_endp < pattern)
234 len = preg->re_endp - pattern;
236 len = strlen(pattern);
238 /* do the mallocs early so failure handling is easy */
239 g = (struct re_guts *)malloc(sizeof(struct re_guts));
243 * Limit the pattern space to avoid a 32-bit overflow on buffer
244 * extension. Also avoid any signed overflow in case of conversion
245 * so make the real limit based on a 31-bit overflow.
247 * Likely not applicable on 64-bit systems but handle the case
248 * generically (who are we to stop people from using ~715MB+
251 maxlen = ((size_t)-1 >> 1) / sizeof(sop) * 2 / 3;
256 p->ssize = len/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
257 assert(p->ssize >= len);
259 p->strip = (sop *)malloc(p->ssize * sizeof(sop));
261 if (p->strip == NULL) {
268 p->next = pattern; /* convenience; we do not modify it */
269 p->end = p->next + len;
273 for (i = 0; i < NPAREN; i++) {
278 if (cflags®_POSIX) {
280 p->allowbranch = (cflags & REG_EXTENDED) != 0;
282 p->gnuext = p->allowbranch = true;
285 p->allowbranch = (cflags & REG_EXTENDED) != 0;
287 if (cflags & REG_EXTENDED) {
289 p->parse_expr = p_ere_exp;
291 p->post_parse = NULL;
294 p->parse_expr = p_simp_re;
295 p->pre_parse = p_bre_pre_parse;
296 p->post_parse = p_bre_post_parse;
314 g->firststate = THERE();
315 if (cflags & REG_NOSPEC)
320 g->laststate = THERE();
322 /* tidy up loose ends and fill things in */
325 /* only use Boyer-Moore algorithm if the pattern is bigger
326 * than three characters
330 computematchjumps(p, g);
331 if(g->matchjump == NULL && g->charjump != NULL) {
336 g->nplus = pluscount(p, g);
338 preg->re_nsub = g->nsub;
340 preg->re_magic = MAGIC1;
342 /* not debugging, so can't rely on the assert() in regexec() */
344 SETERROR(REG_ASSERT);
347 /* win or lose, we're done */
348 if (p->error != 0) /* lose */
354 - regcomp - interface for parser and compilation
355 = extern int regcomp(regex_t *, const char *, int);
356 = #define REG_BASIC 0000
357 = #define REG_EXTENDED 0001
358 = #define REG_ICASE 0002
359 = #define REG_NOSUB 0004
360 = #define REG_NEWLINE 0010
361 = #define REG_NOSPEC 0020
362 = #define REG_PEND 0040
363 = #define REG_DUMP 0200
365 int /* 0 success, otherwise REG_something */
366 regcomp(regex_t * __restrict preg,
367 const char * __restrict pattern,
371 return (regcomp_internal(preg, pattern, cflags, 0));
376 * Legacy interface that requires more lax escaping behavior.
379 freebsd12_regcomp(regex_t * __restrict preg,
380 const char * __restrict pattern,
381 int cflags, int pflags)
384 return (regcomp_internal(preg, pattern, cflags, PFLAG_LEGACY_ESC));
387 __sym_compat(regcomp, freebsd12_regcomp, FBSD_1.0);
388 #endif /* !LIBREGEX */
391 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op,
392 - return whether we should terminate or not
393 == static bool p_ere_exp(struct parse *p);
396 p_ere_exp(struct parse *p, struct branchc *bc)
411 assert(MORE()); /* caller should have ensured this */
420 (void)REQUIRE(MORE(), REG_EPAREN);
424 p->pbegin[subno] = HERE();
425 EMIT(OLPAREN, subno);
428 if (subno < NPAREN) {
429 p->pend[subno] = HERE();
430 assert(p->pend[subno] != 0);
432 EMIT(ORPAREN, subno);
433 (void)MUSTEAT(')', REG_EPAREN);
435 #ifndef POSIX_MISTAKE
436 case ')': /* happens only if no current unmatched ( */
438 * You may ask, why the ifndef? Because I didn't notice
439 * this until slightly too late for 1003.2, and none of the
440 * other 1003.2 regular-expression reviewers noticed it at
441 * all. So an unmatched ) is legal POSIX, at least until
442 * we can get it fixed.
444 SETERROR(REG_EPAREN);
449 p->g->iflags |= USEBOL;
455 p->g->iflags |= USEEOL;
465 SETERROR(REG_BADRPT);
468 if (p->g->cflags®_NEWLINE)
477 (void)REQUIRE(MORE(), REG_EESCAPE);
499 p_b_pseudoclass(p, wc);
512 if (p->pend[i] != 0) {
513 assert(i <= p->g->nsub);
515 assert(p->pbegin[i] != 0);
516 assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
517 assert(OP(p->strip[p->pend[i]]) == ORPAREN);
518 (void) dupl(p, p->pbegin[i]+1, p->pend[i]);
521 SETERROR(REG_ESUBREG);
527 /* Don't proceed to the POSIX bits if we've already handled it */
540 if (may_escape(p, wc))
543 SETERROR(REG_EESCAPE);
559 /* we call { a repetition if followed by a digit */
560 if (!( c == '*' || c == '+' || c == '?' || c == '{'))
561 return (false); /* no repetition, we're done */
563 (void)REQUIRE(MORE2() && \
564 (isdigit((uch)PEEK2()) || PEEK2() == ','), REG_BADRPT);
567 (void)REQUIRE(!wascaret, REG_BADRPT);
569 case '*': /* implemented as +? */
570 /* this case does not require the (y|) trick, noKLUDGE */
573 INSERT(OQUEST_, pos);
574 ASTERN(O_QUEST, pos);
581 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
582 INSERT(OCH_, pos); /* offset slightly wrong */
583 ASTERN(OOR1, pos); /* this one's right */
584 AHEAD(pos); /* fix the OCH_ */
585 EMIT(OOR2, 0); /* offset very wrong... */
586 AHEAD(THERE()); /* ...so fix it */
587 ASTERN(O_CH, THERETHERE());
592 if (isdigit((uch)PEEK())) {
594 (void)REQUIRE(count <= count2, REG_BADBR);
595 } else /* single number with comma */
597 } else /* just a single number */
599 repeat(p, pos, count, count2);
600 if (!EAT('}')) { /* error heuristics */
601 while (MORE() && PEEK() != '}')
603 (void)REQUIRE(MORE(), REG_EBRACE);
612 if (!( c == '*' || c == '+' || c == '?' ||
613 (c == '{' && MORE2() && isdigit((uch)PEEK2())) ) )
615 SETERROR(REG_BADRPT);
620 - p_str - string (no metacharacters) "parser"
621 == static void p_str(struct parse *p);
624 p_str(struct parse *p)
626 (void)REQUIRE(MORE(), REG_EMPTY);
628 ordinary(p, WGETNEXT());
632 * Eat consecutive branch delimiters for the kind of expression that we are
633 * parsing, return the number of delimiters that we ate.
636 p_branch_eat_delim(struct parse *p, struct branchc *bc)
648 * Insert necessary branch book-keeping operations. This emits a
649 * bogus 'next' offset, since we still have more to parse
652 p_branch_ins_offset(struct parse *p, struct branchc *bc)
655 if (bc->nbranch == 0) {
656 INSERT(OCH_, bc->start); /* offset is wrong */
658 bc->back = bc->start;
661 ASTERN(OOR1, bc->back);
663 AHEAD(bc->fwd); /* fix previous offset */
665 EMIT(OOR2, 0); /* offset is very wrong */
670 * Fix the offset of the tail branch, if we actually had any branches.
671 * This is to correct the bogus placeholder offset that we use.
674 p_branch_fix_tail(struct parse *p, struct branchc *bc)
677 /* Fix bogus offset at the tail if we actually have branches */
678 if (bc->nbranch > 0) {
680 ASTERN(O_CH, bc->back);
685 * Signal to the parser that an empty branch has been encountered; this will,
686 * in the future, be used to allow for more permissive behavior with empty
687 * branches. The return value should indicate whether parsing may continue
691 p_branch_empty(struct parse *p, struct branchc *bc)
700 * Take care of any branching requirements. This includes inserting the
701 * appropriate branching instructions as well as eating all of the branch
702 * delimiters until we either run out of pattern or need to parse more pattern.
705 p_branch_do(struct parse *p, struct branchc *bc)
709 ate = p_branch_eat_delim(p, bc);
712 else if ((ate > 1 || (bc->outer && !MORE())) && !p_branch_empty(p, bc))
714 * Halt parsing only if we have an empty branch and p_branch_empty
715 * indicates that we must not continue. In the future, this will not
716 * necessarily be an error.
719 p_branch_ins_offset(p, bc);
725 p_bre_pre_parse(struct parse *p, struct branchc *bc)
730 * Does not move cleanly into expression parser because of
731 * ordinary interpration of * at the beginning position of
736 p->g->iflags |= USEBOL;
742 p_bre_post_parse(struct parse *p, struct branchc *bc)
745 /* Expression is terminating due to EOL token */
749 p->g->iflags |= USEEOL;
755 - p_re - Top level parser, concatenation and BRE anchoring
756 == static void p_re(struct parse *p, int end1, int end2);
757 * Giving end1 as OUT essentially eliminates the end1/end2 check.
759 * This implementation is a bit of a kludge, in that a trailing $ is first
760 * taken as an ordinary character and then revised to be an anchor.
761 * The amount of lookahead needed to avoid this kludge is excessive.
764 p_re(struct parse *p,
765 int end1, /* first terminating character */
766 int end2) /* second terminating character; ignored for EREs */
771 if (end1 == OUT && end2 == OUT)
775 #define SEEEND() (!p->bre ? SEE(end1) : SEETWO(end1, end2))
779 bc.terminate = false;
780 if (p->pre_parse != NULL)
781 p->pre_parse(p, &bc);
782 while (MORE() && (!p->allowbranch || !SEESPEC('|')) && !SEEEND()) {
783 bc.terminate = p->parse_expr(p, &bc);
786 if (p->post_parse != NULL)
787 p->post_parse(p, &bc);
788 (void) REQUIRE(p->gnuext || HERE() != bc.start, REG_EMPTY);
790 if (HERE() == bc.start && !p_branch_empty(p, &bc))
796 * p_branch_do's return value indicates whether we should
797 * continue parsing or not. This is both for correctness and
798 * a slight optimization, because it will check if we've
799 * encountered an empty branch or the end of the string
800 * immediately following a branch delimiter.
802 if (!p_branch_do(p, &bc))
807 p_branch_fix_tail(p, &bc);
808 assert(!MORE() || SEE(end1));
812 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
813 == static bool p_simp_re(struct parse *p, struct branchc *bc);
815 static bool /* was the simple RE an unbackslashed $? */
816 p_simp_re(struct parse *p, struct branchc *bc)
819 int cc; /* convenient/control character */
827 # define BACKSL (1<<CHAR_BIT)
829 pos = HERE(); /* repetition op, if any, covers from here */
832 assert(MORE()); /* caller should have ensured this */
835 (void)REQUIRE(MORE(), REG_EESCAPE);
858 p_b_pseudoclass(p, cc);
869 if (p->g->cflags®_NEWLINE)
884 SETERROR(REG_BADRPT);
890 p->pbegin[subno] = HERE();
891 EMIT(OLPAREN, subno);
892 /* the MORE here is an error heuristic */
893 if (MORE() && !SEETWO('\\', ')'))
895 if (subno < NPAREN) {
896 p->pend[subno] = HERE();
897 assert(p->pend[subno] != 0);
899 EMIT(ORPAREN, subno);
900 (void)REQUIRE(EATTWO('\\', ')'), REG_EPAREN);
902 case BACKSL|')': /* should not get here -- must be user */
903 SETERROR(REG_EPAREN);
914 i = (c&~BACKSL) - '0';
916 if (p->pend[i] != 0) {
917 assert(i <= p->g->nsub);
919 assert(p->pbegin[i] != 0);
920 assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
921 assert(OP(p->strip[p->pend[i]]) == ORPAREN);
922 (void) dupl(p, p->pbegin[i]+1, p->pend[i]);
925 SETERROR(REG_ESUBREG);
930 * Ordinary if used as the first character beyond BOL anchor of
931 * a (sub-)expression, counts as a bad repetition operator if it
934 (void)REQUIRE(bc->nchain == 0, REG_BADRPT);
938 return (false); /* Definitely not $... */
941 if ((c & BACKSL) == 0 || may_escape(p, wc))
944 SETERROR(REG_EESCAPE);
949 if (EAT('*')) { /* implemented as +? */
950 /* this case does not require the (y|) trick, noKLUDGE */
953 INSERT(OQUEST_, pos);
954 ASTERN(O_QUEST, pos);
956 } else if (p->gnuext && EATTWO('\\', '?')) {
957 INSERT(OQUEST_, pos);
958 ASTERN(O_QUEST, pos);
959 } else if (p->gnuext && EATTWO('\\', '+')) {
963 } else if (EATTWO('\\', '{')) {
966 if (MORE() && isdigit((uch)PEEK())) {
968 (void)REQUIRE(count <= count2, REG_BADBR);
969 } else /* single number with comma */
971 } else /* just a single number */
973 repeat(p, pos, count, count2);
974 if (!EATTWO('\\', '}')) { /* error heuristics */
975 while (MORE() && !SEETWO('\\', '}'))
977 (void)REQUIRE(MORE(), REG_EBRACE);
980 } else if (c == '$') /* $ (but not \$) ends it */
987 - p_count - parse a repetition count
988 == static int p_count(struct parse *p);
990 static int /* the value */
991 p_count(struct parse *p)
996 while (MORE() && isdigit((uch)PEEK()) && count <= DUPMAX) {
997 count = count*10 + (GETNEXT() - '0');
1001 (void)REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR);
1006 - p_bracket - parse a bracketed character list
1007 == static void p_bracket(struct parse *p);
1010 p_bracket(struct parse *p)
1015 /* Dept of Truly Sickening Special-Case Kludges */
1016 if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) {
1021 if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) {
1027 if ((cs = allocset(p)) == NULL)
1030 if (p->g->cflags®_ICASE)
1038 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
1042 (void)MUSTEAT(']', REG_EBRACK);
1044 if (p->error != 0) /* don't mess things up further */
1047 if (cs->invert && p->g->cflags®_NEWLINE)
1048 cs->bmp['\n' >> 3] |= 1 << ('\n' & 7);
1050 if ((ch = singleton(cs)) != OUT) { /* optimize singleton sets */
1054 EMIT(OANYOF, (int)(cs - p->g->sets));
1058 p_range_cmp(wchar_t c1, wchar_t c2)
1061 return __wcollate_range_cmp(c1, c2);
1063 /* Copied from libc/collate __wcollate_range_cmp */
1064 wchar_t s1[2], s2[2];
1070 return (wcscoll(s1, s2));
1075 - p_b_term - parse one term of a bracketed character list
1076 == static void p_b_term(struct parse *p, cset *cs);
1079 p_b_term(struct parse *p, cset *cs)
1082 wint_t start, finish;
1085 struct xlocale_collate *table =
1086 (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
1088 /* classify what we've got */
1089 switch ((MORE()) ? PEEK() : '\0') {
1091 c = (MORE2()) ? PEEK2() : '\0';
1094 SETERROR(REG_ERANGE);
1095 return; /* NOTE RETURN */
1102 case ':': /* character class */
1104 (void)REQUIRE(MORE(), REG_EBRACK);
1106 (void)REQUIRE(c != '-' && c != ']', REG_ECTYPE);
1108 (void)REQUIRE(MORE(), REG_EBRACK);
1109 (void)REQUIRE(EATTWO(':', ']'), REG_ECTYPE);
1111 case '=': /* equivalence class */
1113 (void)REQUIRE(MORE(), REG_EBRACK);
1115 (void)REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
1117 (void)REQUIRE(MORE(), REG_EBRACK);
1118 (void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
1120 default: /* symbol, ordinary character, or range */
1121 start = p_b_symbol(p);
1122 if (SEE('-') && MORE2() && PEEK2() != ']') {
1128 finish = p_b_symbol(p);
1131 if (start == finish)
1132 CHadd(p, cs, start);
1135 if (table->__collate_load_error || MB_CUR_MAX > 1) {
1137 if (MB_CUR_MAX > 1) {
1139 (void)REQUIRE(start <= finish, REG_ERANGE);
1140 CHaddrange(p, cs, start, finish);
1142 (void)REQUIRE(p_range_cmp(start, finish) <= 0, REG_ERANGE);
1143 for (i = 0; i <= UCHAR_MAX; i++) {
1144 if (p_range_cmp(start, i) <= 0 &&
1145 p_range_cmp(i, finish) <= 0 )
1155 - p_b_pseudoclass - parse a pseudo-class (\w, \W, \s, \S)
1156 == static int p_b_pseudoclass(struct parse *p, char c)
1159 p_b_pseudoclass(struct parse *p, char c) {
1162 if ((cs = allocset(p)) == NULL)
1165 if (p->g->cflags®_ICASE)
1173 p_b_cclass_named(p, cs, "alnum");
1179 p_b_cclass_named(p, cs, "space");
1185 EMIT(OANYOF, (int)(cs - p->g->sets));
1190 - p_b_cclass - parse a character-class name and deal with it
1191 == static void p_b_cclass(struct parse *p, cset *cs);
1194 p_b_cclass(struct parse *p, cset *cs)
1196 const char *sp = p->next;
1200 while (MORE() && isalpha((uch)PEEK()))
1203 if (len >= sizeof(clname) - 1) {
1204 SETERROR(REG_ECTYPE);
1207 memcpy(clname, sp, len);
1210 p_b_cclass_named(p, cs, clname);
1213 - p_b_cclass_named - deal with a named character class
1214 == static void p_b_cclass_named(struct parse *p, cset *cs, const char []);
1217 p_b_cclass_named(struct parse *p, cset *cs, const char clname[]) {
1220 if ((wct = wctype(clname)) == 0) {
1221 SETERROR(REG_ECTYPE);
1224 CHaddtype(p, cs, wct);
1228 - p_b_eclass - parse an equivalence-class name and deal with it
1229 == static void p_b_eclass(struct parse *p, cset *cs);
1231 * This implementation is incomplete. xxx
1234 p_b_eclass(struct parse *p, cset *cs)
1238 c = p_b_coll_elem(p, '=');
1243 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
1244 == static wint_t p_b_symbol(struct parse *p);
1246 static wint_t /* value of symbol */
1247 p_b_symbol(struct parse *p)
1251 (void)REQUIRE(MORE(), REG_EBRACK);
1252 if (!EATTWO('[', '.'))
1255 /* collating symbol */
1256 value = p_b_coll_elem(p, '.');
1257 (void)REQUIRE(EATTWO('.', ']'), REG_ECOLLATE);
1262 - p_b_coll_elem - parse a collating-element name and look it up
1263 == static wint_t p_b_coll_elem(struct parse *p, wint_t endc);
1265 static wint_t /* value of collating element */
1266 p_b_coll_elem(struct parse *p,
1267 wint_t endc) /* name ended by endc,']' */
1269 const char *sp = p->next;
1275 while (MORE() && !SEETWO(endc, ']'))
1278 SETERROR(REG_EBRACK);
1282 for (cp = cnames; cp->name != NULL; cp++)
1283 if (strncmp(cp->name, sp, len) == 0 && strlen(cp->name) == len)
1284 return(cp->code); /* known name */
1285 memset(&mbs, 0, sizeof(mbs));
1286 if ((clen = mbrtowc(&wc, sp, len, &mbs)) == len)
1287 return (wc); /* single character */
1288 else if (clen == (size_t)-1 || clen == (size_t)-2)
1289 SETERROR(REG_ILLSEQ);
1291 SETERROR(REG_ECOLLATE); /* neither */
1296 - may_escape - determine whether 'ch' is escape-able in the current context
1297 == static int may_escape(struct parse *p, const wint_t ch)
1300 may_escape(struct parse *p, const wint_t ch)
1303 if ((p->pflags & PFLAG_LEGACY_ESC) != 0)
1305 if (isalpha(ch) || ch == '\'' || ch == '`')
1310 * Build a whitelist of characters that may be escaped to produce an
1311 * ordinary in the current context. This assumes that these have not
1312 * been otherwise interpreted as a special character. Escaping an
1313 * ordinary character yields undefined results according to
1314 * IEEE 1003.1-2008. Some extensions (notably, some GNU extensions) take
1315 * advantage of this and use escaped ordinary characters to provide
1316 * special meaning, e.g. \b, \B, \w, \W, \s, \S.
1322 /* The above characters may not be escaped in BREs */
1323 if (!(p->g->cflags®_EXTENDED))
1345 - othercase - return the case counterpart of an alphabetic
1346 == static wint_t othercase(wint_t ch);
1348 static wint_t /* if no counterpart, return ch */
1349 othercase(wint_t ch)
1351 assert(iswalpha(ch));
1353 return(towlower(ch));
1354 else if (iswlower(ch))
1355 return(towupper(ch));
1356 else /* peculiar, but could happen */
1361 - bothcases - emit a dualcase version of a two-case character
1362 == static void bothcases(struct parse *p, wint_t ch);
1364 * Boy, is this implementation ever a kludge...
1367 bothcases(struct parse *p, wint_t ch)
1369 const char *oldnext = p->next;
1370 const char *oldend = p->end;
1371 char bracket[3 + MB_LEN_MAX];
1375 assert(othercase(ch) != ch); /* p_bracket() would recurse */
1377 memset(&mbs, 0, sizeof(mbs));
1378 n = wcrtomb(bracket, ch, &mbs);
1379 assert(n != (size_t)-1);
1381 bracket[n + 1] = '\0';
1382 p->end = bracket+n+1;
1384 assert(p->next == p->end);
1390 - ordinary - emit an ordinary character
1391 == static void ordinary(struct parse *p, wint_t ch);
1394 ordinary(struct parse *p, wint_t ch)
1398 if ((p->g->cflags®_ICASE) && iswalpha(ch) && othercase(ch) != ch)
1400 else if ((ch & OPDMASK) == ch)
1404 * Kludge: character is too big to fit into an OCHAR operand.
1405 * Emit a singleton set.
1407 if ((cs = allocset(p)) == NULL)
1410 EMIT(OANYOF, (int)(cs - p->g->sets));
1415 - nonnewline - emit REG_NEWLINE version of OANY
1416 == static void nonnewline(struct parse *p);
1418 * Boy, is this implementation ever a kludge...
1421 nonnewline(struct parse *p)
1423 const char *oldnext = p->next;
1424 const char *oldend = p->end;
1434 assert(p->next == bracket+3);
1440 - repeat - generate code for a bounded repetition, recursively if needed
1441 == static void repeat(struct parse *p, sopno start, int from, int to);
1444 repeat(struct parse *p,
1445 sopno start, /* operand from here to end of strip */
1446 int from, /* repeated from this number */
1447 int to) /* to this number of times (maybe INFINITY) */
1449 sopno finish = HERE();
1452 # define REP(f, t) ((f)*8 + (t))
1453 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1456 if (p->error != 0) /* head off possible runaway recursion */
1461 switch (REP(MAP(from), MAP(to))) {
1462 case REP(0, 0): /* must be user doing this */
1463 DROP(finish-start); /* drop the operand */
1465 case REP(0, 1): /* as x{1,1}? */
1466 case REP(0, N): /* as x{1,n}? */
1467 case REP(0, INF): /* as x{1,}? */
1468 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1469 INSERT(OCH_, start); /* offset is wrong... */
1470 repeat(p, start+1, 1, to);
1471 ASTERN(OOR1, start);
1472 AHEAD(start); /* ... fix it */
1475 ASTERN(O_CH, THERETHERE());
1477 case REP(1, 1): /* trivial case */
1480 case REP(1, N): /* as x?x{1,n-1} */
1481 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1482 INSERT(OCH_, start);
1483 ASTERN(OOR1, start);
1485 EMIT(OOR2, 0); /* offset very wrong... */
1486 AHEAD(THERE()); /* ...so fix it */
1487 ASTERN(O_CH, THERETHERE());
1488 copy = dupl(p, start+1, finish+1);
1489 assert(copy == finish+4);
1490 repeat(p, copy, 1, to-1);
1492 case REP(1, INF): /* as x+ */
1493 INSERT(OPLUS_, start);
1494 ASTERN(O_PLUS, start);
1496 case REP(N, N): /* as xx{m-1,n-1} */
1497 copy = dupl(p, start, finish);
1498 repeat(p, copy, from-1, to-1);
1500 case REP(N, INF): /* as xx{n-1,INF} */
1501 copy = dupl(p, start, finish);
1502 repeat(p, copy, from-1, to);
1504 default: /* "can't happen" */
1505 SETERROR(REG_ASSERT); /* just in case */
1511 - wgetnext - helper function for WGETNEXT() macro. Gets the next wide
1512 - character from the parse struct, signals a REG_ILLSEQ error if the
1513 - character can't be converted. Returns the number of bytes consumed.
1516 wgetnext(struct parse *p)
1522 memset(&mbs, 0, sizeof(mbs));
1523 n = mbrtowc(&wc, p->next, p->end - p->next, &mbs);
1524 if (n == (size_t)-1 || n == (size_t)-2) {
1525 SETERROR(REG_ILLSEQ);
1535 - seterr - set an error condition
1536 == static int seterr(struct parse *p, int e);
1538 static int /* useless but makes type checking happy */
1539 seterr(struct parse *p, int e)
1541 if (p->error == 0) /* keep earliest error condition */
1543 p->next = nuls; /* try to bring things to a halt */
1545 return(0); /* make the return value well-defined */
1549 - allocset - allocate a set of characters for []
1550 == static cset *allocset(struct parse *p);
1553 allocset(struct parse *p)
1557 ncs = reallocarray(p->g->sets, p->g->ncsets + 1, sizeof(*ncs));
1559 SETERROR(REG_ESPACE);
1563 cs = &p->g->sets[p->g->ncsets++];
1564 memset(cs, 0, sizeof(*cs));
1570 - freeset - free a now-unused set
1571 == static void freeset(struct parse *p, cset *cs);
1574 freeset(struct parse *p, cset *cs)
1576 cset *top = &p->g->sets[p->g->ncsets];
1581 memset(cs, 0, sizeof(*cs));
1582 if (cs == top-1) /* recover only the easy case */
1587 - singleton - Determine whether a set contains only one character,
1588 - returning it if so, otherwise returning OUT.
1595 for (i = n = 0; i < NC; i++)
1602 if (cs->nwides == 1 && cs->nranges == 0 && cs->ntypes == 0 &&
1604 return (cs->wides[0]);
1605 /* Don't bother handling the other cases. */
1610 - CHadd - add character to character set.
1613 CHadd(struct parse *p, cset *cs, wint_t ch)
1615 wint_t nch, *newwides;
1618 cs->bmp[ch >> 3] |= 1 << (ch & 7);
1620 newwides = reallocarray(cs->wides, cs->nwides + 1,
1621 sizeof(*cs->wides));
1622 if (newwides == NULL) {
1623 SETERROR(REG_ESPACE);
1626 cs->wides = newwides;
1627 cs->wides[cs->nwides++] = ch;
1630 if ((nch = towlower(ch)) < NC)
1631 cs->bmp[nch >> 3] |= 1 << (nch & 7);
1632 if ((nch = towupper(ch)) < NC)
1633 cs->bmp[nch >> 3] |= 1 << (nch & 7);
1638 - CHaddrange - add all characters in the range [min,max] to a character set.
1641 CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max)
1645 for (; min < NC && min <= max; min++)
1649 newranges = reallocarray(cs->ranges, cs->nranges + 1,
1650 sizeof(*cs->ranges));
1651 if (newranges == NULL) {
1652 SETERROR(REG_ESPACE);
1655 cs->ranges = newranges;
1656 cs->ranges[cs->nranges].min = min;
1657 cs->ranges[cs->nranges].max = max;
1662 - CHaddtype - add all characters of a certain type to a character set.
1665 CHaddtype(struct parse *p, cset *cs, wctype_t wct)
1670 for (i = 0; i < NC; i++)
1671 if (iswctype(i, wct))
1673 newtypes = reallocarray(cs->types, cs->ntypes + 1,
1674 sizeof(*cs->types));
1675 if (newtypes == NULL) {
1676 SETERROR(REG_ESPACE);
1679 cs->types = newtypes;
1680 cs->types[cs->ntypes++] = wct;
1684 - dupl - emit a duplicate of a bunch of sops
1685 == static sopno dupl(struct parse *p, sopno start, sopno finish);
1687 static sopno /* start of duplicate */
1688 dupl(struct parse *p,
1689 sopno start, /* from here */
1690 sopno finish) /* to this less one */
1693 sopno len = finish - start;
1695 assert(finish >= start);
1698 if (!enlarge(p, p->ssize + len)) /* this many unexpected additions */
1700 (void) memcpy((char *)(p->strip + p->slen),
1701 (char *)(p->strip + start), (size_t)len*sizeof(sop));
1707 - doemit - emit a strip operator
1708 == static void doemit(struct parse *p, sop op, size_t opnd);
1710 * It might seem better to implement this as a macro with a function as
1711 * hard-case backup, but it's just too big and messy unless there are
1712 * some changes to the data structures. Maybe later.
1715 doemit(struct parse *p, sop op, size_t opnd)
1717 /* avoid making error situations worse */
1721 /* deal with oversize operands ("can't happen", more or less) */
1722 assert(opnd < 1<<OPSHIFT);
1724 /* deal with undersized strip */
1725 if (p->slen >= p->ssize)
1726 if (!enlarge(p, (p->ssize+1) / 2 * 3)) /* +50% */
1729 /* finally, it's all reduced to the easy case */
1730 p->strip[p->slen++] = SOP(op, opnd);
1734 - doinsert - insert a sop into the strip
1735 == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
1738 doinsert(struct parse *p, sop op, size_t opnd, sopno pos)
1744 /* avoid making error situations worse */
1749 EMIT(op, opnd); /* do checks, ensure space */
1750 assert(HERE() == sn+1);
1753 /* adjust paren pointers */
1755 for (i = 1; i < NPAREN; i++) {
1756 if (p->pbegin[i] >= pos) {
1759 if (p->pend[i] >= pos) {
1764 memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos],
1765 (HERE()-pos-1)*sizeof(sop));
1770 - dofwd - complete a forward reference
1771 == static void dofwd(struct parse *p, sopno pos, sop value);
1774 dofwd(struct parse *p, sopno pos, sop value)
1776 /* avoid making error situations worse */
1780 assert(value < 1<<OPSHIFT);
1781 p->strip[pos] = OP(p->strip[pos]) | value;
1785 - enlarge - enlarge the strip
1786 == static int enlarge(struct parse *p, sopno size);
1789 enlarge(struct parse *p, sopno size)
1793 if (p->ssize >= size)
1796 sp = reallocarray(p->strip, size, sizeof(sop));
1798 SETERROR(REG_ESPACE);
1807 - stripsnug - compact the strip
1808 == static void stripsnug(struct parse *p, struct re_guts *g);
1811 stripsnug(struct parse *p, struct re_guts *g)
1813 g->nstates = p->slen;
1814 g->strip = reallocarray((char *)p->strip, p->slen, sizeof(sop));
1815 if (g->strip == NULL) {
1816 SETERROR(REG_ESPACE);
1817 g->strip = p->strip;
1822 - findmust - fill in must and mlen with longest mandatory literal string
1823 == static void findmust(struct parse *p, struct re_guts *g);
1825 * This algorithm could do fancy things like analyzing the operands of |
1826 * for common subsequences. Someday. This code is simple and finds most
1827 * of the interesting cases.
1829 * Note that must and mlen got initialized during setup.
1832 findmust(struct parse *p, struct re_guts *g)
1836 sop *newstart = NULL;
1841 char buf[MB_LEN_MAX];
1845 /* avoid making error situations worse */
1850 * It's not generally safe to do a ``char'' substring search on
1851 * multibyte character strings, but it's safe for at least
1852 * UTF-8 (see RFC 3629).
1854 if (MB_CUR_MAX > 1 &&
1855 strcmp(_CurrentRuneLocale->__encoding, "UTF-8") != 0)
1858 /* find the longest OCHAR sequence in strip */
1862 scan = g->strip + 1;
1866 case OCHAR: /* sequence member */
1867 if (newlen == 0) { /* new sequence */
1868 memset(&mbs, 0, sizeof(mbs));
1869 newstart = scan - 1;
1871 clen = wcrtomb(buf, OPND(s), &mbs);
1872 if (clen == (size_t)-1)
1876 case OPLUS_: /* things that don't break one */
1880 case OQUEST_: /* things that must be skipped */
1882 offset = altoffset(scan, offset);
1887 /* assert() interferes w debug printouts */
1888 if (OP(s) != (sop)O_QUEST &&
1889 OP(s) != (sop)O_CH && OP(s) != (sop)OOR2) {
1893 } while (OP(s) != (sop)O_QUEST && OP(s) != (sop)O_CH);
1895 case OBOW: /* things that break a sequence */
1906 if (newlen > (sopno)g->mlen) { /* ends one */
1910 g->moffset += offset;
1913 g->moffset = offset;
1921 if (newlen > (sopno)g->mlen) { /* ends one */
1925 g->moffset += offset;
1928 g->moffset = offset;
1937 case OANYOF: /* may or may not invalidate offset */
1938 /* First, everything as OANY */
1939 if (newlen > (sopno)g->mlen) { /* ends one */
1943 g->moffset += offset;
1946 g->moffset = offset;
1957 /* Anything here makes it impossible or too hard
1958 * to calculate the offset -- so we give up;
1959 * save the last known good offset, in case the
1960 * must sequence doesn't occur later.
1962 if (newlen > (sopno)g->mlen) { /* ends one */
1966 g->moffset += offset;
1968 g->moffset = offset;
1974 } while (OP(s) != OEND);
1976 if (g->mlen == 0) { /* there isn't one */
1981 /* turn it into a character string */
1982 g->must = malloc((size_t)g->mlen + 1);
1983 if (g->must == NULL) { /* argh; just forget it */
1990 memset(&mbs, 0, sizeof(mbs));
1991 while (cp < g->must + g->mlen) {
1992 while (OP(s = *scan++) != OCHAR)
1994 clen = wcrtomb(cp, OPND(s), &mbs);
1995 assert(clen != (size_t)-1);
1998 assert(cp == g->must + g->mlen);
1999 *cp++ = '\0'; /* just on general principles */
2003 - altoffset - choose biggest offset among multiple choices
2004 == static int altoffset(sop *scan, int offset);
2006 * Compute, recursively if necessary, the largest offset among multiple
2010 altoffset(sop *scan, int offset)
2016 /* If we gave up already on offsets, return */
2023 while (OP(s) != (sop)O_QUEST && OP(s) != (sop)O_CH) {
2032 try = altoffset(scan, try);
2039 if (OP(s) != (sop)O_QUEST &&
2040 OP(s) != (sop)O_CH && OP(s) != (sop)OOR2)
2042 } while (OP(s) != (sop)O_QUEST && OP(s) != (sop)O_CH);
2043 /* We must skip to the next position, or we'll
2044 * leave altoffset() too early.
2072 return largest+offset;
2076 - computejumps - compute char jumps for BM scan
2077 == static void computejumps(struct parse *p, struct re_guts *g);
2079 * This algorithm assumes g->must exists and is has size greater than
2080 * zero. It's based on the algorithm found on Computer Algorithms by
2083 * A char jump is the number of characters one needs to jump based on
2084 * the value of the character from the text that was mismatched.
2087 computejumps(struct parse *p, struct re_guts *g)
2092 /* Avoid making errors worse */
2096 g->charjump = (int *)malloc((NC_MAX + 1) * sizeof(int));
2097 if (g->charjump == NULL) /* Not a fatal error */
2099 /* Adjust for signed chars, if necessary */
2100 g->charjump = &g->charjump[-(CHAR_MIN)];
2102 /* If the character does not exist in the pattern, the jump
2103 * is equal to the number of characters in the pattern.
2105 for (ch = CHAR_MIN; ch < (CHAR_MAX + 1); ch++)
2106 g->charjump[ch] = g->mlen;
2108 /* If the character does exist, compute the jump that would
2109 * take us to the last character in the pattern equal to it
2110 * (notice that we match right to left, so that last character
2111 * is the first one that would be matched).
2113 for (mindex = 0; mindex < g->mlen; mindex++)
2114 g->charjump[(int)g->must[mindex]] = g->mlen - mindex - 1;
2118 - computematchjumps - compute match jumps for BM scan
2119 == static void computematchjumps(struct parse *p, struct re_guts *g);
2121 * This algorithm assumes g->must exists and is has size greater than
2122 * zero. It's based on the algorithm found on Computer Algorithms by
2125 * A match jump is the number of characters one needs to advance based
2126 * on the already-matched suffix.
2127 * Notice that all values here are minus (g->mlen-1), because of the way
2128 * the search algorithm works.
2131 computematchjumps(struct parse *p, struct re_guts *g)
2133 int mindex; /* General "must" iterator */
2134 int suffix; /* Keeps track of matching suffix */
2135 int ssuffix; /* Keeps track of suffixes' suffix */
2136 int* pmatches; /* pmatches[k] points to the next i
2137 * such that i+1...mlen is a substring
2138 * of k+1...k+mlen-i-1
2141 /* Avoid making errors worse */
2145 pmatches = (int*) malloc(g->mlen * sizeof(int));
2146 if (pmatches == NULL) {
2147 g->matchjump = NULL;
2151 g->matchjump = (int*) malloc(g->mlen * sizeof(int));
2152 if (g->matchjump == NULL) { /* Not a fatal error */
2157 /* Set maximum possible jump for each character in the pattern */
2158 for (mindex = 0; mindex < g->mlen; mindex++)
2159 g->matchjump[mindex] = 2*g->mlen - mindex - 1;
2161 /* Compute pmatches[] */
2162 for (mindex = g->mlen - 1, suffix = g->mlen; mindex >= 0;
2163 mindex--, suffix--) {
2164 pmatches[mindex] = suffix;
2166 /* If a mismatch is found, interrupting the substring,
2167 * compute the matchjump for that position. If no
2168 * mismatch is found, then a text substring mismatched
2169 * against the suffix will also mismatch against the
2172 while (suffix < g->mlen
2173 && g->must[mindex] != g->must[suffix]) {
2174 g->matchjump[suffix] = MIN(g->matchjump[suffix],
2175 g->mlen - mindex - 1);
2176 suffix = pmatches[suffix];
2180 /* Compute the matchjump up to the last substring found to jump
2181 * to the beginning of the largest must pattern prefix matching
2184 for (mindex = 0; mindex <= suffix; mindex++)
2185 g->matchjump[mindex] = MIN(g->matchjump[mindex],
2186 g->mlen + suffix - mindex);
2188 ssuffix = pmatches[suffix];
2189 while (suffix < g->mlen) {
2190 while (suffix <= ssuffix && suffix < g->mlen) {
2191 g->matchjump[suffix] = MIN(g->matchjump[suffix],
2192 g->mlen + ssuffix - suffix);
2195 if (suffix < g->mlen)
2196 ssuffix = pmatches[ssuffix];
2203 - pluscount - count + nesting
2204 == static sopno pluscount(struct parse *p, struct re_guts *g);
2206 static sopno /* nesting depth */
2207 pluscount(struct parse *p, struct re_guts *g)
2215 return(0); /* there may not be an OEND */
2217 scan = g->strip + 1;
2225 if (plusnest > maxnest)
2230 } while (OP(s) != OEND);