Skip the optional encoding BOM at the start of a file if it exists. As of now, only the UTF-8 BOM is supported.
```rust use skip_bom::{BomType, SkipEncodingBom}; use std::io::{Cursor, Read};
// Read a stream after checking that it starts with the BOM const BOMBYTES: &'static [u8] = b"\xEF\xBB\xBFThis stream starts with a UTF-8 BOM."; let mut reader = SkipEncodingBom::new(Cursor::new(BOMBYTES)); asserteq!(Some(BomType::UTF8), reader.readbom().unwrap()); let mut string = Default::default(); let _ = reader.readtostring(&mut string).unwrap(); assert_eq!("This stream starts with a UTF-8 BOM.", &string);
// Read a stream without a starting BOM const NOBOMBYTES: &'static [u8] = b"This stream does not start with the UTF-8 BOM: \xEF\xBB\xBF."; let mut reader = SkipEncodingBom::new(Cursor::new(NOBOMBYTES)); asserteq!(None, reader.readbom().unwrap()); let mut buf = Default::default(); let _ = reader.readtoend(&mut buf).unwrap(); asserteq!(b"This stream does not start with the UTF-8 BOM: \xEF\xBB\xBF.", buf.asslice());
// Read a stream and disregard the starting BOM completely let mut reader = SkipEncodingBom::new(Cursor::new(BOMBYTES)); let mut buf = Default::default(); let _ = reader.readtoend(&mut buf).unwrap(); asserteq!(b"This stream starts with a UTF-8 BOM.", buf.asslice()); // Check the BOM after the read is over. asserteq!(Some(Some(BomType::UTF8)), reader.bom_found()); ```
Module documentation with examples.
This project is licensed under either of
at your option.