oxml 0.0.11

A pure Rust XML toolkit with zero unsafe code: parsing, an ergonomic tree, and XPath
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2026 oxml. All rights reserved.

//! The `XPath` 1.0 function library, section 4.
//!
//! Dispatch is on [`Func`], resolved at compile time, in five
//! families that mirror the specification's own sections: node-set,
//! string, the string functions that take positions, boolean and
//! number. A function that requires a node-set and is given something
//! else is a type error through the fallible entry point and the
//! documented silent value through the infallible one; `id()` is the
//! exception, since the specification converts its argument.

use alloc::borrow::ToOwned;
use alloc::collections::BTreeMap;
use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;

use super::EvalError;
use super::ast::{Expr, Func};
use super::compare::{is_xml_whitespace, xpath_number, xpath_round};
use super::eval::{Ctx, Ev, eval, type_error};
use super::float;
use super::names::{lang_matches, name_parts, qualified_name};
use super::value::Value;
use crate::tree::{Document, NodeId};

pub(crate) fn eval_function(
    ev: &Ev<'_>,
    func: Func,
    args: &[Expr],
    c: Ctx,
) -> Result<Value, EvalError> {
    match func {
        Func::Last
        | Func::Position
        | Func::Count
        | Func::Id
        | Func::LocalName
        | Func::NamespaceUri
        | Func::Name => node_set_function(ev, func, args, c),
        Func::String
        | Func::Concat
        | Func::StartsWith
        | Func::Contains
        | Func::SubstringBefore
        | Func::SubstringAfter => string_function(ev, func, args, c),
        Func::Substring
        | Func::StringLength
        | Func::NormalizeSpace
        | Func::Translate => positional_string_function(ev, func, args, c),
        Func::Boolean | Func::Not | Func::True | Func::False | Func::Lang => {
            boolean_function(ev, func, args, c)
        }
        Func::Number
        | Func::Sum
        | Func::Floor
        | Func::Ceiling
        | Func::Round => number_function(ev, func, args, c),
    }
}

/// Argument `i`, evaluated, if there is one.
fn arg(
    ev: &Ev<'_>,
    args: &[Expr],
    i: usize,
    c: Ctx,
) -> Result<Option<Value>, EvalError> {
    args.get(i).map(|a| eval(ev, a, c)).transpose()
}

/// Argument `i` as a string, or the empty string when absent.
fn string_arg(
    ev: &Ev<'_>,
    args: &[Expr],
    i: usize,
    c: Ctx,
) -> Result<String, EvalError> {
    match arg(ev, args, i, c)? {
        Some(value) => ev.stringify(&value),
        None => Ok(String::new()),
    }
}

/// Argument `i` as a string, or the context node's string-value when
/// absent: the default the string functions share.
fn string_arg_or_context(
    ev: &Ev<'_>,
    args: &[Expr],
    i: usize,
    c: Ctx,
) -> Result<String, EvalError> {
    match arg(ev, args, i, c)? {
        Some(value) => ev.stringify(&value),
        None => ev.node_string(c.node),
    }
}

/// Argument `i` as a number, or `NaN` when absent.
fn number_arg(
    ev: &Ev<'_>,
    args: &[Expr],
    i: usize,
    c: Ctx,
) -> Result<f64, EvalError> {
    match arg(ev, args, i, c)? {
        Some(value) => ev.number(&value),
        None => Ok(f64::NAN),
    }
}

/// Argument `i`, which `func` requires to be a node-set. `None` when
/// it is absent, or -- outside strict mode -- when it is something
/// else.
fn node_set_arg(
    ev: &Ev<'_>,
    func: Func,
    args: &[Expr],
    i: usize,
    c: Ctx,
) -> Result<Option<Vec<NodeId>>, EvalError> {
    match arg(ev, args, i, c)? {
        Some(Value::NodeSet(nodes)) => Ok(Some(nodes)),
        Some(other) => {
            let message = format!(
                "{}() takes a node-set, not a {}",
                func.name(),
                other.type_name()
            );
            type_error(ev, None, message)
        }
        None => Ok(None),
    }
}

/// The node a node-describing function should report on: the first
/// node of its argument node-set, or the context node when it takes no
/// argument. `None` when an argument was supplied but selected
/// nothing, which is not the same as having no argument at all.
fn node_argument(
    ev: &Ev<'_>,
    func: Func,
    args: &[Expr],
    c: Ctx,
) -> Result<Option<NodeId>, EvalError> {
    if args.is_empty() {
        return Ok(Some(c.node));
    }
    Ok(node_set_arg(ev, func, args, 0, c)?
        .and_then(|nodes| nodes.first().copied()))
}

fn node_set_function(
    ev: &Ev<'_>,
    func: Func,
    args: &[Expr],
    c: Ctx,
) -> Result<Value, EvalError> {
    match func {
        Func::Last => Ok(Value::Number(c.size as f64)),
        Func::Position => Ok(Value::Number(c.position as f64)),
        Func::Count => Ok(Value::Number(
            node_set_arg(ev, func, args, 0, c)?.map_or(0, |n| n.len()) as f64,
        )),
        Func::Id => id_function(ev, args, c),
        // The three name functions take an optional node-set: with
        // one, they describe its *first* node; without one, the
        // context node. Reading the context node unconditionally made
        // `local-name(//x)` answer about whatever the expression
        // happened to be evaluated from -- usually the document root,
        // which has no name, so the answer was always the empty
        // string.
        Func::LocalName => describe_node(ev, func, args, c, local_name),
        Func::NamespaceUri => describe_node(ev, func, args, c, namespace_uri),
        _ => describe_node(ev, func, args, c, qualified_name),
    }
}

/// A name function: `describe` applied to the node the argument (or
/// its absence) selects, and the empty string for a node without a
/// name or an argument that selects nothing.
fn describe_node(
    ev: &Ev<'_>,
    func: Func,
    args: &[Expr],
    c: Ctx,
    describe: fn(&Document, NodeId) -> Option<String>,
) -> Result<Value, EvalError> {
    let node = node_argument(ev, func, args, c)?;
    if node.is_some() {
        ev.budget.string(ev.budget.name_bytes)?;
    }
    Ok(Value::String(
        node.and_then(|n| describe(ev.doc, n)).unwrap_or_default(),
    ))
}

fn local_name(doc: &Document, id: NodeId) -> Option<String> {
    name_parts(doc, id).map(|(local, _)| local.to_owned())
}

fn namespace_uri(doc: &Document, id: NodeId) -> Option<String> {
    name_parts(doc, id).and_then(|(_, namespace)| namespace.map(str::to_owned))
}

/// `id()` takes a whitespace-separated list of IDs, not one ID. Given
/// a node-set it uses each node's string-value as such a list; given
/// anything else, its string-value. The result is a node-set, so it
/// comes back in document order and without duplicates.
fn id_function(ev: &Ev<'_>, args: &[Expr], c: Ctx) -> Result<Value, EvalError> {
    let mut out = Vec::new();
    match arg(ev, args, 0, c)? {
        Some(Value::NodeSet(nodes)) => {
            collect_node_ids(ev, &nodes, &mut out)?;
        }
        Some(other) => collect_ids(ev, &ev.stringify(&other)?, &mut out)?,
        None => {}
    }
    ev.sort(&mut out)?;
    Ok(Value::NodeSet(out))
}

fn collect_node_ids(
    ev: &Ev<'_>,
    nodes: &[NodeId],
    out: &mut Vec<NodeId>,
) -> Result<(), EvalError> {
    for &node in nodes {
        collect_ids(ev, &ev.node_string(node)?, out)?;
    }
    Ok(())
}

/// The elements whose ID is one of the XML-whitespace-separated
/// tokens of `list`.
fn collect_ids(
    ev: &Ev<'_>,
    list: &str,
    out: &mut Vec<NodeId>,
) -> Result<(), EvalError> {
    ev.budget.charge(
        list.len()
            .saturating_mul(super::budget::sort_factor(ev.doc.len())),
    )?;
    for token in list.split(is_xml_whitespace).filter(|t| !t.is_empty()) {
        if let Some(found) = ev.doc.element_by_id(token) {
            ev.budget.nodes(out.len().saturating_add(1))?;
            out.push(found);
        }
    }
    Ok(())
}

fn string_function(
    ev: &Ev<'_>,
    func: Func,
    args: &[Expr],
    c: Ctx,
) -> Result<Value, EvalError> {
    match func {
        Func::String => {
            Ok(Value::String(string_arg_or_context(ev, args, 0, c)?))
        }
        Func::Concat => concat(ev, args, c),
        _ => string_pair_function(ev, func, args, c),
    }
}

fn concat(ev: &Ev<'_>, args: &[Expr], c: Ctx) -> Result<Value, EvalError> {
    let mut s = String::new();
    for i in 0..args.len() {
        let part = string_arg(ev, args, i, c)?;
        ev.budget.string(part.len())?;
        s.push_str(&part);
    }
    Ok(Value::String(s))
}

/// The functions of two strings.
fn string_pair_function(
    ev: &Ev<'_>,
    func: Func,
    args: &[Expr],
    c: Ctx,
) -> Result<Value, EvalError> {
    let a = string_arg(ev, args, 0, c)?;
    let b = string_arg(ev, args, 1, c)?;
    if matches!(func, Func::SubstringBefore | Func::SubstringAfter) {
        ev.budget.string(a.len())?;
    }
    Ok(match func {
        Func::StartsWith => Value::Boolean(a.starts_with(&b)),
        Func::Contains => Value::Boolean(a.contains(&b)),
        Func::SubstringBefore => Value::String(substring_before(&a, &b)),
        _ => Value::String(substring_after(&a, &b)),
    })
}

/// Both are defined over *characters*, not bytes, but `find` returns
/// a byte offset and slicing at it is therefore safe: a match of
/// `needle` inside `haystack` always begins and ends on a character
/// boundary. No match is the empty string, not the whole.
fn substring_before(haystack: &str, needle: &str) -> String {
    haystack
        .find(needle)
        .map(|at| haystack[..at].to_owned())
        .unwrap_or_default()
}

fn substring_after(haystack: &str, needle: &str) -> String {
    haystack
        .find(needle)
        .map(|at| haystack[at + needle.len()..].to_owned())
        .unwrap_or_default()
}

fn positional_string_function(
    ev: &Ev<'_>,
    func: Func,
    args: &[Expr],
    c: Ctx,
) -> Result<Value, EvalError> {
    match func {
        Func::Substring => substring_call(ev, args, c),
        Func::StringLength => Ok(Value::Number(
            string_arg_or_context(ev, args, 0, c)?.chars().count() as f64,
        )),
        Func::NormalizeSpace => {
            let text = string_arg_or_context(ev, args, 0, c)?;
            ev.budget.string(text.len())?;
            ev.budget.charge(text.len().saturating_mul(3))?;
            Ok(Value::String(normalize_space(&text)))
        }
        _ => translate_call(ev, args, c),
    }
}

fn substring_call(
    ev: &Ev<'_>,
    args: &[Expr],
    c: Ctx,
) -> Result<Value, EvalError> {
    let s = string_arg(ev, args, 0, c)?;
    let start = number_arg(ev, args, 1, c)?;
    let length = arg(ev, args, 2, c)?.map(|v| ev.number(&v)).transpose()?;
    ev.budget.string(s.len())?;
    Ok(substring(&s, start, length))
}

fn translate_call(
    ev: &Ev<'_>,
    args: &[Expr],
    c: Ctx,
) -> Result<Value, EvalError> {
    let s = string_arg(ev, args, 0, c)?;
    let from = string_arg(ev, args, 1, c)?;
    let to = string_arg(ev, args, 2, c)?;
    ev.budget.string(s.len().saturating_mul(4))?;
    let bytes = s.len().saturating_add(from.len()).saturating_add(to.len());
    ev.budget
        .charge(bytes.saturating_mul(super::budget::sort_factor(from.len())))?;
    Ok(Value::String(translate(&s, &from, &to)))
}

/// Leading and trailing XML whitespace removed, and every run of it
/// inside collapsed to one space.
fn normalize_space(s: &str) -> String {
    s.split(is_xml_whitespace)
        .filter(|t| !t.is_empty())
        .collect::<Vec<_>>()
        .join(" ")
}

/// `translate(s, from, to)`: each character of `s` found in `from`
/// is replaced by the character at the same position in `to`, or
/// removed when `to` is shorter. A character repeated in `from` takes
/// its *first* position. The map is built once rather than searching
/// `from` per character.
fn translate(s: &str, from: &str, to: &str) -> String {
    let mut map: BTreeMap<char, Option<char>> = BTreeMap::new();
    let mut replacements = to.chars();
    for f in from.chars() {
        let replacement = replacements.next();
        let _ = map.entry(f).or_insert(replacement);
    }
    s.chars()
        .filter_map(|ch| map.get(&ch).map_or(Some(ch), |r| *r))
        .collect()
}

/// `substring(string, start, length?)`.
///
/// The specification defines the result by *position*, not by a start
/// and a count: it keeps every character whose 1-based position `p`
/// satisfies
///
/// ```text
/// p >= round(start)  and  p < round(start) + round(length)
/// ```
///
/// Clamping the start to 1 and then taking `length` characters is a
/// different function -- it gives `"123"` for the specification's own
/// example, `substring("12345", 0, 3)`, which must be `"12"`, because
/// positions 0 and below still consume part of the window.
fn substring(s: &str, start: f64, length: Option<f64>) -> Value {
    let start = xpath_round(start);
    let end = match length {
        Some(length) => {
            let length = xpath_round(length);
            if length.is_nan() || start.is_nan() {
                f64::NAN
            } else {
                start + length
            }
        }
        None => f64::INFINITY,
    };
    Value::String(
        s.chars()
            .enumerate()
            .filter(|(i, _)| {
                let p = *i as f64 + 1.0;
                p >= start && p < end
            })
            .map(|(_, c)| c)
            .collect(),
    )
}

fn boolean_function(
    ev: &Ev<'_>,
    func: Func,
    args: &[Expr],
    c: Ctx,
) -> Result<Value, EvalError> {
    Ok(Value::Boolean(match func {
        Func::True => true,
        Func::False => false,
        Func::Not => !truth_arg(ev, args, c)?,
        Func::Boolean => truth_arg(ev, args, c)?,
        _ => {
            ev.budget.charge(ev.budget.document_cost)?;
            lang_matches(ev.doc, c.node, &string_arg(ev, args, 0, c)?)
        }
    }))
}

/// The first argument's truth; false when absent.
fn truth_arg(ev: &Ev<'_>, args: &[Expr], c: Ctx) -> Result<bool, EvalError> {
    Ok(arg(ev, args, 0, c)?.is_some_and(|v| v.to_boolean()))
}

fn number_function(
    ev: &Ev<'_>,
    func: Func,
    args: &[Expr],
    c: Ctx,
) -> Result<Value, EvalError> {
    if func == Func::Sum {
        // Each node converts as `number()` does, so one node that is
        // not a number makes the sum `NaN` rather than being skipped.
        return sum_function(ev, args, c);
    }
    let n = if args.is_empty() {
        // `number()` with no argument converts the context node.
        xpath_number(&ev.node_string(c.node)?)
    } else {
        number_arg(ev, args, 0, c)?
    };
    Ok(Value::Number(match func {
        Func::Floor => float::floor(n),
        Func::Ceiling => float::ceil(n),
        Func::Round => xpath_round(n),
        _ => n,
    }))
}

fn sum_function(
    ev: &Ev<'_>,
    args: &[Expr],
    c: Ctx,
) -> Result<Value, EvalError> {
    let mut total = 0.0;
    for node in node_set_arg(ev, Func::Sum, args, 0, c)?.unwrap_or_default() {
        total += xpath_number(&ev.node_string(node)?);
    }
    Ok(Value::Number(total))
}