From 378c7bc08ea0b4a2815c68c2a29d67dc3b1fa76e Mon Sep 17 00:00:00 2001 From: Joshua Thijssen Date: Wed, 28 Feb 2024 15:33:19 +0100 Subject: [PATCH] Precompiling regexes --- Cargo.toml | 2 +- crates/gosub_css3/Cargo.toml | 1 - crates/gosub_styling/src/css_colors.rs | 9 +++++++-- src/bin/style-parser.rs | 6 +++--- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2eb9583f0..4ebd49018 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -82,4 +82,4 @@ opt-level = 3 codegen-units = 1 [lib] -crate-type = ["staticlib"] +crate-type = ["staticlib"] \ No newline at end of file diff --git a/crates/gosub_css3/Cargo.toml b/crates/gosub_css3/Cargo.toml index c8b1968d4..24dec6290 100644 --- a/crates/gosub_css3/Cargo.toml +++ b/crates/gosub_css3/Cargo.toml @@ -11,4 +11,3 @@ lazy_static = "1.4" log = "0.4.20" simple_logger = "4.2.0" anyhow = { version = "1.0.80", features = [] } - diff --git a/crates/gosub_styling/src/css_colors.rs b/crates/gosub_styling/src/css_colors.rs index 5ce94d9a3..b678a8bfe 100644 --- a/crates/gosub_styling/src/css_colors.rs +++ b/crates/gosub_styling/src/css_colors.rs @@ -70,8 +70,13 @@ fn get_hex_color_from_name(color_name: &str) -> Option<&str> { } fn parse_hex(value: &str) -> RgbColor { + lazy_static! { + static ref RE_HEX68: Regex = Regex::new(r"^#[0-9a-fA-F]{6,8}$").unwrap(); + static ref RE_HEX34: Regex = Regex::new(r"^#[0-9a-fA-F]{3,4}$").unwrap(); + } + // 6 with RRGGBB or 8 hex digits RRGGBBAA - if Regex::new(r"^#[0-9a-fA-F]{6,8}$").unwrap().is_match(value) { + if RE_HEX68.is_match(value) { let r = i32::from_str_radix(&value[1..3], 16).unwrap(); let g = i32::from_str_radix(&value[3..5], 16).unwrap(); let b = i32::from_str_radix(&value[5..7], 16).unwrap(); @@ -85,7 +90,7 @@ fn parse_hex(value: &str) -> RgbColor { } // 3 with RGB or 4 hex digits RGBA - if Regex::new(r"^#[0-9a-fA-F]{3,4}$").unwrap().is_match(value) { + if RE_HEX34.is_match(value) { let mut r = i32::from_str_radix(&value[1..2], 16).unwrap(); r = r * 16 + r; let mut g = i32::from_str_radix(&value[2..3], 16).unwrap(); diff --git a/src/bin/style-parser.rs b/src/bin/style-parser.rs index 46da57b1d..459894e40 100644 --- a/src/bin/style-parser.rs +++ b/src/bin/style-parser.rs @@ -12,7 +12,6 @@ use gosub_html5::visit::Visitor; use gosub_shared::bytes::{CharIterator, Confidence, Encoding}; use gosub_styling::calculator::StyleCalculator; use gosub_styling::pipeline::Pipeline; -use regex::Regex; use std::fs; use url::Url; @@ -40,8 +39,9 @@ impl Visitor for TextVisitor { fn doctype_leave(&mut self, _node: &Node, _data: &DocTypeData) {} fn text_enter(&mut self, _node: &Node, data: &TextData) { - let re = Regex::new(r"\s{2,}").unwrap(); - let s = re.replace_all(&data.value, " "); + // let re = Regex::new(r"\s{2,}").unwrap(); + // let s = re.replace_all(&data.value, " "); + let s = &data.value; if !self.color.is_empty() { print!("\x1b[{}m", self.color)