diff --git a/benches/file.rs b/benches/file.rs index 1520f1b..6656701 100644 --- a/benches/file.rs +++ b/benches/file.rs @@ -18,6 +18,7 @@ fn process_empty(b: &mut test::Bencher) { sample_path.path(), &corrections, true, + false, typos::report::print_silent, ) }); @@ -37,6 +38,7 @@ fn process_no_tokens(b: &mut test::Bencher) { sample_path.path(), &corrections, true, + false, typos::report::print_silent, ) }); @@ -56,6 +58,7 @@ fn process_single_token(b: &mut test::Bencher) { sample_path.path(), &corrections, true, + false, typos::report::print_silent, ) }); @@ -75,6 +78,7 @@ fn process_sherlock(b: &mut test::Bencher) { sample_path.path(), &corrections, true, + false, typos::report::print_silent, ) }); @@ -94,6 +98,7 @@ fn process_code(b: &mut test::Bencher) { sample_path.path(), &corrections, true, + false, typos::report::print_silent, ) }); @@ -113,6 +118,7 @@ fn process_corpus(b: &mut test::Bencher) { sample_path.path(), &corrections, true, + false, typos::report::print_silent, ) }); diff --git a/src/lib.rs b/src/lib.rs index ae33b11..f46af98 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,10 +18,14 @@ pub fn process_file( path: &std::path::Path, dictionary: &Dictionary, ignore_hex: bool, + binary: bool, report: report::Report, ) -> Result<(), failure::Error> { let mut buffer = Vec::new(); File::open(path)?.read_to_end(&mut buffer)?; + if !binary && buffer.find_byte(b'\0').is_some() { + return Ok(()); + } for (line_idx, line) in buffer.lines().enumerate() { let line_num = line_idx + 1; diff --git a/src/main.rs b/src/main.rs index e5d3ef8..38f025a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,6 +55,12 @@ struct Options { /// The approximate number of threads to use. threads: usize, + #[structopt(long, raw(overrides_with = r#""no-binary""#))] + /// Search binary files. + binary: bool, + #[structopt(long, raw(overrides_with = r#""binary""#), raw(hidden = "true"))] + no_binary: bool, + #[structopt(long, raw(overrides_with = r#""no-hidden""#))] /// Search hidden files and directories. hidden: bool, @@ -118,6 +124,15 @@ impl Options { } } + pub fn binary(&self) -> Option { + match (self.binary, self.no_binary) { + (true, false) => Some(true), + (false, true) => Some(false), + (false, false) => None, + (_, _) => unreachable!("StructOpt should make this impossible"), + } + } + pub fn ignore_hidden(&self) -> Option { match (self.hidden, self.no_hidden) { (true, false) => Some(false), @@ -183,6 +198,7 @@ fn run() -> Result<(), failure::Error> { let dictionary = typos::Dictionary::new(); let ignore_hex = options.ignore_hex().unwrap_or(true); + let binary = options.binary().unwrap_or(false); let first_path = &options .path @@ -207,6 +223,7 @@ fn run() -> Result<(), failure::Error> { entry.path(), &dictionary, ignore_hex, + binary, options.format.report(), )?; }