Check if a file appears to be binary by reading the first 8 KB and looking for null bytes.
(path: &Path)
| 105 | /// Check if a file appears to be binary by reading the first 8 KB and looking |
| 106 | /// for null bytes. |
| 107 | pub fn is_binary(path: &Path) -> bool { |
| 108 | let Ok(mut file) = fs::File::open(path) else { |
| 109 | return true; // can't read → treat as binary |
| 110 | }; |
| 111 | let mut buf = [0u8; 8192]; |
| 112 | let Ok(n) = file.read(&mut buf) else { |
| 113 | return true; |
| 114 | }; |
| 115 | buf[..n].contains(&0) |
| 116 | } |
| 117 | |
| 118 | /// Map a file extension to a human-readable language name. |
| 119 | pub fn detect_language(ext: &str) -> &'static str { |