Merge pull request #206 from matthiasbeyer/regex-lazy-static

Regex lazy static
This commit is contained in:
Matthias Beyer 2016-02-23 10:13:45 +01:00
commit be45c4eb14
6 changed files with 22 additions and 13 deletions

View file

@ -6,6 +6,7 @@ authors = ["Matthias Beyer <mail@beyermatthias.de>"]
[dependencies] [dependencies]
fs2 = "0.2.2" fs2 = "0.2.2"
glob = "0.2.10" glob = "0.2.10"
lazy_static = "0.1.15"
log = "0.3.5" log = "0.3.5"
regex = "0.1.47" regex = "0.1.47"
semver = "0.2" semver = "0.2"

View file

@ -2,6 +2,7 @@
#[macro_use] extern crate version; #[macro_use] extern crate version;
extern crate fs2; extern crate fs2;
extern crate glob; extern crate glob;
#[macro_use] extern crate lazy_static;
extern crate regex; extern crate regex;
extern crate toml; extern crate toml;
#[cfg(test)] extern crate tempdir; #[cfg(test)] extern crate tempdir;

View file

@ -870,14 +870,16 @@ impl Entry {
pub fn from_str(loc: StoreId, s: &str) -> Result<Entry> { pub fn from_str(loc: StoreId, s: &str) -> Result<Entry> {
debug!("Building entry from string"); debug!("Building entry from string");
let re = Regex::new(r"(?smx) lazy_static! {
static ref RE: Regex = Regex::new(r"(?smx)
^---$ ^---$
(?P<header>.*) # Header (?P<header>.*) # Header
^---$\n ^---$\n
(?P<content>.*) # Content (?P<content>.*) # Content
").unwrap(); ").unwrap();
}
let matches = re.captures(s); let matches = RE.captures(s);
if matches.is_none() { if matches.is_none() {
return Err(StoreError::new(StoreErrorKind::MalformedEntry, None)); return Err(StoreError::new(StoreErrorKind::MalformedEntry, None));

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;