Skip to main content

detect

Function detect 

Source
pub fn detect(bytes: &[u8]) -> Encoding
Expand description

Sniff the encoding of an XML document from its first bytes.

The algorithm:

  1. If a BOM is present, use it (currently only UTF-8 BOM is handled in Tier 1; UTF-16 BOMs return Encoding::Other until we add UTF-16).
  2. Otherwise, look for a <?xml ... encoding="..."?> declaration in the first ~200 bytes. The XML spec guarantees the declaration is in an ASCII-compatible form for every encoding the spec mentions, so this lookahead works without knowing the encoding yet.
  3. If neither is found, default to UTF-8.