include/frx/frx_xpath_eval.h

filter: Statementstmt 92.1% 810 / 879 Branchbran 67.7% 603 / 890 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.8% 1413 / 1769 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/xpath.xs ↑ Summary include/frx/frx_xpath_lex.h »
1 #ifndef FRX_XPATH_EVAL_H
2 #define FRX_XPATH_EVAL_H
3  
4 /* XPath 1.0 evaluation: four types, thirteen axes, 27 functions.
5 *
6 * NODE-SETS are arrays of (node, kind, index): kind selects the node
7 * itself, its i'th attribute, or its i'th namespace node, because an
8 * attribute is not an frx_node and a namespace node is not stored at all.
9 * Document order among the three is XPath section 5's: the element, then
10 * its namespace nodes, then its attributes, then its children. An
11 * attribute and a namespace node therefore sort on their element's
12 * `order` and are separated by kind, which is why the kind constants are
13 * numbered in that order and never renumbered.
14 *
15 * NAMESPACE NODES are synthesised from the scope chain at evaluation and
16 * never stored on the tree: the in-scope bindings, nearest per prefix,
17 * with a binding to the empty URI (xmlns="") dropped because it declares
18 * the absence of one, with xml always present, sorted by prefix so the
19 * list is the same list every time an index into it is read back. XPath
20 * leaves the relative order of namespace nodes to the implementation and
21 * requires only that it be stable; this is the stable order chosen.
22 * Synthesis is O(depth) per element and is paid again each time a
23 * namespace item's name or value is read, which is the cost of not
24 * carrying namespace nodes in a tree that has no use for them.
25 *
26 * RECURSION. Over the document, none (rule 2): every axis is an
27 * iterative walk and every predicate is a loop. Over the AST, yes,
28 * bounded by the depth the compiler capped at `max_expr_depth`, which is
29 * the AST's depth and not the parser's, so a long chain of associative
30 * operators is bounded too.
31 *
32 * NUMBERS are C doubles with the specification's semantics throughout:
33 * NaN, the infinities and negative zero all reach the caller. The
34 * number-to-string conversion is section 4.2's and not a printf format:
35 * no exponent ever appears, an integer-valued double has no fraction, and
36 * the digits are the shortest that read back as the same double. It finds
37 * those digits through sprintf and strtod, which agree with each other
38 * whatever LC_NUMERIC says, and then lays the decimal point out itself,
39 * so a locale whose decimal separator is a comma cannot reach the output.
40 *
41 * Needs frx_buf.h, frx_utf8.h, frx_tree.h, frx_ns.h, frx_parse.h,
42 * frx_xpath_parse.h. */
43  
44 #include <math.h>
45 #include <float.h>
46  
47 /* FRX_XP_KNODE, FRX_XP_KNS and FRX_XP_KATTR are frx_abi.h's, numbered in
48 * document order among themselves and never renumbered. */
49  
50 /* the four value types */
51 enum { FRX_XV_NODESET = 0, FRX_XV_BOOLEAN, FRX_XV_NUMBER, FRX_XV_STRING };
52  
53 typedef struct frx_xp_item {
54 const frx_node *node; /* the node, or the owner element of an attribute
55 * or namespace entry */
56 int kind;
57 int index;
58 } frx_xp_item;
59  
60 typedef struct frx_xp_set {
61 frx_xp_item *v;
62 int n, cap;
63 } frx_xp_set;
64  
65 typedef struct frx_xp_val {
66 int kind;
67 int bl;
68 double num;
69 frx_buf str;
70 frx_xp_set set;
71 } frx_xp_val;
72  
73 typedef struct frx_xp_var {
74 frx_str name;
75 frx_xp_val val;
76 } frx_xp_var;
77  
78 typedef struct frx_xeval {
79 frx_doc *doc;
80 const frx_xp_var *vars;
81 int n_vars;
82 const char *err; /* static; NULL while all is well */
83 int failed;
84 } frx_xeval;
85  
86 typedef struct frx_xctx {
87 frx_xp_item item;
88 int position; /* 1-based */
89 int size;
90 } frx_xctx;
91  
92 static int frx_xp_eval(frx_xeval *e, const frx_xn *n, const frx_xctx *c, frx_xp_val *out);
93  
94 static int
95 2 frx_xp_efail(frx_xeval *e, const char *what)
96 {
97 2 ▶ if (!e->failed) { e->failed = 1; e->err = what; }
Branch: 1 branch
BranchTrueFalseTotal
if (!e->failed) { e->failed = 1; e->err = what; } 2 0 2
98 2 return 0;
99 }
100  
101 /* ---- doubles ----------------------------------------------------------- */
102  
103 static double
104 4 frx_xp_nan(void)
105 {
106 4 volatile double z = 0.0;
107 4 return z / z;
108 }
109  
110 static int
111 293 frx_xp_is_nan(double v)
112 {
113 293 return v != v;
114 }
115  
116 static int
117 69 frx_xp_is_inf(double v)
118 {
119 69 ▶ return v > DBL_MAX || v < -DBL_MAX;
Branch: 2 branches
BranchTrueFalseTotal
return v > DBL_MAX || v < -DBL_MAX; 63 6 69
return v > DBL_MAX || v < -DBL_MAX; 1 62 63
120 }
121  
122 /* section 4.2: no exponent, no trailing zeros, the shortest digits that
123 * read back as this double */
124 static void
125 54 frx_xp_num_str(frx_buf *out, double v)
126 {
127 char fmt[16];
128 char buf[512];
129 char digits[40];
130 double av;
131 54 int p, i, ndig = 0, e10 = 0, neg = 0, dp;
132  
133 65 ▶ if (frx_xp_is_nan(v)) { frx_buf_append(out, "NaN"); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (frx_xp_is_nan(v)) { frx_buf_append(out, "NaN"); return; } 7 47 54
134 47 ▶ if (v == 0.0) { frx_buf_append(out, "0"); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (v == 0.0) { frx_buf_append(out, "0"); return; } 8 39 47
135 39 ▶ if (v < 0) { neg = 1; av = -v; } else av = v;
Branch: 1 branch
BranchTrueFalseTotal
if (v < 0) { neg = 1; av = -v; } else av = v; 7 32 39
136 39 ▶ if (frx_xp_is_inf(av)) { frx_buf_append(out, neg ? "-Infinity" : "Infinity"); return; }
Branch: 2 branches
BranchTrueFalseTotal
if (frx_xp_is_inf(av)) { frx_buf_append(out, neg ? "-Infinity" : "Infinity"); return; } 3 36 39
if (frx_xp_is_inf(av)) { frx_buf_append(out, neg ? "-Infinity" : "Infinity"); return; } 1 2 3
137  
138 87 ▶ for (p = 0; p < 17; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < 17; p++) { 87 0 87
139 87 FRX_FMT1(fmt, sizeof fmt, "%%.%de", p);
140 87 FRX_FMT1(buf, sizeof buf, fmt, av);
141 87 ▶ if (strtod(buf, NULL) == av) break;
Branch: 1 branch
BranchTrueFalseTotal
if (strtod(buf, NULL) == av) break; 36 51 87
142 }
143  
144 /* buf is d<sep>ddde<sign>dd, where <sep> is whatever LC_NUMERIC said */
145 36 i = 0;
146 36 digits[ndig++] = buf[i++];
147 36 ▶ if (buf[i] && buf[i] != 'e' && buf[i] != 'E') {
Branch: 3 branches
BranchTrueFalseTotal
if (buf[i] && buf[i] != 'e' && buf[i] != 'E') { 36 0 36
if (buf[i] && buf[i] != 'e' && buf[i] != 'E') { 6 30 36
if (buf[i] && buf[i] != 'e' && buf[i] != 'E') { 6 0 6
148 6 i++; /* the decimal separator */
149 57 ▶ while (buf[i] >= '0' && buf[i] <= '9' && ndig < (int)sizeof digits)
Branch: 3 branches
BranchTrueFalseTotal
while (buf[i] >= '0' && buf[i] <= '9' && ndig < (int)sizeof digits) 57 0 57
while (buf[i] >= '0' && buf[i] <= '9' && ndig < (int)sizeof digits) 51 6 57
while (buf[i] >= '0' && buf[i] <= '9' && ndig < (int)sizeof digits) 51 0 51
150 51 digits[ndig++] = buf[i++];
151 }
152 36 ▶ if (buf[i] == 'e' || buf[i] == 'E') {
Branch: 2 branches
BranchTrueFalseTotal
if (buf[i] == 'e' || buf[i] == 'E') { 0 36 36
if (buf[i] == 'e' || buf[i] == 'E') { 0 0 0
153 36 int sign = 1;
154 36 i++;
155 36 ▶ if (buf[i] == '+') i++;
Branch: 1 branch
BranchTrueFalseTotal
if (buf[i] == '+') i++; 31 5 36
156 5 ▶ else if (buf[i] == '-') { sign = -1; i++; }
Branch: 1 branch
BranchTrueFalseTotal
else if (buf[i] == '-') { sign = -1; i++; } 5 0 5
157 108 ▶ while (buf[i] >= '0' && buf[i] <= '9') e10 = e10 * 10 + (buf[i++] - '0');
Branch: 2 branches
BranchTrueFalseTotal
while (buf[i] >= '0' && buf[i] <= '9') e10 = e10 * 10 + (buf[i++] - '0'); 72 36 108
while (buf[i] >= '0' && buf[i] <= '9') e10 = e10 * 10 + (buf[i++] - '0'); 72 0 72
158 36 e10 *= sign;
159 }
160 36 ▶ while (ndig > 1 && digits[ndig - 1] == '0') ndig--;
Branch: 2 branches
BranchTrueFalseTotal
while (ndig > 1 && digits[ndig - 1] == '0') ndig--; 6 30 36
while (ndig > 1 && digits[ndig - 1] == '0') ndig--; 0 6 6
161  
162 36 dp = e10 + 1; /* digits before the point */
163 36 ▶ if (neg) frx_buf_append_ch(out, '-');
Branch: 1 branch
BranchTrueFalseTotal
if (neg) frx_buf_append_ch(out, '-'); 6 30 36
164 36 ▶ if (dp <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (dp <= 0) { 5 31 36
165 5 frx_buf_append(out, "0.");
166 16 ▶ for (i = 0; i < -dp; i++) frx_buf_append_ch(out, '0');
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < -dp; i++) frx_buf_append_ch(out, '0'); 11 5 16
167 5 frx_buf_append_n(out, digits, (size_t)ndig);
168 31 ▶ } else if (dp >= ndig) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (dp >= ndig) { 30 1 31
169 30 frx_buf_append_n(out, digits, (size_t)ndig);
170 49 ▶ for (i = 0; i < dp - ndig; i++) frx_buf_append_ch(out, '0');
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < dp - ndig; i++) frx_buf_append_ch(out, '0'); 19 30 49
171 } else {
172 1 frx_buf_append_n(out, digits, (size_t)dp);
173 1 frx_buf_append_ch(out, '.');
174 1 frx_buf_append_n(out, digits + dp, (size_t)(ndig - dp));
175 }
176 }
177  
178 /* Section 4.4: optional space, optional '-', Digits ('.' Digits?)? or
179 * '.' Digits, optional space; anything else is NaN. No sign but '-' and
180 * no exponent, so the syntax is checked here and only the span that
181 * passed reaches frx_xp_atod: strtod would take "1e5" and "+1", which
182 * are not Numbers. */
183 static double
184 18 frx_xp_str_num(const char *p, size_t n)
185 {
186 18 size_t at = 0, start;
187 18 int any = 0;
188  
189 20 ▶ while (at < n && frx_is_s((unsigned char)p[at])) at++;
Branch: 2 branches
BranchTrueFalseTotal
while (at < n && frx_is_s((unsigned char)p[at])) at++; 19 1 20
while (at < n && frx_is_s((unsigned char)p[at])) at++; 2 17 19
190 18 start = at;
191 18 ▶ if (at < n && p[at] == '-') at++;
Branch: 2 branches
BranchTrueFalseTotal
if (at < n && p[at] == '-') at++; 17 1 18
if (at < n && p[at] == '-') at++; 1 16 17
192 36 ▶ while (at < n && p[at] >= '0' && p[at] <= '9') { at++; any = 1; }
Branch: 3 branches
BranchTrueFalseTotal
while (at < n && p[at] >= '0' && p[at] <= '9') { at++; any = 1; } 24 12 36
while (at < n && p[at] >= '0' && p[at] <= '9') { at++; any = 1; } 20 4 24
while (at < n && p[at] >= '0' && p[at] <= '9') { at++; any = 1; } 18 2 20
193 18 ▶ if (at < n && p[at] == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (at < n && p[at] == '.') { 6 12 18
if (at < n && p[at] == '.') { 2 4 6
194 2 at++;
195 4 ▶ while (at < n && p[at] >= '0' && p[at] <= '9') { at++; any = 1; }
Branch: 3 branches
BranchTrueFalseTotal
while (at < n && p[at] >= '0' && p[at] <= '9') { at++; any = 1; } 2 2 4
while (at < n && p[at] >= '0' && p[at] <= '9') { at++; any = 1; } 2 0 2
while (at < n && p[at] >= '0' && p[at] <= '9') { at++; any = 1; } 2 0 2
196 }
197 18 ▶ if (!any) return frx_xp_nan();
Branch: 1 branch
BranchTrueFalseTotal
if (!any) return frx_xp_nan(); 2 16 18
198 {
199 16 size_t end = at;
200 18 ▶ while (at < n && frx_is_s((unsigned char)p[at])) at++;
Branch: 2 branches
BranchTrueFalseTotal
while (at < n && frx_is_s((unsigned char)p[at])) at++; 4 14 18
while (at < n && frx_is_s((unsigned char)p[at])) at++; 2 2 4
201 16 ▶ if (at != n) return frx_xp_nan();
Branch: 1 branch
BranchTrueFalseTotal
if (at != n) return frx_xp_nan(); 2 14 16
202 14 return frx_xp_atod(p + start, end - start);
203 }
204 }
205  
206 /* section 4.4: the integer closest to v, ties to positive infinity */
207 static double
208 22 frx_xp_round(double v)
209 {
210 22 ▶ if (frx_xp_is_nan(v) || frx_xp_is_inf(v) || v == 0.0) return v;
Branch: 3 branches
BranchTrueFalseTotal
if (frx_xp_is_nan(v) || frx_xp_is_inf(v) || v == 0.0) return v; 20 2 22
if (frx_xp_is_nan(v) || frx_xp_is_inf(v) || v == 0.0) return v; 17 3 20
if (frx_xp_is_nan(v) || frx_xp_is_inf(v) || v == 0.0) return v; 1 16 17
211 16 ▶ if (v < 0.0 && v >= -0.5) return -0.0;
Branch: 2 branches
BranchTrueFalseTotal
if (v < 0.0 && v >= -0.5) return -0.0; 3 13 16
if (v < 0.0 && v >= -0.5) return -0.0; 1 2 3
212 15 return floor(v + 0.5);
213 }
214  
215 /* ---- buffers and values ------------------------------------------------ */
216  
217 static void
218 275 frx_xp_buf_reset(frx_buf *b)
219 {
220 275 b->len = 0;
221 275 ▶ if (b->p) b->p[0] = '\0';
Branch: 1 branch
BranchTrueFalseTotal
if (b->p) b->p[0] = '\0'; 12 263 275
222 275 }
223  
224 static void
225 1212 frx_xp_val_init(frx_xp_val *v)
226 {
227 1212 memset(v, 0, sizeof *v);
228 1212 v->kind = FRX_XV_NODESET;
229 1212 frx_buf_init(&v->str);
230 1212 }
231  
232 static void
233 1640 frx_xp_set_free(frx_xp_set *s)
234 {
235 1640 free(s->v);
236 1640 s->v = NULL;
237 1640 s->n = s->cap = 0;
238 1640 }
239  
240 static void
241 1208 frx_xp_val_free(frx_xp_val *v)
242 {
243 1208 frx_buf_free(&v->str);
244 1208 frx_xp_set_free(&v->set);
245 1208 }
246  
247 static int
248 3376 frx_xp_set_push(frx_xp_set *s, const frx_xp_item *it)
249 {
250 3376 ▶ if (s->n == s->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->n == s->cap) { 543 2833 3376
251 543 ▶ int ncap = s->cap ? s->cap * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = s->cap ? s->cap * 2 : 16; 0 543 543
252 543 frx_xp_item *nv = (frx_xp_item *)realloc(s->v, (size_t)ncap * sizeof *nv);
253 543 ▶ if (!nv) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!nv) return 0; 0 543 543
254 543 s->v = nv;
255 543 s->cap = ncap;
256 }
257 3376 s->v[s->n++] = *it;
258 3376 return 1;
259 }
260  
261 static int
262 128 frx_xp_set_push3(frx_xp_set *s, const frx_node *n, int kind, int index)
263 {
264 frx_xp_item it;
265 128 it.node = n;
266 128 it.kind = kind;
267 128 it.index = index;
268 128 return frx_xp_set_push(s, &it);
269 }
270  
271 static int
272 3770 frx_xp_item_cmp(const void *pa, const void *pb)
273 {
274 3770 const frx_xp_item *a = (const frx_xp_item *)pa;
275 3770 const frx_xp_item *b = (const frx_xp_item *)pb;
276 3770 ▶ if (a->node->order != b->node->order) return a->node->order < b->node->order ? -1 : 1;
Branch: 2 branches
BranchTrueFalseTotal
if (a->node->order != b->node->order) return a->node->order < b->node->order ? -1 : 1; 3750 20 3770
if (a->node->order != b->node->order) return a->node->order < b->node->order ? -1 : 1; 2364 1386 3750
277 20 ▶ if (a->kind != b->kind) return a->kind < b->kind ? -1 : 1;
Branch: 2 branches
BranchTrueFalseTotal
if (a->kind != b->kind) return a->kind < b->kind ? -1 : 1; 4 16 20
if (a->kind != b->kind) return a->kind < b->kind ? -1 : 1; 2 2 4
278 16 ▶ if (a->index != b->index) return a->index < b->index ? -1 : 1;
Branch: 2 branches
BranchTrueFalseTotal
if (a->index != b->index) return a->index < b->index ? -1 : 1; 8 8 16
if (a->index != b->index) return a->index < b->index ? -1 : 1; 4 4 8
279 8 return 0;
280 }
281  
282 static void
283 296 frx_xp_set_sort(frx_xp_set *s)
284 {
285 int i, w;
286 296 ▶ if (s->n < 2) return;
Branch: 1 branch
BranchTrueFalseTotal
if (s->n < 2) return; 124 172 296
287 172 qsort(s->v, (size_t)s->n, sizeof *s->v, frx_xp_item_cmp);
288 1510 ▶ for (i = 1, w = 1; i < s->n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1, w = 1; i < s->n; i++) 1338 172 1510
289 1338 ▶ if (frx_xp_item_cmp(&s->v[i], &s->v[w - 1]) != 0) s->v[w++] = s->v[i];
Branch: 1 branch
BranchTrueFalseTotal
if (frx_xp_item_cmp(&s->v[i], &s->v[w - 1]) != 0) s->v[w++] = s->v[i]; 1334 4 1338
290 172 s->n = w;
291 }
292  
293 /* ---- namespace nodes --------------------------------------------------- */
294  
295 typedef struct frx_xp_nsnode {
296 frx_str prefix;
297 frx_str uri;
298 } frx_xp_nsnode;
299  
300 static int
301 29 frx_xp_nsnode_cmp(const void *pa, const void *pb)
302 {
303 29 const frx_xp_nsnode *a = (const frx_xp_nsnode *)pa;
304 29 const frx_xp_nsnode *b = (const frx_xp_nsnode *)pb;
305 29 return frx_str_cmp(&a->prefix, &b->prefix);
306 }
307  
308 /* the element's namespace nodes, malloc'd, sorted by prefix; 0 on an
309 * allocation failure */
310 static int
311 29 frx_xp_ns_list(const frx_node *e, frx_xp_nsnode **out, int *n_out)
312 {
313 static const frx_str xmlp = { "xml", 3 };
314 static const frx_str xmlu = { FRX_XML_NS, sizeof FRX_XML_NS - 1 };
315 const frx_scope *s;
316 29 frx_xp_nsnode *v = NULL;
317 29 int n = 0, cap = 0, i, j, seen;
318  
319 29 *out = NULL;
320 29 *n_out = 0;
321 58 ▶ for (s = e->scope; s; s = s->up) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = e->scope; s; s = s->up) { 29 29 58
322 58 ▶ for (i = 0; i < s->n_decls; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->n_decls; i++) { 29 29 58
323 29 const frx_nsdecl *d = &s->decls[i];
324 29 seen = 0;
325 29 ▶ for (j = 0; j < n; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) 0 29 29
326 0 ▶ if (frx_str_eq2(&v[j].prefix, &d->prefix)) { seen = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq2(&v[j].prefix, &d->prefix)) { seen = 1; break; } 0 0 0
327 29 ▶ if (seen) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (seen) continue; 0 29 29
328 29 ▶ if (n == cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == cap) { 29 0 29
329 29 ▶ int ncap = cap ? cap * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = cap ? cap * 2 : 8; 0 29 29
330 29 frx_xp_nsnode *nv = (frx_xp_nsnode *)realloc(v, (size_t)ncap * sizeof *nv);
331 29 ▶ if (!nv) { free(v); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!nv) { free(v); return 0; } 0 29 29
332 29 v = nv; cap = ncap;
333 }
334 29 v[n].prefix = d->prefix;
335 29 v[n].uri = d->uri;
336 29 n++;
337 }
338 }
339 /* an in-scope binding to the empty URI declares no namespace: xmlns=""
340 * removes the default namespace node rather than adding one */
341 58 ▶ for (i = 0, j = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0, j = 0; i < n; i++) 29 29 58
342 29 ▶ if (v[i].uri.len) v[j++] = v[i];
Branch: 1 branch
BranchTrueFalseTotal
if (v[i].uri.len) v[j++] = v[i]; 29 0 29
343 29 n = j;
344 29 ▶ if (n == cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == cap) { 0 29 29
345 0 ▶ int ncap = cap ? cap * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = cap ? cap * 2 : 8; 0 0 0
346 0 frx_xp_nsnode *nv = (frx_xp_nsnode *)realloc(v, (size_t)ncap * sizeof *nv);
347 0 ▶ if (!nv) { free(v); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!nv) { free(v); return 0; } 0 0 0
348 0 v = nv; cap = ncap;
349 }
350 29 v[n].prefix = xmlp;
351 29 v[n].uri = xmlu;
352 29 n++;
353 29 ▶ if (n > 1) qsort(v, (size_t)n, sizeof *v, frx_xp_nsnode_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (n > 1) qsort(v, (size_t)n, sizeof *v, frx_xp_nsnode_cmp); 29 0 29
354 29 *out = v;
355 29 *n_out = n;
356 29 return 1;
357 }
358  
359 static int
360 23 frx_xp_ns_at(const frx_node *e, int i, frx_str *prefix, frx_str *uri)
361 {
362 frx_xp_nsnode *v;
363 int n;
364 23 *prefix = *uri = FRX_EMPTY_STR;
365 23 ▶ if (!frx_xp_ns_list(e, &v, &n)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_ns_list(e, &v, &n)) return 0; 0 23 23
366 23 ▶ if (i >= 0 && i < n) { *prefix = v[i].prefix; *uri = v[i].uri; }
Branch: 2 branches
BranchTrueFalseTotal
if (i >= 0 && i < n) { *prefix = v[i].prefix; *uri = v[i].uri; } 23 0 23
if (i >= 0 && i < n) { *prefix = v[i].prefix; *uri = v[i].uri; } 23 0 23
367 23 free(v);
368 23 return 1;
369 }
370  
371 /* ---- what an item is --------------------------------------------------- */
372  
373 static const frx_node *
374 126 frx_xp_root_of(const frx_node *n)
375 {
376 130 ▶ while (n->parent) n = n->parent;
Branch: 1 branch
BranchTrueFalseTotal
while (n->parent) n = n->parent; 4 126 130
377 126 return n;
378 }
379  
380 static int
381 33 frx_xp_item_string(frx_xeval *e, const frx_xp_item *it, frx_buf *out)
382 {
383 33 switch (it->kind) {
384 1 case FRX_XP_KATTR: {
385 1 const frx_str *v = &it->node->attrs[it->index].value;
386 1 frx_buf_append_n(out, v->p, v->len);
387 1 break;
388 }
389 1 case FRX_XP_KNS: {
390 frx_str prefix, uri;
391 1 ▶ if (!frx_xp_ns_at(it->node, it->index, &prefix, &uri))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_ns_at(it->node, it->index, &prefix, &uri)) 0 1 1
392 0 return frx_xp_efail(e, "out of memory");
393 1 frx_buf_append_n(out, uri.p, uri.len);
394 1 break;
395 }
396 31 default:
397 31 frx_text(it->node, out);
398 31 break;
399 }
400 33 ▶ return out->failed ? frx_xp_efail(e, "out of memory") : 1;
Branch: 1 branch
BranchTrueFalseTotal
return out->failed ? frx_xp_efail(e, "out of memory") : 1; 0 33 33
401 }
402  
403 /* the expanded name's three faces: local part, namespace URI, and the
404 * name as it would be written */
405 static int
406 9 frx_xp_item_name(frx_xeval *e, const frx_xp_item *it, int which, frx_buf *out)
407 {
408 9 const frx_node *n = it->node;
409 9 const frx_str *s = &FRX_EMPTY_STR;
410 frx_str prefix, uri;
411  
412 9 switch (it->kind) {
413 2 case FRX_XP_KATTR:
414 3 s = which == 0 ? &n->attrs[it->index].local
415 3 ▶ : which == 1 ? &n->attrs[it->index].ns
Branch: 1 branch
BranchTrueFalseTotal
: which == 1 ? &n->attrs[it->index].ns 1 1 2
416 1 ▶ : &n->attrs[it->index].qname;
Branch: 1 branch
BranchTrueFalseTotal
: &n->attrs[it->index].qname; 0 1 1
417 2 break;
418 1 case FRX_XP_KNS:
419 1 ▶ if (!frx_xp_ns_at(n, it->index, &prefix, &uri))
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_ns_at(n, it->index, &prefix, &uri)) 0 1 1
420 0 return frx_xp_efail(e, "out of memory");
421 1 ▶ s = which == 1 ? &FRX_EMPTY_STR : &prefix;
Branch: 1 branch
BranchTrueFalseTotal
s = which == 1 ? &FRX_EMPTY_STR : &prefix; 1 0 1
422 1 break;
423 6 default:
424 6 ▶ if (n->kind == FRX_ELEMENT)
Branch: 1 branch
BranchTrueFalseTotal
if (n->kind == FRX_ELEMENT) 6 0 6
425 6 ▶ s = which == 0 ? &n->local : which == 1 ? &n->ns : &n->qname;
Branch: 2 branches
BranchTrueFalseTotal
s = which == 0 ? &n->local : which == 1 ? &n->ns : &n->qname; 4 2 6
s = which == 0 ? &n->local : which == 1 ? &n->ns : &n->qname; 1 1 2
426 0 ▶ else if (n->kind == FRX_PI && which != 1)
Branch: 2 branches
BranchTrueFalseTotal
else if (n->kind == FRX_PI && which != 1) 0 0 0
else if (n->kind == FRX_PI && which != 1) 0 0 0
427 0 s = &n->local;
428 6 break;
429 }
430 9 frx_buf_append_n(out, s->p, s->len);
431 9 ▶ return out->failed ? frx_xp_efail(e, "out of memory") : 1;
Branch: 1 branch
BranchTrueFalseTotal
return out->failed ? frx_xp_efail(e, "out of memory") : 1; 0 9 9
432 }
433  
434 /* ---- conversions ------------------------------------------------------- */
435  
436 static int
437 478 frx_xp_to_bool(frx_xeval *e, const frx_xp_val *v)
438 {
439 (void)e;
440 478 switch (v->kind) {
441 10 case FRX_XV_NODESET: return v->set.n > 0;
442 258 case FRX_XV_BOOLEAN: return v->bl != 0;
443 206 ▶ case FRX_XV_NUMBER: return !frx_xp_is_nan(v->num) && v->num != 0.0;
Branch: 2 branches
BranchTrueFalseTotal
case FRX_XV_NUMBER: return !frx_xp_is_nan(v->num) && v->num != 0.0; 205 1 206
case FRX_XV_NUMBER: return !frx_xp_is_nan(v->num) && v->num != 0.0; 203 2 205
444 4 default: return v->str.len > 0;
445 }
446 }
447  
448 /* the string-value of a value: for a node-set, of its first node in
449 * document order, the set already being sorted */
450 static int
451 253 frx_xp_to_str(frx_xeval *e, const frx_xp_val *v, frx_buf *out)
452 {
453 253 frx_xp_buf_reset(out);
454 253 switch (v->kind) {
455 19 case FRX_XV_NODESET:
456 19 ▶ if (v->set.n) return frx_xp_item_string(e, &v->set.v[0], out);
Branch: 1 branch
BranchTrueFalseTotal
if (v->set.n) return frx_xp_item_string(e, &v->set.v[0], out); 14 5 19
457 5 return 1;
458 36 case FRX_XV_BOOLEAN:
459 36 ▶ frx_buf_append(out, v->bl ? "true" : "false");
Branch: 1 branch
BranchTrueFalseTotal
frx_buf_append(out, v->bl ? "true" : "false"); 23 13 36
460 36 break;
461 54 case FRX_XV_NUMBER:
462 54 frx_xp_num_str(out, v->num);
463 54 break;
464 144 default:
465 144 ▶ frx_buf_append_n(out, v->str.p ? v->str.p : "", v->str.len);
Branch: 1 branch
BranchTrueFalseTotal
frx_buf_append_n(out, v->str.p ? v->str.p : "", v->str.len); 139 5 144
466 144 break;
467 }
468 234 ▶ return out->failed ? frx_xp_efail(e, "out of memory") : 1;
Branch: 1 branch
BranchTrueFalseTotal
return out->failed ? frx_xp_efail(e, "out of memory") : 1; 0 234 234
469 }
470  
471 static int
472 151 frx_xp_to_num(frx_xeval *e, const frx_xp_val *v, double *out)
473 {
474 151 switch (v->kind) {
475 4 ▶ case FRX_XV_BOOLEAN: *out = v->bl ? 1.0 : 0.0; return 1;
Branch: 1 branch
BranchTrueFalseTotal
case FRX_XV_BOOLEAN: *out = v->bl ? 1.0 : 0.0; return 1; 3 1 4
476 138 case FRX_XV_NUMBER: *out = v->num; return 1;
477 9 ▶ case FRX_XV_STRING: *out = frx_xp_str_num(v->str.p ? v->str.p : "", v->str.len); return 1;
Branch: 1 branch
BranchTrueFalseTotal
case FRX_XV_STRING: *out = frx_xp_str_num(v->str.p ? v->str.p : "", v->str.len); return 1; 9 0 9
478 0 default: {
479 frx_buf b;
480 int ok;
481 0 frx_buf_init(&b);
482 0 ok = frx_xp_to_str(e, v, &b);
483 0 ▶ *out = ok ? frx_xp_str_num(b.p ? b.p : "", b.len) : frx_xp_nan();
Branch: 2 branches
BranchTrueFalseTotal
*out = ok ? frx_xp_str_num(b.p ? b.p : "", b.len) : frx_xp_nan(); 0 0 0
*out = ok ? frx_xp_str_num(b.p ? b.p : "", b.len) : frx_xp_nan(); 0 0 0
484 0 frx_buf_free(&b);
485 0 return ok;
486 }
487 }
488 }
489  
490 /* ---- UTF-8 by character ------------------------------------------------ */
491  
492 static size_t
493 2 frx_xp_nchars(const char *p, size_t n)
494 {
495 2 size_t at = 0, count = 0;
496 unsigned long cp;
497 12 ▶ while (at < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (at < n) { 10 2 12
498 10 size_t k = frx_utf8_decode((const unsigned char *)p + at, n - at, &cp);
499 10 ▶ at += k ? k : 1;
Branch: 1 branch
BranchTrueFalseTotal
at += k ? k : 1; 10 0 10
500 10 count++;
501 }
502 2 return count;
503 }
504  
505 /* ---- the axes ---------------------------------------------------------- */
506  
507 /* the next node in document order that is not inside n */
508 static const frx_node *
509 1 frx_xp_after_subtree(const frx_node *n, const frx_node *root)
510 {
511 1 ▶ while (n != root && !n->next) n = n->parent;
Branch: 2 branches
BranchTrueFalseTotal
while (n != root && !n->next) n = n->parent; 1 0 1
while (n != root && !n->next) n = n->parent; 0 1 1
512 1 ▶ return n == root ? NULL : n->next;
Branch: 1 branch
BranchTrueFalseTotal
return n == root ? NULL : n->next; 1 0 1
513 }
514  
515 static int
516 4 frx_xp_is_ancestor(const frx_node *maybe, const frx_node *n)
517 {
518 10 ▶ for (n = n->parent; n; n = n->parent)
Branch: 1 branch
BranchTrueFalseTotal
for (n = n->parent; n; n = n->parent) 7 3 10
519 7 ▶ if (n == maybe) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n == maybe) return 1; 1 6 7
520 3 return 0;
521 }
522  
523 /* does the node test hold for this item on this step's axis? */
524 static int
525 2624 frx_xp_test(const frx_xn *step, const frx_xp_item *it)
526 {
527 2624 const frx_node *n = it->node;
528  
529 2624 switch (step->test) {
530 1308 case FRX_XTEST_NODE:
531 1308 return 1;
532 13 case FRX_XTEST_TEXT:
533 13 ▶ return it->kind == FRX_XP_KNODE && n->kind == FRX_TEXT;
Branch: 2 branches
BranchTrueFalseTotal
return it->kind == FRX_XP_KNODE && n->kind == FRX_TEXT; 13 0 13
return it->kind == FRX_XP_KNODE && n->kind == FRX_TEXT; 4 9 13
534 13 case FRX_XTEST_COMMENT:
535 13 ▶ return it->kind == FRX_XP_KNODE && n->kind == FRX_COMMENT;
Branch: 2 branches
BranchTrueFalseTotal
return it->kind == FRX_XP_KNODE && n->kind == FRX_COMMENT; 13 0 13
return it->kind == FRX_XP_KNODE && n->kind == FRX_COMMENT; 1 12 13
536 39 case FRX_XTEST_PI:
537 39 ▶ if (it->kind != FRX_XP_KNODE || n->kind != FRX_PI) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (it->kind != FRX_XP_KNODE || n->kind != FRX_PI) return 0; 39 0 39
if (it->kind != FRX_XP_KNODE || n->kind != FRX_PI) return 0; 36 3 39
538 3 ▶ return step->str.len == 0 || frx_str_eq2(&n->local, &step->str);
Branch: 2 branches
BranchTrueFalseTotal
return step->str.len == 0 || frx_str_eq2(&n->local, &step->str); 2 1 3
return step->str.len == 0 || frx_str_eq2(&n->local, &step->str); 1 1 2
539 1251 default:
540 1251 break;
541 }
542  
543 /* a name test matches the principal node type of the axis and no other */
544 1251 ▶ if (step->axis == FRX_AX_ATTRIBUTE) {
Branch: 1 branch
BranchTrueFalseTotal
if (step->axis == FRX_AX_ATTRIBUTE) { 35 1216 1251
545 const frx_attr *a;
546 35 ▶ if (it->kind != FRX_XP_KATTR) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (it->kind != FRX_XP_KATTR) return 0; 0 35 35
547 35 ▶ if (step->test == FRX_XTEST_STAR) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (step->test == FRX_XTEST_STAR) return 1; 8 27 35
548 27 a = &n->attrs[it->index];
549 27 ▶ if (step->test == FRX_XTEST_NSSTAR) return frx_str_eq2(&a->ns, &step->ns);
Branch: 1 branch
BranchTrueFalseTotal
if (step->test == FRX_XTEST_NSSTAR) return frx_str_eq2(&a->ns, &step->ns); 0 27 27
550 27 ▶ return frx_str_eq2(&a->local, &step->local) && frx_str_eq2(&a->ns, &step->ns);
Branch: 2 branches
BranchTrueFalseTotal
return frx_str_eq2(&a->local, &step->local) && frx_str_eq2(&a->ns, &step->ns); 21 6 27
return frx_str_eq2(&a->local, &step->local) && frx_str_eq2(&a->ns, &step->ns); 21 0 21
551 }
552 1216 ▶ if (step->axis == FRX_AX_NAMESPACE) {
Branch: 1 branch
BranchTrueFalseTotal
if (step->axis == FRX_AX_NAMESPACE) { 12 1204 1216
553 frx_str prefix, uri;
554 12 ▶ if (it->kind != FRX_XP_KNS) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (it->kind != FRX_XP_KNS) return 0; 0 12 12
555 12 ▶ if (step->test == FRX_XTEST_STAR) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (step->test == FRX_XTEST_STAR) return 1; 4 8 12
556 /* a namespace node's expanded name has a null URI and the prefix
557 * as its local part, so only an unprefixed name test can match */
558 8 ▶ if (step->test == FRX_XTEST_NSSTAR || step->ns.len) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (step->test == FRX_XTEST_NSSTAR || step->ns.len) return 0; 8 0 8
if (step->test == FRX_XTEST_NSSTAR || step->ns.len) return 0; 0 8 8
559 8 ▶ if (!frx_xp_ns_at(n, it->index, &prefix, &uri)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_ns_at(n, it->index, &prefix, &uri)) return 0; 0 8 8
560 8 return frx_str_eq2(&prefix, &step->local);
561 }
562 1204 ▶ if (it->kind != FRX_XP_KNODE || n->kind != FRX_ELEMENT) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (it->kind != FRX_XP_KNODE || n->kind != FRX_ELEMENT) return 0; 1204 0 1204
if (it->kind != FRX_XP_KNODE || n->kind != FRX_ELEMENT) return 0; 523 681 1204
563 681 ▶ if (step->test == FRX_XTEST_STAR) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (step->test == FRX_XTEST_STAR) return 1; 90 591 681
564 591 ▶ if (step->test == FRX_XTEST_NSSTAR) return frx_str_eq2(&n->ns, &step->ns);
Branch: 1 branch
BranchTrueFalseTotal
if (step->test == FRX_XTEST_NSSTAR) return frx_str_eq2(&n->ns, &step->ns); 7 584 591
565 584 ▶ return frx_str_eq2(&n->local, &step->local) && frx_str_eq2(&n->ns, &step->ns);
Branch: 2 branches
BranchTrueFalseTotal
return frx_str_eq2(&n->local, &step->local) && frx_str_eq2(&n->ns, &step->ns); 186 398 584
return frx_str_eq2(&n->local, &step->local) && frx_str_eq2(&n->ns, &step->ns); 185 1 186
566 }
567  
568 static int
569 2624 frx_xp_take(frx_xeval *e, const frx_xn *step, frx_xp_set *out,
570 const frx_node *n, int kind, int index)
571 {
572 frx_xp_item it;
573 2624 it.node = n;
574 2624 it.kind = kind;
575 2624 it.index = index;
576 2624 ▶ if (!frx_xp_test(step, &it)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_test(step, &it)) return 1; 996 1628 2624
577 1628 ▶ if (!frx_xp_set_push(out, &it)) return frx_xp_efail(e, "out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_set_push(out, &it)) return frx_xp_efail(e, "out of memory"); 0 1628 1628
578 1628 return 1;
579 }
580  
581 static void
582 3 frx_xp_reverse(frx_xp_set *s, int from)
583 {
584 3 int i = from, j = s->n - 1;
585 4 ▶ while (i < j) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < j) { 1 3 4
586 1 frx_xp_item t = s->v[i];
587 1 s->v[i++] = s->v[j];
588 1 s->v[j--] = t;
589 }
590 3 }
591  
592 /* every node on the axis, in the axis's own order (reverse document order
593 * for the four reverse axes), filtered by the node test */
594 static int
595 1485 frx_xp_axis(frx_xeval *e, const frx_xn *step, const frx_xp_item *ctx, frx_xp_set *out)
596 {
597 1485 const frx_node *self = ctx->node;
598 const frx_node *n;
599 1485 int base = out->n;
600 int i;
601  
602 1485 switch (step->axis) {
603 3 case FRX_AX_SELF:
604 3 return frx_xp_take(e, step, out, self, ctx->kind, ctx->index);
605  
606 4 case FRX_AX_PARENT:
607 4 ▶ if (ctx->kind != FRX_XP_KNODE)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->kind != FRX_XP_KNODE) 2 2 4
608 2 return frx_xp_take(e, step, out, self, FRX_XP_KNODE, 0);
609 2 ▶ if (self->parent) return frx_xp_take(e, step, out, self->parent, FRX_XP_KNODE, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (self->parent) return frx_xp_take(e, step, out, self->parent, FRX_XP_KNODE, 0); 2 0 2
610 0 return 1;
611  
612 1 case FRX_AX_ANCESTOR_OR_SELF:
613 1 ▶ if (!frx_xp_take(e, step, out, self, ctx->kind, ctx->index)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_take(e, step, out, self, ctx->kind, ctx->index)) return 0; 0 1 1
614 1 ▶ if (ctx->kind != FRX_XP_KNODE) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->kind != FRX_XP_KNODE) { 0 1 1
615 0 ▶ for (n = self; n; n = n->parent)
Branch: 1 branch
BranchTrueFalseTotal
for (n = self; n; n = n->parent) 0 0 0
616 0 ▶ if (!frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0; 0 0 0
617 0 return 1;
618 }
619 3 ▶ for (n = self->parent; n; n = n->parent)
Branch: 1 branch
BranchTrueFalseTotal
for (n = self->parent; n; n = n->parent) 2 1 3
620 2 ▶ if (!frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0; 0 2 2
621 1 return 1;
622  
623 5 case FRX_AX_ANCESTOR:
624 5 ▶ n = ctx->kind == FRX_XP_KNODE ? self->parent : self;
Branch: 1 branch
BranchTrueFalseTotal
n = ctx->kind == FRX_XP_KNODE ? self->parent : self; 3 2 5
625 20 ▶ for (; n; n = n->parent)
Branch: 1 branch
BranchTrueFalseTotal
for (; n; n = n->parent) 15 5 20
626 15 ▶ if (!frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0; 0 15 15
627 5 return 1;
628  
629 1285 case FRX_AX_CHILD:
630 1285 ▶ if (ctx->kind != FRX_XP_KNODE) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->kind != FRX_XP_KNODE) return 1; 0 1285 1285
631 2526 ▶ for (n = self->first_child; n; n = n->next)
Branch: 1 branch
BranchTrueFalseTotal
for (n = self->first_child; n; n = n->next) 1241 1285 2526
632 1241 ▶ if (!frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0; 0 1241 1241
633 1285 return 1;
634  
635 104 case FRX_AX_DESCENDANT_OR_SELF:
636 104 ▶ if (!frx_xp_take(e, step, out, self, ctx->kind, ctx->index)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_take(e, step, out, self, ctx->kind, ctx->index)) return 0; 0 104 104
637 104 ▶ if (ctx->kind != FRX_XP_KNODE) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->kind != FRX_XP_KNODE) return 1; 0 104 104
638 1295 ▶ for (n = self->first_child; n; n = frx_walk_next(n, self))
Branch: 1 branch
BranchTrueFalseTotal
for (n = self->first_child; n; n = frx_walk_next(n, self)) 1191 104 1295
639 1191 ▶ if (!frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0; 0 1191 1191
640 104 return 1;
641  
642 1 case FRX_AX_DESCENDANT:
643 1 ▶ if (ctx->kind != FRX_XP_KNODE) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->kind != FRX_XP_KNODE) return 1; 0 1 1
644 6 ▶ for (n = self->first_child; n; n = frx_walk_next(n, self))
Branch: 1 branch
BranchTrueFalseTotal
for (n = self->first_child; n; n = frx_walk_next(n, self)) 5 1 6
645 5 ▶ if (!frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0; 0 5 5
646 1 return 1;
647  
648 1 case FRX_AX_FOLLOWING_SIBLING:
649 1 ▶ if (ctx->kind != FRX_XP_KNODE) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->kind != FRX_XP_KNODE) return 1; 0 1 1
650 3 ▶ for (n = self->next; n; n = n->next)
Branch: 1 branch
BranchTrueFalseTotal
for (n = self->next; n; n = n->next) 2 1 3
651 2 ▶ if (!frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0; 0 2 2
652 1 return 1;
653  
654 2 case FRX_AX_PRECEDING_SIBLING:
655 2 ▶ if (ctx->kind != FRX_XP_KNODE || !self->parent) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (ctx->kind != FRX_XP_KNODE || !self->parent) return 1; 2 0 2
if (ctx->kind != FRX_XP_KNODE || !self->parent) return 1; 0 2 2
656 5 ▶ for (n = self->parent->first_child; n && n != self; n = n->next)
Branch: 2 branches
BranchTrueFalseTotal
for (n = self->parent->first_child; n && n != self; n = n->next) 5 0 5
for (n = self->parent->first_child; n && n != self; n = n->next) 3 2 5
657 3 ▶ if (!frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0; 0 3 3
658 2 frx_xp_reverse(out, base); /* nearest first */
659 2 return 1;
660  
661 1 case FRX_AX_FOLLOWING: {
662 1 const frx_node *root = frx_xp_root_of(self);
663 /* an attribute or namespace node stands just before its element's
664 * children, so those are following it; the element's own subtree
665 * is not following the element */
666 2 n = ctx->kind == FRX_XP_KNODE
667 1 ? frx_xp_after_subtree(self, root)
668 1 ▶ : (self->first_child ? self->first_child : frx_xp_after_subtree(self, root));
Branch: 2 branches
BranchTrueFalseTotal
: (self->first_child ? self->first_child : frx_xp_after_subtree(self, root)); 1 0 1
: (self->first_child ? self->first_child : frx_xp_after_subtree(self, root)); 0 0 0
669 4 ▶ for (; n; n = frx_walk_next(n, root))
Branch: 1 branch
BranchTrueFalseTotal
for (; n; n = frx_walk_next(n, root)) 3 1 4
670 3 ▶ if (!frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0; 0 3 3
671 1 return 1;
672 }
673  
674 1 case FRX_AX_PRECEDING: {
675 1 const frx_node *root = frx_xp_root_of(self);
676 5 ▶ for (n = root->first_child; n && n != self; n = frx_walk_next(n, root))
Branch: 2 branches
BranchTrueFalseTotal
for (n = root->first_child; n && n != self; n = frx_walk_next(n, root)) 5 0 5
for (n = root->first_child; n && n != self; n = frx_walk_next(n, root)) 4 1 5
677 4 ▶ if (!frx_xp_is_ancestor(n, self)
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_is_ancestor(n, self) 3 1 4
678 3 ▶ && !frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
&& !frx_xp_take(e, step, out, n, FRX_XP_KNODE, 0)) return 0; 0 3 3
679 1 frx_xp_reverse(out, base); /* nearest first */
680 1 return 1;
681 }
682  
683 71 case FRX_AX_ATTRIBUTE:
684 71 ▶ if (ctx->kind != FRX_XP_KNODE || self->kind != FRX_ELEMENT) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (ctx->kind != FRX_XP_KNODE || self->kind != FRX_ELEMENT) return 1; 71 0 71
if (ctx->kind != FRX_XP_KNODE || self->kind != FRX_ELEMENT) return 1; 22 49 71
685 84 ▶ for (i = 0; i < self->n_attrs; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < self->n_attrs; i++) 35 49 84
686 35 ▶ if (!frx_xp_take(e, step, out, self, FRX_XP_KATTR, i)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_take(e, step, out, self, FRX_XP_KATTR, i)) return 0; 0 35 35
687 49 return 1;
688  
689 6 case FRX_AX_NAMESPACE: {
690 frx_xp_nsnode *v;
691 int n_ns;
692 6 ▶ if (ctx->kind != FRX_XP_KNODE || self->kind != FRX_ELEMENT) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (ctx->kind != FRX_XP_KNODE || self->kind != FRX_ELEMENT) return 1; 6 0 6
if (ctx->kind != FRX_XP_KNODE || self->kind != FRX_ELEMENT) return 1; 0 6 6
693 6 ▶ if (!frx_xp_ns_list(self, &v, &n_ns)) return frx_xp_efail(e, "out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_ns_list(self, &v, &n_ns)) return frx_xp_efail(e, "out of memory"); 0 6 6
694 6 free(v);
695 18 ▶ for (i = 0; i < n_ns; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_ns; i++) 12 6 18
696 12 ▶ if (!frx_xp_take(e, step, out, self, FRX_XP_KNS, i)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_take(e, step, out, self, FRX_XP_KNS, i)) return 0; 0 12 12
697 6 return 1;
698 }
699  
700 0 default:
701 0 return frx_xp_efail(e, "unknown axis");
702 }
703 }
704  
705 /* ---- steps and paths --------------------------------------------------- */
706  
707 static int
708 234 frx_xp_filter_set(frx_xeval *e, const frx_xn *pred, frx_xp_set *s)
709 {
710 234 int i, w = 0, n = s->n;
711 327 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 93 234 327
712 frx_xctx c;
713 frx_xp_val v;
714 int keep;
715 93 c.item = s->v[i];
716 93 c.position = i + 1;
717 93 c.size = n;
718 93 frx_xp_val_init(&v);
719 93 ▶ if (!frx_xp_eval(e, pred, &c, &v)) { frx_xp_val_free(&v); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_eval(e, pred, &c, &v)) { frx_xp_val_free(&v); return 0; } 0 93 93
720 /* a predicate that is a number selects by proximity position */
721 93 ▶ keep = v.kind == FRX_XV_NUMBER ? v.num == (double)(i + 1) : frx_xp_to_bool(e, &v);
Branch: 1 branch
BranchTrueFalseTotal
keep = v.kind == FRX_XV_NUMBER ? v.num == (double)(i + 1) : frx_xp_to_bool(e, &v); 35 58 93
722 93 frx_xp_val_free(&v);
723 93 ▶ if (keep) s->v[w++] = s->v[i];
Branch: 1 branch
BranchTrueFalseTotal
if (keep) s->v[w++] = s->v[i]; 46 47 93
724 }
725 234 s->n = w;
726 234 return 1;
727 }
728  
729 static int
730 289 frx_xp_step_over(frx_xeval *e, const frx_xn *step, const frx_xp_set *in, frx_xp_set *out)
731 {
732 frx_xp_set axis;
733 289 int i, k, ok = 1;
734  
735 289 memset(&axis, 0, sizeof axis);
736 1774 ▶ for (i = 0; i < in->n && ok; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < in->n && ok; i++) { 1485 289 1774
for (i = 0; i < in->n && ok; i++) { 1485 0 1485
737 1485 axis.n = 0;
738 1485 ok = frx_xp_axis(e, step, &in->v[i], &axis);
739 1717 ▶ for (k = 0; ok && k < step->n_preds; k++)
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; ok && k < step->n_preds; k++) 1717 0 1717
for (k = 0; ok && k < step->n_preds; k++) 232 1485 1717
740 232 ok = frx_xp_filter_set(e, step->preds[k], &axis);
741 3070 ▶ for (k = 0; ok && k < axis.n; k++)
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; ok && k < axis.n; k++) 3070 0 3070
for (k = 0; ok && k < axis.n; k++) 1585 1485 3070
742 1585 ▶ if (!frx_xp_set_push(out, &axis.v[k])) ok = frx_xp_efail(e, "out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_set_push(out, &axis.v[k])) ok = frx_xp_efail(e, "out of memory"); 0 1585 1585
743 }
744 289 frx_xp_set_free(&axis);
745 289 ▶ if (ok) frx_xp_set_sort(out);
Branch: 1 branch
BranchTrueFalseTotal
if (ok) frx_xp_set_sort(out); 289 0 289
746 289 return ok;
747 }
748  
749 static int
750 143 frx_xp_eval_path(frx_xeval *e, const frx_xn *path, const frx_xctx *c, frx_xp_val *out)
751 {
752 frx_xp_set cur, next;
753 143 int i, ok = 1;
754  
755 143 memset(&cur, 0, sizeof cur);
756 143 memset(&next, 0, sizeof next);
757  
758 143 ▶ if (path->a) {
Branch: 1 branch
BranchTrueFalseTotal
if (path->a) { 0 143 143
759 frx_xp_val v;
760 0 frx_xp_val_init(&v);
761 0 ▶ if (!frx_xp_eval(e, path->a, c, &v)) { frx_xp_val_free(&v); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_eval(e, path->a, c, &v)) { frx_xp_val_free(&v); return 0; } 0 0 0
762 0 ▶ if (v.kind != FRX_XV_NODESET) {
Branch: 1 branch
BranchTrueFalseTotal
if (v.kind != FRX_XV_NODESET) { 0 0 0
763 0 frx_xp_val_free(&v);
764 0 return frx_xp_efail(e, "the left side of a path step is not a node-set");
765 }
766 0 cur = v.set;
767 0 frx_buf_free(&v.str);
768 143 ▶ } else if (path->absolute) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (path->absolute) { 124 19 143
769 124 ok = frx_xp_set_push3(&cur, frx_xp_root_of(c->item.node), FRX_XP_KNODE, 0);
770 124 ▶ if (!ok) (void)frx_xp_efail(e, "out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) (void)frx_xp_efail(e, "out of memory"); 0 124 124
771 } else {
772 19 ok = frx_xp_set_push(&cur, &c->item);
773 19 ▶ if (!ok) (void)frx_xp_efail(e, "out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) (void)frx_xp_efail(e, "out of memory"); 0 19 19
774 }
775  
776 432 ▶ for (i = 0; ok && i < path->n_steps; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; ok && i < path->n_steps; i++) { 432 0 432
for (i = 0; ok && i < path->n_steps; i++) { 289 143 432
777 frx_xp_set tmp;
778 289 next.n = 0;
779 289 ok = frx_xp_step_over(e, path->steps[i], &cur, &next);
780 289 tmp = cur; cur = next; next = tmp;
781 }
782 143 frx_xp_set_free(&next);
783 143 ▶ if (!ok) { frx_xp_set_free(&cur); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { frx_xp_set_free(&cur); return 0; } 0 143 143
784 143 out->kind = FRX_XV_NODESET;
785 143 out->set = cur;
786 143 return 1;
787 }
788  
789 /* ---- comparison, section 3.4 ------------------------------------------- */
790  
791 static int
792 27 frx_xp_cmp_num(int op, double a, double b)
793 {
794 27 switch (op) {
795 16 case FRX_XN_EQ: return a == b;
796 0 case FRX_XN_NE: return a != b;
797 5 case FRX_XN_LT: return a < b;
798 0 case FRX_XN_LE: return a <= b;
799 6 case FRX_XN_GT: return a > b;
800 0 default: return a >= b;
801 }
802 }
803  
804 static int
805 19 frx_xp_set_str(frx_xeval *e, const frx_xp_set *s, int i, frx_buf *b)
806 {
807 19 frx_xp_buf_reset(b);
808 19 return frx_xp_item_string(e, &s->v[i], b);
809 }
810  
811 static int
812 36 frx_xp_compare(frx_xeval *e, int op, const frx_xp_val *l, const frx_xp_val *r, int *res)
813 {
814 36 ▶ int rel = op != FRX_XN_EQ && op != FRX_XN_NE;
Branch: 2 branches
BranchTrueFalseTotal
int rel = op != FRX_XN_EQ && op != FRX_XN_NE; 9 27 36
int rel = op != FRX_XN_EQ && op != FRX_XN_NE; 8 1 9
815 36 int i, j, ok = 1;
816 frx_buf a, b;
817  
818 36 *res = 0;
819 36 frx_buf_init(&a);
820 36 frx_buf_init(&b);
821  
822 36 ▶ if (l->kind == FRX_XV_NODESET && r->kind == FRX_XV_NODESET) {
Branch: 2 branches
BranchTrueFalseTotal
if (l->kind == FRX_XV_NODESET && r->kind == FRX_XV_NODESET) { 9 27 36
if (l->kind == FRX_XV_NODESET && r->kind == FRX_XV_NODESET) { 1 8 9
823 4 ▶ for (i = 0; ok && !*res && i < l->set.n; i++) {
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; ok && !*res && i < l->set.n; i++) { 4 0 4
for (i = 0; ok && !*res && i < l->set.n; i++) { 4 0 4
for (i = 0; ok && !*res && i < l->set.n; i++) { 3 1 4
824 3 ▶ if (!(ok = frx_xp_set_str(e, &l->set, i, &a))) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!(ok = frx_xp_set_str(e, &l->set, i, &a))) break; 0 3 3
825 6 ▶ for (j = 0; ok && !*res && j < r->set.n; j++) {
Branch: 3 branches
BranchTrueFalseTotal
for (j = 0; ok && !*res && j < r->set.n; j++) { 6 0 6
for (j = 0; ok && !*res && j < r->set.n; j++) { 6 0 6
for (j = 0; ok && !*res && j < r->set.n; j++) { 3 3 6
826 3 ▶ if (!(ok = frx_xp_set_str(e, &r->set, j, &b))) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!(ok = frx_xp_set_str(e, &r->set, j, &b))) break; 0 3 3
827 3 ▶ if (rel) {
Branch: 1 branch
BranchTrueFalseTotal
if (rel) { 0 3 3
828 0 ▶ *res = frx_xp_cmp_num(op, frx_xp_str_num(a.p ? a.p : "", a.len),
Branch: 1 branch
BranchTrueFalseTotal
*res = frx_xp_cmp_num(op, frx_xp_str_num(a.p ? a.p : "", a.len), 0 0 0
829 0 ▶ frx_xp_str_num(b.p ? b.p : "", b.len));
Branch: 1 branch
BranchTrueFalseTotal
frx_xp_str_num(b.p ? b.p : "", b.len)); 0 0 0
830 } else {
831 3 ▶ int eq = a.len == b.len && (a.len == 0 || memcmp(a.p, b.p, a.len) == 0);
Branch: 3 branches
BranchTrueFalseTotal
int eq = a.len == b.len && (a.len == 0 || memcmp(a.p, b.p, a.len) == 0); 0 3 3
int eq = a.len == b.len && (a.len == 0 || memcmp(a.p, b.p, a.len) == 0); 0 0 0
int eq = a.len == b.len && (a.len == 0 || memcmp(a.p, b.p, a.len) == 0); 0 0 0
832 3 ▶ *res = op == FRX_XN_EQ ? eq : !eq;
Branch: 1 branch
BranchTrueFalseTotal
*res = op == FRX_XN_EQ ? eq : !eq; 0 3 3
833 }
834 }
835 }
836 1 goto done;
837 }
838  
839 35 ▶ if (l->kind == FRX_XV_NODESET || r->kind == FRX_XV_NODESET) {
Branch: 2 branches
BranchTrueFalseTotal
if (l->kind == FRX_XV_NODESET || r->kind == FRX_XV_NODESET) { 27 8 35
if (l->kind == FRX_XV_NODESET || r->kind == FRX_XV_NODESET) { 0 27 27
840 8 ▶ const frx_xp_val *ns = l->kind == FRX_XV_NODESET ? l : r;
Branch: 1 branch
BranchTrueFalseTotal
const frx_xp_val *ns = l->kind == FRX_XV_NODESET ? l : r; 8 0 8
841 8 ▶ const frx_xp_val *other = l->kind == FRX_XV_NODESET ? r : l;
Branch: 1 branch
BranchTrueFalseTotal
const frx_xp_val *other = l->kind == FRX_XV_NODESET ? r : l; 8 0 8
842 8 int flip = l->kind != FRX_XV_NODESET; /* the node-set is on the right */
843 0 ▶ int effective = flip ? (op == FRX_XN_LT ? FRX_XN_GT : op == FRX_XN_GT ? FRX_XN_LT
Branch: 1 branch
BranchTrueFalseTotal
int effective = flip ? (op == FRX_XN_LT ? FRX_XN_GT : op == FRX_XN_GT ? FRX_XN_LT 0 0 0
844 0 ▶ : op == FRX_XN_LE ? FRX_XN_GE : op == FRX_XN_GE ? FRX_XN_LE : op)
Branch: 3 branches
BranchTrueFalseTotal
: op == FRX_XN_LE ? FRX_XN_GE : op == FRX_XN_GE ? FRX_XN_LE : op) 0 0 0
: op == FRX_XN_LE ? FRX_XN_GE : op == FRX_XN_GE ? FRX_XN_LE : op) 0 0 0
: op == FRX_XN_LE ? FRX_XN_GE : op == FRX_XN_GE ? FRX_XN_LE : op) 0 0 0
845 8 ▶ : op;
Branch: 1 branch
BranchTrueFalseTotal
: op; 0 8 8
846  
847 8 ▶ if (!rel && other->kind == FRX_XV_BOOLEAN) {
Branch: 2 branches
BranchTrueFalseTotal
if (!rel && other->kind == FRX_XV_BOOLEAN) { 6 2 8
if (!rel && other->kind == FRX_XV_BOOLEAN) { 2 4 6
848 2 int nb = ns->set.n > 0;
849 2 ▶ *res = op == FRX_XN_EQ ? nb == (other->bl != 0) : nb != (other->bl != 0);
Branch: 1 branch
BranchTrueFalseTotal
*res = op == FRX_XN_EQ ? nb == (other->bl != 0) : nb != (other->bl != 0); 2 0 2
850 2 goto done;
851 }
852 6 ▶ if (rel || other->kind == FRX_XV_NUMBER) {
Branch: 2 branches
BranchTrueFalseTotal
if (rel || other->kind == FRX_XV_NUMBER) { 4 2 6
if (rel || other->kind == FRX_XV_NUMBER) { 1 3 4
853 double x;
854 3 ▶ if (!(ok = frx_xp_to_num(e, other, &x))) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (!(ok = frx_xp_to_num(e, other, &x))) goto done; 0 3 3
855 9 ▶ for (i = 0; ok && !*res && i < ns->set.n; i++) {
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; ok && !*res && i < ns->set.n; i++) { 9 0 9
for (i = 0; ok && !*res && i < ns->set.n; i++) { 7 2 9
for (i = 0; ok && !*res && i < ns->set.n; i++) { 6 1 7
856 6 ▶ if (!(ok = frx_xp_set_str(e, &ns->set, i, &a))) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!(ok = frx_xp_set_str(e, &ns->set, i, &a))) break; 0 6 6
857 6 ▶ *res = frx_xp_cmp_num(effective, frx_xp_str_num(a.p ? a.p : "", a.len), x);
Branch: 1 branch
BranchTrueFalseTotal
*res = frx_xp_cmp_num(effective, frx_xp_str_num(a.p ? a.p : "", a.len), x); 6 0 6
858 }
859 3 goto done;
860 }
861 3 ▶ if (!(ok = frx_xp_to_str(e, other, &b))) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (!(ok = frx_xp_to_str(e, other, &b))) goto done; 0 3 3
862 7 ▶ for (i = 0; ok && !*res && i < ns->set.n; i++) {
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; ok && !*res && i < ns->set.n; i++) { 7 0 7
for (i = 0; ok && !*res && i < ns->set.n; i++) { 4 3 7
for (i = 0; ok && !*res && i < ns->set.n; i++) { 4 0 4
863 int eq;
864 4 ▶ if (!(ok = frx_xp_set_str(e, &ns->set, i, &a))) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!(ok = frx_xp_set_str(e, &ns->set, i, &a))) break; 0 4 4
865 4 ▶ eq = a.len == b.len && (a.len == 0 || memcmp(a.p, b.p, a.len) == 0);
Branch: 3 branches
BranchTrueFalseTotal
eq = a.len == b.len && (a.len == 0 || memcmp(a.p, b.p, a.len) == 0); 4 0 4
eq = a.len == b.len && (a.len == 0 || memcmp(a.p, b.p, a.len) == 0); 4 0 4
eq = a.len == b.len && (a.len == 0 || memcmp(a.p, b.p, a.len) == 0); 3 1 4
866 4 ▶ *res = op == FRX_XN_EQ ? eq : !eq;
Branch: 1 branch
BranchTrueFalseTotal
*res = op == FRX_XN_EQ ? eq : !eq; 2 2 4
867 }
868 3 goto done;
869 }
870  
871 27 ▶ if (rel) {
Branch: 1 branch
BranchTrueFalseTotal
if (rel) { 6 21 27
872 double x, y;
873 6 ▶ ok = frx_xp_to_num(e, l, &x) && frx_xp_to_num(e, r, &y);
Branch: 2 branches
BranchTrueFalseTotal
ok = frx_xp_to_num(e, l, &x) && frx_xp_to_num(e, r, &y); 6 0 6
ok = frx_xp_to_num(e, l, &x) && frx_xp_to_num(e, r, &y); 6 0 6
874 6 ▶ if (ok) *res = frx_xp_cmp_num(op, x, y);
Branch: 1 branch
BranchTrueFalseTotal
if (ok) *res = frx_xp_cmp_num(op, x, y); 6 0 6
875 6 goto done;
876 }
877 21 ▶ if (l->kind == FRX_XV_BOOLEAN || r->kind == FRX_XV_BOOLEAN) {
Branch: 2 branches
BranchTrueFalseTotal
if (l->kind == FRX_XV_BOOLEAN || r->kind == FRX_XV_BOOLEAN) { 21 0 21
if (l->kind == FRX_XV_BOOLEAN || r->kind == FRX_XV_BOOLEAN) { 4 17 21
878 4 int x = frx_xp_to_bool(e, l), y = frx_xp_to_bool(e, r);
879 4 ▶ *res = op == FRX_XN_EQ ? x == y : x != y;
Branch: 1 branch
BranchTrueFalseTotal
*res = op == FRX_XN_EQ ? x == y : x != y; 4 0 4
880 4 goto done;
881 }
882 17 ▶ if (l->kind == FRX_XV_NUMBER || r->kind == FRX_XV_NUMBER) {
Branch: 2 branches
BranchTrueFalseTotal
if (l->kind == FRX_XV_NUMBER || r->kind == FRX_XV_NUMBER) { 2 15 17
if (l->kind == FRX_XV_NUMBER || r->kind == FRX_XV_NUMBER) { 0 2 2
883 double x, y;
884 15 ▶ ok = frx_xp_to_num(e, l, &x) && frx_xp_to_num(e, r, &y);
Branch: 2 branches
BranchTrueFalseTotal
ok = frx_xp_to_num(e, l, &x) && frx_xp_to_num(e, r, &y); 15 0 15
ok = frx_xp_to_num(e, l, &x) && frx_xp_to_num(e, r, &y); 15 0 15
885 15 ▶ if (ok) *res = frx_xp_cmp_num(op, x, y);
Branch: 1 branch
BranchTrueFalseTotal
if (ok) *res = frx_xp_cmp_num(op, x, y); 15 0 15
886 15 goto done;
887 }
888 {
889 int eq;
890 2 ▶ ok = frx_xp_to_str(e, l, &a) && frx_xp_to_str(e, r, &b);
Branch: 2 branches
BranchTrueFalseTotal
ok = frx_xp_to_str(e, l, &a) && frx_xp_to_str(e, r, &b); 2 0 2
ok = frx_xp_to_str(e, l, &a) && frx_xp_to_str(e, r, &b); 2 0 2
891 2 ▶ eq = a.len == b.len && (a.len == 0 || memcmp(a.p, b.p, a.len) == 0);
Branch: 3 branches
BranchTrueFalseTotal
eq = a.len == b.len && (a.len == 0 || memcmp(a.p, b.p, a.len) == 0); 2 0 2
eq = a.len == b.len && (a.len == 0 || memcmp(a.p, b.p, a.len) == 0); 2 0 2
eq = a.len == b.len && (a.len == 0 || memcmp(a.p, b.p, a.len) == 0); 1 1 2
892 2 ▶ if (ok) *res = op == FRX_XN_EQ ? eq : !eq;
Branch: 2 branches
BranchTrueFalseTotal
if (ok) *res = op == FRX_XN_EQ ? eq : !eq; 0 2 2
if (ok) *res = op == FRX_XN_EQ ? eq : !eq; 0 2 2
893 }
894  
895 0 done:
896 36 frx_buf_free(&a);
897 36 frx_buf_free(&b);
898 36 return ok;
899 }
900  
901 /* ---- the core function library, section 4 ------------------------------ */
902  
903 /* The node-set an optional argument names, or a set of just the context
904 * node. `one` is the caller's storage for that single entry, so the
905 * borrowed set is never pushed to and never freed. */
906 static int
907 10 frx_xp_arg_set(frx_xeval *e, const frx_xn *n, const frx_xctx *c, frx_xp_val *tmp,
908 const frx_xp_set **out, frx_xp_set *self, frx_xp_item *one)
909 {
910 10 ▶ if (n->n_args == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->n_args == 0) { 0 10 10
911 0 *one = c->item;
912 0 self->v = one;
913 0 self->n = 1;
914 0 self->cap = 0;
915 0 *out = self;
916 0 return 1;
917 }
918 10 ▶ if (!frx_xp_eval(e, n->args[0], c, tmp)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_eval(e, n->args[0], c, tmp)) return 0; 0 10 10
919 10 ▶ if (tmp->kind != FRX_XV_NODESET)
Branch: 1 branch
BranchTrueFalseTotal
if (tmp->kind != FRX_XV_NODESET) 0 10 10
920 0 return frx_xp_efail(e, "the argument must be a node-set");
921 10 *out = &tmp->set;
922 10 return 1;
923 }
924  
925 /* the string an optional argument names, or the context node's */
926 static int
927 239 frx_xp_arg_str(frx_xeval *e, const frx_xn *n, const frx_xctx *c, int i, frx_buf *out)
928 {
929 frx_xp_val v;
930 int ok;
931 239 ▶ if (i >= n->n_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (i >= n->n_args) { 0 239 239
932 0 frx_xp_buf_reset(out);
933 0 return frx_xp_item_string(e, &c->item, out);
934 }
935 239 frx_xp_val_init(&v);
936 239 ▶ ok = frx_xp_eval(e, n->args[i], c, &v) && frx_xp_to_str(e, &v, out);
Branch: 2 branches
BranchTrueFalseTotal
ok = frx_xp_eval(e, n->args[i], c, &v) && frx_xp_to_str(e, &v, out); 239 0 239
ok = frx_xp_eval(e, n->args[i], c, &v) && frx_xp_to_str(e, &v, out); 239 0 239
937 239 frx_xp_val_free(&v);
938 239 return ok;
939 }
940  
941 static int
942 38 frx_xp_arg_num(frx_xeval *e, const frx_xn *n, const frx_xctx *c, int i, double *out)
943 {
944 frx_xp_val v;
945 int ok;
946 38 ▶ if (i >= n->n_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (i >= n->n_args) { 0 38 38
947 frx_buf b;
948 0 frx_buf_init(&b);
949 0 ok = frx_xp_item_string(e, &c->item, &b);
950 0 ▶ *out = ok ? frx_xp_str_num(b.p ? b.p : "", b.len) : frx_xp_nan();
Branch: 2 branches
BranchTrueFalseTotal
*out = ok ? frx_xp_str_num(b.p ? b.p : "", b.len) : frx_xp_nan(); 0 0 0
*out = ok ? frx_xp_str_num(b.p ? b.p : "", b.len) : frx_xp_nan(); 0 0 0
951 0 frx_buf_free(&b);
952 0 return ok;
953 }
954 38 frx_xp_val_init(&v);
955 38 ▶ ok = frx_xp_eval(e, n->args[i], c, &v) && frx_xp_to_num(e, &v, out);
Branch: 2 branches
BranchTrueFalseTotal
ok = frx_xp_eval(e, n->args[i], c, &v) && frx_xp_to_num(e, &v, out); 38 0 38
ok = frx_xp_eval(e, n->args[i], c, &v) && frx_xp_to_num(e, &v, out); 38 0 38
956 38 frx_xp_val_free(&v);
957 38 return ok;
958 }
959  
960 static void
961 1 frx_xp_normalize_space(frx_buf *out, const char *p, size_t n)
962 {
963 1 size_t i = 0, run;
964 1 int wrote = 0;
965 5 ▶ while (i < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < n) { 4 1 5
966 13 ▶ while (i < n && frx_is_s((unsigned char)p[i])) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < n && frx_is_s((unsigned char)p[i])) i++; 12 1 13
while (i < n && frx_is_s((unsigned char)p[i])) i++; 9 3 12
967 4 run = i;
968 7 ▶ while (i < n && !frx_is_s((unsigned char)p[i])) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < n && !frx_is_s((unsigned char)p[i])) i++; 6 1 7
while (i < n && !frx_is_s((unsigned char)p[i])) i++; 3 3 6
969 4 ▶ if (i > run) {
Branch: 1 branch
BranchTrueFalseTotal
if (i > run) { 3 1 4
970 3 ▶ if (wrote) frx_buf_append_ch(out, ' ');
Branch: 1 branch
BranchTrueFalseTotal
if (wrote) frx_buf_append_ch(out, ' '); 2 1 3
971 3 frx_buf_append_n(out, p + run, i - run);
972 3 wrote = 1;
973 }
974 }
975 1 }
976  
977 /* translate(), by character: a character of `from` maps to the character
978 * of `to` at the same position, and to nothing when `to` is shorter */
979 static void
980 4 frx_xp_translate(frx_buf *out, const char *s, size_t sn,
981 const char *f, size_t fn, const char *t, size_t tn)
982 {
983 4 size_t at = 0;
984 unsigned long cp;
985 22 ▶ while (at < sn) {
Branch: 1 branch
BranchTrueFalseTotal
while (at < sn) { 18 4 22
986 18 size_t k = frx_utf8_decode((const unsigned char *)s + at, sn - at, &cp);
987 18 size_t fa = 0, ti = 0, hit = 0;
988 18 ▶ if (!k) k = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!k) k = 1; 0 18 18
989 47 ▶ while (fa < fn) {
Branch: 1 branch
BranchTrueFalseTotal
while (fa < fn) { 42 5 47
990 unsigned long fc;
991 42 size_t fk = frx_utf8_decode((const unsigned char *)f + fa, fn - fa, &fc);
992 42 ▶ if (!fk) fk = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!fk) fk = 1; 0 42 42
993 42 ▶ if (fk == k && memcmp(f + fa, s + at, k) == 0) { hit = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (fk == k && memcmp(f + fa, s + at, k) == 0) { hit = 1; break; } 41 1 42
if (fk == k && memcmp(f + fa, s + at, k) == 0) { hit = 1; break; } 13 28 41
994 29 fa += fk;
995 29 ti++;
996 }
997 18 ▶ if (!hit) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hit) { 5 13 18
998 5 frx_buf_append_n(out, s + at, k);
999 } else {
1000 13 size_t ta = 0, seen = 0;
1001 28 ▶ while (ta < tn && seen < ti) {
Branch: 2 branches
BranchTrueFalseTotal
while (ta < tn && seen < ti) { 22 6 28
while (ta < tn && seen < ti) { 15 7 22
1002 unsigned long tc;
1003 15 size_t tk = frx_utf8_decode((const unsigned char *)t + ta, tn - ta, &tc);
1004 15 ▶ if (!tk) tk = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!tk) tk = 1; 0 15 15
1005 15 ta += tk;
1006 15 seen++;
1007 }
1008 13 ▶ if (ta < tn) {
Branch: 1 branch
BranchTrueFalseTotal
if (ta < tn) { 7 6 13
1009 unsigned long tc;
1010 7 size_t tk = frx_utf8_decode((const unsigned char *)t + ta, tn - ta, &tc);
1011 7 ▶ if (!tk) tk = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!tk) tk = 1; 0 7 7
1012 7 frx_buf_append_n(out, t + ta, tk);
1013 }
1014 }
1015 18 at += k;
1016 }
1017 4 }
1018  
1019 /* the characters of s whose 1-based position p satisfies lo <= p < hi; a
1020 * NaN bound excludes everything, which is what section 4.2's examples
1021 * with `0 div 0` require */
1022 static void
1023 9 frx_xp_substring(frx_buf *out, const char *s, size_t n, double lo, double hi)
1024 {
1025 9 size_t at = 0;
1026 9 double pos = 1.0;
1027 unsigned long cp;
1028 52 ▶ while (at < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (at < n) { 43 9 52
1029 43 size_t k = frx_utf8_decode((const unsigned char *)s + at, n - at, &cp);
1030 43 ▶ if (!k) k = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!k) k = 1; 0 43 43
1031 43 ▶ if (pos >= lo && pos < hi) frx_buf_append_n(out, s + at, k);
Branch: 2 branches
BranchTrueFalseTotal
if (pos >= lo && pos < hi) frx_buf_append_n(out, s + at, k); 34 9 43
if (pos >= lo && pos < hi) frx_buf_append_n(out, s + at, k); 18 16 34
1032 43 at += k;
1033 43 pos += 1.0;
1034 }
1035 9 }
1036  
1037 /* the element carrying the nearest xml:lang above (and at) the item */
1038 static const frx_str *
1039 35 frx_xp_lang_of(const frx_xp_item *it)
1040 {
1041 35 const frx_node *n = it->node;
1042 75 ▶ for (; n; n = n->parent) {
Branch: 1 branch
BranchTrueFalseTotal
for (; n; n = n->parent) { 75 0 75
1043 75 ▶ if (n->kind != FRX_ELEMENT) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (n->kind != FRX_ELEMENT) continue; 0 75 75
1044 {
1045 75 const frx_str *v = frx_attr_value(n, FRX_XML_NS, "lang");
1046 75 ▶ if (v) return v;
Branch: 1 branch
BranchTrueFalseTotal
if (v) return v; 35 40 75
1047 }
1048 }
1049 0 return NULL;
1050 }
1051  
1052 static int
1053 140 frx_xp_ascii_lower(int c)
1054 {
1055 140 ▶ return c >= 'A' && c <= 'Z' ? c + 32 : c;
Branch: 2 branches
BranchTrueFalseTotal
return c >= 'A' && c <= 'Z' ? c + 32 : c; 126 14 140
return c >= 'A' && c <= 'Z' ? c + 32 : c; 28 98 126
1056 }
1057  
1058 static int
1059 35 frx_xp_lang_match(const frx_str *have, const char *want, size_t wn)
1060 {
1061 size_t i;
1062 35 ▶ if (have->len < wn) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (have->len < wn) return 0; 7 28 35
1063 91 ▶ for (i = 0; i < wn; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < wn; i++) 70 21 91
1064 70 ▶ if (frx_xp_ascii_lower((unsigned char)have->p[i]) != frx_xp_ascii_lower((unsigned char)want[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (frx_xp_ascii_lower((unsigned char)have->p[i]) != frx_xp_ascii_lower((unsigned char)want[i])) 7 63 70
1065 7 return 0;
1066 21 ▶ return have->len == wn || have->p[wn] == '-';
Branch: 2 branches
BranchTrueFalseTotal
return have->len == wn || have->p[wn] == '-'; 14 7 21
return have->len == wn || have->p[wn] == '-'; 14 0 14
1067 }
1068  
1069 /* id(): every whitespace-separated token looked up in the document's ID
1070 * index, over every attribute name the index was built from */
1071 static int
1072 4 frx_xp_id_lookup(frx_xeval *e, const char *p, size_t n, frx_xp_set *out)
1073 {
1074 4 size_t i = 0;
1075 9 ▶ while (i < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < n) { 5 4 9
1076 size_t start;
1077 6 ▶ while (i < n && frx_is_s((unsigned char)p[i])) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < n && frx_is_s((unsigned char)p[i])) i++; 6 0 6
while (i < n && frx_is_s((unsigned char)p[i])) i++; 1 5 6
1078 5 start = i;
1079 17 ▶ while (i < n && !frx_is_s((unsigned char)p[i])) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < n && !frx_is_s((unsigned char)p[i])) i++; 13 4 17
while (i < n && !frx_is_s((unsigned char)p[i])) i++; 12 1 13
1080 5 ▶ if (i > start) {
Branch: 1 branch
BranchTrueFalseTotal
if (i > start) { 5 0 5
1081 5 const frx_node *best = NULL;
1082 int k;
1083 10 ▶ for (k = 0; k < e->doc->n_id_attrs; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < e->doc->n_id_attrs; k++) { 5 5 10
1084 5 const frx_node *hit = frx_by_id(e->doc, e->doc->id_attrs[k].p,
1085 p + start, i - start);
1086 5 ▶ if (hit && (!best || hit->order < best->order)) best = hit;
Branch: 3 branches
BranchTrueFalseTotal
if (hit && (!best || hit->order < best->order)) best = hit; 4 1 5
if (hit && (!best || hit->order < best->order)) best = hit; 0 4 4
if (hit && (!best || hit->order < best->order)) best = hit; 0 0 0
1087 }
1088 5 ▶ if (best && !frx_xp_set_push3(out, best, FRX_XP_KNODE, 0))
Branch: 2 branches
BranchTrueFalseTotal
if (best && !frx_xp_set_push3(out, best, FRX_XP_KNODE, 0)) 4 1 5
if (best && !frx_xp_set_push3(out, best, FRX_XP_KNODE, 0)) 0 4 4
1089 0 return frx_xp_efail(e, "out of memory");
1090 }
1091 }
1092 4 return 1;
1093 }
1094  
1095 static int
1096 317 frx_xp_func(frx_xeval *e, const frx_xn *n, const frx_xctx *c, frx_xp_val *out)
1097 {
1098 frx_xp_val a0;
1099 frx_buf s0, s1, s2;
1100 317 int ok = 1;
1101  
1102 317 switch (n->func) {
1103 5 case FRX_FN_LAST:
1104 5 out->kind = FRX_XV_NUMBER;
1105 5 out->num = (double)c->size;
1106 5 return 1;
1107 7 case FRX_FN_POSITION:
1108 7 out->kind = FRX_XV_NUMBER;
1109 7 out->num = (double)c->position;
1110 7 return 1;
1111 10 case FRX_FN_TRUE:
1112 case FRX_FN_FALSE:
1113 10 out->kind = FRX_XV_BOOLEAN;
1114 10 out->bl = n->func == FRX_FN_TRUE;
1115 10 return 1;
1116 295 default:
1117 295 break;
1118 }
1119  
1120 295 switch (n->func) {
1121 30 case FRX_FN_COUNT:
1122 case FRX_FN_SUM: {
1123 const frx_xp_set *set;
1124 30 frx_xp_val_init(&a0);
1125 30 ▶ if (!frx_xp_eval(e, n->args[0], c, &a0)) { frx_xp_val_free(&a0); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_eval(e, n->args[0], c, &a0)) { frx_xp_val_free(&a0); return 0; } 0 30 30
1126 30 ▶ if (a0.kind != FRX_XV_NODESET) {
Branch: 1 branch
BranchTrueFalseTotal
if (a0.kind != FRX_XV_NODESET) { 1 29 30
1127 1 frx_xp_val_free(&a0);
1128 1 return frx_xp_efail(e, "count() and sum() take a node-set");
1129 }
1130 29 set = &a0.set;
1131 29 out->kind = FRX_XV_NUMBER;
1132 29 ▶ if (n->func == FRX_FN_COUNT) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->func == FRX_FN_COUNT) { 28 1 29
1133 28 out->num = (double)set->n;
1134 } else {
1135 1 double total = 0.0;
1136 int i;
1137 1 frx_buf_init(&s0);
1138 4 ▶ for (i = 0; i < set->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < set->n; i++) { 3 1 4
1139 3 ▶ if (!(ok = frx_xp_set_str(e, set, i, &s0))) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!(ok = frx_xp_set_str(e, set, i, &s0))) break; 0 3 3
1140 3 ▶ total += frx_xp_str_num(s0.p ? s0.p : "", s0.len);
Branch: 1 branch
BranchTrueFalseTotal
total += frx_xp_str_num(s0.p ? s0.p : "", s0.len); 3 0 3
1141 }
1142 1 frx_buf_free(&s0);
1143 1 out->num = total;
1144 }
1145 29 frx_xp_val_free(&a0);
1146 29 return ok;
1147 }
1148  
1149 4 case FRX_FN_ID: {
1150 4 frx_xp_val_init(&a0);
1151 4 ▶ if (!frx_xp_eval(e, n->args[0], c, &a0)) { frx_xp_val_free(&a0); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_eval(e, n->args[0], c, &a0)) { frx_xp_val_free(&a0); return 0; } 0 4 4
1152 4 out->kind = FRX_XV_NODESET;
1153 4 frx_buf_init(&s0);
1154 4 ▶ if (a0.kind == FRX_XV_NODESET) {
Branch: 1 branch
BranchTrueFalseTotal
if (a0.kind == FRX_XV_NODESET) { 0 4 4
1155 int i;
1156 0 ▶ for (i = 0; ok && i < a0.set.n; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; ok && i < a0.set.n; i++) 0 0 0
for (i = 0; ok && i < a0.set.n; i++) 0 0 0
1157 0 ok = frx_xp_set_str(e, &a0.set, i, &s0)
1158 0 ▶ && frx_xp_id_lookup(e, s0.p ? s0.p : "", s0.len, &out->set);
Branch: 3 branches
BranchTrueFalseTotal
&& frx_xp_id_lookup(e, s0.p ? s0.p : "", s0.len, &out->set); 0 0 0
&& frx_xp_id_lookup(e, s0.p ? s0.p : "", s0.len, &out->set); 0 0 0
&& frx_xp_id_lookup(e, s0.p ? s0.p : "", s0.len, &out->set); 0 0 0
1159 } else {
1160 8 ok = frx_xp_to_str(e, &a0, &s0)
1161 4 ▶ && frx_xp_id_lookup(e, s0.p ? s0.p : "", s0.len, &out->set);
Branch: 3 branches
BranchTrueFalseTotal
&& frx_xp_id_lookup(e, s0.p ? s0.p : "", s0.len, &out->set); 4 0 4
&& frx_xp_id_lookup(e, s0.p ? s0.p : "", s0.len, &out->set); 4 0 4
&& frx_xp_id_lookup(e, s0.p ? s0.p : "", s0.len, &out->set); 4 0 4
1162 }
1163 4 frx_buf_free(&s0);
1164 4 frx_xp_val_free(&a0);
1165 4 ▶ if (ok) frx_xp_set_sort(&out->set);
Branch: 1 branch
BranchTrueFalseTotal
if (ok) frx_xp_set_sort(&out->set); 4 0 4
1166 4 return ok;
1167 }
1168  
1169 10 case FRX_FN_LOCAL_NAME:
1170 case FRX_FN_NAMESPACE_URI:
1171 case FRX_FN_NAME: {
1172 const frx_xp_set *set;
1173 frx_xp_set self;
1174 frx_xp_item one;
1175 10 frx_xp_val_init(&a0);
1176 10 memset(&self, 0, sizeof self);
1177 10 ▶ if (!frx_xp_arg_set(e, n, c, &a0, &set, &self, &one)) { frx_xp_val_free(&a0); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_arg_set(e, n, c, &a0, &set, &self, &one)) { frx_xp_val_free(&a0); return 0; } 0 10 10
1178 10 out->kind = FRX_XV_STRING;
1179 10 ▶ if (set->n)
Branch: 1 branch
BranchTrueFalseTotal
if (set->n) 9 1 10
1180 9 ok = frx_xp_item_name(e, &set->v[0],
1181 9 ▶ n->func == FRX_FN_LOCAL_NAME ? 0
Branch: 1 branch
BranchTrueFalseTotal
n->func == FRX_FN_LOCAL_NAME ? 0 4 5 9
1182 4 ▶ : n->func == FRX_FN_NAMESPACE_URI ? 1 : 2,
Branch: 1 branch
BranchTrueFalseTotal
: n->func == FRX_FN_NAMESPACE_URI ? 1 : 2, 1 3 4
1183 &out->str);
1184 10 frx_xp_val_free(&a0);
1185 10 return ok;
1186 }
1187  
1188 152 case FRX_FN_STRING:
1189 152 out->kind = FRX_XV_STRING;
1190 152 return frx_xp_arg_str(e, n, c, 0, &out->str);
1191  
1192 1 case FRX_FN_CONCAT: {
1193 int i;
1194 1 out->kind = FRX_XV_STRING;
1195 1 frx_buf_init(&s0);
1196 4 ▶ for (i = 0; ok && i < n->n_args; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; ok && i < n->n_args; i++) { 4 0 4
for (i = 0; ok && i < n->n_args; i++) { 3 1 4
1197 3 frx_xp_val_init(&a0);
1198 3 ▶ ok = frx_xp_eval(e, n->args[i], c, &a0) && frx_xp_to_str(e, &a0, &s0);
Branch: 2 branches
BranchTrueFalseTotal
ok = frx_xp_eval(e, n->args[i], c, &a0) && frx_xp_to_str(e, &a0, &s0); 3 0 3
ok = frx_xp_eval(e, n->args[i], c, &a0) && frx_xp_to_str(e, &a0, &s0); 3 0 3
1199 3 ▶ if (ok) frx_buf_append_n(&out->str, s0.p ? s0.p : "", s0.len);
Branch: 2 branches
BranchTrueFalseTotal
if (ok) frx_buf_append_n(&out->str, s0.p ? s0.p : "", s0.len); 3 0 3
if (ok) frx_buf_append_n(&out->str, s0.p ? s0.p : "", s0.len); 3 0 3
1200 3 frx_xp_val_free(&a0);
1201 }
1202 1 frx_buf_free(&s0);
1203 1 return ok;
1204 }
1205  
1206 14 case FRX_FN_STARTS_WITH:
1207 case FRX_FN_CONTAINS:
1208 case FRX_FN_SUBSTRING_BEFORE:
1209 case FRX_FN_SUBSTRING_AFTER: {
1210 /* an frx_buf that never had to grow has a NULL p, which the empty
1211 * string reaches here as; the base of the search must not be it */
1212 const char *sp, *fp;
1213 14 size_t at = 0;
1214 14 int found = 0;
1215 14 frx_buf_init(&s0);
1216 14 frx_buf_init(&s1);
1217 14 ▶ ok = frx_xp_arg_str(e, n, c, 0, &s0) && frx_xp_arg_str(e, n, c, 1, &s1);
Branch: 2 branches
BranchTrueFalseTotal
ok = frx_xp_arg_str(e, n, c, 0, &s0) && frx_xp_arg_str(e, n, c, 1, &s1); 14 0 14
ok = frx_xp_arg_str(e, n, c, 0, &s0) && frx_xp_arg_str(e, n, c, 1, &s1); 14 0 14
1218 14 ▶ sp = s0.p ? s0.p : "";
Branch: 1 branch
BranchTrueFalseTotal
sp = s0.p ? s0.p : ""; 9 5 14
1219 14 ▶ fp = s1.p ? s1.p : "";
Branch: 1 branch
BranchTrueFalseTotal
fp = s1.p ? s1.p : ""; 14 0 14
1220 14 ▶ if (ok && s1.len <= s0.len) {
Branch: 2 branches
BranchTrueFalseTotal
if (ok && s1.len <= s0.len) { 14 0 14
if (ok && s1.len <= s0.len) { 13 1 14
1221 13 size_t last = s0.len - s1.len;
1222 27 for (at = 0; ; at++) {
1223 27 ▶ if (s1.len == 0 || memcmp(sp + at, fp, s1.len) == 0) { found = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (s1.len == 0 || memcmp(sp + at, fp, s1.len) == 0) { found = 1; break; } 21 6 27
if (s1.len == 0 || memcmp(sp + at, fp, s1.len) == 0) { found = 1; break; } 5 16 21
1224 16 ▶ if (at == last) break;
Branch: 1 branch
BranchTrueFalseTotal
if (at == last) break; 2 14 16
1225 }
1226 }
1227 14 ▶ if (ok && n->func == FRX_FN_STARTS_WITH) {
Branch: 2 branches
BranchTrueFalseTotal
if (ok && n->func == FRX_FN_STARTS_WITH) { 14 0 14
if (ok && n->func == FRX_FN_STARTS_WITH) { 3 11 14
1228 3 out->kind = FRX_XV_BOOLEAN;
1229 3 ▶ out->bl = found && at == 0;
Branch: 2 branches
BranchTrueFalseTotal
out->bl = found && at == 0; 3 0 3
out->bl = found && at == 0; 2 1 3
1230 11 ▶ } else if (ok && n->func == FRX_FN_CONTAINS) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (ok && n->func == FRX_FN_CONTAINS) { 11 0 11
} else if (ok && n->func == FRX_FN_CONTAINS) { 5 6 11
1231 5 out->kind = FRX_XV_BOOLEAN;
1232 5 out->bl = found;
1233 6 ▶ } else if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ok) { 6 0 6
1234 6 out->kind = FRX_XV_STRING;
1235 6 ▶ if (found) {
Branch: 1 branch
BranchTrueFalseTotal
if (found) { 5 1 6
1236 5 ▶ if (n->func == FRX_FN_SUBSTRING_BEFORE)
Branch: 1 branch
BranchTrueFalseTotal
if (n->func == FRX_FN_SUBSTRING_BEFORE) 2 3 5
1237 2 frx_buf_append_n(&out->str, sp, at);
1238 else
1239 3 frx_buf_append_n(&out->str, sp + at + s1.len, s0.len - at - s1.len);
1240 }
1241 }
1242 14 frx_buf_free(&s0);
1243 14 frx_buf_free(&s1);
1244 14 return ok;
1245 }
1246  
1247 9 case FRX_FN_SUBSTRING: {
1248 9 double start = 0.0, len = 0.0, lo, hi;
1249 9 frx_buf_init(&s0);
1250 9 ▶ ok = frx_xp_arg_str(e, n, c, 0, &s0) && frx_xp_arg_num(e, n, c, 1, &start);
Branch: 2 branches
BranchTrueFalseTotal
ok = frx_xp_arg_str(e, n, c, 0, &s0) && frx_xp_arg_num(e, n, c, 1, &start); 9 0 9
ok = frx_xp_arg_str(e, n, c, 0, &s0) && frx_xp_arg_num(e, n, c, 1, &start); 9 0 9
1251 9 ▶ if (ok && n->n_args > 2) ok = frx_xp_arg_num(e, n, c, 2, &len);
Branch: 2 branches
BranchTrueFalseTotal
if (ok && n->n_args > 2) ok = frx_xp_arg_num(e, n, c, 2, &len); 9 0 9
if (ok && n->n_args > 2) ok = frx_xp_arg_num(e, n, c, 2, &len); 8 1 9
1252 9 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 9 0 9
1253 9 lo = frx_xp_round(start);
1254 9 ▶ hi = n->n_args > 2 ? lo + frx_xp_round(len) : DBL_MAX;
Branch: 1 branch
BranchTrueFalseTotal
hi = n->n_args > 2 ? lo + frx_xp_round(len) : DBL_MAX; 8 1 9
1255 9 out->kind = FRX_XV_STRING;
1256 9 ▶ frx_xp_substring(&out->str, s0.p ? s0.p : "", s0.len, lo, hi);
Branch: 1 branch
BranchTrueFalseTotal
frx_xp_substring(&out->str, s0.p ? s0.p : "", s0.len, lo, hi); 9 0 9
1257 }
1258 9 frx_buf_free(&s0);
1259 9 return ok;
1260 }
1261  
1262 2 case FRX_FN_STRING_LENGTH:
1263 2 frx_buf_init(&s0);
1264 2 ok = frx_xp_arg_str(e, n, c, 0, &s0);
1265 2 out->kind = FRX_XV_NUMBER;
1266 2 ▶ if (ok) out->num = (double)frx_xp_nchars(s0.p ? s0.p : "", s0.len);
Branch: 2 branches
BranchTrueFalseTotal
if (ok) out->num = (double)frx_xp_nchars(s0.p ? s0.p : "", s0.len); 2 0 2
if (ok) out->num = (double)frx_xp_nchars(s0.p ? s0.p : "", s0.len); 2 0 2
1267 2 frx_buf_free(&s0);
1268 2 return ok;
1269  
1270 1 case FRX_FN_NORMALIZE_SPACE:
1271 1 frx_buf_init(&s0);
1272 1 ok = frx_xp_arg_str(e, n, c, 0, &s0);
1273 1 out->kind = FRX_XV_STRING;
1274 1 ▶ if (ok) frx_xp_normalize_space(&out->str, s0.p ? s0.p : "", s0.len);
Branch: 2 branches
BranchTrueFalseTotal
if (ok) frx_xp_normalize_space(&out->str, s0.p ? s0.p : "", s0.len); 1 0 1
if (ok) frx_xp_normalize_space(&out->str, s0.p ? s0.p : "", s0.len); 1 0 1
1275 1 frx_buf_free(&s0);
1276 1 return ok;
1277  
1278 4 case FRX_FN_TRANSLATE:
1279 4 frx_buf_init(&s0);
1280 4 frx_buf_init(&s1);
1281 4 frx_buf_init(&s2);
1282 4 ok = frx_xp_arg_str(e, n, c, 0, &s0)
1283 4 ▶ && frx_xp_arg_str(e, n, c, 1, &s1)
Branch: 1 branch
BranchTrueFalseTotal
&& frx_xp_arg_str(e, n, c, 1, &s1) 4 0 4
1284 8 ▶ && frx_xp_arg_str(e, n, c, 2, &s2);
Branch: 2 branches
BranchTrueFalseTotal
&& frx_xp_arg_str(e, n, c, 2, &s2); 4 0 4
&& frx_xp_arg_str(e, n, c, 2, &s2); 4 0 4
1285 4 out->kind = FRX_XV_STRING;
1286 12 ▶ if (ok) frx_xp_translate(&out->str, s0.p ? s0.p : "", s0.len,
Branch: 2 branches
BranchTrueFalseTotal
if (ok) frx_xp_translate(&out->str, s0.p ? s0.p : "", s0.len, 4 0 4
if (ok) frx_xp_translate(&out->str, s0.p ? s0.p : "", s0.len, 4 0 4
1287 8 ▶ s1.p ? s1.p : "", s1.len, s2.p ? s2.p : "", s2.len);
Branch: 2 branches
BranchTrueFalseTotal
s1.p ? s1.p : "", s1.len, s2.p ? s2.p : "", s2.len); 4 0 4
s1.p ? s1.p : "", s1.len, s2.p ? s2.p : "", s2.len); 4 0 4
1288 4 frx_buf_free(&s0);
1289 4 frx_buf_free(&s1);
1290 4 frx_buf_free(&s2);
1291 4 return ok;
1292  
1293 12 case FRX_FN_BOOLEAN:
1294 case FRX_FN_NOT:
1295 12 frx_xp_val_init(&a0);
1296 12 ok = frx_xp_eval(e, n->args[0], c, &a0);
1297 12 out->kind = FRX_XV_BOOLEAN;
1298 12 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 12 0 12
1299 12 int b = frx_xp_to_bool(e, &a0);
1300 12 ▶ out->bl = n->func == FRX_FN_NOT ? !b : b;
Branch: 1 branch
BranchTrueFalseTotal
out->bl = n->func == FRX_FN_NOT ? !b : b; 5 7 12
1301 }
1302 12 frx_xp_val_free(&a0);
1303 12 return ok;
1304  
1305 35 case FRX_FN_LANG: {
1306 const frx_str *have;
1307 35 frx_buf_init(&s0);
1308 35 ok = frx_xp_arg_str(e, n, c, 0, &s0);
1309 35 out->kind = FRX_XV_BOOLEAN;
1310 35 have = frx_xp_lang_of(&c->item);
1311 35 ▶ if (ok && have) out->bl = frx_xp_lang_match(have, s0.p ? s0.p : "", s0.len);
Branch: 3 branches
BranchTrueFalseTotal
if (ok && have) out->bl = frx_xp_lang_match(have, s0.p ? s0.p : "", s0.len); 35 0 35
if (ok && have) out->bl = frx_xp_lang_match(have, s0.p ? s0.p : "", s0.len); 35 0 35
if (ok && have) out->bl = frx_xp_lang_match(have, s0.p ? s0.p : "", s0.len); 35 0 35
1312 35 frx_buf_free(&s0);
1313 35 return ok;
1314 }
1315  
1316 10 case FRX_FN_NUMBER:
1317 10 out->kind = FRX_XV_NUMBER;
1318 10 return frx_xp_arg_num(e, n, c, 0, &out->num);
1319  
1320 11 case FRX_FN_FLOOR:
1321 case FRX_FN_CEILING:
1322 case FRX_FN_ROUND: {
1323 11 double v = 0.0;
1324 11 ok = frx_xp_arg_num(e, n, c, 0, &v);
1325 11 out->kind = FRX_XV_NUMBER;
1326 11 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 11 0 11
1327 11 ▶ if (frx_xp_is_nan(v) || frx_xp_is_inf(v) || v == 0.0) out->num = v;
Branch: 3 branches
BranchTrueFalseTotal
if (frx_xp_is_nan(v) || frx_xp_is_inf(v) || v == 0.0) out->num = v; 10 1 11
if (frx_xp_is_nan(v) || frx_xp_is_inf(v) || v == 0.0) out->num = v; 9 1 10
if (frx_xp_is_nan(v) || frx_xp_is_inf(v) || v == 0.0) out->num = v; 0 9 9
1328 9 ▶ else if (n->func == FRX_FN_FLOOR) out->num = floor(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (n->func == FRX_FN_FLOOR) out->num = floor(v); 2 7 9
1329 7 ▶ else if (n->func == FRX_FN_CEILING) out->num = ceil(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (n->func == FRX_FN_CEILING) out->num = ceil(v); 2 5 7
1330 5 else out->num = frx_xp_round(v);
1331 }
1332 11 return ok;
1333 }
1334  
1335 0 default:
1336 0 return frx_xp_efail(e, "unknown function");
1337 }
1338 }
1339  
1340 /* ---- the evaluator ----------------------------------------------------- */
1341  
1342 static int
1343 2041 frx_xp_eval(frx_xeval *e, const frx_xn *n, const frx_xctx *c, frx_xp_val *out)
1344 {
1345 frx_xp_val l, r;
1346 int ok;
1347  
1348 2041 ▶ if (e->failed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (e->failed) return 0; 0 2041 2041
1349  
1350 2041 switch (n->kind) {
1351 143 case FRX_XN_PATH:
1352 143 return frx_xp_eval_path(e, n, c, out);
1353  
1354 0 case FRX_XN_STEP: {
1355 frx_xn path;
1356 frx_xn *steps[1];
1357 0 memset(&path, 0, sizeof path);
1358 0 path.kind = FRX_XN_PATH;
1359 0 steps[0] = (frx_xn *)n;
1360 0 path.steps = steps;
1361 0 path.n_steps = 1;
1362 0 return frx_xp_eval_path(e, &path, c, out);
1363 }
1364  
1365 108 case FRX_XN_LITERAL:
1366 108 out->kind = FRX_XV_STRING;
1367 108 frx_buf_append_n(&out->str, n->str.p, n->str.len);
1368 108 ▶ return out->str.failed ? frx_xp_efail(e, "out of memory") : 1;
Branch: 1 branch
BranchTrueFalseTotal
return out->str.failed ? frx_xp_efail(e, "out of memory") : 1; 0 108 108
1369  
1370 341 case FRX_XN_NUMBER:
1371 341 out->kind = FRX_XV_NUMBER;
1372 341 out->num = n->num;
1373 341 return 1;
1374  
1375 6 case FRX_XN_VAR: {
1376 const frx_xp_val *v;
1377 6 ▶ if (n->var < 0 || n->var >= e->n_vars)
Branch: 2 branches
BranchTrueFalseTotal
if (n->var < 0 || n->var >= e->n_vars) 6 0 6
if (n->var < 0 || n->var >= e->n_vars) 0 6 6
1378 0 return frx_xp_efail(e, "the expression uses a variable that was not given a value");
1379 6 v = &e->vars[n->var].val;
1380 6 out->kind = v->kind;
1381 6 out->bl = v->bl;
1382 6 out->num = v->num;
1383 6 ▶ if (v->kind == FRX_XV_STRING)
Branch: 1 branch
BranchTrueFalseTotal
if (v->kind == FRX_XV_STRING) 3 3 6
1384 3 ▶ frx_buf_append_n(&out->str, v->str.p ? v->str.p : "", v->str.len);
Branch: 1 branch
BranchTrueFalseTotal
frx_buf_append_n(&out->str, v->str.p ? v->str.p : "", v->str.len); 3 0 3
1385 6 ▶ return out->str.failed ? frx_xp_efail(e, "out of memory") : 1;
Branch: 1 branch
BranchTrueFalseTotal
return out->str.failed ? frx_xp_efail(e, "out of memory") : 1; 0 6 6
1386 }
1387  
1388 317 case FRX_XN_FUNC:
1389 317 return frx_xp_func(e, n, c, out);
1390  
1391 845 case FRX_XN_FILTER: {
1392 int i;
1393 845 ▶ if (!frx_xp_eval(e, n->a, c, out)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_eval(e, n->a, c, out)) return 0; 1 844 845
1394 844 ▶ if (!n->n_preds) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!n->n_preds) return 1; 842 2 844
1395 2 ▶ if (out->kind != FRX_XV_NODESET)
Branch: 1 branch
BranchTrueFalseTotal
if (out->kind != FRX_XV_NODESET) 0 2 2
1396 0 return frx_xp_efail(e, "a predicate can only filter a node-set");
1397 4 ▶ for (i = 0; i < n->n_preds; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n->n_preds; i++) 2 2 4
1398 2 ▶ if (!frx_xp_filter_set(e, n->preds[i], &out->set)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_filter_set(e, n->preds[i], &out->set)) return 0; 0 2 2
1399 2 return 1;
1400 }
1401  
1402 4 case FRX_XN_UNION: {
1403 int i;
1404 4 frx_xp_val_init(&l);
1405 4 frx_xp_val_init(&r);
1406 4 ▶ ok = frx_xp_eval(e, n->a, c, &l) && frx_xp_eval(e, n->b, c, &r);
Branch: 2 branches
BranchTrueFalseTotal
ok = frx_xp_eval(e, n->a, c, &l) && frx_xp_eval(e, n->b, c, &r); 4 0 4
ok = frx_xp_eval(e, n->a, c, &l) && frx_xp_eval(e, n->b, c, &r); 4 0 4
1407 4 ▶ if (ok && (l.kind != FRX_XV_NODESET || r.kind != FRX_XV_NODESET))
Branch: 3 branches
BranchTrueFalseTotal
if (ok && (l.kind != FRX_XV_NODESET || r.kind != FRX_XV_NODESET)) 4 0 4
if (ok && (l.kind != FRX_XV_NODESET || r.kind != FRX_XV_NODESET)) 3 1 4
if (ok && (l.kind != FRX_XV_NODESET || r.kind != FRX_XV_NODESET)) 0 3 3
1408 1 ok = frx_xp_efail(e, "'|' joins two node-sets and nothing else");
1409 4 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 3 1 4
1410 3 out->kind = FRX_XV_NODESET;
1411 12 ▶ for (i = 0; ok && i < l.set.n; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; ok && i < l.set.n; i++) 12 0 12
for (i = 0; ok && i < l.set.n; i++) 9 3 12
1412 9 ▶ if (!frx_xp_set_push(&out->set, &l.set.v[i])) ok = frx_xp_efail(e, "out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_set_push(&out->set, &l.set.v[i])) ok = frx_xp_efail(e, "out of memory"); 0 9 9
1413 10 ▶ for (i = 0; ok && i < r.set.n; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; ok && i < r.set.n; i++) 10 0 10
for (i = 0; ok && i < r.set.n; i++) 7 3 10
1414 7 ▶ if (!frx_xp_set_push(&out->set, &r.set.v[i])) ok = frx_xp_efail(e, "out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_set_push(&out->set, &r.set.v[i])) ok = frx_xp_efail(e, "out of memory"); 0 7 7
1415 3 ▶ if (ok) frx_xp_set_sort(&out->set);
Branch: 1 branch
BranchTrueFalseTotal
if (ok) frx_xp_set_sort(&out->set); 3 0 3
1416 }
1417 4 frx_xp_val_free(&l);
1418 4 frx_xp_val_free(&r);
1419 4 return ok;
1420 }
1421  
1422 200 case FRX_XN_OR:
1423 case FRX_XN_AND: {
1424 int b;
1425 200 frx_xp_val_init(&l);
1426 200 ▶ if (!frx_xp_eval(e, n->a, c, &l)) { frx_xp_val_free(&l); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_eval(e, n->a, c, &l)) { frx_xp_val_free(&l); return 0; } 0 200 200
1427 200 b = frx_xp_to_bool(e, &l);
1428 200 frx_xp_val_free(&l);
1429 200 out->kind = FRX_XV_BOOLEAN;
1430 /* both operators short-circuit: section 3.4 says the right side is
1431 * not evaluated when the left settles it */
1432 200 ▶ if ((n->kind == FRX_XN_OR && b) || (n->kind == FRX_XN_AND && !b)) {
Branch: 4 branches
BranchTrueFalseTotal
if ((n->kind == FRX_XN_OR && b) || (n->kind == FRX_XN_AND && !b)) { 0 200 200
if ((n->kind == FRX_XN_OR && b) || (n->kind == FRX_XN_AND && !b)) { 0 0 0
if ((n->kind == FRX_XN_OR && b) || (n->kind == FRX_XN_AND && !b)) { 200 0 200
if ((n->kind == FRX_XN_OR && b) || (n->kind == FRX_XN_AND && !b)) { 0 200 200
1433 0 out->bl = b;
1434 0 return 1;
1435 }
1436 200 frx_xp_val_init(&r);
1437 200 ▶ if (!frx_xp_eval(e, n->b, c, &r)) { frx_xp_val_free(&r); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_xp_eval(e, n->b, c, &r)) { frx_xp_val_free(&r); return 0; } 0 200 200
1438 200 out->bl = frx_xp_to_bool(e, &r);
1439 200 frx_xp_val_free(&r);
1440 200 return 1;
1441 }
1442  
1443 14 case FRX_XN_NEG: {
1444 double v;
1445 14 frx_xp_val_init(&l);
1446 14 ▶ ok = frx_xp_eval(e, n->a, c, &l) && frx_xp_to_num(e, &l, &v);
Branch: 2 branches
BranchTrueFalseTotal
ok = frx_xp_eval(e, n->a, c, &l) && frx_xp_to_num(e, &l, &v); 14 0 14
ok = frx_xp_eval(e, n->a, c, &l) && frx_xp_to_num(e, &l, &v); 14 0 14
1447 14 frx_xp_val_free(&l);
1448 14 out->kind = FRX_XV_NUMBER;
1449 14 ▶ if (ok) out->num = -v;
Branch: 1 branch
BranchTrueFalseTotal
if (ok) out->num = -v; 14 0 14
1450 14 return ok;
1451 }
1452  
1453 36 case FRX_XN_EQ: case FRX_XN_NE: case FRX_XN_LT:
1454 case FRX_XN_LE: case FRX_XN_GT: case FRX_XN_GE: {
1455 36 int res = 0;
1456 36 frx_xp_val_init(&l);
1457 36 frx_xp_val_init(&r);
1458 72 ▶ ok = frx_xp_eval(e, n->a, c, &l) && frx_xp_eval(e, n->b, c, &r)
Branch: 1 branch
BranchTrueFalseTotal
ok = frx_xp_eval(e, n->a, c, &l) && frx_xp_eval(e, n->b, c, &r) 36 0 36
1459 72 ▶ && frx_xp_compare(e, n->kind, &l, &r, &res);
Branch: 2 branches
BranchTrueFalseTotal
&& frx_xp_compare(e, n->kind, &l, &r, &res); 36 0 36
&& frx_xp_compare(e, n->kind, &l, &r, &res); 36 0 36
1460 36 frx_xp_val_free(&l);
1461 36 frx_xp_val_free(&r);
1462 36 out->kind = FRX_XV_BOOLEAN;
1463 36 out->bl = res;
1464 36 return ok;
1465 }
1466  
1467 27 default: {
1468 27 double x = 0.0, y = 0.0;
1469 27 frx_xp_val_init(&l);
1470 27 frx_xp_val_init(&r);
1471 54 ▶ ok = frx_xp_eval(e, n->a, c, &l) && frx_xp_eval(e, n->b, c, &r)
Branch: 1 branch
BranchTrueFalseTotal
ok = frx_xp_eval(e, n->a, c, &l) && frx_xp_eval(e, n->b, c, &r) 27 0 27
1472 54 ▶ && frx_xp_to_num(e, &l, &x) && frx_xp_to_num(e, &r, &y);
Branch: 3 branches
BranchTrueFalseTotal
&& frx_xp_to_num(e, &l, &x) && frx_xp_to_num(e, &r, &y); 27 0 27
&& frx_xp_to_num(e, &l, &x) && frx_xp_to_num(e, &r, &y); 27 0 27
&& frx_xp_to_num(e, &l, &x) && frx_xp_to_num(e, &r, &y); 27 0 27
1473 27 frx_xp_val_free(&l);
1474 27 frx_xp_val_free(&r);
1475 27 out->kind = FRX_XV_NUMBER;
1476 27 ▶ if (!ok) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return 0; 0 27 27
1477 27 switch (n->kind) {
1478 7 case FRX_XN_ADD: out->num = x + y; break;
1479 0 case FRX_XN_SUB: out->num = x - y; break;
1480 2 case FRX_XN_MUL: out->num = x * y; break;
1481 /* IEEE gives what section 3.5 asks for on its own: a division by
1482 * zero is an infinity with the sign of the operands, 0 div 0 is
1483 * NaN, and mod by zero is NaN through fmod */
1484 14 case FRX_XN_DIV: out->num = x / y; break;
1485 4 default: out->num = fmod(x, y); break;
1486 }
1487 27 return 1;
1488 }
1489 }
1490 }
1491  
1492 #endif /* FRX_XPATH_EVAL_H */
« xs/xpath.xs ↑ Summary include/frx/frx_xpath_lex.h »