Skip to main content

decode

Function decode 

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

The text of a document given as bytes, in UTF-8, without its byte order mark: the bytes themselves when they are UTF-8 (no copy), decoded otherwise.

The encoding is found as XML 1.0 (4.3.3, Appendix F) says and as the reference parsers find it: a UTF-16 byte order mark, or <? in UTF-16 at the start, gives UTF-16 in that byte order, and the XML declaration may then name UTF-16 or that byte order; otherwise the encoding is the one the XML declaration names, UTF-8 without one (after a UTF-8 byte order mark too, the declaration giving the encoding of what follows, as both reference parsers read it). See Encoding for the encodings read, and encoding_names for their names.

ยงErrors

Error::UnsupportedEncoding for an encoding not read (named by the XML declaration, or UCS-4 and EBCDIC by the first bytes); Error::EncodingMismatch when the XML declaration names UTF-16 in a document that is not (no UTF-16 byte order mark nor <? in UTF-16), or another encoding in a document that is; Error::InvalidEncodedBytes for bytes that are not a character of the encoding (in UTF-16, an unpaired surrogate or an odd number of bytes; in windows-1252, one of the five bytes that are no character), at the position of the character they would be in the text; Error::UnknownToken at 1:1 for a text that starts with U+FEFF after the byte order mark (a second byte order mark), which the Reader would otherwise skip.