File-Raw-XML
view release on metacpan or search on metacpan
include/frx/frx_xpath_eval.h view on Meta::CPAN
#ifndef FRX_XPATH_EVAL_H
#define FRX_XPATH_EVAL_H
/* XPath 1.0 evaluation: four types, thirteen axes, 27 functions.
*
* NODE-SETS are arrays of (node, kind, index): kind selects the node
* itself, its i'th attribute, or its i'th namespace node, because an
* attribute is not an frx_node and a namespace node is not stored at all.
* Document order among the three is XPath section 5's: the element, then
* its namespace nodes, then its attributes, then its children. An
* attribute and a namespace node therefore sort on their element's
* `order` and are separated by kind, which is why the kind constants are
* numbered in that order and never renumbered.
*
* NAMESPACE NODES are synthesised from the scope chain at evaluation and
* never stored on the tree: the in-scope bindings, nearest per prefix,
* with a binding to the empty URI (xmlns="") dropped because it declares
* the absence of one, with xml always present, sorted by prefix so the
* list is the same list every time an index into it is read back. XPath
* leaves the relative order of namespace nodes to the implementation and
* requires only that it be stable; this is the stable order chosen.
* Synthesis is O(depth) per element and is paid again each time a
* namespace item's name or value is read, which is the cost of not
* carrying namespace nodes in a tree that has no use for them.
*
* RECURSION. Over the document, none (rule 2): every axis is an
* iterative walk and every predicate is a loop. Over the AST, yes,
* bounded by the depth the compiler capped at `max_expr_depth`, which is
* the AST's depth and not the parser's, so a long chain of associative
* operators is bounded too.
*
* NUMBERS are C doubles with the specification's semantics throughout:
* NaN, the infinities and negative zero all reach the caller. The
* number-to-string conversion is section 4.2's and not a printf format:
* no exponent ever appears, an integer-valued double has no fraction, and
* the digits are the shortest that read back as the same double. It finds
* those digits through sprintf and strtod, which agree with each other
* whatever LC_NUMERIC says, and then lays the decimal point out itself,
* so a locale whose decimal separator is a comma cannot reach the output.
*
* Needs frx_buf.h, frx_utf8.h, frx_tree.h, frx_ns.h, frx_parse.h,
* frx_xpath_parse.h. */
#include <math.h>
#include <float.h>
/* FRX_XP_KNODE, FRX_XP_KNS and FRX_XP_KATTR are frx_abi.h's, numbered in
* document order among themselves and never renumbered. */
/* the four value types */
enum { FRX_XV_NODESET = 0, FRX_XV_BOOLEAN, FRX_XV_NUMBER, FRX_XV_STRING };
typedef struct frx_xp_item {
const frx_node *node; /* the node, or the owner element of an attribute
* or namespace entry */
int kind;
int index;
} frx_xp_item;
typedef struct frx_xp_set {
frx_xp_item *v;
int n, cap;
} frx_xp_set;
typedef struct frx_xp_val {
int kind;
int bl;
double num;
frx_buf str;
frx_xp_set set;
} frx_xp_val;
typedef struct frx_xp_var {
frx_str name;
frx_xp_val val;
} frx_xp_var;
typedef struct frx_xeval {
frx_doc *doc;
const frx_xp_var *vars;
int n_vars;
const char *err; /* static; NULL while all is well */
int failed;
} frx_xeval;
typedef struct frx_xctx {
frx_xp_item item;
int position; /* 1-based */
int size;
} frx_xctx;
static int frx_xp_eval(frx_xeval *e, const frx_xn *n, const frx_xctx *c, frx_xp_val *out);
static int
frx_xp_efail(frx_xeval *e, const char *what)
{
if (!e->failed) { e->failed = 1; e->err = what; }
return 0;
}
/* ---- doubles ----------------------------------------------------------- */
static double
frx_xp_nan(void)
{
volatile double z = 0.0;
return z / z;
}
static int
frx_xp_is_nan(double v)
{
return v != v;
}
static int
frx_xp_is_inf(double v)
{
return v > DBL_MAX || v < -DBL_MAX;
}
/* section 4.2: no exponent, no trailing zeros, the shortest digits that
* read back as this double */
static void
frx_xp_num_str(frx_buf *out, double v)
{
char fmt[16];
char buf[512];
char digits[40];
double av;
int p, i, ndig = 0, e10 = 0, neg = 0, dp;
if (frx_xp_is_nan(v)) { frx_buf_append(out, "NaN"); return; }
if (v == 0.0) { frx_buf_append(out, "0"); return; }
if (v < 0) { neg = 1; av = -v; } else av = v;
if (frx_xp_is_inf(av)) { frx_buf_append(out, neg ? "-Infinity" : "Infinity"); return; }
for (p = 0; p < 17; p++) {
FRX_FMT1(fmt, sizeof fmt, "%%.%de", p);
FRX_FMT1(buf, sizeof buf, fmt, av);
if (strtod(buf, NULL) == av) break;
}
/* buf is d<sep>ddde<sign>dd, where <sep> is whatever LC_NUMERIC said */
i = 0;
digits[ndig++] = buf[i++];
if (buf[i] && buf[i] != 'e' && buf[i] != 'E') {
i++; /* the decimal separator */
while (buf[i] >= '0' && buf[i] <= '9' && ndig < (int)sizeof digits)
digits[ndig++] = buf[i++];
}
if (buf[i] == 'e' || buf[i] == 'E') {
int sign = 1;
i++;
if (buf[i] == '+') i++;
else if (buf[i] == '-') { sign = -1; i++; }
while (buf[i] >= '0' && buf[i] <= '9') e10 = e10 * 10 + (buf[i++] - '0');
e10 *= sign;
}
while (ndig > 1 && digits[ndig - 1] == '0') ndig--;
dp = e10 + 1; /* digits before the point */
if (neg) frx_buf_append_ch(out, '-');
if (dp <= 0) {
frx_buf_append(out, "0.");
for (i = 0; i < -dp; i++) frx_buf_append_ch(out, '0');
frx_buf_append_n(out, digits, (size_t)ndig);
} else if (dp >= ndig) {
frx_buf_append_n(out, digits, (size_t)ndig);
for (i = 0; i < dp - ndig; i++) frx_buf_append_ch(out, '0');
} else {
frx_buf_append_n(out, digits, (size_t)dp);
frx_buf_append_ch(out, '.');
frx_buf_append_n(out, digits + dp, (size_t)(ndig - dp));
}
}
/* Section 4.4: optional space, optional '-', Digits ('.' Digits?)? or
* '.' Digits, optional space; anything else is NaN. No sign but '-' and
* no exponent, so the syntax is checked here and only the span that
* passed reaches frx_xp_atod: strtod would take "1e5" and "+1", which
* are not Numbers. */
static double
frx_xp_str_num(const char *p, size_t n)
{
size_t at = 0, start;
int any = 0;
while (at < n && frx_is_s((unsigned char)p[at])) at++;
start = at;
if (at < n && p[at] == '-') at++;
while (at < n && p[at] >= '0' && p[at] <= '9') { at++; any = 1; }
if (at < n && p[at] == '.') {
at++;
while (at < n && p[at] >= '0' && p[at] <= '9') { at++; any = 1; }
}
if (!any) return frx_xp_nan();
{
size_t end = at;
while (at < n && frx_is_s((unsigned char)p[at])) at++;
if (at != n) return frx_xp_nan();
return frx_xp_atod(p + start, end - start);
}
}
/* section 4.4: the integer closest to v, ties to positive infinity */
static double
frx_xp_round(double v)
{
if (frx_xp_is_nan(v) || frx_xp_is_inf(v) || v == 0.0) return v;
if (v < 0.0 && v >= -0.5) return -0.0;
return floor(v + 0.5);
}
/* ---- buffers and values ------------------------------------------------ */
static void
frx_xp_buf_reset(frx_buf *b)
{
b->len = 0;
if (b->p) b->p[0] = '\0';
}
static void
frx_xp_val_init(frx_xp_val *v)
{
memset(v, 0, sizeof *v);
v->kind = FRX_XV_NODESET;
frx_buf_init(&v->str);
}
static void
frx_xp_set_free(frx_xp_set *s)
{
free(s->v);
s->v = NULL;
s->n = s->cap = 0;
}
include/frx/frx_xpath_eval.h view on Meta::CPAN
static void
frx_xp_normalize_space(frx_buf *out, const char *p, size_t n)
{
size_t i = 0, run;
int wrote = 0;
while (i < n) {
while (i < n && frx_is_s((unsigned char)p[i])) i++;
run = i;
while (i < n && !frx_is_s((unsigned char)p[i])) i++;
if (i > run) {
if (wrote) frx_buf_append_ch(out, ' ');
frx_buf_append_n(out, p + run, i - run);
wrote = 1;
}
}
}
/* translate(), by character: a character of `from` maps to the character
* of `to` at the same position, and to nothing when `to` is shorter */
static void
frx_xp_translate(frx_buf *out, const char *s, size_t sn,
const char *f, size_t fn, const char *t, size_t tn)
{
size_t at = 0;
unsigned long cp;
while (at < sn) {
size_t k = frx_utf8_decode((const unsigned char *)s + at, sn - at, &cp);
size_t fa = 0, ti = 0, hit = 0;
if (!k) k = 1;
while (fa < fn) {
unsigned long fc;
size_t fk = frx_utf8_decode((const unsigned char *)f + fa, fn - fa, &fc);
if (!fk) fk = 1;
if (fk == k && memcmp(f + fa, s + at, k) == 0) { hit = 1; break; }
fa += fk;
ti++;
}
if (!hit) {
frx_buf_append_n(out, s + at, k);
} else {
size_t ta = 0, seen = 0;
while (ta < tn && seen < ti) {
unsigned long tc;
size_t tk = frx_utf8_decode((const unsigned char *)t + ta, tn - ta, &tc);
if (!tk) tk = 1;
ta += tk;
seen++;
}
if (ta < tn) {
unsigned long tc;
size_t tk = frx_utf8_decode((const unsigned char *)t + ta, tn - ta, &tc);
if (!tk) tk = 1;
frx_buf_append_n(out, t + ta, tk);
}
}
at += k;
}
}
/* the characters of s whose 1-based position p satisfies lo <= p < hi; a
* NaN bound excludes everything, which is what section 4.2's examples
* with `0 div 0` require */
static void
frx_xp_substring(frx_buf *out, const char *s, size_t n, double lo, double hi)
{
size_t at = 0;
double pos = 1.0;
unsigned long cp;
while (at < n) {
size_t k = frx_utf8_decode((const unsigned char *)s + at, n - at, &cp);
if (!k) k = 1;
if (pos >= lo && pos < hi) frx_buf_append_n(out, s + at, k);
at += k;
pos += 1.0;
}
}
/* the element carrying the nearest xml:lang above (and at) the item */
static const frx_str *
frx_xp_lang_of(const frx_xp_item *it)
{
const frx_node *n = it->node;
for (; n; n = n->parent) {
if (n->kind != FRX_ELEMENT) continue;
{
const frx_str *v = frx_attr_value(n, FRX_XML_NS, "lang");
if (v) return v;
}
}
return NULL;
}
static int
frx_xp_ascii_lower(int c)
{
return c >= 'A' && c <= 'Z' ? c + 32 : c;
}
static int
frx_xp_lang_match(const frx_str *have, const char *want, size_t wn)
{
size_t i;
if (have->len < wn) return 0;
for (i = 0; i < wn; i++)
if (frx_xp_ascii_lower((unsigned char)have->p[i]) != frx_xp_ascii_lower((unsigned char)want[i]))
return 0;
return have->len == wn || have->p[wn] == '-';
}
/* id(): every whitespace-separated token looked up in the document's ID
* index, over every attribute name the index was built from */
static int
frx_xp_id_lookup(frx_xeval *e, const char *p, size_t n, frx_xp_set *out)
{
size_t i = 0;
while (i < n) {
size_t start;
while (i < n && frx_is_s((unsigned char)p[i])) i++;
start = i;
while (i < n && !frx_is_s((unsigned char)p[i])) i++;
if (i > start) {
include/frx/frx_xpath_eval.h view on Meta::CPAN
case FRX_XN_AND: {
int b;
frx_xp_val_init(&l);
if (!frx_xp_eval(e, n->a, c, &l)) { frx_xp_val_free(&l); return 0; }
b = frx_xp_to_bool(e, &l);
frx_xp_val_free(&l);
out->kind = FRX_XV_BOOLEAN;
/* both operators short-circuit: section 3.4 says the right side is
* not evaluated when the left settles it */
if ((n->kind == FRX_XN_OR && b) || (n->kind == FRX_XN_AND && !b)) {
out->bl = b;
return 1;
}
frx_xp_val_init(&r);
if (!frx_xp_eval(e, n->b, c, &r)) { frx_xp_val_free(&r); return 0; }
out->bl = frx_xp_to_bool(e, &r);
frx_xp_val_free(&r);
return 1;
}
case FRX_XN_NEG: {
double v;
frx_xp_val_init(&l);
ok = frx_xp_eval(e, n->a, c, &l) && frx_xp_to_num(e, &l, &v);
frx_xp_val_free(&l);
out->kind = FRX_XV_NUMBER;
if (ok) out->num = -v;
return ok;
}
case FRX_XN_EQ: case FRX_XN_NE: case FRX_XN_LT:
case FRX_XN_LE: case FRX_XN_GT: case FRX_XN_GE: {
int res = 0;
frx_xp_val_init(&l);
frx_xp_val_init(&r);
ok = frx_xp_eval(e, n->a, c, &l) && frx_xp_eval(e, n->b, c, &r)
&& frx_xp_compare(e, n->kind, &l, &r, &res);
frx_xp_val_free(&l);
frx_xp_val_free(&r);
out->kind = FRX_XV_BOOLEAN;
out->bl = res;
return ok;
}
default: {
double x = 0.0, y = 0.0;
frx_xp_val_init(&l);
frx_xp_val_init(&r);
ok = frx_xp_eval(e, n->a, c, &l) && frx_xp_eval(e, n->b, c, &r)
&& frx_xp_to_num(e, &l, &x) && frx_xp_to_num(e, &r, &y);
frx_xp_val_free(&l);
frx_xp_val_free(&r);
out->kind = FRX_XV_NUMBER;
if (!ok) return 0;
switch (n->kind) {
case FRX_XN_ADD: out->num = x + y; break;
case FRX_XN_SUB: out->num = x - y; break;
case FRX_XN_MUL: out->num = x * y; break;
/* IEEE gives what section 3.5 asks for on its own: a division by
* zero is an infinity with the sign of the operands, 0 div 0 is
* NaN, and mod by zero is NaN through fmod */
case FRX_XN_DIV: out->num = x / y; break;
default: out->num = fmod(x, y); break;
}
return 1;
}
}
}
#endif /* FRX_XPATH_EVAL_H */
( run in 1.019 second using v1.01-cache-2.11-cpan-60884a8df76 )