Skip to main content

sniff_xml_encoding

Function sniff_xml_encoding 

Source
pub fn sniff_xml_encoding(bytes: &[u8]) -> SniffResult
Expand description

Sniff the encoding of an XML entity from its leading bytes, per XML 1.0 Appendix F.

See the module-level documentation for the two-stage algorithm this implements, and SniffResult for how to interpret the result.

ยงExamples

use oxixml_encoding::{sniff_xml_encoding, ByteOrderGuess, Encoding};

// Appendix F pattern: "<?xm" as UTF-16BE with no BOM.
let utf16be_prologue: &[u8] = &[0x00, b'<', 0x00, b'?', 0x00, b'x', 0x00, b'm'];
let result = sniff_xml_encoding(utf16be_prologue);
assert_eq!(result.byte_order_guess, Some(ByteOrderGuess::Utf16Be));
assert_eq!(result.bom_len, 0);

// A UTF-8 BOM plus an explicit (if redundant) encoding declaration.
let mut with_bom = vec![0xEF, 0xBB, 0xBF];
with_bom.extend_from_slice(b"<?xml version=\"1.0\" encoding=\"UTF-8\"?><doc/>");
let result = sniff_xml_encoding(&with_bom);
assert_eq!(result.bom_len, 3);
assert_eq!(result.declared_encoding, Some(Encoding::Utf8));