1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2025-06-30 22:23:44 +02:00
This commit is contained in:
Andrew Gallant
2016-03-30 22:24:59 -04:00
parent f1a91307cd
commit 07bff7409b
2 changed files with 20 additions and 7 deletions

View File

@ -9,6 +9,9 @@ extern crate rustc_serialize;
const USAGE: &'static str = "
Usage: rep [options] <pattern> [<file> ...]
Options:
-c, --count Suppress normal output and show count of matches.
";
use std::error::Error;
@ -32,6 +35,7 @@ pub type Result<T> = result::Result<T, Box<Error + Send + Sync>>;
struct Args {
arg_pattern: String,
arg_file: Vec<String>,
flag_count: bool,
}
fn main() {
@ -71,10 +75,15 @@ fn run_mmap(args: &Args, searcher: &LineSearcher) -> Result<u64> {
let mmap = try!(Mmap::open_path(&args.arg_file[0], Protection::Read));
let text = unsafe { mmap.as_slice() };
for m in searcher.search(text) {
try!(wtr.write(&text[m.start..m.end]));
try!(wtr.write(b"\n"));
if !args.flag_count {
try!(wtr.write(&text[m.start..m.end]));
try!(wtr.write(b"\n"));
}
count += 1;
}
if args.flag_count {
try!(writeln!(wtr, "{}", count));
}
Ok(count)
}