Skip to content

Commit 0f3896b

Browse files
authored
refactor: speed-up bulk validations by compiling Regexes only once (#13)
1 parent 6b70f86 commit 0f3896b

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ crate-type = ["cdylib", "rlib"]
1111
[dependencies]
1212
clap = { version = "4.5", features = ["derive" ]}
1313
console_error_panic_hook = "0.1"
14+
once_cell = "1.19.0"
1415
regex = "1.10.4"
1516
serde = { version = "1.0", features = ["derive"] }
1617
serde-wasm-bindgen = "0.4"

src/parser.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use once_cell::sync::Lazy;
12
use regex::Regex;
23

34
#[derive(Debug, PartialEq)]
@@ -47,11 +48,12 @@ pub struct ParserError {
4748
pub message: String,
4849
}
4950

51+
static PAT_COMMAND: Lazy<Regex> =
52+
Lazy::new(|| Regex::new(r"^\{(?:(\d+):)?(|\{|[A-Z]+[A-Z0-9_]*)(?:\.(\w+))?\}$").unwrap());
53+
5054
impl StringCommand {
5155
fn parse(string: &str) -> Option<StringCommand> {
52-
let pat_command =
53-
Regex::new(r"^\{(?:(\d+):)?(|\{|[A-Z]+[A-Z0-9_]*)(?:\.(\w+))?\}$").unwrap();
54-
let caps = pat_command.captures(string)?;
56+
let caps = PAT_COMMAND.captures(string)?;
5557
Some(StringCommand {
5658
index: caps.get(1).and_then(|v| v.as_str().parse().ok()),
5759
name: String::from(&caps[2]),
@@ -73,10 +75,11 @@ impl StringCommand {
7375
}
7476
}
7577

78+
static PAT_GENDER: Lazy<Regex> = Lazy::new(|| Regex::new(r"^\{G\s*=\s*(\w+)\}$").unwrap());
79+
7680
impl GenderDefinition {
7781
fn parse(string: &str) -> Option<GenderDefinition> {
78-
let pat_gender = Regex::new(r"^\{G\s*=\s*(\w+)\}$").unwrap();
79-
let caps = pat_gender.captures(string)?;
82+
let caps = PAT_GENDER.captures(string)?;
8083
Some(GenderDefinition {
8184
gender: String::from(&caps[1]),
8285
})
@@ -87,12 +90,14 @@ impl GenderDefinition {
8790
}
8891
}
8992

93+
static PAT_CHOICE: Lazy<Regex> =
94+
Lazy::new(|| Regex::new(r"^\{([PG])(?:\s+(\d+)(?::(\d+))?)?(\s+[^\s0-9].*?)\s*\}$").unwrap());
95+
static PAT_ITEM: Lazy<Regex> =
96+
Lazy::new(|| Regex::new(r##"^\s+(?:([^\s"]+)|"([^"]*)")"##).unwrap());
97+
9098
impl ChoiceList {
9199
fn parse(string: &str) -> Option<ChoiceList> {
92-
let pat_choice =
93-
Regex::new(r"^\{([PG])(?:\s+(\d+)(?::(\d+))?)?(\s+[^\s0-9].*?)\s*\}$").unwrap();
94-
let pat_item = Regex::new(r##"^\s+(?:([^\s"]+)|"([^"]*)")"##).unwrap();
95-
let caps = pat_choice.captures(string)?;
100+
let caps = PAT_CHOICE.captures(string)?;
96101
let mut result = ChoiceList {
97102
name: String::from(&caps[1]),
98103
indexref: caps.get(2).and_then(|v| v.as_str().parse().ok()),
@@ -101,7 +106,7 @@ impl ChoiceList {
101106
};
102107
let mut rest = &caps[4];
103108
while !rest.is_empty() {
104-
let m = pat_item.captures(rest)?;
109+
let m = PAT_ITEM.captures(rest)?;
105110
result
106111
.choices
107112
.push(String::from(m.get(1).or(m.get(2)).unwrap().as_str()));

0 commit comments

Comments
 (0)