Check if content is binary. # Arguments `content` - The content to check # Returns `true` if the content appears to be binary.
(content: &[u8])
| 765 | /// |
| 766 | /// `true` if the content appears to be binary. |
| 767 | pub fn is_binary_content(content: &[u8]) -> bool { |
| 768 | // Quick check: null bytes indicate binary |
| 769 | if content.contains(&0) { |
| 770 | return true; |
| 771 | } |
| 772 | |
| 773 | // Check for high ratio of non-printable characters |
| 774 | let non_printable = content |
| 775 | .iter() |
| 776 | .filter(|&&b| b < 0x20 && b != b'\n' && b != b'\r' && b != b'\t') |
| 777 | .count(); |
| 778 | |
| 779 | // If more than 30% non-printable, consider binary |
| 780 | if !content.is_empty() && non_printable * 100 / content.len() > 30 { |
| 781 | return true; |
| 782 | } |
| 783 | |
| 784 | false |
| 785 | } |
| 786 | |
| 787 | // TESTS |
| 788 |