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

Method detect

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

Detect the encoding of the given content. This function analyzes the content to determine its encoding: 1. Checks for BOM (Byte Order Mark) 2. Checks for null bytes (indicates binary) 3. Validates as UTF-8 4. Falls back to Latin-1 for high bytes # Arguments `content` - The raw bytes to analyze # Returns The detected encoding. # Examples ```rust use atomic_core::change::Encoding; // UTF-8

(content: &[u8])

Source from the content-addressed store, hash-verified

106 /// assert_eq!(Encoding::detect(b"Hello\x00World"), Encoding::Binary);
107 /// ```
108 pub fn detect(content: &[u8]) -> Self {
109 // Check for BOM first
110 if let Some(encoding) = Self::detect_bom(content) {
111 return encoding;
112 }
113
114 // Check for null bytes (binary indicator)
115 if content.contains(&0) {
116 return Encoding::Binary;
117 }
118
119 // Try UTF-8 validation
120 if std::str::from_utf8(content).is_ok() {
121 return Encoding::Utf8;
122 }
123
124 // Contains high bytes but not valid UTF-8, assume Latin-1
125 Encoding::Latin1
126 }
127
128 /// Detect encoding from BOM (Byte Order Mark).
129 ///

Callers 2

runMethod · 0.45
runMethod · 0.45

Calls 1

containsMethod · 0.45

Tested by

no test coverage detected