[librsvg: 22/25] stroke-dash-array: Use lazy_static to cache the regex query.
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 22/25] stroke-dash-array: Use lazy_static to cache the regex query.
- Date: Fri, 2 Feb 2018 00:30:27 +0000 (UTC)
commit 156c2aa758657f746de8705d836763bfbe8750fc
Author: Jordan Petridis <jordanpetridis protonmail com>
Date: Thu Feb 1 22:21:32 2018 +0200
stroke-dash-array: Use lazy_static to cache the regex query.
rust/Cargo.lock | 1 +
rust/Cargo.toml | 1 +
rust/src/length.rs | 12 ++++++++----
rust/src/lib.rs | 3 +++
4 files changed, 13 insertions(+), 4 deletions(-)
---
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 68faf02..779a004 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -283,6 +283,7 @@ dependencies = [
"glib 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
"pango 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pango-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 8e2b450..487e91d 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -11,6 +11,7 @@ itertools = "0.7.4"
pango = "0.3.0"
pango-sys = "0.5.0"
cssparser = "0.23"
+lazy_static = "1.0.0"
[dependencies.cairo-sys-rs]
version = "0.5.0"
diff --git a/rust/src/length.rs b/rust/src/length.rs
index d091aa6..0cf1b0e 100644
--- a/rust/src/length.rs
+++ b/rust/src/length.rs
@@ -330,6 +330,11 @@ fn parse_stroke_dash_array(s: &str) -> Result<StrokeDasharray, AttributeError> {
// This does not handle "inherit" or "none" state, the caller is responsible for that.
fn parse_dash_array(s: &str) -> Result<Vec<RsvgLength>, AttributeError> {
+ lazy_static!{
+ // The unwrap here is fine AS LONG the regex query is valid.
+ static ref COMMAS: Regex = Regex::new(r",\s*,").unwrap();
+ };
+
let s = s.trim();
if s.is_empty() {
@@ -343,10 +348,8 @@ fn parse_dash_array(s: &str) -> Result<Vec<RsvgLength>, AttributeError> {
}
}
- // TODO: Use lazy static to avoid constructing the regex on each function call.
- let commas = Regex::new(r",\s*,").unwrap();
-
- if commas.is_match(s) {
+ // Commas must be followed by a value.
+ if COMMAS.is_match(s) {
return Err(AttributeError::Parse(ParseError::new("expected number, found comma")));
}
@@ -598,6 +601,7 @@ mod tests {
assert_eq!(parse_dash_array(""), Err(AttributeError::Parse(ParseError::new("empty string"))));
assert_eq!(parse_dash_array("\t \n "), Err(AttributeError::Parse(ParseError::new("empty
string"))));
assert!(parse_dash_array(",,,").is_err());
+ assert!(parse_dash_array("10, \t, 20 \n").is_err());
// No trailling commas allowed, parse error
assert!(parse_dash_array("10,").is_err());
// A comma should be followed by a number
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index 4823049..453dac8 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -13,6 +13,9 @@ extern crate pango;
extern crate pango_sys;
extern crate regex;
+#[macro_use]
+extern crate lazy_static;
+
#[macro_use]
extern crate downcast_rs;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]