Files
rgs/src/printer.rs
Andrew Gallant c809679cf2 Lots of improvements. Most notably, removal of memory maps for searching.
Memory maps appear to degrade quite a bit in the presence of multithreading.

Also, switch to lock free data structures for synchronization. Give each
worker an input and output buffer which require no synchronization.
2016-08-28 20:18:34 -04:00

55 lines
1.2 KiB
Rust

use std::io;
use std::path::Path;
use grep::Match;
macro_rules! wln {
($($tt:tt)*) => {
let _ = writeln!($($tt)*);
}
}
pub struct Printer<W> {
wtr: W,
}
impl<W: io::Write> Printer<W> {
pub fn new(wtr: W) -> Printer<W> {
Printer {
wtr: wtr,
}
}
pub fn into_inner(self) -> W {
self.wtr
}
pub fn path<P: AsRef<Path>>(&mut self, path: P) {
wln!(&mut self.wtr, "{}", path.as_ref().display());
}
pub fn path_count<P: AsRef<Path>>(&mut self, path: P, count: u64) {
wln!(&mut self.wtr, "{}:{}", path.as_ref().display(), count);
}
pub fn count(&mut self, count: u64) {
wln!(&mut self.wtr, "{}", count);
}
pub fn matched<P: AsRef<Path>>(
&mut self,
path: P,
buf: &[u8],
m: &Match,
) {
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");
}
pub fn binary_matched<P: AsRef<Path>>(&mut self, path: P) {
wln!(&mut self.wtr, "binary file {} matches", path.as_ref().display());
}
}