The search code is a mess, but...

... we now support inverted matches and line numbers!
This commit is contained in:
Andrew Gallant
2016-08-29 22:44:15 -04:00
parent c809679cf2
commit d011cea053
4 changed files with 196 additions and 27 deletions

View File

@@ -1,14 +1,18 @@
use std::io;
use std::path::Path;
use grep::Match;
macro_rules! wln {
($($tt:tt)*) => {
let _ = writeln!($($tt)*);
}
}
macro_rules! w {
($($tt:tt)*) => {
let _ = write!($($tt)*);
}
}
pub struct Printer<W> {
wtr: W,
}
@@ -40,15 +44,25 @@ impl<W: io::Write> Printer<W> {
&mut self,
path: P,
buf: &[u8],
m: &Match,
start: usize,
end: usize,
line_number: Option<u64>,
) {
let _ = self.wtr.write(path.as_ref().to_string_lossy().as_bytes());
let _ = self.wtr.write(b":");
let _ = self.wtr.write(&buf[m.start()..m.end()]);
let _ = self.wtr.write(b"\n");
self.write(path.as_ref().to_string_lossy().as_bytes());
self.write(b":");
if let Some(line_number) = line_number {
self.write(line_number.to_string().as_bytes());
self.write(b":");
}
self.write(&buf[start..end]);
self.write(b"\n");
}
pub fn binary_matched<P: AsRef<Path>>(&mut self, path: P) {
wln!(&mut self.wtr, "binary file {} matches", path.as_ref().display());
}
fn write(&mut self, buf: &[u8]) {
let _ = self.wtr.write_all(buf);
}
}