Skip to content

Commit

Permalink
feat: Ignore binary files
Browse files Browse the repository at this point in the history
Fixes #29
  • Loading branch information
epage committed Jul 14, 2019
1 parent 4ce7303 commit da156e3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions benches/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn process_empty(b: &mut test::Bencher) {
sample_path.path(),
&corrections,
true,
false,
typos::report::print_silent,
)
});
Expand All @@ -37,6 +38,7 @@ fn process_no_tokens(b: &mut test::Bencher) {
sample_path.path(),
&corrections,
true,
false,
typos::report::print_silent,
)
});
Expand All @@ -56,6 +58,7 @@ fn process_single_token(b: &mut test::Bencher) {
sample_path.path(),
&corrections,
true,
false,
typos::report::print_silent,
)
});
Expand All @@ -75,6 +78,7 @@ fn process_sherlock(b: &mut test::Bencher) {
sample_path.path(),
&corrections,
true,
false,
typos::report::print_silent,
)
});
Expand All @@ -94,6 +98,7 @@ fn process_code(b: &mut test::Bencher) {
sample_path.path(),
&corrections,
true,
false,
typos::report::print_silent,
)
});
Expand All @@ -113,6 +118,7 @@ fn process_corpus(b: &mut test::Bencher) {
sample_path.path(),
&corrections,
true,
false,
typos::report::print_silent,
)
});
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand All @@ -207,6 +223,7 @@ fn run() -> Result<(), failure::Error> {
entry.path(),
&dictionary,
ignore_hex,
binary,
options.format.report(),
)?;
}
Expand Down

0 comments on commit da156e3

Please sign in to comment.