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>"]
[dependencies]
lazy_static = "0.1.15"
log = "0.3.5"
regex = "0.1.47"

View file

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

View file

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