src/stencil_regex.c

filter: Statementstmt 95.8% 817 / 852 Branchbran 86.1% 677 / 786 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 91.2% 1494 / 1638 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/stencil_filters.c ↑ Summary include/stencil_str.h »
1 /* stencil_regex.c - the non-backtracking regex engine; see stencil_regex.h
2 * for the contract. Perl-free: no perl headers, no allocation, no statics
3 * beyond const tables, so it is re-entrant and thread-agnostic.
4 *
5 * pattern --parse--> tree (w->nodes) --emit--> NFA program (w->out)
6 *
7 * The program is the classic Thompson/Pike instruction list: CHAR, CLASS and
8 * ANY consume one character; SPLIT, JMP and ASSERT are epsilon moves; MATCH
9 * accepts. Jump targets are instruction indices, so the program is position
10 * independent. Matching runs every live state in lock-step over the subject,
11 * so the cost is bounded by (subject length) x (number of states), whatever
12 * the pattern looks like.
13 */
14 #include "stencil_regex.h"
15  
16 #include <string.h>
17  
18 /* ---- parse tree -------------------------------------------------------- */
19  
20 enum {
21 N_EMPTY, /* matches the empty string */
22 N_CHAR, /* val = code point (lower-cased ASCII letter under NF_FOLD) */
23 N_ANY, /* . */
24 N_CLASS, /* val = class index */
25 N_ASSERT, /* val = A_* */
26 N_CAT, /* children in sequence */
27 N_ALT, /* children as alternatives */
28 N_REP /* first child repeated min..max (NF_INF: no upper bound) */
29 };
30  
31 #define NF_FOLD 0x1u
32 #define NF_DOTALL 0x2u
33 #define NF_INF 0x4u
34  
35 #define NONE (-1)
36 #define NOLINK 0xFFFFFFFFu
37  
38 /* ---- program ------------------------------------------------------------ */
39  
40 enum {
41 OP_CHAR, /* x = code point */
42 OP_CHARI, /* x = lower-case ASCII letter; matches either case */
43 OP_ANY, /* anything but \n */
44 OP_ANYNL, /* anything */
45 OP_CLASS, /* x = class index */
46 OP_SPLIT, /* epsilon to x and y */
47 OP_JMP, /* epsilon to x */
48 OP_ASSERT, /* arg = A_*; epsilon to the next instruction if it holds */
49 OP_MATCH
50 };
51  
52 enum {
53 A_BOS, /* \A, and ^ without /m */
54 A_MBOL, /* ^ under /m */
55 A_EOS, /* \z */
56 A_EOSNL, /* \Z, and $ without /m: end, or before a final \n */
57 A_MEOL, /* $ under /m: end, or before any \n */
58 A_WORDB, /* \b */
59 A_NWORDB /* \B */
60 };
61  
62 /* A malformed UTF-8 byte in a subject decodes to this | byte: above every
63 * real code point, so no literal or positive range matches it, while '.'
64 * and negated classes still do. */
65 #define BADCP 0xFFFFFF00u
66  
67 /* Emission visits each tree node once per copy it emits. Repeats of
68 * something that emits no states at all - ((){255}){255} - would not trip
69 * the state cap, so the total work is capped as well. */
70 #define MAX_VISITS 65536
71  
72 #define RX_STR2(x) #x
73 #define RX_STR(x) RX_STR2(x)
74  
75 typedef struct rx_parse {
76 const unsigned char *pat, *cur, *end;
77 int utf8;
78 unsigned flags;
79 stencil_rx_work *w;
80 stencil_rx_err *err;
81 int failed;
82 int n_nodes, n_classes, n_ranges, depth;
83 int shorthand[6]; /* class per \d \D \w \W \s \S */
84 stencil_rx_inst *insts;
85 uint32_t n_insts;
86 uint32_t visits;
87 } rx_parse;
88  
89 /* ---- errors --------------------------------------------------------------- */
90  
91 /* Record the first error (message = concatenation of the non-NULL parts);
92 * returns -1 so a parse function can `return fail(...)`. */
93 378 static int fail(rx_parse *ps, const unsigned char *at, const char *m1,
94 const char *m2, const char *m3)
95 {
96 const char *parts[3];
97 378 size_t n = 0, cap = sizeof ps->err->msg - 1;
98 int i;
99 378 ▶ if (ps->failed)
Branch: 1 branch
BranchTrueFalseTotal
if (ps->failed) 0 378 378
100 0 return -1;
101 378 ps->failed = 1;
102 378 ▶ ps->err->at = at ? (size_t)(at - ps->pat) : 0;
Branch: 1 branch
BranchTrueFalseTotal
ps->err->at = at ? (size_t)(at - ps->pat) : 0; 363 15 378
103 378 parts[0] = m1;
104 378 parts[1] = m2;
105 378 parts[2] = m3;
106 1512 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 1134 378 1512
107 1134 const char *s = parts[i];
108 12860 ▶ while (s && *s && n < cap)
Branch: 3 branches
BranchTrueFalseTotal
while (s && *s && n < cap) 12116 744 12860
while (s && *s && n < cap) 11726 390 12116
while (s && *s && n < cap) 11726 0 11726
109 11726 ps->err->msg[n++] = *s++;
110 }
111 378 ps->err->msg[n] = '\0';
112 378 return -1;
113 }
114  
115 /* ---- characters ------------------------------------------------------------ */
116  
117 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
118 #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
119 #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
120 #define IS_ALPHA(c) (IS_UPPER(c) || IS_LOWER(c))
121 #define IS_ALNUM(c) (IS_ALPHA(c) || IS_DIGIT(c))
122 #define IS_WORD(c) (IS_ALNUM(c) || (c) == '_')
123 #define IS_XDIGIT(c) (IS_DIGIT(c) || ((c) >= 'a' && (c) <= 'f') \
124 || ((c) >= 'A' && (c) <= 'F'))
125 #define IS_XSPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n' \
126 || (c) == '\r' || (c) == '\f' || (c) == 0x0B)
127  
128 26395 static unsigned hexval(unsigned c)
129 {
130 26395 ▶ return IS_DIGIT(c) ? c - '0' : IS_UPPER(c) ? c - 'A' + 10 : c - 'a' + 10;
Branch: 4 branches
BranchTrueFalseTotal
return IS_DIGIT(c) ? c - '0' : IS_UPPER(c) ? c - 'A' + 10 : c - 'a' + 10; 26395 0 26395
return IS_DIGIT(c) ? c - '0' : IS_UPPER(c) ? c - 'A' + 10 : c - 'a' + 10; 19798 6597 26395
return IS_DIGIT(c) ? c - '0' : IS_UPPER(c) ? c - 'A' + 10 : c - 'a' + 10; 6597 0 6597
return IS_DIGIT(c) ? c - '0' : IS_UPPER(c) ? c - 'A' + 10 : c - 'a' + 10; 6595 2 6597
131 }
132  
133 /* Decode one UTF-8 character from s[0..rem): returns its length, or 0 if
134 * malformed (bad lead, truncated, bad continuation, or overlong). Accepts
135 * what perl can hold in a string up to 4 bytes (surrogates, and code points
136 * above U+10FFFF up to 0x1FFFFF). */
137 19367 static int u8_decode(const unsigned char *s, size_t rem, uint32_t *cp)
138 {
139 19367 unsigned c = s[0];
140 uint32_t v;
141 int n, i;
142 19367 ▶ if (c < 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) { 0 19367 19367
143 0 *cp = c;
144 0 return 1;
145 }
146 19367 ▶ if (c >= 0xC2 && c <= 0xDF) { n = 2; v = c & 0x1Fu; }
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 0xC2 && c <= 0xDF) { n = 2; v = c & 0x1Fu; } 19365 2 19367
if (c >= 0xC2 && c <= 0xDF) { n = 2; v = c & 0x1Fu; } 7401 11964 19365
147 11966 ▶ else if (c >= 0xE0 && c <= 0xEF) { n = 3; v = c & 0x0Fu; }
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 0xE0 && c <= 0xEF) { n = 3; v = c & 0x0Fu; } 11964 2 11966
else if (c >= 0xE0 && c <= 0xEF) { n = 3; v = c & 0x0Fu; } 11964 0 11964
148 2 ▶ else if (c >= 0xF0 && c <= 0xF7) { n = 4; v = c & 0x07u; }
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 0xF0 && c <= 0xF7) { n = 4; v = c & 0x07u; } 0 2 2
else if (c >= 0xF0 && c <= 0xF7) { n = 4; v = c & 0x07u; } 0 0 0
149 else
150 2 return 0;
151 19365 ▶ if ((size_t)n > rem)
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)n > rem) 5 19360 19365
152 5 return 0;
153 50684 ▶ for (i = 1; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < n; i++) { 31324 19360 50684
154 31324 ▶ if ((s[i] & 0xC0) != 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if ((s[i] & 0xC0) != 0x80) 0 31324 31324
155 0 return 0;
156 31324 v = (v << 6) | (s[i] & 0x3Fu);
157 }
158 19360 ▶ if ((n == 3 && v < 0x800) || (n == 4 && v < 0x10000))
Branch: 4 branches
BranchTrueFalseTotal
if ((n == 3 && v < 0x800) || (n == 4 && v < 0x10000)) 11964 7396 19360
if ((n == 3 && v < 0x800) || (n == 4 && v < 0x10000)) 11964 0 11964
if ((n == 3 && v < 0x800) || (n == 4 && v < 0x10000)) 0 19360 19360
if ((n == 3 && v < 0x800) || (n == 4 && v < 0x10000)) 0 0 0
159 0 return 0;
160 19360 *cp = v;
161 19360 return n;
162 }
163  
164 /* the next pattern character, decoded; advances ps->cur */
165 59279 static int pat_cp(rx_parse *ps, uint32_t *cp)
166 {
167 int n;
168 59279 ▶ if (!ps->utf8 || *ps->cur < 0x80) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ps->utf8 || *ps->cur < 0x80) { 17671 41608 59279
if (!ps->utf8 || *ps->cur < 0x80) { 10770 6901 17671
169 52378 *cp = *ps->cur++;
170 52378 return 0;
171 }
172 6901 n = u8_decode(ps->cur, (size_t)(ps->end - ps->cur), cp);
173 6901 ▶ if (!n)
Branch: 1 branch
BranchTrueFalseTotal
if (!n) 1 6900 6901
174 1 return fail(ps, ps->cur, "malformed UTF-8 in the pattern", NULL, NULL);
175 6900 ps->cur += n;
176 6900 return 0;
177 }
178  
179 /* ---- tree building ----------------------------------------------------------- */
180  
181 158983 static int new_node(rx_parse *ps, unsigned type)
182 {
183 stencil_rx_node *node;
184 158983 ▶ if (ps->n_nodes >= STENCIL_RX_MAX_NODES)
Branch: 1 branch
BranchTrueFalseTotal
if (ps->n_nodes >= STENCIL_RX_MAX_NODES) 0 158983 158983
185 0 return fail(ps, ps->cur, "regex is too complex", NULL, NULL);
186 158983 node = &ps->w->nodes[ps->n_nodes];
187 158983 node->type = (uint8_t)type;
188 158983 node->flags = 0;
189 158983 node->min = node->max = 0;
190 158983 node->first = node->next = NONE;
191 158983 node->val = 0;
192 158983 return ps->n_nodes++;
193 }
194  
195 39982 static int new_char(rx_parse *ps, uint32_t cp)
196 {
197 39982 int n = new_node(ps, N_CHAR);
198 39982 ▶ if (n < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) 0 39982 39982
199 0 return -1;
200 39982 ▶ if ((ps->flags & STENCIL_RX_I) && cp < 0x80 && IS_ALPHA(cp)) {
Branch: 6 branches
BranchTrueFalseTotal
if ((ps->flags & STENCIL_RX_I) && cp < 0x80 && IS_ALPHA(cp)) { 12307 27675 39982
if ((ps->flags & STENCIL_RX_I) && cp < 0x80 && IS_ALPHA(cp)) { 9768 2539 12307
if ((ps->flags & STENCIL_RX_I) && cp < 0x80 && IS_ALPHA(cp)) { 4940 4828 9768
if ((ps->flags & STENCIL_RX_I) && cp < 0x80 && IS_ALPHA(cp)) { 3128 1812 4940
if ((ps->flags & STENCIL_RX_I) && cp < 0x80 && IS_ALPHA(cp)) { 2455 5501 7956
if ((ps->flags & STENCIL_RX_I) && cp < 0x80 && IS_ALPHA(cp)) { 2455 0 2455
201 4267 ps->w->nodes[n].flags = NF_FOLD;
202 4267 cp |= 0x20u; /* lower case */
203 }
204 39982 ps->w->nodes[n].val = cp;
205 39982 return n;
206 }
207  
208 13086 static int new_assert(rx_parse *ps, unsigned kind)
209 {
210 13086 int n = new_node(ps, N_ASSERT);
211 13086 ▶ if (n >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n >= 0) 13086 0 13086
212 13086 ps->w->nodes[n].val = kind;
213 13086 return n;
214 }
215  
216 /* ---- classes -------------------------------------------------------------------- */
217  
218 #define BIT_SET(cls, c) ((cls)->bits[(c) >> 5] |= 1u << ((c) & 31))
219 #define BIT_HAS(cls, c) (((cls)->bits[(c) >> 5] >> ((c) & 31)) & 1u)
220  
221 30837 static int new_class(rx_parse *ps)
222 {
223 stencil_rx_class *cls;
224 30837 ▶ if (ps->n_classes >= STENCIL_RX_MAX_CLASSES)
Branch: 1 branch
BranchTrueFalseTotal
if (ps->n_classes >= STENCIL_RX_MAX_CLASSES) 2 30835 30837
225 2 return fail(ps, ps->cur, "too many character classes (more than "
226 RX_STR(STENCIL_RX_MAX_CLASSES) ")", NULL, NULL);
227 30835 cls = &ps->w->classes[ps->n_classes];
228 30835 memset(cls, 0, sizeof *cls);
229 30835 cls->range_idx = (uint16_t)ps->n_ranges;
230 30835 return ps->n_classes++;
231 }
232  
233 /* Add lo..hi to class cls: the part below 256 goes in the bitmap, the rest
234 * becomes a range entry. A class's ranges are contiguous because classes
235 * are parsed one at a time. */
236 23989 static int add_range(rx_parse *ps, stencil_rx_class *cls, uint32_t lo, uint32_t hi)
237 {
238 uint32_t c;
239 101405 ▶ for (c = lo; c <= hi && c < 256; c++)
Branch: 2 branches
BranchTrueFalseTotal
for (c = lo; c <= hi && c < 256; c++) 83412 17993 101405
for (c = lo; c <= hi && c < 256; c++) 77416 5996 83412
240 77416 BIT_SET(cls, c);
241 23989 ▶ if (hi >= 256) {
Branch: 1 branch
BranchTrueFalseTotal
if (hi >= 256) { 5996 17993 23989
242 stencil_rx_range *r;
243 5996 ▶ if (ps->n_ranges >= STENCIL_RX_MAX_RANGES)
Branch: 1 branch
BranchTrueFalseTotal
if (ps->n_ranges >= STENCIL_RX_MAX_RANGES) 1 5995 5996
244 1 return fail(ps, ps->cur, "character classes are too large", NULL,
245 NULL);
246 5995 r = &ps->w->ranges[ps->n_ranges++];
247 5995 r->lo = lo < 256 ? 256 : lo;
248 5995 r->hi = hi;
249 5995 cls->n_ranges++;
250 }
251 23988 return 0;
252 }
253  
254 /* the ASCII set behind \d, \w or \s */
255 23354 static void base_set(stencil_rx_class *cls, unsigned which)
256 {
257 unsigned c;
258 23354 switch (which) {
259 7244 case 'd':
260 79684 ▶ for (c = '0'; c <= '9'; c++) BIT_SET(cls, c);
Branch: 1 branch
BranchTrueFalseTotal
for (c = '0'; c <= '9'; c++) BIT_SET(cls, c); 72440 7244 79684
261 7244 break;
262 8915 case 'w':
263 1150035 ▶ for (c = 0; c < 128; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < 128; c++) 1141120 8915 1150035
264 1141120 ▶ if (IS_WORD(c)) BIT_SET(cls, c);
Branch: 7 branches
BranchTrueFalseTotal
if (IS_WORD(c)) BIT_SET(cls, c); 561645 579475 1141120
if (IS_WORD(c)) BIT_SET(cls, c); 329855 231790 561645
if (IS_WORD(c)) BIT_SET(cls, c); 276365 632965 909330
if (IS_WORD(c)) BIT_SET(cls, c); 44575 231790 276365
if (IS_WORD(c)) BIT_SET(cls, c); 249620 427920 677540
if (IS_WORD(c)) BIT_SET(cls, c); 160470 89150 249620
if (IS_WORD(c)) BIT_SET(cls, c); 8915 579475 588390
265 8915 break;
266 7195 case 's':
267 928155 ▶ for (c = 0; c < 128; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < 128; c++) 920960 7195 928155
268 920960 ▶ if (IS_XSPACE(c)) BIT_SET(cls, c);
Branch: 6 branches
BranchTrueFalseTotal
if (IS_XSPACE(c)) BIT_SET(cls, c); 913765 7195 920960
if (IS_XSPACE(c)) BIT_SET(cls, c); 906570 7195 913765
if (IS_XSPACE(c)) BIT_SET(cls, c); 899375 7195 906570
if (IS_XSPACE(c)) BIT_SET(cls, c); 892180 7195 899375
if (IS_XSPACE(c)) BIT_SET(cls, c); 884985 7195 892180
if (IS_XSPACE(c)) BIT_SET(cls, c); 7195 877790 884985
269 7195 break;
270 }
271 23354 }
272  
273 /* \d \D \w \W \s \S inside brackets: union into cls */
274 7258 static int add_shorthand(rx_parse *ps, stencil_rx_class *cls, unsigned c)
275 {
276 stencil_rx_class tmp;
277 int i;
278 7258 memset(&tmp, 0, sizeof tmp);
279 7258 base_set(&tmp, c | 0x20u);
280 7258 ▶ if (IS_LOWER(c)) {
Branch: 2 branches
BranchTrueFalseTotal
if (IS_LOWER(c)) { 5490 1768 7258
if (IS_LOWER(c)) { 5490 0 5490
281 49410 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 43920 5490 49410
282 43920 cls->bits[i] |= tmp.bits[i];
283 5490 return 0;
284 }
285 15912 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 14144 1768 15912
286 14144 cls->bits[i] |= ~tmp.bits[i];
287 1768 return add_range(ps, cls, 256, 0xFFFFFFFFu);
288 }
289  
290 /* a standalone \d \D \w \W \s \S: one shared class per kind */
291 16999 static int shorthand_node(rx_parse *ps, unsigned c)
292 {
293 static const char kinds[] = "dDwWsS";
294 16999 int slot = (int)(strchr(kinds, (int)c) - kinds);
295 int idx, n;
296 16999 ▶ if (ps->shorthand[slot] < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->shorthand[slot] < 0) { 16096 903 16999
297 16096 idx = new_class(ps);
298 16096 ▶ if (idx < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) 0 16096 16096
299 0 return -1;
300 16096 base_set(&ps->w->classes[idx], c | 0x20u);
301 16096 ▶ ps->w->classes[idx].neg = IS_UPPER(c) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
ps->w->classes[idx].neg = IS_UPPER(c) ? 1 : 0; 16096 0 16096
ps->w->classes[idx].neg = IS_UPPER(c) ? 1 : 0; 7913 8183 16096
302 16096 ps->shorthand[slot] = idx;
303 }
304 16999 n = new_node(ps, N_CLASS);
305 16999 ▶ if (n >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n >= 0) 16999 0 16999
306 16999 ps->w->nodes[n].val = (uint32_t)ps->shorthand[slot];
307 16999 return n;
308 }
309  
310 /* /i: close the class over ASCII case */
311 4889 static void fold_class(stencil_rx_class *cls)
312 {
313 unsigned c;
314 132003 ▶ for (c = 'a'; c <= 'z'; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 'a'; c <= 'z'; c++) 127114 4889 132003
315 127114 ▶ if (BIT_HAS(cls, c) || BIT_HAS(cls, c - 32)) {
Branch: 2 branches
BranchTrueFalseTotal
if (BIT_HAS(cls, c) || BIT_HAS(cls, c - 32)) { 110403 16711 127114
if (BIT_HAS(cls, c) || BIT_HAS(cls, c - 32)) { 16885 93518 110403
316 33596 BIT_SET(cls, c);
317 33596 BIT_SET(cls, c - 32);
318 }
319 4889 }
320  
321 /* ---- escapes -------------------------------------------------------------------- */
322  
323 /* A character escape; ps->cur is just past the backslash. Sets *cp. */
324 14168 static int escape_char(rx_parse *ps, const unsigned char *at, uint32_t *cp,
325 int in_class)
326 {
327 14168 unsigned c = *ps->cur;
328 char shown[3];
329 14168 switch (c) {
330 1 case 't': ps->cur++; *cp = 0x09; return 0;
331 2426 case 'n': ps->cur++; *cp = 0x0A; return 0;
332 1 case 'r': ps->cur++; *cp = 0x0D; return 0;
333 1 case 'f': ps->cur++; *cp = 0x0C; return 0;
334 1 case 'e': ps->cur++; *cp = 0x1B; return 0;
335 2 case 'a': ps->cur++; *cp = 0x07; return 0;
336 2 case '0': {
337 /* \0, \0o, \0oo: octal, as in perl */
338 2 uint32_t v = 0;
339 2 int ndig = 0;
340 2 ps->cur++;
341 4 ▶ while (ndig < 2 && ps->cur < ps->end && *ps->cur >= '0' && *ps->cur <= '7') {
Branch: 4 branches
BranchTrueFalseTotal
while (ndig < 2 && ps->cur < ps->end && *ps->cur >= '0' && *ps->cur <= '7') { 3 1 4
while (ndig < 2 && ps->cur < ps->end && *ps->cur >= '0' && *ps->cur <= '7') { 2 1 3
while (ndig < 2 && ps->cur < ps->end && *ps->cur >= '0' && *ps->cur <= '7') { 2 0 2
while (ndig < 2 && ps->cur < ps->end && *ps->cur >= '0' && *ps->cur <= '7') { 2 0 2
342 2 v = v * 8 + (uint32_t)(*ps->cur++ - '0');
343 2 ndig++;
344 }
345 2 *cp = v;
346 2 return 0;
347 }
348 6603 case 'x': {
349 6603 uint32_t v = 0;
350 6603 int ndig = 0;
351 6603 ps->cur++;
352 6603 ▶ if (ps->cur < ps->end && *ps->cur == '{') {
Branch: 2 branches
BranchTrueFalseTotal
if (ps->cur < ps->end && *ps->cur == '{') { 6602 1 6603
if (ps->cur < ps->end && *ps->cur == '{') { 6600 2 6602
353 6600 ps->cur++;
354 32991 ▶ while (ps->cur < ps->end && IS_XDIGIT(*ps->cur)) {
Branch: 7 branches
BranchTrueFalseTotal
while (ps->cur < ps->end && IS_XDIGIT(*ps->cur)) { 32990 1 32991
while (ps->cur < ps->end && IS_XDIGIT(*ps->cur)) { 32990 0 32990
while (ps->cur < ps->end && IS_XDIGIT(*ps->cur)) { 19795 13195 32990
while (ps->cur < ps->end && IS_XDIGIT(*ps->cur)) { 6600 6595 13195
while (ps->cur < ps->end && IS_XDIGIT(*ps->cur)) { 2 6598 6600
while (ps->cur < ps->end && IS_XDIGIT(*ps->cur)) { 13193 0 13193
while (ps->cur < ps->end && IS_XDIGIT(*ps->cur)) { 6595 6598 13193
355 26392 v = v * 16 + hexval(*ps->cur++);
356 26392 ▶ if (v > 0x10FFFF)
Branch: 1 branch
BranchTrueFalseTotal
if (v > 0x10FFFF) 1 26391 26392
357 1 return fail(ps, at, "\\x{...} is above U+10FFFF", NULL,
358 NULL);
359 26391 ndig++;
360 }
361 6599 ▶ if (ps->cur >= ps->end || *ps->cur != '}')
Branch: 2 branches
BranchTrueFalseTotal
if (ps->cur >= ps->end || *ps->cur != '}') 6598 1 6599
if (ps->cur >= ps->end || *ps->cur != '}') 0 6598 6598
362 1 return fail(ps, at, "unterminated \\x{...}", NULL, NULL);
363 6598 ▶ if (!ndig)
Branch: 1 branch
BranchTrueFalseTotal
if (!ndig) 1 6597 6598
364 1 return fail(ps, at, "empty \\x{}", NULL, NULL);
365 6597 ps->cur++;
366 } else {
367 6 ▶ while (ndig < 2 && ps->cur < ps->end && IS_XDIGIT(*ps->cur)) {
Branch: 8 branches
BranchTrueFalseTotal
while (ndig < 2 && ps->cur < ps->end && IS_XDIGIT(*ps->cur)) { 5 1 6
while (ndig < 2 && ps->cur < ps->end && IS_XDIGIT(*ps->cur)) { 3 2 5
while (ndig < 2 && ps->cur < ps->end && IS_XDIGIT(*ps->cur)) { 3 0 3
while (ndig < 2 && ps->cur < ps->end && IS_XDIGIT(*ps->cur)) { 3 0 3
while (ndig < 2 && ps->cur < ps->end && IS_XDIGIT(*ps->cur)) { 0 0 0
while (ndig < 2 && ps->cur < ps->end && IS_XDIGIT(*ps->cur)) { 0 0 0
while (ndig < 2 && ps->cur < ps->end && IS_XDIGIT(*ps->cur)) { 0 0 0
while (ndig < 2 && ps->cur < ps->end && IS_XDIGIT(*ps->cur)) { 0 0 0
368 3 v = v * 16 + hexval(*ps->cur++);
369 3 ndig++;
370 }
371 3 ▶ if (!ndig)
Branch: 1 branch
BranchTrueFalseTotal
if (!ndig) 1 2 3
372 1 return fail(ps, at, "\\x needs hex digits", NULL, NULL);
373 }
374 6599 *cp = v;
375 6599 return 0;
376 }
377 51 case '1': case '2': case '3': case '4': case '5':
378 case '6': case '7': case '8': case '9':
379 return in_class
380 17 ? fail(ps, at, "octal escapes other than \\0 are not supported",
381 NULL, NULL)
382 68 ▶ : fail(ps, at, "backreferences are not supported", NULL, NULL);
Branch: 1 branch
BranchTrueFalseTotal
: fail(ps, at, "backreferences are not supported", NULL, NULL); 17 34 51
383 2 case 'g': case 'k':
384 2 return fail(ps, at, "backreferences are not supported", NULL, NULL);
385 2 case 'p': case 'P':
386 2 return fail(ps, at, "Unicode properties (\\p, \\P) are not supported",
387 NULL, NULL);
388 1 case 'G':
389 1 return fail(ps, at, "\\G is not supported", NULL, NULL);
390 1 case 'K':
391 1 return fail(ps, at, "\\K is not supported", NULL, NULL);
392 }
393 5074 ▶ if (c < 0x80 && IS_ALNUM(c)) {
Branch: 7 branches
BranchTrueFalseTotal
if (c < 0x80 && IS_ALNUM(c)) { 5071 3 5074
if (c < 0x80 && IS_ALNUM(c)) { 31 5040 5071
if (c < 0x80 && IS_ALNUM(c)) { 28 3 31
if (c < 0x80 && IS_ALNUM(c)) { 11 5057 5068
if (c < 0x80 && IS_ALNUM(c)) { 8 3 11
if (c < 0x80 && IS_ALNUM(c)) { 27 5038 5065
if (c < 0x80 && IS_ALNUM(c)) { 0 27 27
394 6 shown[0] = '\\';
395 6 shown[1] = (char)c;
396 6 shown[2] = '\0';
397 6 ▶ return fail(ps, at, "unsupported escape '", shown,
Branch: 1 branch
BranchTrueFalseTotal
return fail(ps, at, "unsupported escape '", shown, 1 5 6
398 in_class ? "' in a character class" : "'");
399 }
400 /* backslash + punctuation, space or a non-ASCII character: literal */
401 5068 return pat_cp(ps, cp);
402 }
403  
404 /* an escape outside brackets; ps->cur at the backslash */
405 36206 static int parse_escape(rx_parse *ps)
406 {
407 36206 const unsigned char *at = ps->cur;
408 uint32_t cp;
409 36206 ps->cur++;
410 36206 ▶ if (ps->cur >= ps->end)
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cur >= ps->end) 1 36205 36206
411 1 return fail(ps, at, "trailing backslash", NULL, NULL);
412 36205 switch (*ps->cur) {
413 16999 case 'd': case 'D': case 'w': case 'W': case 's': case 'S':
414 16999 return shorthand_node(ps, *ps->cur++);
415 1885 case 'b': ps->cur++; return new_assert(ps, A_WORDB);
416 1808 case 'B': ps->cur++; return new_assert(ps, A_NWORDB);
417 1622 case 'A': ps->cur++; return new_assert(ps, A_BOS);
418 1645 case 'z': ps->cur++; return new_assert(ps, A_EOS);
419 1969 case 'Z': ps->cur++; return new_assert(ps, A_EOSNL);
420 }
421 10277 ▶ if (escape_char(ps, at, &cp, 0) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (escape_char(ps, at, &cp, 0) < 0) 49 10228 10277
422 49 return -1;
423 10228 return new_char(ps, cp);
424 }
425  
426 /* ---- bracketed classes ----------------------------------------------------------- */
427  
428 /* [:name:], [=x=] or [.x.] at ps->cur? (perl's POSIX forms) */
429 2 static int posix_form(const rx_parse *ps)
430 {
431 2 const unsigned char *q = ps->cur + 2;
432 2 unsigned kind = ps->cur[1];
433 7 ▶ while (q < ps->end && (IS_ALNUM(*q) || *q == '^'))
Branch: 8 branches
BranchTrueFalseTotal
while (q < ps->end && (IS_ALNUM(*q) || *q == '^')) 6 1 7
while (q < ps->end && (IS_ALNUM(*q) || *q == '^')) 5 1 6
while (q < ps->end && (IS_ALNUM(*q) || *q == '^')) 0 5 5
while (q < ps->end && (IS_ALNUM(*q) || *q == '^')) 5 1 6
while (q < ps->end && (IS_ALNUM(*q) || *q == '^')) 5 0 5
while (q < ps->end && (IS_ALNUM(*q) || *q == '^')) 1 0 1
while (q < ps->end && (IS_ALNUM(*q) || *q == '^')) 0 1 1
while (q < ps->end && (IS_ALNUM(*q) || *q == '^')) 0 1 1
434 5 q++;
435 2 ▶ return q + 1 < ps->end && *q == kind && q[1] == ']';
Branch: 3 branches
BranchTrueFalseTotal
return q + 1 < ps->end && *q == kind && q[1] == ']'; 1 1 2
return q + 1 < ps->end && *q == kind && q[1] == ']'; 1 0 1
return q + 1 < ps->end && *q == kind && q[1] == ']'; 1 0 1
436 }
437  
438 /* One class member: a character (*kind = 0, *cp set) or a shorthand
439 * (*kind = 'd', 'D', ...). */
440 35607 static int class_atom(rx_parse *ps, const unsigned char *open, uint32_t *cp,
441 unsigned *kind)
442 {
443 35607 const unsigned char *at = ps->cur;
444 35607 *kind = 0;
445 35607 ▶ if (*ps->cur != '\\')
Branch: 1 branch
BranchTrueFalseTotal
if (*ps->cur != '\\') 24456 11151 35607
446 24456 return pat_cp(ps, cp);
447 11151 ps->cur++;
448 11151 ▶ if (ps->cur >= ps->end)
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cur >= ps->end) 0 11151 11151
449 0 return fail(ps, open, "unterminated character class", NULL, NULL);
450 11151 switch (*ps->cur) {
451 7258 case 'd': case 'D': case 'w': case 'W': case 's': case 'S':
452 7258 *kind = *ps->cur++;
453 7258 return 0;
454 2 case 'b':
455 2 ps->cur++;
456 2 *cp = 0x08; /* [\b] is backspace, as in perl */
457 2 return 0;
458 }
459 3891 return escape_char(ps, at, cp, 1);
460 }
461  
462 14741 static int parse_class(rx_parse *ps)
463 {
464 14741 const unsigned char *open = ps->cur;
465 stencil_rx_class *cls;
466 14741 int idx, n, first = 1;
467 14741 ps->cur++;
468 14741 idx = new_class(ps);
469 14741 ▶ if (idx < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) 2 14739 14741
470 2 return -1;
471 14739 cls = &ps->w->classes[idx];
472 14739 ▶ if (ps->cur < ps->end && *ps->cur == '^') {
Branch: 2 branches
BranchTrueFalseTotal
if (ps->cur < ps->end && *ps->cur == '^') { 14738 1 14739
if (ps->cur < ps->end && *ps->cur == '^') { 4262 10476 14738
473 4262 cls->neg = 1;
474 4262 ps->cur++;
475 }
476 29360 for (;;) {
477 uint32_t lo, hi;
478 unsigned kind;
479 44099 ▶ if (ps->cur >= ps->end)
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cur >= ps->end) 17 44082 44099
480 48 return fail(ps, open, "unterminated character class", NULL, NULL);
481 44082 ▶ if (*ps->cur == ']' && !first) {
Branch: 2 branches
BranchTrueFalseTotal
if (*ps->cur == ']' && !first) { 14693 29389 44082
if (*ps->cur == ']' && !first) { 14691 2 14693
482 14691 ps->cur++;
483 14691 break;
484 }
485 29391 first = 0;
486 29391 ▶ if (*ps->cur == '[' && ps->cur + 1 < ps->end
Branch: 2 branches
BranchTrueFalseTotal
if (*ps->cur == '[' && ps->cur + 1 < ps->end 27 29364 29391
if (*ps->cur == '[' && ps->cur + 1 < ps->end 26 1 27
487 26 ▶ && (ps->cur[1] == ':' || ps->cur[1] == '=' || ps->cur[1] == '.')
Branch: 3 branches
BranchTrueFalseTotal
&& (ps->cur[1] == ':' || ps->cur[1] == '=' || ps->cur[1] == '.') 25 1 26
&& (ps->cur[1] == ':' || ps->cur[1] == '=' || ps->cur[1] == '.') 25 0 25
&& (ps->cur[1] == ':' || ps->cur[1] == '=' || ps->cur[1] == '.') 1 24 25
488 2 ▶ && posix_form(ps))
Branch: 1 branch
BranchTrueFalseTotal
&& posix_form(ps)) 1 1 2
489 1 return fail(ps, ps->cur, "POSIX classes like [:alpha:] are not "
490 "supported", NULL, NULL);
491 29390 ▶ if (class_atom(ps, open, &lo, &kind) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (class_atom(ps, open, &lo, &kind) < 0) 18 29372 29390
492 18 return -1;
493 29372 ▶ if (kind) {
Branch: 1 branch
BranchTrueFalseTotal
if (kind) { 7199 22173 29372
494 7199 ▶ if (add_shorthand(ps, cls, kind) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (add_shorthand(ps, cls, kind) < 0) 0 7199 7199
495 0 return -1;
496 7258 continue;
497 }
498 28320 ▶ if (ps->cur + 1 < ps->end && *ps->cur == '-' && ps->cur[1] != ']') {
Branch: 3 branches
BranchTrueFalseTotal
if (ps->cur + 1 < ps->end && *ps->cur == '-' && ps->cur[1] != ']') { 21004 1169 22173
if (ps->cur + 1 < ps->end && *ps->cur == '-' && ps->cur[1] != ']') { 6551 14453 21004
if (ps->cur + 1 < ps->end && *ps->cur == '-' && ps->cur[1] != ']') { 6217 334 6551
499 6217 const unsigned char *dash = ps->cur;
500 6217 ps->cur++;
501 6217 ▶ if (class_atom(ps, open, &hi, &kind) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (class_atom(ps, open, &hi, &kind) < 0) 0 6217 6217
502 0 return -1;
503 6217 ▶ if (kind) {
Branch: 1 branch
BranchTrueFalseTotal
if (kind) { 59 6158 6217
504 /* [a-\d]: perl calls this a "false range" - a, '-' and
505 * the shorthand, all literal members - and so do we */
506 59 ▶ if (add_range(ps, cls, lo, lo) < 0
Branch: 1 branch
BranchTrueFalseTotal
if (add_range(ps, cls, lo, lo) < 0 59 0 59
507 59 ▶ || add_range(ps, cls, '-', '-') < 0
Branch: 1 branch
BranchTrueFalseTotal
|| add_range(ps, cls, '-', '-') < 0 59 0 59
508 59 ▶ || add_shorthand(ps, cls, kind) < 0)
Branch: 1 branch
BranchTrueFalseTotal
|| add_shorthand(ps, cls, kind) < 0) 0 59 59
509 0 return -1;
510 59 continue;
511 }
512 6158 ▶ if (hi < lo)
Branch: 1 branch
BranchTrueFalseTotal
if (hi < lo) 11 6147 6158
513 11 return fail(ps, dash, "invalid range in a character class "
514 "(end before start)", NULL, NULL);
515 6147 ▶ if (add_range(ps, cls, lo, hi) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (add_range(ps, cls, lo, hi) < 0) 0 6147 6147
516 0 return -1;
517 15956 ▶ } else if (add_range(ps, cls, lo, lo) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (add_range(ps, cls, lo, lo) < 0) { 1 15955 15956
518 1 return -1;
519 }
520 }
521 14691 ▶ if (ps->flags & STENCIL_RX_I)
Branch: 1 branch
BranchTrueFalseTotal
if (ps->flags & STENCIL_RX_I) 4889 9802 14691
522 4889 fold_class(cls);
523 14691 n = new_node(ps, N_CLASS);
524 14691 ▶ if (n >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n >= 0) 14691 0 14691
525 14691 ps->w->nodes[n].val = (uint32_t)idx;
526 14691 return n;
527 }
528  
529 /* ---- quantifiers ------------------------------------------------------------------ */
530  
531 /* A {n}, {n,}, {n,m} or {,m} at ps->cur? Returns 1 (and consumes it), 0 if
532 * the brace is not a quantifier (nothing consumed), -1 on error. */
533 19014 static int parse_braces(rx_parse *ps, unsigned *min, unsigned *max, int *inf)
534 {
535 19014 const unsigned char *q = ps->cur + 1;
536 19014 unsigned long a = 0, b = 0;
537 19014 int have_a = 0, have_b = 0, comma = 0;
538 34310 ▶ while (q < ps->end && IS_DIGIT(*q)) {
Branch: 3 branches
BranchTrueFalseTotal
while (q < ps->end && IS_DIGIT(*q)) { 34306 4 34310
while (q < ps->end && IS_DIGIT(*q)) { 19295 15011 34306
while (q < ps->end && IS_DIGIT(*q)) { 15296 3999 19295
539 15296 ▶ if (a <= STENCIL_RX_MAX_REPEAT)
Branch: 1 branch
BranchTrueFalseTotal
if (a <= STENCIL_RX_MAX_REPEAT) 15296 0 15296
540 15296 a = a * 10 + (unsigned long)(*q - '0');
541 15296 have_a = 1;
542 15296 q++;
543 }
544 19014 ▶ if (q < ps->end && *q == ',') {
Branch: 2 branches
BranchTrueFalseTotal
if (q < ps->end && *q == ',') { 19010 4 19014
if (q < ps->end && *q == ',') { 15001 4009 19010
545 15001 comma = 1;
546 15001 q++;
547 26301 ▶ while (q < ps->end && IS_DIGIT(*q)) {
Branch: 3 branches
BranchTrueFalseTotal
while (q < ps->end && IS_DIGIT(*q)) { 26301 0 26301
while (q < ps->end && IS_DIGIT(*q)) { 26299 2 26301
while (q < ps->end && IS_DIGIT(*q)) { 11300 14999 26299
548 11300 ▶ if (b <= STENCIL_RX_MAX_REPEAT)
Branch: 1 branch
BranchTrueFalseTotal
if (b <= STENCIL_RX_MAX_REPEAT) 11283 17 11300
549 11283 b = b * 10 + (unsigned long)(*q - '0');
550 11300 have_b = 1;
551 11300 q++;
552 }
553 }
554 19014 ▶ if (q >= ps->end || *q != '}' || (!have_a && !have_b))
Branch: 4 branches
BranchTrueFalseTotal
if (q >= ps->end || *q != '}' || (!have_a && !have_b)) 19010 4 19014
if (q >= ps->end || *q != '}' || (!have_a && !have_b)) 18964 46 19010
if (q >= ps->end || *q != '}' || (!have_a && !have_b)) 3729 15235 18964
if (q >= ps->end || *q != '}' || (!have_a && !have_b)) 0 3729 3729
555 50 return 0;
556 18964 ▶ if (a > STENCIL_RX_MAX_REPEAT || b > STENCIL_RX_MAX_REPEAT)
Branch: 2 branches
BranchTrueFalseTotal
if (a > STENCIL_RX_MAX_REPEAT || b > STENCIL_RX_MAX_REPEAT) 18962 2 18964
if (a > STENCIL_RX_MAX_REPEAT || b > STENCIL_RX_MAX_REPEAT) 1 18961 18962
557 3 return fail(ps, ps->cur, "a {n,m} count is above "
558 RX_STR(STENCIL_RX_MAX_REPEAT), NULL, NULL);
559 18961 *min = (unsigned)a;
560 18961 ▶ *inf = comma && !have_b;
Branch: 2 branches
BranchTrueFalseTotal
*inf = comma && !have_b; 14998 3963 18961
*inf = comma && !have_b; 3718 11280 14998
561 18961 ▶ *max = comma ? (unsigned)b : (unsigned)a;
Branch: 1 branch
BranchTrueFalseTotal
*max = comma ? (unsigned)b : (unsigned)a; 14998 3963 18961
562 18961 ▶ if (!*inf && *max < *min)
Branch: 2 branches
BranchTrueFalseTotal
if (!*inf && *max < *min) 15243 3718 18961
if (!*inf && *max < *min) 1 15242 15243
563 1 return fail(ps, ps->cur, "{n,m} with n greater than m", NULL, NULL);
564 18960 ps->cur = q + 1;
565 18960 return 1;
566 }
567  
568 /* A quantifier at ps->cur? 1 (consumed), 0 (none), -1 (error). */
569 100837 static int parse_quant(rx_parse *ps, unsigned *min, unsigned *max, int *inf)
570 {
571 100837 ▶ if (ps->cur >= ps->end)
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cur >= ps->end) 18736 82101 100837
572 18736 return 0;
573 82101 *inf = 0;
574 82101 switch (*ps->cur) {
575 3858 case '*': ps->cur++; *min = 0; *max = 0; *inf = 1; return 1;
576 3450 case '+': ps->cur++; *min = 1; *max = 0; *inf = 1; return 1;
577 4091 case '?': ps->cur++; *min = 0; *max = 1; return 1;
578 18946 case '{': return parse_braces(ps, min, max, inf);
579 }
580 51756 return 0;
581 }
582  
583 /* ---- filler: /x whitespace and comments, (?#...) ----------------------------------- */
584  
585 311980 static void skip_x(rx_parse *ps)
586 {
587 311980 ▶ if (!(ps->flags & STENCIL_RX_X))
Branch: 1 branch
BranchTrueFalseTotal
if (!(ps->flags & STENCIL_RX_X)) 229039 82941 311980
588 229039 return;
589 83469 ▶ while (ps->cur < ps->end) {
Branch: 1 branch
BranchTrueFalseTotal
while (ps->cur < ps->end) { 66652 16817 83469
590 66652 ▶ if (IS_XSPACE(*ps->cur)) {
Branch: 6 branches
BranchTrueFalseTotal
if (IS_XSPACE(*ps->cur)) { 66131 521 66652
if (IS_XSPACE(*ps->cur)) { 66131 0 66131
if (IS_XSPACE(*ps->cur)) { 66126 5 66131
if (IS_XSPACE(*ps->cur)) { 66126 0 66126
if (IS_XSPACE(*ps->cur)) { 66126 0 66126
if (IS_XSPACE(*ps->cur)) { 0 66126 66126
591 526 ps->cur++;
592 66126 ▶ } else if (*ps->cur == '#') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*ps->cur == '#') { 2 66124 66126
593 14 ▶ while (ps->cur < ps->end && *ps->cur != '\n')
Branch: 2 branches
BranchTrueFalseTotal
while (ps->cur < ps->end && *ps->cur != '\n') 14 0 14
while (ps->cur < ps->end && *ps->cur != '\n') 12 2 14
594 12 ps->cur++;
595 } else {
596 66124 break;
597 }
598 }
599 }
600  
601 150573 static int skip_filler(rx_parse *ps)
602 {
603 for (;;) {
604 150575 skip_x(ps);
605 150575 ▶ if (ps->end - ps->cur >= 3 && ps->cur[0] == '(' && ps->cur[1] == '?'
Branch: 3 branches
BranchTrueFalseTotal
if (ps->end - ps->cur >= 3 && ps->cur[0] == '(' && ps->cur[1] == '?' 104561 46014 150575
if (ps->end - ps->cur >= 3 && ps->cur[0] == '(' && ps->cur[1] == '?' 13391 91170 104561
if (ps->end - ps->cur >= 3 && ps->cur[0] == '(' && ps->cur[1] == '?' 6633 6758 13391
606 6633 ▶ && ps->cur[2] == '#') {
Branch: 1 branch
BranchTrueFalseTotal
&& ps->cur[2] == '#') { 3 6630 6633
607 3 const unsigned char *open = ps->cur;
608 3 ps->cur += 3;
609 521 ▶ while (ps->cur < ps->end && *ps->cur != ')')
Branch: 2 branches
BranchTrueFalseTotal
while (ps->cur < ps->end && *ps->cur != ')') 520 1 521
while (ps->cur < ps->end && *ps->cur != ')') 518 2 520
610 518 ps->cur++;
611 3 ▶ if (ps->cur >= ps->end)
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cur >= ps->end) 1 2 3
612 1 return fail(ps, open, "unterminated (?#...) comment", NULL,
613 NULL);
614 2 ps->cur++;
615 2 continue;
616 }
617 150572 return 0;
618 }
619 }
620  
621 /* ---- the grammar ---------------------------------------------------------------------- */
622  
623 static int parse_alt(rx_parse *ps);
624  
625 /* [A-Za-z_][A-Za-z0-9_]* then `close`, starting at q; moves ps->cur past it */
626 27 static int skip_name(rx_parse *ps, const unsigned char *q, unsigned close)
627 {
628 27 ▶ if (q >= ps->end || !(IS_ALPHA(*q) || *q == '_'))
Branch: 6 branches
BranchTrueFalseTotal
if (q >= ps->end || !(IS_ALPHA(*q) || *q == '_')) 27 0 27
if (q >= ps->end || !(IS_ALPHA(*q) || *q == '_')) 27 0 27
if (q >= ps->end || !(IS_ALPHA(*q) || *q == '_')) 27 0 27
if (q >= ps->end || !(IS_ALPHA(*q) || *q == '_')) 27 0 27
if (q >= ps->end || !(IS_ALPHA(*q) || *q == '_')) 0 27 27
if (q >= ps->end || !(IS_ALPHA(*q) || *q == '_')) 0 0 0
629 0 return 0;
630 57 ▶ while (q < ps->end && IS_WORD(*q))
Branch: 8 branches
BranchTrueFalseTotal
while (q < ps->end && IS_WORD(*q)) 57 0 57
while (q < ps->end && IS_WORD(*q)) 30 27 57
while (q < ps->end && IS_WORD(*q)) 0 30 30
while (q < ps->end && IS_WORD(*q)) 30 27 57
while (q < ps->end && IS_WORD(*q)) 30 0 30
while (q < ps->end && IS_WORD(*q)) 25 2 27
while (q < ps->end && IS_WORD(*q)) 0 25 25
while (q < ps->end && IS_WORD(*q)) 0 27 27
631 30 q++;
632 27 ▶ if (q >= ps->end || *q != close)
Branch: 2 branches
BranchTrueFalseTotal
if (q >= ps->end || *q != close) 27 0 27
if (q >= ps->end || *q != close) 1 26 27
633 1 return 0;
634 26 ps->cur = q + 1;
635 26 return 1;
636 }
637  
638 13416 static int parse_group(rx_parse *ps)
639 {
640 13416 const unsigned char *open = ps->cur;
641 int n;
642 13416 ps->cur++;
643 13416 ▶ if (ps->cur < ps->end && *ps->cur == '*')
Branch: 2 branches
BranchTrueFalseTotal
if (ps->cur < ps->end && *ps->cur == '*') 13396 20 13416
if (ps->cur < ps->end && *ps->cur == '*') 2 13394 13396
644 2 return fail(ps, open, "backtracking verbs and (*...) assertions are "
645 "not supported", NULL, NULL);
646 13414 ▶ if (ps->cur < ps->end && *ps->cur == '?') {
Branch: 2 branches
BranchTrueFalseTotal
if (ps->cur < ps->end && *ps->cur == '?') { 13394 20 13414
if (ps->cur < ps->end && *ps->cur == '?') { 6630 6764 13394
647 6630 const unsigned char *q = ps->cur + 1;
648 6630 ▶ unsigned c = q < ps->end ? *q : 0;
Branch: 1 branch
BranchTrueFalseTotal
unsigned c = q < ps->end ? *q : 0; 6630 0 6630
649 6630 ▶ unsigned c2 = q + 1 < ps->end ? q[1] : 0;
Branch: 1 branch
BranchTrueFalseTotal
unsigned c2 = q + 1 < ps->end ? q[1] : 0; 6629 1 6630
650 6630 switch (c) {
651 6559 case ':':
652 6559 ps->cur = q + 1;
653 6559 break;
654 22 case '=': case '!':
655 22 return fail(ps, open, "lookahead (?= (?! is not supported", NULL,
656 NULL);
657 27 case '<':
658 27 ▶ if (c2 == '=' || c2 == '!')
Branch: 2 branches
BranchTrueFalseTotal
if (c2 == '=' || c2 == '!') 26 1 27
if (c2 == '=' || c2 == '!') 1 25 26
659 2 return fail(ps, open, "lookbehind (?<= (?<! is not supported",
660 NULL, NULL);
661 25 ▶ if (!skip_name(ps, q + 1, '>'))
Branch: 1 branch
BranchTrueFalseTotal
if (!skip_name(ps, q + 1, '>')) 1 24 25
662 1 return fail(ps, open, "malformed named group", NULL, NULL);
663 24 break;
664 1 case '\'':
665 1 ▶ if (!skip_name(ps, q + 1, '\''))
Branch: 1 branch
BranchTrueFalseTotal
if (!skip_name(ps, q + 1, '\'')) 0 1 1
666 0 return fail(ps, open, "malformed named group", NULL, NULL);
667 1 break;
668 3 case 'P':
669 3 ▶ if (c2 == '<') {
Branch: 1 branch
BranchTrueFalseTotal
if (c2 == '<') { 1 2 3
670 1 ▶ if (!skip_name(ps, q + 2, '>'))
Branch: 1 branch
BranchTrueFalseTotal
if (!skip_name(ps, q + 2, '>')) 0 1 1
671 0 return fail(ps, open, "malformed named group", NULL, NULL);
672 1 break;
673 }
674 2 ▶ if (c2 == '=')
Branch: 1 branch
BranchTrueFalseTotal
if (c2 == '=') 1 1 2
675 1 return fail(ps, open, "backreferences are not supported", NULL,
676 NULL);
677 1 ▶ if (c2 == '>')
Branch: 1 branch
BranchTrueFalseTotal
if (c2 == '>') 1 0 1
678 1 return fail(ps, open, "recursion is not supported", NULL, NULL);
679 0 return fail(ps, open, "unsupported group syntax", NULL, NULL);
680 1 case '>':
681 1 return fail(ps, open, "atomic groups (?> are not supported", NULL,
682 NULL);
683 3 case '{': case '?':
684 3 return fail(ps, open, "embedded code is not supported", NULL, NULL);
685 1 case '(':
686 1 return fail(ps, open, "conditionals (?( are not supported", NULL,
687 NULL);
688 1 case '|':
689 1 return fail(ps, open, "branch reset (?| is not supported", NULL,
690 NULL);
691 3 case 'R': case '&': case '+':
692 case '0': case '1': case '2': case '3': case '4':
693 case '5': case '6': case '7': case '8': case '9':
694 3 return fail(ps, open, "recursion is not supported", NULL, NULL);
695 2 case '-':
696 2 ▶ if (IS_DIGIT(c2))
Branch: 2 branches
BranchTrueFalseTotal
if (IS_DIGIT(c2)) 2 0 2
if (IS_DIGIT(c2)) 1 1 2
697 1 return fail(ps, open, "recursion is not supported", NULL, NULL);
698 /* FALLTHROUGH */
699 case '^':
700 2 return fail(ps, open, "inline modifiers like (?i) are not "
701 "supported; put the flags after the pattern", NULL,
702 NULL);
703 6 default:
704 6 ▶ if (IS_ALPHA(c))
Branch: 4 branches
BranchTrueFalseTotal
if (IS_ALPHA(c)) 6 0 6
if (IS_ALPHA(c)) 6 0 6
if (IS_ALPHA(c)) 5 1 6
if (IS_ALPHA(c)) 3 2 5
705 3 return fail(ps, open, "inline modifiers like (?i) are not "
706 "supported; put the flags after the pattern",
707 NULL, NULL);
708 3 return fail(ps, open, "unsupported group syntax", NULL, NULL);
709 }
710 }
711 13369 ▶ if (++ps->depth > STENCIL_RX_MAX_DEPTH)
Branch: 1 branch
BranchTrueFalseTotal
if (++ps->depth > STENCIL_RX_MAX_DEPTH) 4 13365 13369
712 4 return fail(ps, open, "groups nest deeper than "
713 RX_STR(STENCIL_RX_MAX_DEPTH), NULL, NULL);
714 13365 n = parse_alt(ps);
715 13365 ▶ if (n < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) 194 13171 13365
716 194 return -1;
717 13171 ▶ if (ps->cur >= ps->end || *ps->cur != ')')
Branch: 2 branches
BranchTrueFalseTotal
if (ps->cur >= ps->end || *ps->cur != ')') 13134 37 13171
if (ps->cur >= ps->end || *ps->cur != ')') 0 13134 13134
718 37 return fail(ps, open, "unmatched '('", NULL, NULL);
719 13134 ps->cur++;
720 13134 ps->depth--;
721 13134 return n;
722 }
723  
724 101313 static int parse_atom(rx_parse *ps)
725 {
726 101313 const unsigned char *at = ps->cur;
727 uint32_t cp;
728 int n;
729 101313 switch (*ps->cur) {
730 13416 case '(':
731 13416 return parse_group(ps);
732 35 case '*': case '+': case '?':
733 35 return fail(ps, at, "quantifier follows nothing", NULL, NULL);
734 58 case '{': {
735 unsigned a, b;
736 58 int inf, r = parse_braces(ps, &a, &b, &inf);
737 58 ▶ if (r < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (r < 0) 0 58 58
738 0 return -1;
739 58 ps->cur = at;
740 58 ▶ return fail(ps, at, r ? "quantifier follows nothing"
Branch: 1 branch
BranchTrueFalseTotal
return fail(ps, at, r ? "quantifier follows nothing" 29 29 58
741 : "unescaped '{' (write \\{ for a literal brace)",
742 NULL, NULL);
743 }
744 2945 case '.':
745 2945 ps->cur++;
746 2945 n = new_node(ps, N_ANY);
747 2945 ▶ if (n >= 0 && (ps->flags & STENCIL_RX_S))
Branch: 2 branches
BranchTrueFalseTotal
if (n >= 0 && (ps->flags & STENCIL_RX_S)) 2945 0 2945
if (n >= 0 && (ps->flags & STENCIL_RX_S)) 1033 1912 2945
748 1033 ps->w->nodes[n].flags = NF_DOTALL;
749 2945 return n;
750 2135 case '^':
751 2135 ps->cur++;
752 2135 return new_assert(ps, (ps->flags & STENCIL_RX_M) ? A_MBOL : A_BOS);
753 2022 case '$':
754 2022 ps->cur++;
755 2022 ▶ return new_assert(ps, (ps->flags & STENCIL_RX_M) ? A_MEOL : A_EOSNL);
Branch: 1 branch
BranchTrueFalseTotal
return new_assert(ps, (ps->flags & STENCIL_RX_M) ? A_MEOL : A_EOSNL); 606 1416 2022
756 14741 case '[':
757 14741 return parse_class(ps);
758 36206 case '\\':
759 36206 return parse_escape(ps);
760 }
761 29755 ▶ if (pat_cp(ps, &cp) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (pat_cp(ps, &cp) < 0) 1 29754 29755
762 1 return -1;
763 29754 return new_char(ps, cp);
764 }
765  
766 101313 static int parse_repeat(rx_parse *ps)
767 {
768 const unsigned char *qat;
769 101313 unsigned min = 0, max = 0;
770 101313 int inf = 0, r, rep, atom = parse_atom(ps);
771 101313 ▶ if (atom < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (atom < 0) 476 100837 101313
772 476 return -1;
773 100837 skip_x(ps); /* /x allows space between an atom and its quantifier */
774 100837 qat = ps->cur;
775 100837 r = parse_quant(ps, &min, &max, &inf);
776 100837 ▶ if (r <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (r <= 0) 70515 30322 100837
777 70515 ▶ return r < 0 ? -1 : atom;
Branch: 1 branch
BranchTrueFalseTotal
return r < 0 ? -1 : atom; 70511 4 70515
778 30322 ▶ if (ps->w->nodes[atom].type == N_ASSERT)
Branch: 1 branch
BranchTrueFalseTotal
if (ps->w->nodes[atom].type == N_ASSERT) 35 30287 30322
779 35 return fail(ps, qat, "a quantifier cannot follow an anchor or \\b",
780 NULL, NULL);
781 30287 skip_x(ps); /* and between a quantifier and its ? or + (perl does) */
782 30287 ▶ if (ps->cur < ps->end && *ps->cur == '+')
Branch: 2 branches
BranchTrueFalseTotal
if (ps->cur < ps->end && *ps->cur == '+') 23813 6474 30287
if (ps->cur < ps->end && *ps->cur == '+') 6 23807 23813
783 6 return fail(ps, ps->cur, "possessive quantifiers are not supported",
784 NULL, NULL);
785 30281 ▶ if (ps->cur < ps->end && *ps->cur == '?')
Branch: 2 branches
BranchTrueFalseTotal
if (ps->cur < ps->end && *ps->cur == '?') 23807 6474 30281
if (ps->cur < ps->end && *ps->cur == '?') 6295 17512 23807
786 6295 ps->cur++; /* lazy: the same yes/no answer */
787 30281 skip_x(ps);
788 30281 ▶ if (ps->cur < ps->end) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->cur < ps->end) { 22361 7920 30281
789 unsigned a, b;
790 int i2;
791 22361 const unsigned char *save = ps->cur;
792 22360 ▶ int q2 = (*ps->cur == '*' || *ps->cur == '+' || *ps->cur == '?')
Branch: 2 branches
BranchTrueFalseTotal
int q2 = (*ps->cur == '*' || *ps->cur == '+' || *ps->cur == '?') 22360 0 22360
int q2 = (*ps->cur == '*' || *ps->cur == '+' || *ps->cur == '?') 22358 2 22360
793 44721 ▶ || (*ps->cur == '{' && parse_braces(ps, &a, &b, &i2) == 1);
Branch: 3 branches
BranchTrueFalseTotal
|| (*ps->cur == '{' && parse_braces(ps, &a, &b, &i2) == 1); 22360 1 22361
|| (*ps->cur == '{' && parse_braces(ps, &a, &b, &i2) == 1); 10 22348 22358
|| (*ps->cur == '{' && parse_braces(ps, &a, &b, &i2) == 1); 8 2 10
794 22361 ps->cur = save;
795 22361 ▶ if (ps->failed)
Branch: 1 branch
BranchTrueFalseTotal
if (ps->failed) 0 22361 22361
796 11 return -1;
797 22361 ▶ if (q2)
Branch: 1 branch
BranchTrueFalseTotal
if (q2) 11 22350 22361
798 11 return fail(ps, ps->cur, "nested quantifiers", NULL, NULL);
799 }
800 30270 rep = new_node(ps, N_REP);
801 30270 ▶ if (rep < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rep < 0) 0 30270 30270
802 0 return -1;
803 30270 ps->w->nodes[rep].first = (int16_t)atom;
804 30270 ps->w->nodes[rep].min = (uint8_t)min;
805 30270 ps->w->nodes[rep].max = (uint8_t)max;
806 30270 ▶ ps->w->nodes[rep].flags = inf ? NF_INF : 0;
Branch: 1 branch
BranchTrueFalseTotal
ps->w->nodes[rep].flags = inf ? NF_INF : 0; 10990 19280 30270
807 30270 return rep;
808 }
809  
810 49792 static int parse_cat(rx_parse *ps)
811 {
812 49792 int head = NONE, tail = NONE, count = 0, n;
813 for (;;) {
814 150573 ▶ if (skip_filler(ps) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (skip_filler(ps) < 0) 1 150572 150573
815 1 return -1;
816 150572 ▶ if (ps->cur >= ps->end || *ps->cur == '|' || *ps->cur == ')')
Branch: 3 branches
BranchTrueFalseTotal
if (ps->cur >= ps->end || *ps->cur == '|' || *ps->cur == ')') 123865 26707 150572
if (ps->cur >= ps->end || *ps->cur == '|' || *ps->cur == ')') 114471 9394 123865
if (ps->cur >= ps->end || *ps->cur == '|' || *ps->cur == ')') 101313 13158 114471
817 break;
818 101313 n = parse_repeat(ps);
819 101313 ▶ if (n < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) 532 100781 101313
820 532 return -1;
821 100781 ▶ if (tail == NONE)
Branch: 1 branch
BranchTrueFalseTotal
if (tail == NONE) 49307 51474 100781
822 49307 head = n;
823 else
824 51474 ps->w->nodes[tail].next = (int16_t)n;
825 100781 tail = n;
826 100781 count++;
827 }
828 49259 ▶ if (count == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count == 1) 16156 33103 49259
829 16156 return head;
830 33103 ▶ n = new_node(ps, count ? N_CAT : N_EMPTY);
Branch: 1 branch
BranchTrueFalseTotal
n = new_node(ps, count ? N_CAT : N_EMPTY); 33005 98 33103
831 33103 ▶ if (n >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n >= 0) 33103 0 33103
832 33103 ps->w->nodes[n].first = (int16_t)head;
833 33103 return n;
834 }
835  
836 40398 static int parse_alt(rx_parse *ps)
837 {
838 40398 int first = parse_cat(ps), alt, tail, n;
839 40398 ▶ if (first < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (first < 0) 506 39892 40398
840 506 return -1;
841 39892 ▶ if (ps->cur >= ps->end || *ps->cur != '|')
Branch: 2 branches
BranchTrueFalseTotal
if (ps->cur >= ps->end || *ps->cur != '|') 19449 20443 39892
if (ps->cur >= ps->end || *ps->cur != '|') 11542 7907 19449
842 31985 return first;
843 7907 alt = new_node(ps, N_ALT);
844 7907 ▶ if (alt < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (alt < 0) 0 7907 7907
845 0 return -1;
846 7907 ps->w->nodes[alt].first = (int16_t)first;
847 7907 tail = first;
848 17274 ▶ while (ps->cur < ps->end && *ps->cur == '|') {
Branch: 2 branches
BranchTrueFalseTotal
while (ps->cur < ps->end && *ps->cur == '|') { 11010 6264 17274
while (ps->cur < ps->end && *ps->cur == '|') { 9394 1616 11010
849 9394 ps->cur++;
850 9394 n = parse_cat(ps);
851 9394 ▶ if (n < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) 27 9367 9394
852 27 return -1;
853 9367 ps->w->nodes[tail].next = (int16_t)n;
854 9367 tail = n;
855 }
856 7880 return alt;
857 }
858  
859 /* ---- emission ----------------------------------------------------------------------- */
860  
861 204239 static int inst(rx_parse *ps, unsigned op, unsigned arg, uint32_t x, uint32_t y)
862 {
863 stencil_rx_inst *ins;
864 204239 ▶ if (ps->n_insts >= STENCIL_RX_MAX_INSTS)
Branch: 1 branch
BranchTrueFalseTotal
if (ps->n_insts >= STENCIL_RX_MAX_INSTS) 8 204231 204239
865 8 return fail(ps, NULL, "regex is too complex (more than "
866 RX_STR(STENCIL_RX_MAX_INSTS) " states)", NULL, NULL);
867 204231 ins = &ps->insts[ps->n_insts];
868 204231 ins->op = (uint8_t)op;
869 204231 ins->arg = (uint8_t)arg;
870 204231 ins->pad = 0;
871 204231 ins->x = x;
872 204231 ins->y = y;
873 204231 return (int)ps->n_insts++;
874 }
875  
876 static int emit(rx_parse *ps, int n);
877  
878 33056 static int emit_rep(rx_parse *ps, const stencil_rx_node *node)
879 {
880 unsigned i;
881 33056 int s, child = node->first;
882 33056 ▶ if (node->flags & NF_INF) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->flags & NF_INF) { 11743 21313 33056
883 11743 ▶ if (node->min == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->min == 0) { 4118 7625 11743
884 /* L: SPLIT body, out; body; JMP L; out: */
885 4118 ▶ if ((s = inst(ps, OP_SPLIT, 0, 0, 0)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((s = inst(ps, OP_SPLIT, 0, 0, 0)) < 0) 0 4118 4118
886 0 return -1;
887 4118 ps->insts[s].x = (uint32_t)s + 1;
888 4118 ▶ if (emit(ps, child) < 0
Branch: 1 branch
BranchTrueFalseTotal
if (emit(ps, child) < 0 4118 0 4118
889 4118 ▶ || inst(ps, OP_JMP, 0, (uint32_t)s, 0) < 0)
Branch: 1 branch
BranchTrueFalseTotal
|| inst(ps, OP_JMP, 0, (uint32_t)s, 0) < 0) 0 4118 4118
890 0 return -1;
891 4118 ps->insts[s].y = ps->n_insts;
892 } else {
893 /* min-1 copies, then L: body; SPLIT L, out; out: */
894 uint32_t start;
895 11476 ▶ for (i = 1; i < node->min; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < node->min; i++) 3851 7625 11476
896 3851 ▶ if (emit(ps, child) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (emit(ps, child) < 0) 0 3851 3851
897 0 return -1;
898 7625 start = ps->n_insts;
899 7625 ▶ if (emit(ps, child) < 0
Branch: 1 branch
BranchTrueFalseTotal
if (emit(ps, child) < 0 7625 0 7625
900 7625 ▶ || (s = inst(ps, OP_SPLIT, 0, start, 0)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
|| (s = inst(ps, OP_SPLIT, 0, start, 0)) < 0) 0 7625 7625
901 0 return -1;
902 7625 ps->insts[s].y = (uint32_t)s + 1;
903 }
904 11743 return 0;
905 }
906 163620 ▶ for (i = 0; i < node->min; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < node->min; i++) 142328 21292 163620
907 142328 ▶ if (emit(ps, child) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (emit(ps, child) < 0) 21 142307 142328
908 21 return -1;
909 {
910 /* max-min optional copies, nested: (x(x(x)?)?)? - every skip
911 * edge goes to the common end, chained through .y until then */
912 21292 uint32_t chain = NOLINK;
913 49746 ▶ for (i = node->min; i < node->max; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = node->min; i < node->max; i++) { 28455 21291 49746
914 28455 ▶ if ((s = inst(ps, OP_SPLIT, 0, 0, chain)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((s = inst(ps, OP_SPLIT, 0, 0, chain)) < 0) 1 28454 28455
915 1 return -1;
916 28454 ps->insts[s].x = (uint32_t)s + 1;
917 28454 chain = (uint32_t)s;
918 28454 ▶ if (emit(ps, child) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (emit(ps, child) < 0) 0 28454 28454
919 0 return -1;
920 }
921 49745 ▶ while (chain != NOLINK) {
Branch: 1 branch
BranchTrueFalseTotal
while (chain != NOLINK) { 28454 21291 49745
922 28454 uint32_t next = ps->insts[chain].y;
923 28454 ps->insts[chain].y = ps->n_insts;
924 28454 chain = next;
925 }
926 }
927 21291 return 0;
928 }
929  
930 321171 static int emit(rx_parse *ps, int n)
931 {
932 321171 const stencil_rx_node *node = &ps->w->nodes[n];
933 int c;
934 321171 ▶ if (++ps->visits > MAX_VISITS)
Branch: 1 branch
BranchTrueFalseTotal
if (++ps->visits > MAX_VISITS) 2 321169 321171
935 2 return fail(ps, NULL, "regex is too complex", NULL, NULL);
936 321169 switch (node->type) {
937 130606 case N_EMPTY:
938 130606 return 0;
939 54762 case N_CHAR:
940 54762 return inst(ps, (node->flags & NF_FOLD) ? OP_CHARI : OP_CHAR, 0,
941 54762 ▶ node->val, 0) < 0 ? -1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
node->val, 0) < 0 ? -1 : 0; 6 54756 54762
942 3884 case N_ANY:
943 3884 ▶ return inst(ps, (node->flags & NF_DOTALL) ? OP_ANYNL : OP_ANY, 0, 0, 0)
Branch: 1 branch
BranchTrueFalseTotal
return inst(ps, (node->flags & NF_DOTALL) ? OP_ANYNL : OP_ANY, 0, 0, 0) 1413 2471 3884
944 3884 ▶ < 0 ? -1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
< 0 ? -1 : 0; 0 3884 3884
945 41160 case N_CLASS:
946 41160 ▶ return inst(ps, OP_CLASS, 0, node->val, 0) < 0 ? -1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return inst(ps, OP_CLASS, 0, node->val, 0) < 0 ? -1 : 0; 0 41160 41160
947 13970 case N_ASSERT:
948 13970 ▶ return inst(ps, OP_ASSERT, node->val, 0, 0) < 0 ? -1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return inst(ps, OP_ASSERT, node->val, 0, 0) < 0 ? -1 : 0; 0 13970 13970
949 35473 case N_CAT:
950 125595 ▶ for (c = node->first; c != NONE; c = ps->w->nodes[c].next)
Branch: 1 branch
BranchTrueFalseTotal
for (c = node->first; c != NONE; c = ps->w->nodes[c].next) 90124 35471 125595
951 90124 ▶ if (emit(ps, c) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (emit(ps, c) < 0) 2 90122 90124
952 2 return -1;
953 35471 return 0;
954 8258 case N_ALT: {
955 /* SPLIT a, next; a; JMP end; next: SPLIT b, next2; ... last; end:
956 * the JMPs chain through .x until the end is known */
957 8258 uint32_t chain = NOLINK;
958 int s, j;
959 18001 ▶ for (c = node->first; c != NONE; c = ps->w->nodes[c].next) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = node->first; c != NONE; c = ps->w->nodes[c].next) { 18001 0 18001
960 18001 ▶ if (ps->w->nodes[c].next == NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (ps->w->nodes[c].next == NONE) { 8258 9743 18001
961 8258 ▶ if (emit(ps, c) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (emit(ps, c) < 0) 0 8258 8258
962 0 return -1;
963 8258 break;
964 }
965 9743 ▶ if ((s = inst(ps, OP_SPLIT, 0, 0, 0)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((s = inst(ps, OP_SPLIT, 0, 0, 0)) < 0) 0 9743 9743
966 0 return -1;
967 9743 ps->insts[s].x = (uint32_t)s + 1;
968 9743 ▶ if (emit(ps, c) < 0 || (j = inst(ps, OP_JMP, 0, chain, 0)) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (emit(ps, c) < 0 || (j = inst(ps, OP_JMP, 0, chain, 0)) < 0) 9743 0 9743
if (emit(ps, c) < 0 || (j = inst(ps, OP_JMP, 0, chain, 0)) < 0) 0 9743 9743
969 0 return -1;
970 9743 chain = (uint32_t)j;
971 9743 ps->insts[s].y = ps->n_insts;
972 }
973 18001 ▶ while (chain != NOLINK) {
Branch: 1 branch
BranchTrueFalseTotal
while (chain != NOLINK) { 9743 8258 18001
974 9743 uint32_t next = ps->insts[chain].x;
975 9743 ps->insts[chain].x = ps->n_insts;
976 9743 chain = next;
977 }
978 8258 return 0;
979 }
980 33056 case N_REP:
981 33056 return emit_rep(ps, node);
982 }
983 0 return fail(ps, NULL, "internal error: bad regex node", NULL, NULL);
984 }
985  
986 /* ---- start-byte prefilter ---------------------------------------------------------------- */
987  
988 /* the first byte of cp's UTF-8 encoding */
989 34262 static unsigned utf8_lead(uint32_t cp)
990 {
991 34262 ▶ if (cp < 0x80) return cp;
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x80) return cp; 25948 8314 34262
992 8314 ▶ if (cp < 0x800) return 0xC0u | (cp >> 6);
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x800) return 0xC0u | (cp >> 6); 2944 5370 8314
993 5370 ▶ if (cp < 0x10000) return 0xE0u | (cp >> 12);
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x10000) return 0xE0u | (cp >> 12); 5370 0 5370
994 0 return 0xF0u | ((cp >> 18) & 0x07u);
995 }
996  
997 #define PF_SET(set, b) ((set)[(b) >> 5] |= 1u << ((b) & 31))
998 #define PF_HAS(set, b) (((set)[(b) >> 5] >> ((b) & 31)) & 1u)
999  
1000 /* Which bytes can begin a match? Walk the epsilon paths from the start,
1001 * passing every assertion (that only widens the set, never wrongly), and
1002 * collect the first byte each reachable consuming instruction accepts.
1003 * Off when the start reaches MATCH or '.' without consuming, and for
1004 * UTF-8 when a continuation byte (0x80-0xBF) could begin a match, since a
1005 * skip must never land inside a character. */
1006 26660 static void prefilter(stencil_rx *rx, const stencil_rx_inst *insts,
1007 const stencil_rx_class *classes)
1008 {
1009 uint8_t seen[STENCIL_RX_MAX_INSTS];
1010 uint16_t stack[2 * STENCIL_RX_MAX_INSTS + 2];
1011 26660 unsigned sp = 0, m, b, c;
1012 26660 int ok = 1;
1013  
1014 26660 memset(rx->pf_set, 0, sizeof rx->pf_set);
1015 26660 memset(seen, 0, rx->n_insts);
1016 26660 stack[sp++] = 0;
1017 93311 ▶ while (sp && ok) {
Branch: 2 branches
BranchTrueFalseTotal
while (sp && ok) { 68263 25048 93311
while (sp && ok) { 66651 1612 68263
1018 66651 uint32_t pc = stack[--sp];
1019 66651 const stencil_rx_inst *ins = &insts[pc];
1020 66651 ▶ if (seen[pc])
Branch: 1 branch
BranchTrueFalseTotal
if (seen[pc]) 488 66163 66651
1021 488 continue;
1022 66163 seen[pc] = 1;
1023 66163 switch (ins->op) {
1024 1255 case OP_JMP:
1025 1255 stack[sp++] = (uint16_t)ins->x;
1026 1255 break;
1027 16764 case OP_SPLIT:
1028 16764 stack[sp++] = (uint16_t)ins->y;
1029 16764 stack[sp++] = (uint16_t)ins->x;
1030 16764 break;
1031 6994 case OP_ASSERT:
1032 6994 stack[sp++] = (uint16_t)(pc + 1);
1033 6994 break;
1034 5633 case OP_MATCH:
1035 case OP_ANY:
1036 case OP_ANYNL:
1037 5633 ok = 0;
1038 5633 break;
1039 17131 case OP_CHAR:
1040 17131 ▶ if (ins->x < 256)
Branch: 1 branch
BranchTrueFalseTotal
if (ins->x < 256) 14445 2686 17131
1041 14445 PF_SET(rx->pf_set[0], ins->x);
1042 17131 PF_SET(rx->pf_set[1], utf8_lead(ins->x));
1043 17131 break;
1044 2028 case OP_CHARI:
1045 6084 ▶ for (m = 0; m < 2; m++) {
Branch: 1 branch
BranchTrueFalseTotal
for (m = 0; m < 2; m++) { 4056 2028 6084
1046 4056 PF_SET(rx->pf_set[m], ins->x);
1047 4056 PF_SET(rx->pf_set[m], ins->x - 32);
1048 }
1049 2028 break;
1050 16358 case OP_CLASS: {
1051 /* a word at a time: the class's bytes 0..255 (complemented
1052 * if negated) for latin-1 subjects; for UTF-8, its ASCII part,
1053 * plus every byte from 0x80 if it can match above 0x7F at all */
1054 16358 const stencil_rx_class *cls = &classes[ins->x];
1055 16358 ▶ uint32_t inv = cls->neg ? 0xFFFFFFFFu : 0;
Branch: 1 branch
BranchTrueFalseTotal
uint32_t inv = cls->neg ? 0xFFFFFFFFu : 0; 6579 9779 16358
1056 16358 ▶ int high = cls->neg || cls->n_ranges;
Branch: 2 branches
BranchTrueFalseTotal
int high = cls->neg || cls->n_ranges; 9779 6579 16358
int high = cls->neg || cls->n_ranges; 1935 7844 9779
1057 147222 ▶ for (c = 0; c < 8; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < 8; c++) { 130864 16358 147222
1058 130864 uint32_t w = cls->bits[c] ^ inv;
1059 130864 rx->pf_set[0][c] |= w;
1060 130864 ▶ if (c < 4)
Branch: 1 branch
BranchTrueFalseTotal
if (c < 4) 65432 65432 130864
1061 65432 rx->pf_set[1][c] |= w;
1062 65432 ▶ else if (w)
Branch: 1 branch
BranchTrueFalseTotal
else if (w) 28761 36671 65432
1063 28761 high = 1;
1064 }
1065 16358 ▶ if (high)
Branch: 1 branch
BranchTrueFalseTotal
if (high) 9119 7239 16358
1066 45595 ▶ for (c = 4; c < 8; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 4; c < 8; c++) 36476 9119 45595
1067 36476 rx->pf_set[1][c] = 0xFFFFFFFFu;
1068 16358 break;
1069 }
1070 }
1071 }
1072 79980 ▶ for (m = 0; m < 2; m++) {
Branch: 1 branch
BranchTrueFalseTotal
for (m = 0; m < 2; m++) { 53320 26660 79980
1073 /* a single byte (memchr) if exactly one bit is set in the set */
1074 53320 int words = 0, one = -1;
1075 53320 uint32_t w = 0;
1076 53320 rx->pf_ok[m] = (uint8_t)ok;
1077 479880 ▶ for (b = 0; b < 8; b++)
Branch: 1 branch
BranchTrueFalseTotal
for (b = 0; b < 8; b++) 426560 53320 479880
1078 426560 ▶ if (rx->pf_set[m][b]) {
Branch: 1 branch
BranchTrueFalseTotal
if (rx->pf_set[m][b]) { 165453 261107 426560
1079 165453 words++;
1080 165453 w = rx->pf_set[m][b];
1081 165453 one = (int)b;
1082 }
1083 53320 ▶ if (words == 1 && (w & (w - 1)) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (words == 1 && (w & (w - 1)) == 0) { 19209 34111 53320
if (words == 1 && (w & (w - 1)) == 0) { 16597 2612 19209
1084 16597 b = 0;
1085 170855 ▶ while (!(w & 1u)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!(w & 1u)) { 154258 16597 170855
1086 154258 w >>= 1;
1087 154258 b++;
1088 }
1089 16597 one = one * 32 + (int)b;
1090 } else {
1091 36723 one = -1;
1092 }
1093 53320 rx->pf_one[m] = (int16_t)one;
1094 }
1095 /* UTF-8: a continuation byte (0x80-0xBF, words 4 and 5) that could
1096 * begin a match would let a skip land inside a character */
1097 26660 ▶ if (rx->pf_set[1][4] || rx->pf_set[1][5])
Branch: 2 branches
BranchTrueFalseTotal
if (rx->pf_set[1][4] || rx->pf_set[1][5]) 18607 8053 26660
if (rx->pf_set[1][4] || rx->pf_set[1][5]) 0 18607 18607
1098 8053 rx->pf_ok[1] = 0;
1099 26660 rx->pf_pad = 0;
1100 26660 }
1101  
1102 /* ---- compile ------------------------------------------------------------------------- */
1103  
1104 27038 const stencil_rx *stencil_rx_compile(const char *pat, size_t len,
1105 int pat_utf8, unsigned flags,
1106 stencil_rx_work *w, stencil_rx_err *err)
1107 {
1108 rx_parse ps;
1109 27038 stencil_rx *rx = &w->out.rx;
1110 int root, i;
1111  
1112 27038 memset(&ps, 0, sizeof ps);
1113 27038 ps.pat = ps.cur = (const unsigned char *)pat;
1114 27038 ps.end = ps.pat + len;
1115 27038 ps.utf8 = pat_utf8 ? 1 : 0;
1116 27038 ps.flags = flags;
1117 27038 ps.w = w;
1118 27038 ps.err = err;
1119 189266 ▶ for (i = 0; i < 6; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 6; i++) 162228 27038 189266
1120 162228 ps.shorthand[i] = -1;
1121 27038 err->at = 0;
1122 27038 err->msg[0] = '\0';
1123  
1124 27038 ▶ if (flags & ~(STENCIL_RX_I | STENCIL_RX_M | STENCIL_RX_S | STENCIL_RX_X)) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & ~(STENCIL_RX_I | STENCIL_RX_M | STENCIL_RX_S | STENCIL_RX_X)) { 0 27038 27038
1125 0 fail(&ps, NULL, "unknown regex flag", NULL, NULL);
1126 0 return NULL;
1127 }
1128 27038 ▶ if (len > STENCIL_RX_MAX_PATTERN) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > STENCIL_RX_MAX_PATTERN) { 5 27033 27038
1129 5 fail(&ps, NULL, "regex is longer than "
1130 RX_STR(STENCIL_RX_MAX_PATTERN) " bytes", NULL, NULL);
1131 5 return NULL;
1132 }
1133  
1134 27033 root = parse_alt(&ps);
1135 27033 ▶ if (root >= 0 && ps.cur < ps.end)
Branch: 2 branches
BranchTrueFalseTotal
if (root >= 0 && ps.cur < ps.end) 26694 339 27033
if (root >= 0 && ps.cur < ps.end) 24 26670 26694
1136 24 fail(&ps, ps.cur, "unmatched ')'", NULL, NULL); /* parse_alt stops at ')' */
1137 27033 ▶ if (ps.failed)
Branch: 1 branch
BranchTrueFalseTotal
if (ps.failed) 363 26670 27033
1138 363 return NULL;
1139  
1140 /* layout: header | classes | ranges | instructions (emitted in place) */
1141 26670 rx->classes_off = (uint32_t)sizeof(stencil_rx);
1142 26670 rx->ranges_off = rx->classes_off
1143 26670 + (uint32_t)(ps.n_classes * sizeof(stencil_rx_class));
1144 26670 rx->insts_off = rx->ranges_off
1145 26670 + (uint32_t)(ps.n_ranges * sizeof(stencil_rx_range));
1146 26670 ▶ if (ps.n_classes)
Branch: 1 branch
BranchTrueFalseTotal
if (ps.n_classes) 18114 8556 26670
1147 18114 memcpy((char *)rx + rx->classes_off, w->classes,
1148 18114 ps.n_classes * sizeof(stencil_rx_class));
1149 26670 ▶ if (ps.n_ranges)
Branch: 1 branch
BranchTrueFalseTotal
if (ps.n_ranges) 4512 22158 26670
1150 4512 memcpy((char *)rx + rx->ranges_off, w->ranges,
1151 4512 ps.n_ranges * sizeof(stencil_rx_range));
1152 26670 ps.insts = (stencil_rx_inst *)((char *)rx + rx->insts_off);
1153  
1154 26670 ▶ if (emit(&ps, root) < 0 || inst(&ps, OP_MATCH, 0, 0, 0) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (emit(&ps, root) < 0 || inst(&ps, OP_MATCH, 0, 0, 0) < 0) 26661 9 26670
if (emit(&ps, root) < 0 || inst(&ps, OP_MATCH, 0, 0, 0) < 0) 1 26660 26661
1155 10 return NULL;
1156  
1157 26660 rx->n_insts = (uint16_t)ps.n_insts;
1158 26660 rx->n_classes = (uint16_t)ps.n_classes;
1159 26660 rx->n_ranges = (uint16_t)ps.n_ranges;
1160 53320 rx->anchored = (uint16_t)(ps.insts[0].op == OP_ASSERT
1161 26660 ▶ && ps.insts[0].arg == A_BOS);
Branch: 2 branches
BranchTrueFalseTotal
&& ps.insts[0].arg == A_BOS); 3295 23365 26660
&& ps.insts[0].arg == A_BOS); 1086 2209 3295
1162 26660 rx->size = rx->insts_off
1163 26660 + (uint32_t)(ps.n_insts * sizeof(stencil_rx_inst));
1164 26660 prefilter(rx, ps.insts,
1165 26660 (const stencil_rx_class *)((char *)rx + rx->classes_off));
1166 26660 return rx;
1167 }
1168  
1169 /* ---- match --------------------------------------------------------------------------- */
1170  
1171 #define RX_STACK (2 * STENCIL_RX_MAX_INSTS + 2)
1172  
1173 typedef struct rx_sim {
1174 const stencil_rx_inst *insts;
1175 const stencil_rx_class *classes;
1176 const stencil_rx_range *ranges;
1177 const unsigned char *s;
1178 size_t len;
1179 uint32_t gen;
1180 uint32_t mark[STENCIL_RX_MAX_INSTS]; /* == gen: visited */
1181 uint16_t stack[RX_STACK];
1182 } rx_sim;
1183  
1184 281680 static int assert_ok(unsigned kind, const unsigned char *s, size_t len,
1185 size_t pos)
1186 {
1187 /* Byte tests are exact here even for UTF-8: \n and the ASCII word
1188 * characters are single bytes, and no byte of a multi-byte character
1189 * is either. */
1190 int before, after;
1191 281680 switch (kind) {
1192 8979 case A_BOS: return pos == 0;
1193 920 ▶ case A_MBOL: return pos == 0 || (s[pos - 1] == '\n' && pos < len);
Branch: 3 branches
BranchTrueFalseTotal
case A_MBOL: return pos == 0 || (s[pos - 1] == '\n' && pos < len); 749 171 920
case A_MBOL: return pos == 0 || (s[pos - 1] == '\n' && pos < len); 41 708 749
case A_MBOL: return pos == 0 || (s[pos - 1] == '\n' && pos < len); 30 11 41
1194 4733 case A_EOS: return pos == len;
1195 260690 ▶ case A_EOSNL: return pos == len || (pos + 1 == len && s[pos] == '\n');
Branch: 3 branches
BranchTrueFalseTotal
case A_EOSNL: return pos == len || (pos + 1 == len && s[pos] == '\n'); 258503 2187 260690
case A_EOSNL: return pos == len || (pos + 1 == len && s[pos] == '\n'); 818 257685 258503
case A_EOSNL: return pos == len || (pos + 1 == len && s[pos] == '\n'); 66 752 818
1196 1315 ▶ case A_MEOL: return pos == len || s[pos] == '\n';
Branch: 2 branches
BranchTrueFalseTotal
case A_MEOL: return pos == len || s[pos] == '\n'; 1186 129 1315
case A_MEOL: return pos == len || s[pos] == '\n'; 57 1129 1186
1197 }
1198 5043 ▶ before = pos > 0 && IS_WORD(s[pos - 1]);
Branch: 8 branches
BranchTrueFalseTotal
before = pos > 0 && IS_WORD(s[pos - 1]); 3944 1099 5043
before = pos > 0 && IS_WORD(s[pos - 1]); 1974 1970 3944
before = pos > 0 && IS_WORD(s[pos - 1]); 1603 371 1974
before = pos > 0 && IS_WORD(s[pos - 1]); 1423 2150 3573
before = pos > 0 && IS_WORD(s[pos - 1]); 815 608 1423
before = pos > 0 && IS_WORD(s[pos - 1]); 1368 1597 2965
before = pos > 0 && IS_WORD(s[pos - 1]); 1132 236 1368
before = pos > 0 && IS_WORD(s[pos - 1]); 180 2549 2729
1199 5043 ▶ after = pos < len && IS_WORD(s[pos]);
Branch: 8 branches
BranchTrueFalseTotal
after = pos < len && IS_WORD(s[pos]); 4508 535 5043
after = pos < len && IS_WORD(s[pos]); 2419 2089 4508
after = pos < len && IS_WORD(s[pos]); 1925 494 2419
after = pos < len && IS_WORD(s[pos]); 1696 2318 4014
after = pos < len && IS_WORD(s[pos]); 952 744 1696
after = pos < len && IS_WORD(s[pos]); 1624 1646 3270
after = pos < len && IS_WORD(s[pos]); 1350 274 1624
after = pos < len && IS_WORD(s[pos]); 229 2767 2996
1200 5043 return (before != after) == (kind == A_WORDB);
1201 }
1202  
1203 55646 static int class_has(const rx_sim *sim, const stencil_rx_class *cls, uint32_t cp)
1204 {
1205 55646 int found = 0;
1206 55646 ▶ if (cp < 256) {
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 256) { 49828 5818 55646
1207 49828 found = (int)BIT_HAS(cls, cp);
1208 } else {
1209 5818 const stencil_rx_range *r = sim->ranges + cls->range_idx;
1210 unsigned i;
1211 6092 ▶ for (i = 0; i < cls->n_ranges; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cls->n_ranges; i++) 1481 4611 6092
1212 1481 ▶ if (cp >= r[i].lo && cp <= r[i].hi) {
Branch: 2 branches
BranchTrueFalseTotal
if (cp >= r[i].lo && cp <= r[i].hi) { 1209 272 1481
if (cp >= r[i].lo && cp <= r[i].hi) { 1207 2 1209
1213 1207 found = 1;
1214 1207 break;
1215 }
1216 }
1217 55646 return found ^ (cls->neg ? 1 : 0);
1218 }
1219  
1220 570286 static int step_ok(const rx_sim *sim, const stencil_rx_inst *ins, uint32_t cp)
1221 {
1222 570286 switch (ins->op) {
1223 502635 case OP_CHAR: return cp == ins->x;
1224 5580 ▶ case OP_CHARI: return (IS_UPPER(cp) ? cp | 0x20u : cp) == ins->x;
Branch: 2 branches
BranchTrueFalseTotal
case OP_CHARI: return (IS_UPPER(cp) ? cp | 0x20u : cp) == ins->x; 3005 2575 5580
case OP_CHARI: return (IS_UPPER(cp) ? cp | 0x20u : cp) == ins->x; 667 2338 3005
1225 5297 case OP_ANY: return cp != '\n';
1226 1128 case OP_ANYNL: return 1;
1227 55646 case OP_CLASS: return class_has(sim, &sim->classes[ins->x], cp);
1228 }
1229 0 return 0;
1230 }
1231  
1232 /* Add pc and everything reachable from it by epsilon moves at position
1233 * `pos` to the list; iterative, each state at most once per generation.
1234 * Returns 1 if MATCH is reachable. */
1235 679323 static int closure(rx_sim *sim, uint32_t pc0, size_t pos, uint16_t *list,
1236 unsigned *n)
1237 {
1238 679323 unsigned sp = 0;
1239 679323 sim->stack[sp++] = (uint16_t)pc0;
1240 2572815 ▶ while (sp) {
Branch: 1 branch
BranchTrueFalseTotal
while (sp) { 1904529 668286 2572815
1241 1904529 uint32_t pc = sim->stack[--sp];
1242 const stencil_rx_inst *ins;
1243 1904529 ▶ if (sim->mark[pc] == sim->gen)
Branch: 1 branch
BranchTrueFalseTotal
if (sim->mark[pc] == sim->gen) 361679 1542850 1904529
1244 361679 continue;
1245 1542850 sim->mark[pc] = sim->gen;
1246 1542850 ins = &sim->insts[pc];
1247 1542850 switch (ins->op) {
1248 104135 case OP_JMP:
1249 104135 sim->stack[sp++] = (uint16_t)ins->x;
1250 104135 break;
1251 555598 case OP_SPLIT:
1252 555598 sim->stack[sp++] = (uint16_t)ins->y;
1253 555598 sim->stack[sp++] = (uint16_t)ins->x;
1254 555598 break;
1255 281680 case OP_ASSERT:
1256 281680 ▶ if (assert_ok(ins->arg, sim->s, sim->len, pos))
Branch: 1 branch
BranchTrueFalseTotal
if (assert_ok(ins->arg, sim->s, sim->len, pos)) 10806 270874 281680
1257 10806 sim->stack[sp++] = (uint16_t)(pc + 1);
1258 281680 break;
1259 11037 case OP_MATCH:
1260 11037 return 1;
1261 590400 default:
1262 590400 list[(*n)++] = (uint16_t)pc;
1263 }
1264 }
1265 668286 return 0;
1266 }
1267  
1268 /* Start a fresh visited-set generation (marks equal to gen are visited). */
1269 327988 static void sim_new_gen(rx_sim *sim, unsigned n_insts)
1270 {
1271 327988 ▶ if (++sim->gen == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (++sim->gen == 0) { 0 327988 327988
1272 0 memset(sim->mark, 0, n_insts * sizeof sim->mark[0]);
1273 0 sim->gen = 1;
1274 }
1275 327988 }
1276  
1277 /* The first position >= from whose byte can begin a match (prefilter on),
1278 * or len if there is none. memchr when the set is a single byte. */
1279 20247 static size_t pf_next(const stencil_rx *rx, int m, const unsigned char *s,
1280 size_t len, size_t from)
1281 {
1282 20247 const uint32_t *set = rx->pf_set[m];
1283 20247 ▶ if (from >= len)
Branch: 1 branch
BranchTrueFalseTotal
if (from >= len) 2121 18126 20247
1284 2121 return len;
1285 18126 ▶ if (rx->pf_one[m] >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rx->pf_one[m] >= 0) { 8031 10095 18126
1286 const unsigned char *hit = (const unsigned char *)
1287 8031 memchr(s + from, rx->pf_one[m], len - from);
1288 8031 ▶ return hit ? (size_t)(hit - s) : len;
Branch: 1 branch
BranchTrueFalseTotal
return hit ? (size_t)(hit - s) : len; 3790 4241 8031
1289 }
1290 54648 ▶ while (from < len && !PF_HAS(set, s[from]))
Branch: 2 branches
BranchTrueFalseTotal
while (from < len && !PF_HAS(set, s[from])) 51596 3052 54648
while (from < len && !PF_HAS(set, s[from])) 44553 7043 51596
1291 44553 from++;
1292 10095 return from;
1293 }
1294  
1295 27279 int stencil_rx_match(const stencil_rx *rx, const char *str, size_t len,
1296 int s_utf8)
1297 {
1298 rx_sim sim;
1299 uint16_t la[STENCIL_RX_MAX_INSTS], lb[STENCIL_RX_MAX_INSTS];
1300 27279 uint16_t *cl = la, *nl = lb, *tl; /* current and next state lists */
1301 27279 unsigned cn = 0, nn;
1302 27279 size_t pos = 0;
1303 27279 int enc = s_utf8 ? 1 : 0; /* prefilter set for this encoding */
1304 int pf;
1305  
1306 27279 sim.insts = (const stencil_rx_inst *)((const char *)rx + rx->insts_off);
1307 27279 sim.classes = (const stencil_rx_class *)((const char *)rx
1308 27279 + rx->classes_off);
1309 27279 sim.ranges = (const stencil_rx_range *)((const char *)rx
1310 27279 + rx->ranges_off);
1311 27279 sim.s = (const unsigned char *)str;
1312 27279 sim.len = len;
1313 27279 memset(sim.mark, 0, rx->n_insts * sizeof sim.mark[0]);
1314 27279 sim.gen = 1;
1315  
1316 27279 ▶ pf = rx->pf_ok[enc] && !rx->anchored;
Branch: 2 branches
BranchTrueFalseTotal
pf = rx->pf_ok[enc] && !rx->anchored; 18127 9152 27279
pf = rx->pf_ok[enc] && !rx->anchored; 13651 4476 18127
1317 27279 ▶ if (pf) {
Branch: 1 branch
BranchTrueFalseTotal
if (pf) { 13651 13628 27279
1318 /* a match consumes its start byte: no possible start, no match */
1319 13651 pos = pf_next(rx, enc, sim.s, len, 0);
1320 13651 ▶ if (pos >= len)
Branch: 1 branch
BranchTrueFalseTotal
if (pos >= len) 7118 6533 13651
1321 7118 return 0;
1322 }
1323 20161 ▶ if (closure(&sim, 0, pos, cl, &cn))
Branch: 1 branch
BranchTrueFalseTotal
if (closure(&sim, 0, pos, cl, &cn)) 2712 17449 20161
1324 2712 return 1;
1325 332074 ▶ while (pos < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (pos < len) { 325967 6107 332074
1326 uint32_t cp;
1327 size_t npos;
1328 unsigned i;
1329 325967 ▶ if (cn == 0 && rx->anchored)
Branch: 2 branches
BranchTrueFalseTotal
if (cn == 0 && rx->anchored) 7487 318480 325967
if (cn == 0 && rx->anchored) 721 6766 7487
1330 11342 return 0;
1331 /* decode the character at pos */
1332 325246 ▶ if (!s_utf8 || sim.s[pos] < 0x80) {
Branch: 2 branches
BranchTrueFalseTotal
if (!s_utf8 || sim.s[pos] < 0x80) { 51352 273894 325246
if (!s_utf8 || sim.s[pos] < 0x80) { 38886 12466 51352
1333 312780 cp = sim.s[pos];
1334 312780 npos = pos + 1;
1335 } else {
1336 12466 int nbytes = u8_decode(sim.s + pos, len - pos, &cp);
1337 12466 ▶ if (nbytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (nbytes) { 12460 6 12466
1338 12460 npos = pos + (size_t)nbytes;
1339 } else {
1340 6 cp = BADCP | sim.s[pos];
1341 6 npos = pos + 1;
1342 }
1343 }
1344 325246 sim_new_gen(&sim, rx->n_insts);
1345 325246 nn = 0;
1346 888030 ▶ for (i = 0; i < cn; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cn; i++) 570286 317744 888030
1347 570286 ▶ if (step_ok(&sim, &sim.insts[cl[i]], cp)
Branch: 1 branch
BranchTrueFalseTotal
if (step_ok(&sim, &sim.insts[cl[i]], cp) 508648 61638 570286
1348 508648 ▶ && closure(&sim, (uint32_t)cl[i] + 1, npos, nl, &nn))
Branch: 1 branch
BranchTrueFalseTotal
&& closure(&sim, (uint32_t)cl[i] + 1, npos, nl, &nn)) 7502 501146 508648
1349 7502 return 1;
1350 317744 ▶ if (!rx->anchored) {
Branch: 1 branch
BranchTrueFalseTotal
if (!rx->anchored) { 152810 164934 317744
1351 152810 ▶ if (pf && nn == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (pf && nn == 0) { 115493 37317 152810
if (pf && nn == 0) { 6596 108897 115493
1352 /* no state survived: skip to the next byte a match can
1353 * begin with */
1354 6596 size_t np = pf_next(rx, enc, sim.s, len, npos);
1355 6596 ▶ if (np >= len)
Branch: 1 branch
BranchTrueFalseTotal
if (np >= len) 2296 4300 6596
1356 2296 return 0;
1357 4300 ▶ if (np != npos) {
Branch: 1 branch
BranchTrueFalseTotal
if (np != npos) { 2742 1558 4300
1358 /* the marks made at npos must not hide states at np:
1359 * an assertion that failed there may hold here */
1360 2742 npos = np;
1361 2742 sim_new_gen(&sim, rx->n_insts);
1362 }
1363 }
1364 150514 ▶ if (closure(&sim, 0, npos, nl, &nn))
Branch: 1 branch
BranchTrueFalseTotal
if (closure(&sim, 0, npos, nl, &nn)) 823 149691 150514
1365 823 return 1;
1366 }
1367 314625 tl = cl; cl = nl; nl = tl;
1368 314625 cn = nn;
1369 314625 pos = npos;
1370 }
1371 6107 return 0;
1372 }
« src/stencil_filters.c ↑ Summary include/stencil_str.h »