use lazy_static so we do not compile regex multiple times

This commit is contained in:
Matthias Beyer 2016-02-20 21:10:22 +01:00
parent df4bc13018
commit 1b8838bf1f
3 changed files with 11 additions and 6 deletions

View file

@ -4,6 +4,7 @@ version = "0.1.0"
authors = ["Matthias Beyer <mail@beyermatthias.de>"] authors = ["Matthias Beyer <mail@beyermatthias.de>"]
[dependencies] [dependencies]
lazy_static = "0.1.15"
log = "0.3.5" log = "0.3.5"
regex = "0.1.47" regex = "0.1.47"

View file

@ -43,16 +43,19 @@ impl IntoKeyValue<String, String> for String {
fn into_kv(self) -> Option<KeyValue<String, String>> { fn into_kv(self) -> Option<KeyValue<String, String>> {
let key = { let key = {
let r = "^(?P<KEY>([^=]*))=(.*)$"; lazy_static! {
let r = Regex::new(r).unwrap(); static ref R: Regex = Regex::new("^(?P<KEY>([^=]*))=(.*)$").unwrap();
r.captures(&self[..]) }
R.captures(&self[..])
.and_then(|caps| caps.name("KEY")) .and_then(|caps| caps.name("KEY"))
}; };
let value = { let value = {
let r = "(.*)=(\"(?P<QVALUE>([^\"]*))\"|(?P<VALUE>(.*)))$"; lazy_static! {
let r = Regex::new(r).unwrap(); static ref R: Regex = Regex::new("(.*)=(\"(?P<QVALUE>([^\"]*))\"|(?P<VALUE>(.*)))$")
r.captures(&self[..]) .unwrap();
}
R.captures(&self[..])
.map(|caps| { .map(|caps| {
caps.name("VALUE") caps.name("VALUE")
.or(caps.name("QVALUE")) .or(caps.name("QVALUE"))

View file

@ -1,3 +1,4 @@
#[macro_use] extern crate lazy_static;
#[macro_use] extern crate log; #[macro_use] extern crate log;
extern crate regex; extern crate regex;