From 5f1da8e5337900b9d94b55ec65618f0223608f6c Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 21 Jun 2019 12:04:27 -0400 Subject: [PATCH] Cache Regex's --- Cargo.lock | 1 + src/tools/tidy/Cargo.toml | 1 + src/tools/tidy/src/features.rs | 17 ++++++++++++++--- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 40b8cf507e9..a9f9cd2b977 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3801,6 +3801,7 @@ dependencies = [ name = "tidy" version = "0.1.0" dependencies = [ + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/src/tools/tidy/Cargo.toml b/src/tools/tidy/Cargo.toml index eeac6cfbb30..fb58adcf9b2 100644 --- a/src/tools/tidy/Cargo.toml +++ b/src/tools/tidy/Cargo.toml @@ -8,3 +8,4 @@ edition = "2018" regex = "1" serde = { version = "1.0.8", features = ["derive"] } serde_json = "1.0.2" +lazy_static = "1" diff --git a/src/tools/tidy/src/features.rs b/src/tools/tidy/src/features.rs index 6aa093c1ef0..ced30eb6d58 100644 --- a/src/tools/tidy/src/features.rs +++ b/src/tools/tidy/src/features.rs @@ -15,7 +15,7 @@ use std::io::prelude::*; use std::path::Path; -use regex::{Regex, escape}; +use regex::Regex; mod version; use version::Version; @@ -159,8 +159,19 @@ fn format_features<'a>(features: &'a Features, family: &'a str) -> impl Iterator } fn find_attr_val<'a>(line: &'a str, attr: &str) -> Option<&'a str> { - let r = Regex::new(&format!(r#"{}\s*=\s*"([^"]*)""#, escape(attr))) - .expect("malformed regex for find_attr_val"); + lazy_static::lazy_static! { + static ref ISSUE: Regex = Regex::new(r#"issue\s*=\s*"([^"]*)""#).unwrap(); + static ref FEATURE: Regex = Regex::new(r#"feature\s*=\s*"([^"]*)""#).unwrap(); + static ref SINCE: Regex = Regex::new(r#"since\s*=\s*"([^"]*)""#).unwrap(); + } + + let r = match attr { + "issue" => &*ISSUE, + "feature" => &*FEATURE, + "since" => &*SINCE, + _ => unimplemented!("{} not handled", attr), + }; + r.captures(line) .and_then(|c| c.get(1)) .map(|m| m.as_str()) -- 2.44.0