File-Raw-XML
view release on metacpan or search on metacpan
include/frx/frx_xpath_eval.h view on Meta::CPAN
* 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/frx/frx_xpath_eval.h view on Meta::CPAN
* 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;
}
include/frx/frx_xpath_eval.h view on Meta::CPAN
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++;
include/frx/frx_xpath_eval.h view on Meta::CPAN
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;
include/frx/frx_xpath_eval.h view on Meta::CPAN
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 */
lib/File/Raw/XML/XPath.pm view on Meta::CPAN
once; use a compiled object where one expression is evaluated many times.
=head1 NOTES
C<//a[1]> is not the first C<a> in the document. C<//> is
C<< /descendant-or-self::node()/ >>, so the predicate belongs to the
C<child::a> step and selects the first C<a> child of each element that
has one. C<(//a)[1]> is the first in the document.
Numbers are IEEE doubles with the specification's semantics: C<1 div 0>
is C<Infinity>, C<0 div 0> is C<NaN>, and C<string()> of a number never
uses exponent notation and never shows a trailing zero.
C<id()> answers over whatever index the document has: the attribute names
given in C<id_attrs>, or the ID attributes its document type declaration
named when it was parsed with C<< validate => 1 >>, or C<xml:id> under
C<< profile => 'full' >>. A document with none of those has an empty
index, and C<id()> finds nothing rather than guessing.
=head1 SEE ALSO
t/40-xpath.t view on Meta::CPAN
is(str('contains(//zz, "x")'), 'false', 'and a needle that is not there');
is(str('substring-before(//zz, "")'), '', 'substring-before of nothing');
is(str('substring-after(//zz, "")'), '', 'substring-after of nothing');
is(str(q{substring-after('abc', '')}), 'abc', 'and after the empty prefix of a string');
# 4.2's own examples for substring, infinities included
is(str(q{substring('12345', 2, 3)}), '234', 'substring');
is(str(q{substring('12345', 2)}), '2345', 'substring to the end');
is(str(q{substring('12345', 1.5, 2.6)}), '234', 'the arguments are rounded');
is(str(q{substring('12345', 0, 3)}), '12', 'a start before the string');
is(str(q{substring('12345', 0 div 0, 3)}), '', 'a NaN start selects nothing');
is(str(q{substring('12345', 1, 0 div 0)}), '', 'nor does a NaN length');
is(str(q{substring('12345', -42, 1 div 0)}), '12345', 'an infinite length reaches the end');
is(str(q{substring('12345', -1 div 0, 1 div 0)}), '', 'and -Infinity + Infinity is NaN, so nothing');
is(str(q{string-length('hello')}), '5', 'string-length');
is(str(q{normalize-space(' a b c ')}), 'a b c', 'normalize-space strips and collapses');
is(str(q{translate('bar', 'abc', 'ABC')}), 'BAr', 'translate');
is(str(q{translate('--aaa--', 'abc-', 'ABC')}), 'AAA', 'a character with no replacement is removed');
is(str(q{translate('abcdef', 'abc', 'x')}), 'xdef', 'and so is one past the end of the replacement');
# by character, not by byte. The expression crosses the seam as its UTF-8
# bytes when it is flagged as characters and as its raw bytes when it is
# not, which is what every other input to this dist does, so a Latin-1
t/40-xpath.t view on Meta::CPAN
my $latin1 = $e;
utf8::downgrade($latin1);
ok(!eval { $doc->xpath("string($latin1)"); 1 }, 'a Latin-1 byte expression is not UTF-8 and is refused');
like($@, qr/the expression is not UTF-8/, ' saying so');
}
# ---- 4.3: the boolean functions ----------------------------------------
is(str('boolean(1)'), 'true', 'boolean of a non-zero number');
is(str('boolean(0)'), 'false', 'of zero');
is(str('boolean(0 div 0)'), 'false', 'of NaN');
is(str(q{boolean('')}), 'false', 'of the empty string');
is(str(q{boolean('x')}), 'true', 'of a non-empty one');
is(str('boolean(//a)'), 'true', 'of a non-empty node-set');
is(str('boolean(//zz)'), 'false', 'of an empty one');
is(str('not(1)'), 'false', 'not');
is(str('true()'), 'true', 'true');
is(str('false()'), 'false', 'false');
is(str('local-name(//*[lang("en")])'), 'r', 'lang() matches a language subtag of xml:lang');
is(str('count(//*[lang("en")])'), '7', 'and xml:lang is inherited by every descendant');
is(str('count(//*[lang("fr")])'), '0', 'and does not match another language');
is(str('count(//*[lang("EN-gb")])'), '7', 'the comparison is case-insensitive');
is(str('count(//*[lang("en-GB-x")])'), '0', 'and a longer tag does not match a shorter value');
# ---- 4.4: the number functions -----------------------------------------
is(str(q{number(' 12 ')}), '12', 'number strips surrounding whitespace');
is(str(q{number('12.5')}), '12.5', 'a fraction');
is(str(q{number('.5')}), '0.5', 'a leading point');
is(str(q{number('-3')}), '-3', 'a minus');
is(str(q{number('12x')}), 'NaN', 'anything else is NaN');
is(str(q{number('')}), 'NaN', 'including the empty string');
is(str(q{number('1e5')}), 'NaN', 'and an exponent, which is not a Number');
is(str(q{number('+1')}), 'NaN', 'and a plus sign');
is(str('number(true())'), '1', 'a boolean numbers to 1');
is(str('number(false())'), '0', 'or 0');
is(str('floor(1.5)'), '1', 'floor');
is(str('floor(-1.5)'), '-2', 'floor of a negative');
is(str('ceiling(1.5)'), '2', 'ceiling');
is(str('ceiling(-1.5)'), '-1', 'ceiling of a negative');
is(str('round(1.5)'), '2', 'round');
is(str('round(2.5)'), '3', 'a tie goes towards positive infinity');
is(str('round(-1.5)'), '-1', 'so -1.5 rounds to -1');
is(str('round(0.5)'), '1', 'and 0.5 to 1');
is(str('round(-0.5)'), '0', 'and -0.5 to negative zero, which prints as 0');
is(str('round(0 div 0)'), 'NaN', 'round of NaN');
is(str('round(1 div 0)'), 'Infinity', 'round of an infinity');
# ---- 4.2's number to string: no exponent, shortest round trip ----------
is(str('string(0)'), '0', 'zero');
is(str('string(-0)'), '0', 'negative zero prints as 0');
is(str('string(1000000)'), '1000000', 'an integer has no fraction');
is(str('string(1 div 0)'), 'Infinity', 'positive infinity');
is(str('string(-1 div 0)'), '-Infinity', 'negative infinity');
is(str('string(0 div 0)'), 'NaN', 'NaN');
is(str('string(-1 div 0 + 1 div 0)'), 'NaN', 'and an infinity less an infinity');
is(str('string(1 div 3)'), '0.3333333333333333',
'as many digits as distinguish the value: fifteen threes is a different double');
is(str('string(0.1 + 0.2)'), '0.30000000000000004',
'and 0.1 + 0.2 is not 0.3');
is(str('string(0.000001)'), '0.000001', 'no exponent, however small');
is(str('string(0.0000001)'), '0.0000001', 'still no exponent');
is(str('string(123456789012345678901234567890)'), '123456789012345680000000000000',
'no exponent, however large, and the digits are the double the literal names');
# ---- 3.3: union --------------------------------------------------------
xt/xpath-libxml.t view on Meta::CPAN
'and 1e-07',
],
'string(1e0 * 1)' => [
'REFUSED',
'XPath 3.7: Number is Digits ("." Digits?)? or "." Digits. There is '
. 'no exponent, so "1e0" is the Number 1 followed by the name "e0", '
. 'and a name where an operator belongs is an error. libxml2 accepts '
. 'an exponent as an extension.',
],
q{number('1e5')} => [
'NaN',
'XPath 4.4: a string is converted to a number when it is optional '
. 'whitespace, an optional minus sign, a Number and optional '
. 'whitespace, and to NaN otherwise. A Number has no exponent (3.7), '
. 'so "1e5" is not one. libxml2 converts it with strtod.',
],
);
sub render_mine {
my ($o) = @_;
my $r = ref $o;
return 'A:{' . $o->ns . '}' . $o->local . '=' . $o->value if $r eq 'File::Raw::XML::Attr';
return 'N:' . $o->prefix . '=' . $o->uri if $r eq 'File::Raw::XML::Namespace';
my $k = $o->kind;
( run in 1.756 second using v1.01-cache-2.11-cpan-60884a8df76 )