Created
December 3, 2017 15:45
-
-
Save vasily-kirichenko/d88a15d62c19b87042c937136896bcb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(universal_impl_trait)] | |
extern crate time; | |
extern crate regex; | |
extern crate itertools; | |
#[macro_use] | |
extern crate error_chain; | |
use time::{PreciseTime, Duration}; | |
use std::io::prelude::*; | |
use std::io::BufReader; | |
use std::fs::File; | |
use regex::Regex; | |
mod errors { | |
error_chain! { | |
foreign_links { | |
Io(::std::io::Error); | |
Regex(::regex::Error); | |
} | |
} | |
} | |
use errors::*; | |
fn tc<T>(f: impl Fn() -> Result<T>) -> Result<(T, Duration)> { | |
let start = PreciseTime::now(); | |
let res = f()?; | |
Ok((res, start.to(PreciseTime::now()))) | |
} | |
fn cleanup_name(line: &str) -> String { | |
use itertools::*; | |
line.split(|x| x == ' ' || x == ';' || x == '"') | |
.filter(|x| !x.is_empty()) | |
.join(".") | |
} | |
fn search(file: &str, pattern: &str) -> Result<Vec<String>> { | |
let r = Regex::new(pattern)?; | |
let f = File::open(file)?; | |
let buff = BufReader::new(f); | |
let mut res = Vec::new(); | |
for line in buff.lines().take(10_000_000) { | |
let line = line?; | |
if let Some(name) = r.captures(&line).and_then(|caps| caps.name("name")) { | |
res.push(cleanup_name(name.as_str())) | |
} | |
} | |
Ok(res) | |
} | |
fn run() -> Result<()> { | |
let pat = r"(?i)\{.*(?P<name>microsoft.*)\|\]"; | |
let (res, elapsed) = tc(|| { | |
search("d:\\big.txt", pat) | |
})?; | |
println!("Res count = {}, Elapsed {}", res.len(), elapsed); | |
Ok(()) | |
} | |
fn main() { | |
if let Err(ref e) = run() { | |
use std::io::Write; | |
use error_chain::ChainedError; | |
let stderr = &mut ::std::io::stderr(); | |
let errmsg = "Error writing to stderr"; | |
writeln!(stderr, "{}", e.display_chain()).expect(errmsg); | |
std::process::exit(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment