Skip to main content

strip_bom/
lib.rs

1///! use strip_bom::StripBom;
2///!
3///! let my_string: Vec<u8> = vec![0xefu8, 0xbb, 0xbf, 0xf0, 0x9f, 0x8d, 0xa3];
4///! let my_string: String = String::from_utf8(my_string).unwrap();
5///!
6///! // In this time, my_string has the BOM => true 🍣
7///! println!("{} {}", my_string.starts_with("\u{feff}"), &my_string);
8///!
9///! // Strip BOM
10///! let my_string: &str = my_string.strip_bom();
11///!
12///! // my_string (slice) has not the BOM => false 🍣
13///! println!("{} {}", my_string.starts_with("\u{feff}"), &my_string);
14
15pub trait StripBom
16{
17 fn strip_bom(&self) -> &str;
18}
19
20impl StripBom for str
21{
22 fn strip_bom(&self) -> &str
23 {
24  if self.starts_with("\u{feff}")
25  {
26   &self[3..]
27  }
28  else
29  {
30   &self[..]
31  }
32 }
33}
34
35impl StripBom for String
36{
37 fn strip_bom(&self) -> &str
38 {
39  &self[..].strip_bom()
40 }
41}