Skip to content

Commit 23babfe

Browse files
committed
now PATTERN is in front of FILE and handle the print-offset option
1 parent cb57517 commit 23babfe

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/lib.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ pub fn setup_args() -> ArgMatches {
1717

1818
command!()
1919
.arg(
20-
Ascii strings should be passed inside quotes like so '\"This is a string\"'
20+
arg!(<PATTERN> "Ascii strings should be passed inside quotes like so '\"This is a string\"'
2121
Escaping quotes '\"This is a \\\"quoted string\\\"\"'
22-
All of these byte sequence are valid: f9b4ca, F9B4CA and f9B4Ca")
22+
All of these byte sequence are valid: f9b4ca, F9B4CA and f9B4Ca"
23+
),
2324
)
25+
.arg(arg!(<FILE> ... "The filepath"))
2426
.arg(
2527
arg!(-f <filetype> ... "Filter the search by the file extensions.
2628
Examples of input: jpg, mp3, exe")
@@ -72,29 +74,32 @@ pub fn run(args: ArgMatches) {
7274
}),
7375
};
7476

75-
let context_bytes_size: usize = args
76-
.value_of("context_bytes_size")
77-
.unwrap()
78-
.parse()
79-
.unwrap();
77+
let context_bytes_size: usize = args.value_of("context_bytes_size").unwrap().parse().unwrap();
8078
let skip_bytes: u64 = args.value_of("skip_bytes").unwrap().parse().unwrap();
8179

8280
for filename in files {
8381
let mut searcher = search::Searcher::new(&pattern, context_bytes_size, skip_bytes);
8482
let filename = filename.to_str().unwrap();
8583

86-
searcher.search_in_file(filename).unwrap_or_else(|error| {
84+
let result = searcher.search_in_file(filename).unwrap_or_else(|error| {
8785
eprintln!("{}: {}", filename, error);
8886
process::exit(1);
8987
});
9088

91-
let result = searcher.result();
9289
if !result.is_empty() {
9390
println!("{}", Colour::Purple.paint(filename));
9491
}
9592

93+
if args.is_present("print_offset") {
94+
result
95+
.iter()
96+
.map(|_match| _match.offset)
97+
.for_each(|offset| println!("0x{:08X}", offset));
98+
return;
99+
}
100+
96101
if !args.is_present("print_only") {
97-
print_hexdump_output(result, searcher.context_bytes_size());
102+
print_hexdump_output(&result, context_bytes_size);
98103
}
99104
}
100105
}

src/utils/search.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::collections::BTreeMap;
22
use std::ops::Range;
33

4-
54
use super::file::read_file_and;
65

76
#[derive(Debug, Clone)]
@@ -26,8 +25,6 @@ impl<'a> Searcher<'a> {
2625
}
2726
}
2827

29-
30-
3128
pub fn search_in_file(&mut self, filepath: &str) -> std::io::Result<Vec<Match>> {
3229
let mut matches = Vec::new();
3330
read_file_and(filepath, self.skip_bytes, |buffer, pos_in_file| {

0 commit comments

Comments
 (0)