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

Function detect_encoding

atomic-core/src/record/workflow/compare.rs:239–251  ·  view source on GitHub ↗

Detect the encoding of content. Analyzes content to determine if it's valid UTF-8 text or binary data. # Arguments `content` - The content to analyze # Returns `Encoding::Utf8` for valid text, `Encoding::Binary` otherwise. # Algorithm Content is considered binary if: 1. It contains null bytes (0x00) 2. It's not valid UTF-8 # Example ```rust use atomic_core::record::workflow::compare::dete

(content: &[u8])

Source from the content-addressed store, hash-verified

237/// assert_eq!(detect_encoding(&[0x00, 0x01, 0x02]), Encoding::Binary);
238/// ```
239pub fn detect_encoding(content: &[u8]) -> Encoding {
240 // Check for null bytes (strong indicator of binary)
241 if content.contains(&0) {
242 return Encoding::Binary;
243 }
244
245 // Check for valid UTF-8
246 if std::str::from_utf8(content).is_ok() {
247 Encoding::Utf8
248 } else {
249 Encoding::Binary
250 }
251}
252
253/// Check if content is binary.
254///

Callers 5

compare_contentFunction · 0.70
record_added_fileFunction · 0.50
record_modified_fileFunction · 0.50

Calls 1

containsMethod · 0.45

Tested by 1