MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / detect_bom

Method detect_bom

atomic-core/src/change/encoding.rs:131–152  ·  view source on GitHub ↗

Detect encoding from BOM (Byte Order Mark). Returns `Some(encoding)` if a BOM is detected, `None` otherwise.

(content: &[u8])

Source from the content-addressed store, hash-verified

129 ///
130 /// Returns `Some(encoding)` if a BOM is detected, `None` otherwise.
131 fn detect_bom(content: &[u8]) -> Option<Self> {
132 if content.len() < 2 {
133 return None;
134 }
135
136 // UTF-8 BOM: EF BB BF
137 if content.len() >= 3 && content[0] == 0xEF && content[1] == 0xBB && content[2] == 0xBF {
138 return Some(Encoding::Utf8);
139 }
140
141 // UTF-16 LE BOM: FF FE
142 if content[0] == 0xFF && content[1] == 0xFE {
143 return Some(Encoding::Utf16Le);
144 }
145
146 // UTF-16 BE BOM: FE FF
147 if content[0] == 0xFE && content[1] == 0xFF {
148 return Some(Encoding::Utf16Be);
149 }
150
151 None
152 }
153
154 /// Check if this encoding represents text (not binary).
155 ///

Callers

nothing calls this directly

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected