Detect the encoding of content. # Arguments `content` - The content to analyze # Returns The detected encoding.
(content: &[u8])
| 742 | /// |
| 743 | /// The detected encoding. |
| 744 | pub fn detect_encoding(content: &[u8]) -> Encoding { |
| 745 | // Check for UTF-8 validity |
| 746 | if std::str::from_utf8(content).is_ok() { |
| 747 | // Check for null bytes (binary indicator even in valid UTF-8) |
| 748 | if content.contains(&0) { |
| 749 | Encoding::Binary |
| 750 | } else { |
| 751 | Encoding::Utf8 |
| 752 | } |
| 753 | } else { |
| 754 | Encoding::Binary |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | /// Check if content is binary. |
| 759 | /// |