Skip to main content

transcode_to_utf8_strict

Function transcode_to_utf8_strict 

Source
pub fn transcode_to_utf8_strict(bytes: &[u8]) -> Result<Cow<'_, [u8]>>
Expand description

Like transcode_to_utf8 but also verifies that the inner XML declaration’s encoding="X" attribute agrees with the encoding detected from the byte stream. Catches malformed documents like a UTF-8 BOM paired with encoding="ISO-8859-1" or a UTF-16 BOM paired with encoding="UTF-8".

Use this when consuming untrusted XML from external sources. Synthetic re-encoded fixtures (e.g. a UTF-8 doc re-encoded to UTF-16 keeping its encoding="UTF-8" declaration) will fail this check — use the lenient transcode_to_utf8 for those.