Skip to content

Commit

Permalink
Precompiling regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph committed Feb 28, 2024
1 parent 8a72219 commit 378c7bc
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ opt-level = 3
codegen-units = 1

[lib]
crate-type = ["staticlib"]
crate-type = ["staticlib"]
1 change: 0 additions & 1 deletion crates/gosub_css3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ lazy_static = "1.4"
log = "0.4.20"
simple_logger = "4.2.0"
anyhow = { version = "1.0.80", features = [] }

9 changes: 7 additions & 2 deletions crates/gosub_styling/src/css_colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/bin/style-parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -40,8 +39,9 @@ impl Visitor<Node> 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)
Expand Down

0 comments on commit 378c7bc

Please sign in to comment.