fsearch/binary.rs
1// File: src\binary.rs
2// Author: Hadi Cahyadi <cumulus13@gmail.com>
3// Date: 2026-05-11
4// Description:
5// License: MIT
6
7use std::fs::File;
8use std::io::Read;
9use std::path::Path;
10
11/// Returns `true` when the file looks like binary content.
12///
13/// The heuristic reads the first `check_bytes` bytes of the file and checks:
14/// * whether a null byte (`\0`) is present — classic binary signal
15/// * whether the bytes fail UTF-8 decoding
16///
17/// Any IO error is treated conservatively as "binary" so the file is skipped.
18pub fn is_binary(path: &Path, check_bytes: usize) -> bool {
19 let mut file = match File::open(path) {
20 Ok(f) => f,
21 Err(_) => return true,
22 };
23
24 let mut buf = vec![0u8; check_bytes];
25 let n = match file.read(&mut buf) {
26 Ok(n) => n,
27 Err(_) => return true,
28 };
29
30 let chunk = &buf[..n];
31
32 // Null byte → almost certainly binary
33 if chunk.contains(&0u8) {
34 return true;
35 }
36
37 // Non-UTF-8 sequence → treat as binary
38 std::str::from_utf8(chunk).is_err()
39}