tinyxml2 0.1.13

A ground-up Rust implementation of the TinyXML2 API — behavioral compatibility, idiomatic internals
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
//! XML entity encoding and decoding.
//!
//! Handles the five predefined XML entities and numeric character references:
//!
//! | Entity | Character | Code Point |
//! |--------|-----------|------------|
//! | `&` | `&` | U+0026 |
//! | `&lt;` | `<` | U+003C |
//! | `&gt;` | `>` | U+003E |
//! | `&quot;` | `"` | U+0022 |
//! | `&apos;` | `'` | U+0027 |
//!
//! Numeric character references (`&#123;` and `&#x7B;`) are also supported
//! for decoding.
//!
//! # Compatibility with TinyXML2
//!
//! Entity handling matches TinyXML2 exactly:
//! - Only the 5 predefined XML entities are recognized as named entities
//! - Numeric references support both decimal (`&#N;`) and hexadecimal (`&#xN;`)
//! - Invalid numeric references (e.g., `&#0;`, `&#x110000;`) are left as-is
//! - Entity processing can be disabled via `ParseOptions::process_entities`

/// Decodes XML entities in a string, replacing entity references with their
/// corresponding characters.
///
/// Handles:
/// - Named entities: `&amp;`, `&lt;`, `&gt;`, `&quot;`, `&apos;`
/// - Decimal numeric references: `&#123;`
/// - Hexadecimal numeric references: `&#x7B;`
///
/// Invalid or unrecognized entities are left unchanged in the output.
///
/// # Examples
///
/// ```
/// use tinyxml2::entity::decode;
///
/// assert_eq!(decode("Hello &amp; World"), "Hello & World");
/// assert_eq!(decode("a &lt; b &gt; c"), "a < b > c");
/// assert_eq!(decode("&#65;"), "A");
/// assert_eq!(decode("&#x41;"), "A");
/// assert_eq!(decode("&unknown;"), "&unknown;");
/// ```
pub fn decode(input: &str) -> String {
    let mut output = String::with_capacity(input.len());
    let chars = input.as_bytes();
    let mut i = 0;

    while i < chars.len() {
        if chars[i] == b'&' {
            if let Some((decoded_char, consumed)) = try_decode_entity(&chars[i..]) {
                output.push(decoded_char);
                i += consumed;
                continue;
            }
        }
        output.push(chars[i] as char);
        i += 1;
    }

    output
}

/// Decodes entities in place, returning a `Cow` that avoids allocation when
/// no entities are present.
///
/// This is the preferred decoding function for performance-sensitive paths.
///
/// # Examples
///
/// ```
/// use tinyxml2::entity::decode_cow;
/// use std::borrow::Cow;
///
/// // No entities — returns borrowed reference (no allocation)
/// let result = decode_cow("Hello World");
/// assert!(matches!(result, Cow::Borrowed(_)));
///
/// // With entities — returns owned string
/// let result = decode_cow("a &amp; b");
/// assert_eq!(result, "a & b");
/// ```
pub fn decode_cow(input: &str) -> std::borrow::Cow<'_, str> {
    if !input.contains('&') {
        return std::borrow::Cow::Borrowed(input);
    }
    std::borrow::Cow::Owned(decode(input))
}

/// Decodes ONLY numeric character references in a string, leaving named entities (like `&amp;`) as-is.
///
/// This is used when entity processing is disabled (`ParseOptions::process_entities = false`),
/// in which case TinyXML2 still decodes numeric character references.
#[must_use]
pub fn decode_numeric_only(input: &str) -> String {
    let mut output = String::with_capacity(input.len());
    let chars = input.as_bytes();
    let mut i = 0;

    while i < chars.len() {
        if chars[i] == b'&' {
            if let Some((decoded_char, consumed)) = try_decode_numeric_entity(&chars[i..]) {
                output.push(decoded_char);
                i += consumed;
                continue;
            }
        }
        output.push(chars[i] as char);
        i += 1;
    }

    output
}

/// Attempts to decode a single numeric entity reference starting at the given byte slice.
fn try_decode_numeric_entity(bytes: &[u8]) -> Option<(char, usize)> {
    debug_assert!(bytes[0] == b'&');

    // Find the semicolon
    let semi_pos = bytes.iter().position(|&b| b == b';')?;

    // Must have at least `&#N;` (4 bytes)
    if semi_pos < 3 {
        return None;
    }

    let entity_body = &bytes[1..semi_pos];
    let consumed = semi_pos + 1;

    // Check numeric references only
    if entity_body.first() == Some(&b'#') {
        let num_body = &entity_body[1..];
        let code_point = if num_body.first() == Some(&b'x') || num_body.first() == Some(&b'X') {
            // Hexadecimal: &#xNN;
            let hex_str = std::str::from_utf8(&num_body[1..]).ok()?;
            u32::from_str_radix(hex_str, 16).ok()?
        } else {
            // Decimal: &#NN;
            let dec_str = std::str::from_utf8(num_body).ok()?;
            dec_str.parse::<u32>().ok()?
        };

        // Validate: must be a valid Unicode scalar value and not U+0000
        if code_point == 0 {
            return None;
        }
        let c = char::from_u32(code_point)?;
        return Some((c, consumed));
    }

    None
}

/// Attempts to decode a single entity reference starting at the given byte slice.
///
/// Returns `Some((char, bytes_consumed))` if a valid entity was decoded,
/// or `None` if the input does not start with a valid entity reference.
fn try_decode_entity(bytes: &[u8]) -> Option<(char, usize)> {
    debug_assert!(bytes[0] == b'&');

    // Find the semicolon
    let semi_pos = bytes.iter().position(|&b| b == b';')?;

    // Must have at least `&X;` (3 bytes)
    if semi_pos < 2 {
        return None;
    }

    let entity_body = &bytes[1..semi_pos];
    let consumed = semi_pos + 1;

    // Check named entities first
    match entity_body {
        b"amp" => return Some(('&', consumed)),
        b"lt" => return Some(('<', consumed)),
        b"gt" => return Some(('>', consumed)),
        b"quot" => return Some(('"', consumed)),
        b"apos" => return Some(('\'', consumed)),
        _ => {}
    }

    // Check numeric references
    try_decode_numeric_entity(bytes)
}

/// Encodes special XML characters in text content.
///
/// Replaces `&`, `<`, `>` with their entity equivalents. This is the encoding
/// used for element text content (not attribute values).
///
/// # Examples
///
/// ```
/// use tinyxml2::entity::encode_text;
///
/// assert_eq!(encode_text("a < b & c > d"), "a &lt; b &amp; c &gt; d");
/// assert_eq!(encode_text("no special chars"), "no special chars");
/// ```
pub fn encode_text(input: &str) -> std::borrow::Cow<'_, str> {
    if !input.contains(['&', '<', '>']) {
        return std::borrow::Cow::Borrowed(input);
    }

    let mut output = String::with_capacity(input.len() + input.len() / 8);
    for ch in input.chars() {
        match ch {
            '&' => output.push_str("&amp;"),
            '<' => output.push_str("&lt;"),
            '>' => output.push_str("&gt;"),
            other => output.push(other),
        }
    }
    std::borrow::Cow::Owned(output)
}

/// Encodes special XML characters in attribute values.
///
/// Replaces `&`, `<`, `>`, `"`, and `'` with their entity equivalents.
/// Attribute values require more escaping than text content because they
/// appear inside quoted strings.
///
/// # Examples
///
/// ```
/// use tinyxml2::entity::encode_attribute;
///
/// assert_eq!(
///     encode_attribute("value with \"quotes\" & 'apostrophes'"),
///     "value with &quot;quotes&quot; &amp; &apos;apostrophes&apos;"
/// );
/// ```
pub fn encode_attribute(input: &str) -> std::borrow::Cow<'_, str> {
    if !input.contains(['&', '<', '>', '"', '\'']) {
        return std::borrow::Cow::Borrowed(input);
    }

    let mut output = String::with_capacity(input.len() + input.len() / 4);
    for ch in input.chars() {
        match ch {
            '&' => output.push_str("&amp;"),
            '<' => output.push_str("&lt;"),
            '>' => output.push_str("&gt;"),
            '"' => output.push_str("&quot;"),
            '\'' => output.push_str("&apos;"),
            other => output.push(other),
        }
    }
    std::borrow::Cow::Owned(output)
}

#[cfg(test)]
mod tests {
    use super::*;

    // ---- Decode tests ----

    #[test]
    fn decode_named_entities() {
        assert_eq!(decode("&amp;"), "&");
        assert_eq!(decode("&lt;"), "<");
        assert_eq!(decode("&gt;"), ">");
        assert_eq!(decode("&quot;"), "\"");
        assert_eq!(decode("&apos;"), "'");
    }

    #[test]
    fn decode_multiple_entities() {
        assert_eq!(decode("a &amp; b &lt; c"), "a & b < c");
        assert_eq!(decode("&lt;&gt;&amp;"), "<>&");
    }

    #[test]
    fn decode_no_entities() {
        assert_eq!(decode("hello world"), "hello world");
        assert_eq!(decode(""), "");
    }

    #[test]
    fn decode_decimal_numeric() {
        assert_eq!(decode("&#65;"), "A");
        assert_eq!(decode("&#97;"), "a");
        assert_eq!(decode("&#8364;"), "");
        assert_eq!(decode("&#128512;"), "😀");
    }

    #[test]
    fn decode_hex_numeric() {
        assert_eq!(decode("&#x41;"), "A");
        assert_eq!(decode("&#x61;"), "a");
        assert_eq!(decode("&#x20AC;"), "");
        assert_eq!(decode("&#x1F600;"), "😀");
        // Uppercase X
        assert_eq!(decode("&#X41;"), "A");
    }

    #[test]
    fn decode_invalid_numeric_left_unchanged() {
        // U+0000 is not a valid XML character
        assert_eq!(decode("&#0;"), "&#0;");
        // Beyond Unicode range
        assert_eq!(decode("&#x110000;"), "&#x110000;");
        // Empty numeric
        assert_eq!(decode("&#;"), "&#;");
    }

    #[test]
    fn decode_unknown_entity_left_unchanged() {
        assert_eq!(decode("&unknown;"), "&unknown;");
        assert_eq!(decode("&nbsp;"), "&nbsp;");
    }

    #[test]
    fn decode_ampersand_without_semicolon() {
        assert_eq!(decode("a & b"), "a & b");
        assert_eq!(decode("&"), "&");
        assert_eq!(decode("&&"), "&&");
    }

    #[test]
    fn decode_nested_entity_reference() {
        // &amp;amp; should decode to &amp; (one level of decoding)
        assert_eq!(decode("&amp;amp;"), "&amp;");
    }

    #[test]
    fn decode_entity_at_end() {
        assert_eq!(decode("text&amp;"), "text&");
    }

    #[test]
    fn decode_cow_borrows_when_no_entities() {
        let result = decode_cow("no entities here");
        assert!(matches!(result, std::borrow::Cow::Borrowed(_)));
    }

    #[test]
    fn decode_cow_owns_when_entities_present() {
        let result = decode_cow("has &amp; entity");
        assert!(matches!(result, std::borrow::Cow::Owned(_)));
        assert_eq!(result, "has & entity");
    }

    // ---- Encode tests ----

    #[test]
    fn encode_text_special_chars() {
        assert_eq!(encode_text("a & b"), "a &amp; b");
        assert_eq!(encode_text("a < b"), "a &lt; b");
        assert_eq!(encode_text("a > b"), "a &gt; b");
        assert_eq!(encode_text("a < b & c > d"), "a &lt; b &amp; c &gt; d");
    }

    #[test]
    fn encode_text_no_special_chars() {
        let result = encode_text("hello world");
        assert!(matches!(result, std::borrow::Cow::Borrowed(_)));
    }

    #[test]
    fn encode_text_preserves_quotes() {
        // Text content does NOT need to escape quotes
        assert_eq!(encode_text("he said \"hello\""), "he said \"hello\"");
    }

    #[test]
    fn encode_attribute_all_special_chars() {
        assert_eq!(encode_attribute("&"), "&amp;");
        assert_eq!(encode_attribute("<"), "&lt;");
        assert_eq!(encode_attribute(">"), "&gt;");
        assert_eq!(encode_attribute("\""), "&quot;");
        assert_eq!(encode_attribute("'"), "&apos;");
    }

    #[test]
    fn encode_attribute_no_special_chars() {
        let result = encode_attribute("hello world");
        assert!(matches!(result, std::borrow::Cow::Borrowed(_)));
    }

    #[test]
    fn encode_attribute_mixed() {
        assert_eq!(
            encode_attribute("val=\"a & b\""),
            "val=&quot;a &amp; b&quot;"
        );
    }

    // ---- Round-trip tests ----

    #[test]
    fn roundtrip_text() {
        let original = "a < b & c > d";
        let encoded = encode_text(original);
        let decoded = decode(&encoded);
        assert_eq!(decoded, original);
    }

    #[test]
    fn roundtrip_attribute() {
        let original = "he said \"it's < 5 & > 3\"";
        let encoded = encode_attribute(original);
        let decoded = decode(&encoded);
        assert_eq!(decoded, original);
    }

    #[test]
    fn roundtrip_empty() {
        assert_eq!(decode(&encode_text("")), "");
        assert_eq!(decode(&encode_attribute("")), "");
    }

    #[test]
    fn roundtrip_no_special_chars() {
        let s = "hello world 12345";
        assert_eq!(decode(&encode_text(s)), s);
        assert_eq!(decode(&encode_attribute(s)), s);
    }

    #[test]
    fn roundtrip_all_entities() {
        let original = "&<>\"'";
        let encoded = encode_attribute(original);
        assert_eq!(encoded, "&amp;&lt;&gt;&quot;&apos;");
        let decoded = decode(&encoded);
        assert_eq!(decoded, original);
    }

    #[test]
    fn decode_numeric_only_behavior() {
        assert_eq!(decode_numeric_only("a &amp; b"), "a &amp; b");
        assert_eq!(decode_numeric_only("a &#65; b"), "a A b");
        assert_eq!(decode_numeric_only("a &#x41; b"), "a A b");
        assert_eq!(decode_numeric_only("a &#x1F600; &amp;"), "a 😀 &amp;");
    }
}