Skip to main content

detect_bom

Function detect_bom 

Source
pub fn detect_bom(bytes: &[u8]) -> Option<(Encoding, usize)>
Expand description

Detect a byte-order mark (BOM) at the start of bytes, one of the three signatures relevant to the encodings this crate supports (see XML 1.0 Appendix F.1):

BytesEncoding
EF BB BFEncoding::Utf8
FE FFEncoding::Utf16Be
FF FEEncoding::Utf16Le

Returns the detected encoding together with the number of leading bytes that make up the BOM, so a caller can skip exactly that many bytes before decoding the rest of the stream. Returns None if bytes does not start with any of the three signatures (this includes streams that are simply too short to contain one).

ยงExamples

use oxixml_encoding::{detect_bom, Encoding};

assert_eq!(detect_bom(&[0xEF, 0xBB, 0xBF, b'<']), Some((Encoding::Utf8, 3)));
assert_eq!(detect_bom(&[0xFF, 0xFE, b'<', 0x00]), Some((Encoding::Utf16Le, 2)));
assert_eq!(detect_bom(b"<?xml"), None);