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])
| 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 | /// |