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->end - p->next > 0)
181 #define MORE2() (p->end - p->next > 1)
182 #define SEE(c) (MORE() && PEEK() == (c))
183 #define SEETWO(a, b) (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->end - p->next > 5) {
1017 if (strncmp(p->next, "[:<:]]", 6) == 0) {
1022 if (strncmp(p->next, "[:>:]]", 6) == 0) {
1029 if ((cs = allocset(p)) == NULL)
1032 if (p->g->cflags®_ICASE)
1040 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
1044 (void)MUSTEAT(']', REG_EBRACK);
1046 if (p->error != 0) /* don't mess things up further */
1049 if (cs->invert && p->g->cflags®_NEWLINE)
1050 cs->bmp['\n' >> 3] |= 1 << ('\n' & 7);
1052 if ((ch = singleton(cs)) != OUT) { /* optimize singleton sets */
1056 EMIT(OANYOF, (int)(cs - p->g->sets));
1060 p_range_cmp(wchar_t c1, wchar_t c2)
1063 return __wcollate_range_cmp(c1, c2);
1065 /* Copied from libc/collate __wcollate_range_cmp */
1066 wchar_t s1[2], s2[2];
1072 return (wcscoll(s1, s2));
1077 - p_b_term - parse one term of a bracketed character list
1078 == static void p_b_term(struct parse *p, cset *cs);
1081 p_b_term(struct parse *p, cset *cs)
1084 wint_t start, finish;
1087 struct xlocale_collate *table =
1088 (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
1090 /* classify what we've got */
1091 switch ((MORE()) ? PEEK() : '\0') {
1093 c = (MORE2()) ? PEEK2() : '\0';
1096 SETERROR(REG_ERANGE);
1097 return; /* NOTE RETURN */
1104 case ':': /* character class */
1106 (void)REQUIRE(MORE(), REG_EBRACK);
1108 (void)REQUIRE(c != '-' && c != ']', REG_ECTYPE);
1110 (void)REQUIRE(MORE(), REG_EBRACK);
1111 (void)REQUIRE(EATTWO(':', ']'), REG_ECTYPE);
1113 case '=': /* equivalence class */
1115 (void)REQUIRE(MORE(), REG_EBRACK);
1117 (void)REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
1119 (void)REQUIRE(MORE(), REG_EBRACK);
1120 (void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
1122 default: /* symbol, ordinary character, or range */
1123 start = p_b_symbol(p);
1124 if (SEE('-') && MORE2() && PEEK2() != ']') {
1130 finish = p_b_symbol(p);
1133 if (start == finish)
1134 CHadd(p, cs, start);
1137 if (table->__collate_load_error || MB_CUR_MAX > 1) {
1139 if (MB_CUR_MAX > 1) {
1141 (void)REQUIRE(start <= finish, REG_ERANGE);
1142 CHaddrange(p, cs, start, finish);
1144 (void)REQUIRE(p_range_cmp(start, finish) <= 0, REG_ERANGE);
1145 for (i = 0; i <= UCHAR_MAX; i++) {
1146 if (p_range_cmp(start, i) <= 0 &&
1147 p_range_cmp(i, finish) <= 0 )
1157 - p_b_pseudoclass - parse a pseudo-class (\w, \W, \s, \S)
1158 == static int p_b_pseudoclass(struct parse *p, char c)
1161 p_b_pseudoclass(struct parse *p, char c) {
1164 if ((cs = allocset(p)) == NULL)
1167 if (p->g->cflags®_ICASE)
1175 p_b_cclass_named(p, cs, "alnum");
1181 p_b_cclass_named(p, cs, "space");
1187 EMIT(OANYOF, (int)(cs - p->g->sets));
1192 - p_b_cclass - parse a character-class name and deal with it
1193 == static void p_b_cclass(struct parse *p, cset *cs);
1196 p_b_cclass(struct parse *p, cset *cs)
1198 const char *sp = p->next;
1202 while (MORE() && isalpha((uch)PEEK()))
1205 if (len >= sizeof(clname) - 1) {
1206 SETERROR(REG_ECTYPE);
1209 memcpy(clname, sp, len);
1212 p_b_cclass_named(p, cs, clname);
1215 - p_b_cclass_named - deal with a named character class
1216 == static void p_b_cclass_named(struct parse *p, cset *cs, const char []);
1219 p_b_cclass_named(struct parse *p, cset *cs, const char clname[]) {
1222 if ((wct = wctype(clname)) == 0) {
1223 SETERROR(REG_ECTYPE);
1226 CHaddtype(p, cs, wct);
1230 - p_b_eclass - parse an equivalence-class name and deal with it
1231 == static void p_b_eclass(struct parse *p, cset *cs);
1233 * This implementation is incomplete. xxx
1236 p_b_eclass(struct parse *p, cset *cs)
1240 c = p_b_coll_elem(p, '=');
1245 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
1246 == static wint_t p_b_symbol(struct parse *p);
1248 static wint_t /* value of symbol */
1249 p_b_symbol(struct parse *p)
1253 (void)REQUIRE(MORE(), REG_EBRACK);
1254 if (!EATTWO('[', '.'))
1257 /* collating symbol */
1258 value = p_b_coll_elem(p, '.');
1259 (void)REQUIRE(EATTWO('.', ']'), REG_ECOLLATE);
1264 - p_b_coll_elem - parse a collating-element name and look it up
1265 == static wint_t p_b_coll_elem(struct parse *p, wint_t endc);
1267 static wint_t /* value of collating element */
1268 p_b_coll_elem(struct parse *p,
1269 wint_t endc) /* name ended by endc,']' */
1271 const char *sp = p->next;
1277 while (MORE() && !SEETWO(endc, ']'))
1280 SETERROR(REG_EBRACK);
1284 for (cp = cnames; cp->name != NULL; cp++)
1285 if (strncmp(cp->name, sp, len) == 0 && strlen(cp->name) == len)
1286 return(cp->code); /* known name */
1287 memset(&mbs, 0, sizeof(mbs));
1288 if ((clen = mbrtowc(&wc, sp, len, &mbs)) == len)
1289 return (wc); /* single character */
1290 else if (clen == (size_t)-1 || clen == (size_t)-2)
1291 SETERROR(REG_ILLSEQ);
1293 SETERROR(REG_ECOLLATE); /* neither */
1298 - may_escape - determine whether 'ch' is escape-able in the current context
1299 == static int may_escape(struct parse *p, const wint_t ch)
1302 may_escape(struct parse *p, const wint_t ch)
1305 if ((p->pflags & PFLAG_LEGACY_ESC) != 0)
1307 if (isalpha(ch) || ch == '\'' || ch == '`')
1312 * Build a whitelist of characters that may be escaped to produce an
1313 * ordinary in the current context. This assumes that these have not
1314 * been otherwise interpreted as a special character. Escaping an
1315 * ordinary character yields undefined results according to
1316 * IEEE 1003.1-2008. Some extensions (notably, some GNU extensions) take
1317 * advantage of this and use escaped ordinary characters to provide
1318 * special meaning, e.g. \b, \B, \w, \W, \s, \S.
1324 /* The above characters may not be escaped in BREs */
1325 if (!(p->g->cflags®_EXTENDED))
1347 - othercase - return the case counterpart of an alphabetic
1348 == static wint_t othercase(wint_t ch);
1350 static wint_t /* if no counterpart, return ch */
1351 othercase(wint_t ch)
1353 assert(iswalpha(ch));
1355 return(towlower(ch));
1356 else if (iswlower(ch))
1357 return(towupper(ch));
1358 else /* peculiar, but could happen */
1363 - bothcases - emit a dualcase version of a two-case character
1364 == static void bothcases(struct parse *p, wint_t ch);
1366 * Boy, is this implementation ever a kludge...
1369 bothcases(struct parse *p, wint_t ch)
1371 const char *oldnext = p->next;
1372 const char *oldend = p->end;
1373 char bracket[3 + MB_LEN_MAX];
1377 assert(othercase(ch) != ch); /* p_bracket() would recurse */
1379 memset(&mbs, 0, sizeof(mbs));
1380 n = wcrtomb(bracket, ch, &mbs);
1381 assert(n != (size_t)-1);
1383 bracket[n + 1] = '\0';
1384 p->end = bracket+n+1;
1386 assert(p->next == p->end);
1392 - ordinary - emit an ordinary character
1393 == static void ordinary(struct parse *p, wint_t ch);
1396 ordinary(struct parse *p, wint_t ch)
1400 if ((p->g->cflags®_ICASE) && iswalpha(ch) && othercase(ch) != ch)
1402 else if ((ch & OPDMASK) == ch)
1406 * Kludge: character is too big to fit into an OCHAR operand.
1407 * Emit a singleton set.
1409 if ((cs = allocset(p)) == NULL)
1412 EMIT(OANYOF, (int)(cs - p->g->sets));
1417 - nonnewline - emit REG_NEWLINE version of OANY
1418 == static void nonnewline(struct parse *p);
1420 * Boy, is this implementation ever a kludge...
1423 nonnewline(struct parse *p)
1425 const char *oldnext = p->next;
1426 const char *oldend = p->end;
1436 assert(p->next == bracket+3);
1442 - repeat - generate code for a bounded repetition, recursively if needed
1443 == static void repeat(struct parse *p, sopno start, int from, int to);
1446 repeat(struct parse *p,
1447 sopno start, /* operand from here to end of strip */
1448 int from, /* repeated from this number */
1449 int to) /* to this number of times (maybe INFINITY) */
1451 sopno finish = HERE();
1454 # define REP(f, t) ((f)*8 + (t))
1455 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1458 if (p->error != 0) /* head off possible runaway recursion */
1463 switch (REP(MAP(from), MAP(to))) {
1464 case REP(0, 0): /* must be user doing this */
1465 DROP(finish-start); /* drop the operand */
1467 case REP(0, 1): /* as x{1,1}? */
1468 case REP(0, N): /* as x{1,n}? */
1469 case REP(0, INF): /* as x{1,}? */
1470 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1471 INSERT(OCH_, start); /* offset is wrong... */
1472 repeat(p, start+1, 1, to);
1473 ASTERN(OOR1, start);
1474 AHEAD(start); /* ... fix it */
1477 ASTERN(O_CH, THERETHERE());
1479 case REP(1, 1): /* trivial case */
1482 case REP(1, N): /* as x?x{1,n-1} */
1483 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1484 INSERT(OCH_, start);
1485 ASTERN(OOR1, start);
1487 EMIT(OOR2, 0); /* offset very wrong... */
1488 AHEAD(THERE()); /* ...so fix it */
1489 ASTERN(O_CH, THERETHERE());
1490 copy = dupl(p, start+1, finish+1);
1491 assert(copy == finish+4);
1492 repeat(p, copy, 1, to-1);
1494 case REP(1, INF): /* as x+ */
1495 INSERT(OPLUS_, start);
1496 ASTERN(O_PLUS, start);
1498 case REP(N, N): /* as xx{m-1,n-1} */
1499 copy = dupl(p, start, finish);
1500 repeat(p, copy, from-1, to-1);
1502 case REP(N, INF): /* as xx{n-1,INF} */
1503 copy = dupl(p, start, finish);
1504 repeat(p, copy, from-1, to);
1506 default: /* "can't happen" */
1507 SETERROR(REG_ASSERT); /* just in case */
1513 - wgetnext - helper function for WGETNEXT() macro. Gets the next wide
1514 - character from the parse struct, signals a REG_ILLSEQ error if the
1515 - character can't be converted. Returns the number of bytes consumed.
1518 wgetnext(struct parse *p)
1524 memset(&mbs, 0, sizeof(mbs));
1525 n = mbrtowc(&wc, p->next, p->end - p->next, &mbs);
1526 if (n == (size_t)-1 || n == (size_t)-2) {
1527 SETERROR(REG_ILLSEQ);
1537 - seterr - set an error condition
1538 == static int seterr(struct parse *p, int e);
1540 static int /* useless but makes type checking happy */
1541 seterr(struct parse *p, int e)
1543 if (p->error == 0) /* keep earliest error condition */
1545 p->next = nuls; /* try to bring things to a halt */
1547 return(0); /* make the return value well-defined */
1551 - allocset - allocate a set of characters for []
1552 == static cset *allocset(struct parse *p);
1555 allocset(struct parse *p)
1559 ncs = reallocarray(p->g->sets, p->g->ncsets + 1, sizeof(*ncs));
1561 SETERROR(REG_ESPACE);
1565 cs = &p->g->sets[p->g->ncsets++];
1566 memset(cs, 0, sizeof(*cs));
1572 - freeset - free a now-unused set
1573 == static void freeset(struct parse *p, cset *cs);
1576 freeset(struct parse *p, cset *cs)
1578 cset *top = &p->g->sets[p->g->ncsets];
1583 memset(cs, 0, sizeof(*cs));
1584 if (cs == top-1) /* recover only the easy case */
1589 - singleton - Determine whether a set contains only one character,
1590 - returning it if so, otherwise returning OUT.
1597 for (i = n = 0; i < NC; i++)
1604 if (cs->nwides == 1 && cs->nranges == 0 && cs->ntypes == 0 &&
1606 return (cs->wides[0]);
1607 /* Don't bother handling the other cases. */
1612 - CHadd - add character to character set.
1615 CHadd(struct parse *p, cset *cs, wint_t ch)
1617 wint_t nch, *newwides;
1620 cs->bmp[ch >> 3] |= 1 << (ch & 7);
1622 newwides = reallocarray(cs->wides, cs->nwides + 1,
1623 sizeof(*cs->wides));
1624 if (newwides == NULL) {
1625 SETERROR(REG_ESPACE);
1628 cs->wides = newwides;
1629 cs->wides[cs->nwides++] = ch;
1632 if ((nch = towlower(ch)) < NC)
1633 cs->bmp[nch >> 3] |= 1 << (nch & 7);
1634 if ((nch = towupper(ch)) < NC)
1635 cs->bmp[nch >> 3] |= 1 << (nch & 7);
1640 - CHaddrange - add all characters in the range [min,max] to a character set.
1643 CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max)
1647 for (; min < NC && min <= max; min++)
1651 newranges = reallocarray(cs->ranges, cs->nranges + 1,
1652 sizeof(*cs->ranges));
1653 if (newranges == NULL) {
1654 SETERROR(REG_ESPACE);
1657 cs->ranges = newranges;
1658 cs->ranges[cs->nranges].min = min;
1659 cs->ranges[cs->nranges].max = max;
1664 - CHaddtype - add all characters of a certain type to a character set.
1667 CHaddtype(struct parse *p, cset *cs, wctype_t wct)
1672 for (i = 0; i < NC; i++)
1673 if (iswctype(i, wct))
1675 newtypes = reallocarray(cs->types, cs->ntypes + 1,
1676 sizeof(*cs->types));
1677 if (newtypes == NULL) {
1678 SETERROR(REG_ESPACE);
1681 cs->types = newtypes;
1682 cs->types[cs->ntypes++] = wct;
1686 - dupl - emit a duplicate of a bunch of sops
1687 == static sopno dupl(struct parse *p, sopno start, sopno finish);
1689 static sopno /* start of duplicate */
1690 dupl(struct parse *p,
1691 sopno start, /* from here */
1692 sopno finish) /* to this less one */
1695 sopno len = finish - start;
1697 assert(finish >= start);
1700 if (!enlarge(p, p->ssize + len)) /* this many unexpected additions */
1702 (void) memcpy((char *)(p->strip + p->slen),
1703 (char *)(p->strip + start), (size_t)len*sizeof(sop));
1709 - doemit - emit a strip operator
1710 == static void doemit(struct parse *p, sop op, size_t opnd);
1712 * It might seem better to implement this as a macro with a function as
1713 * hard-case backup, but it's just too big and messy unless there are
1714 * some changes to the data structures. Maybe later.
1717 doemit(struct parse *p, sop op, size_t opnd)
1719 /* avoid making error situations worse */
1723 /* deal with oversize operands ("can't happen", more or less) */
1724 assert(opnd < 1<<OPSHIFT);
1726 /* deal with undersized strip */
1727 if (p->slen >= p->ssize)
1728 if (!enlarge(p, (p->ssize+1) / 2 * 3)) /* +50% */
1731 /* finally, it's all reduced to the easy case */
1732 p->strip[p->slen++] = SOP(op, opnd);
1736 - doinsert - insert a sop into the strip
1737 == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
1740 doinsert(struct parse *p, sop op, size_t opnd, sopno pos)
1746 /* avoid making error situations worse */
1751 EMIT(op, opnd); /* do checks, ensure space */
1752 assert(HERE() == sn+1);
1755 /* adjust paren pointers */
1757 for (i = 1; i < NPAREN; i++) {
1758 if (p->pbegin[i] >= pos) {
1761 if (p->pend[i] >= pos) {
1766 memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos],
1767 (HERE()-pos-1)*sizeof(sop));
1772 - dofwd - complete a forward reference
1773 == static void dofwd(struct parse *p, sopno pos, sop value);
1776 dofwd(struct parse *p, sopno pos, sop value)
1778 /* avoid making error situations worse */
1782 assert(value < 1<<OPSHIFT);
1783 p->strip[pos] = OP(p->strip[pos]) | value;
1787 - enlarge - enlarge the strip
1788 == static int enlarge(struct parse *p, sopno size);
1791 enlarge(struct parse *p, sopno size)
1795 if (p->ssize >= size)
1798 sp = reallocarray(p->strip, size, sizeof(sop));
1800 SETERROR(REG_ESPACE);
1809 - stripsnug - compact the strip
1810 == static void stripsnug(struct parse *p, struct re_guts *g);
1813 stripsnug(struct parse *p, struct re_guts *g)
1815 g->nstates = p->slen;
1816 g->strip = reallocarray((char *)p->strip, p->slen, sizeof(sop));
1817 if (g->strip == NULL) {
1818 SETERROR(REG_ESPACE);
1819 g->strip = p->strip;
1824 - findmust - fill in must and mlen with longest mandatory literal string
1825 == static void findmust(struct parse *p, struct re_guts *g);
1827 * This algorithm could do fancy things like analyzing the operands of |
1828 * for common subsequences. Someday. This code is simple and finds most
1829 * of the interesting cases.
1831 * Note that must and mlen got initialized during setup.
1834 findmust(struct parse *p, struct re_guts *g)
1838 sop *newstart = NULL;
1843 char buf[MB_LEN_MAX];
1847 /* avoid making error situations worse */
1852 * It's not generally safe to do a ``char'' substring search on
1853 * multibyte character strings, but it's safe for at least
1854 * UTF-8 (see RFC 3629).
1856 if (MB_CUR_MAX > 1 &&
1857 strcmp(_CurrentRuneLocale->__encoding, "UTF-8") != 0)
1860 /* find the longest OCHAR sequence in strip */
1864 scan = g->strip + 1;
1868 case OCHAR: /* sequence member */
1869 if (newlen == 0) { /* new sequence */
1870 memset(&mbs, 0, sizeof(mbs));
1871 newstart = scan - 1;
1873 clen = wcrtomb(buf, OPND(s), &mbs);
1874 if (clen == (size_t)-1)
1878 case OPLUS_: /* things that don't break one */
1882 case OQUEST_: /* things that must be skipped */
1884 offset = altoffset(scan, offset);
1889 /* assert() interferes w debug printouts */
1890 if (OP(s) != (sop)O_QUEST &&
1891 OP(s) != (sop)O_CH && OP(s) != (sop)OOR2) {
1895 } while (OP(s) != (sop)O_QUEST && OP(s) != (sop)O_CH);
1897 case OBOW: /* things that break a sequence */
1908 if (newlen > (sopno)g->mlen) { /* ends one */
1912 g->moffset += offset;
1915 g->moffset = offset;
1923 if (newlen > (sopno)g->mlen) { /* ends one */
1927 g->moffset += offset;
1930 g->moffset = offset;
1939 case OANYOF: /* may or may not invalidate offset */
1940 /* First, everything as OANY */
1941 if (newlen > (sopno)g->mlen) { /* ends one */
1945 g->moffset += offset;
1948 g->moffset = offset;
1959 /* Anything here makes it impossible or too hard
1960 * to calculate the offset -- so we give up;
1961 * save the last known good offset, in case the
1962 * must sequence doesn't occur later.
1964 if (newlen > (sopno)g->mlen) { /* ends one */
1968 g->moffset += offset;
1970 g->moffset = offset;
1976 } while (OP(s) != OEND);
1978 if (g->mlen == 0) { /* there isn't one */
1983 /* turn it into a character string */
1984 g->must = malloc((size_t)g->mlen + 1);
1985 if (g->must == NULL) { /* argh; just forget it */
1992 memset(&mbs, 0, sizeof(mbs));
1993 while (cp < g->must + g->mlen) {
1994 while (OP(s = *scan++) != OCHAR)
1996 clen = wcrtomb(cp, OPND(s), &mbs);
1997 assert(clen != (size_t)-1);
2000 assert(cp == g->must + g->mlen);
2001 *cp++ = '\0'; /* just on general principles */
2005 - altoffset - choose biggest offset among multiple choices
2006 == static int altoffset(sop *scan, int offset);
2008 * Compute, recursively if necessary, the largest offset among multiple
2012 altoffset(sop *scan, int offset)
2018 /* If we gave up already on offsets, return */
2025 while (OP(s) != (sop)O_QUEST && OP(s) != (sop)O_CH) {
2034 try = altoffset(scan, try);
2041 if (OP(s) != (sop)O_QUEST &&
2042 OP(s) != (sop)O_CH && OP(s) != (sop)OOR2)
2044 } while (OP(s) != (sop)O_QUEST && OP(s) != (sop)O_CH);
2045 /* We must skip to the next position, or we'll
2046 * leave altoffset() too early.
2074 return largest+offset;
2078 - computejumps - compute char jumps for BM scan
2079 == static void computejumps(struct parse *p, struct re_guts *g);
2081 * This algorithm assumes g->must exists and is has size greater than
2082 * zero. It's based on the algorithm found on Computer Algorithms by
2085 * A char jump is the number of characters one needs to jump based on
2086 * the value of the character from the text that was mismatched.
2089 computejumps(struct parse *p, struct re_guts *g)
2094 /* Avoid making errors worse */
2098 g->charjump = (int *)malloc((NC_MAX + 1) * sizeof(int));
2099 if (g->charjump == NULL) /* Not a fatal error */
2101 /* Adjust for signed chars, if necessary */
2102 g->charjump = &g->charjump[-(CHAR_MIN)];
2104 /* If the character does not exist in the pattern, the jump
2105 * is equal to the number of characters in the pattern.
2107 for (ch = CHAR_MIN; ch < (CHAR_MAX + 1); ch++)
2108 g->charjump[ch] = g->mlen;
2110 /* If the character does exist, compute the jump that would
2111 * take us to the last character in the pattern equal to it
2112 * (notice that we match right to left, so that last character
2113 * is the first one that would be matched).
2115 for (mindex = 0; mindex < g->mlen; mindex++)
2116 g->charjump[(int)g->must[mindex]] = g->mlen - mindex - 1;
2120 - computematchjumps - compute match jumps for BM scan
2121 == static void computematchjumps(struct parse *p, struct re_guts *g);
2123 * This algorithm assumes g->must exists and is has size greater than
2124 * zero. It's based on the algorithm found on Computer Algorithms by
2127 * A match jump is the number of characters one needs to advance based
2128 * on the already-matched suffix.
2129 * Notice that all values here are minus (g->mlen-1), because of the way
2130 * the search algorithm works.
2133 computematchjumps(struct parse *p, struct re_guts *g)
2135 int mindex; /* General "must" iterator */
2136 int suffix; /* Keeps track of matching suffix */
2137 int ssuffix; /* Keeps track of suffixes' suffix */
2138 int* pmatches; /* pmatches[k] points to the next i
2139 * such that i+1...mlen is a substring
2140 * of k+1...k+mlen-i-1
2143 /* Avoid making errors worse */
2147 pmatches = (int*) malloc(g->mlen * sizeof(int));
2148 if (pmatches == NULL) {
2149 g->matchjump = NULL;
2153 g->matchjump = (int*) malloc(g->mlen * sizeof(int));
2154 if (g->matchjump == NULL) { /* Not a fatal error */
2159 /* Set maximum possible jump for each character in the pattern */
2160 for (mindex = 0; mindex < g->mlen; mindex++)
2161 g->matchjump[mindex] = 2*g->mlen - mindex - 1;
2163 /* Compute pmatches[] */
2164 for (mindex = g->mlen - 1, suffix = g->mlen; mindex >= 0;
2165 mindex--, suffix--) {
2166 pmatches[mindex] = suffix;
2168 /* If a mismatch is found, interrupting the substring,
2169 * compute the matchjump for that position. If no
2170 * mismatch is found, then a text substring mismatched
2171 * against the suffix will also mismatch against the
2174 while (suffix < g->mlen
2175 && g->must[mindex] != g->must[suffix]) {
2176 g->matchjump[suffix] = MIN(g->matchjump[suffix],
2177 g->mlen - mindex - 1);
2178 suffix = pmatches[suffix];
2182 /* Compute the matchjump up to the last substring found to jump
2183 * to the beginning of the largest must pattern prefix matching
2186 for (mindex = 0; mindex <= suffix; mindex++)
2187 g->matchjump[mindex] = MIN(g->matchjump[mindex],
2188 g->mlen + suffix - mindex);
2190 ssuffix = pmatches[suffix];
2191 while (suffix < g->mlen) {
2192 while (suffix <= ssuffix && suffix < g->mlen) {
2193 g->matchjump[suffix] = MIN(g->matchjump[suffix],
2194 g->mlen + ssuffix - suffix);
2197 if (suffix < g->mlen)
2198 ssuffix = pmatches[ssuffix];
2205 - pluscount - count + nesting
2206 == static sopno pluscount(struct parse *p, struct re_guts *g);
2208 static sopno /* nesting depth */
2209 pluscount(struct parse *p, struct re_guts *g)
2217 return(0); /* there may not be an OEND */
2219 scan = g->strip + 1;
2227 if (plusnest > maxnest)
2232 } while (OP(s) != OEND);