Skip to content

Commit

Permalink
Merge pull request #36 from epage/bin
Browse files Browse the repository at this point in the history
 feat: Ignore binary files
  • Loading branch information
epage authored Jul 14, 2019
2 parents 9a3aef7 + da156e3 commit 807a4a8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 58 deletions.
80 changes: 24 additions & 56 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ ignore = "0.4"
phf = { version = "0.7", features = ["unicase"] }
regex = "1.0"
lazy_static = "1.2.0"
grep-searcher = "0.1"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
itertools = "0.8"
unicase = "1.1"
bstr = "0.2"

[dev-dependencies]
assert_fs = "0.10"
Expand Down
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
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@ pub use crate::dict::*;
use std::fs::File;
use std::io::Read;

use bstr::ByteSlice;

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)?;
for (line_idx, line) in grep_searcher::LineIter::new(b'\n', &buffer).enumerate() {
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;
for ident in tokens::Identifier::parse(line) {
if !ignore_hex && is_hex(ident.token()) {
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 807a4a8

Please sign in to comment.