1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2024-12-12 19:18:24 +02:00
ripgrep/src/main.rs

136 lines
3.4 KiB
Rust
Raw Normal View History

2016-03-11 03:48:44 +02:00
#![allow(dead_code, unused_variables)]
2016-02-27 18:07:26 +02:00
extern crate docopt;
2016-03-11 03:48:44 +02:00
extern crate memchr;
extern crate memmap;
2016-02-27 18:07:26 +02:00
extern crate regex;
2016-03-11 03:48:44 +02:00
extern crate regex_syntax as syntax;
2016-02-27 18:07:26 +02:00
extern crate rustc_serialize;
const USAGE: &'static str = "
Usage: rep [options] <pattern> [<file> ...]
2016-03-31 04:24:59 +02:00
Options:
-c, --count Suppress normal output and show count of matches.
2016-02-27 18:07:26 +02:00
";
use std::error::Error;
use std::io::{self, BufRead, Write};
use std::process;
use std::result;
use docopt::Docopt;
2016-03-11 03:48:44 +02:00
use regex::bytes::Regex;
2016-02-27 18:07:26 +02:00
2016-03-29 02:07:25 +02:00
use literals::LiteralSets;
2016-03-30 03:21:34 +02:00
use search::{LineSearcher, LineSearcherBuilder};
2016-03-29 02:07:25 +02:00
mod literals;
2016-03-11 03:48:44 +02:00
mod nonl;
2016-03-30 03:21:34 +02:00
mod search;
2016-03-11 03:48:44 +02:00
pub type Result<T> = result::Result<T, Box<Error + Send + Sync>>;
2016-02-27 18:07:26 +02:00
#[derive(RustcDecodable)]
struct Args {
arg_pattern: String,
arg_file: Vec<String>,
2016-03-31 04:24:59 +02:00
flag_count: bool,
2016-02-27 18:07:26 +02:00
}
fn main() {
let args = Docopt::new(USAGE).and_then(|d| d.decode())
.unwrap_or_else(|e| e.exit());
match run(&args) {
Ok(count) if count == 0 => process::exit(1),
Ok(_) => process::exit(0),
Err(err) => {
let _ = writeln!(&mut io::stderr(), "{}", err);
process::exit(1);
}
}
}
fn run(args: &Args) -> Result<u64> {
2016-03-11 03:48:44 +02:00
if args.arg_file.is_empty() {
2016-03-30 03:21:34 +02:00
let expr = try!(parse(&args.arg_pattern));
let literals = LiteralSets::create(&expr);
let re = Regex::new(&expr.to_string()).unwrap();
2016-03-11 03:48:44 +02:00
let _stdin = io::stdin();
let stdin = _stdin.lock();
run_by_line(args, &re, stdin)
} else {
2016-03-30 03:21:34 +02:00
let searcher =
try!(LineSearcherBuilder::new(&args.arg_pattern).create());
2016-04-04 03:22:09 +02:00
if args.flag_count {
run_mmap_count_only(args, &searcher)
} else {
run_mmap(args, &searcher)
}
2016-03-11 03:48:44 +02:00
}
}
2016-04-04 03:22:09 +02:00
#[inline(never)]
2016-03-30 03:21:34 +02:00
fn run_mmap(args: &Args, searcher: &LineSearcher) -> Result<u64> {
use memmap::{Mmap, Protection};
assert!(args.arg_file.len() == 1);
let mut wtr = io::BufWriter::new(io::stdout());
let mmap = try!(Mmap::open_path(&args.arg_file[0], Protection::Read));
let text = unsafe { mmap.as_slice() };
2016-04-04 03:22:09 +02:00
let mut count = 0;
2016-03-30 03:21:34 +02:00
for m in searcher.search(text) {
2016-04-04 03:22:09 +02:00
try!(wtr.write(&text[m.start..m.end]));
try!(wtr.write(b"\n"));
2016-03-30 03:21:34 +02:00
count += 1;
}
2016-04-04 03:22:09 +02:00
Ok(count)
}
#[inline(never)]
fn run_mmap_count_only(args: &Args, searcher: &LineSearcher) -> Result<u64> {
use memmap::{Mmap, Protection};
assert!(args.arg_file.len() == 1);
let mut wtr = io::BufWriter::new(io::stdout());
let mmap = try!(Mmap::open_path(&args.arg_file[0], Protection::Read));
let text = unsafe { mmap.as_slice() };
let count = searcher.search(text).last().map_or(0, |m| m.count + 1);
try!(writeln!(wtr, "{}", count));
2016-03-30 03:21:34 +02:00
Ok(count)
}
2016-03-11 03:48:44 +02:00
fn run_by_line<B: BufRead>(
args: &Args,
re: &Regex,
mut rdr: B,
) -> Result<u64> {
2016-02-27 18:07:26 +02:00
let mut wtr = io::BufWriter::new(io::stdout());
let mut count = 0;
let mut nline = 0;
let mut line = vec![];
loop {
line.clear();
let n = try!(rdr.read_until(b'\n', &mut line));
if n == 0 {
break;
}
nline += 1;
2016-03-11 03:48:44 +02:00
if re.is_match(&line) {
2016-02-27 18:07:26 +02:00
count += 1;
try!(wtr.write(&line));
}
}
Ok(count)
}
2016-03-11 03:48:44 +02:00
fn parse(re: &str) -> Result<syntax::Expr> {
let expr =
try!(syntax::ExprBuilder::new()
.allow_bytes(true)
.unicode(false)
.parse(re));
Ok(try!(nonl::remove(expr)))
}