feat: Ignore binary files

Fixes #29
This commit is contained in:
Ed Page 2019-07-13 20:14:06 -06:00
parent 4ce7303fc2
commit da156e3f23
3 changed files with 27 additions and 0 deletions

View file

@ -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,
)
});

View file

@ -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;

View file

@ -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<bool> {
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<bool> {
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(),
)?;
}