Skip to content

Commit

Permalink
refactor: speed-up bulk validations by compiling Regexes only once (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
frosch123 authored Apr 26, 2024
1 parent 6b70f86 commit 0f3896b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
clap = { version = "4.5", features = ["derive" ]}
console_error_panic_hook = "0.1"
once_cell = "1.19.0"
regex = "1.10.4"
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4"
Expand Down
25 changes: 15 additions & 10 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use once_cell::sync::Lazy;
use regex::Regex;

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -47,11 +48,12 @@ pub struct ParserError {
pub message: String,
}

static PAT_COMMAND: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^\{(?:(\d+):)?(|\{|[A-Z]+[A-Z0-9_]*)(?:\.(\w+))?\}$").unwrap());

impl StringCommand {
fn parse(string: &str) -> Option<StringCommand> {
let pat_command =
Regex::new(r"^\{(?:(\d+):)?(|\{|[A-Z]+[A-Z0-9_]*)(?:\.(\w+))?\}$").unwrap();
let caps = pat_command.captures(string)?;
let caps = PAT_COMMAND.captures(string)?;
Some(StringCommand {
index: caps.get(1).and_then(|v| v.as_str().parse().ok()),
name: String::from(&caps[2]),
Expand All @@ -73,10 +75,11 @@ impl StringCommand {
}
}

static PAT_GENDER: Lazy<Regex> = Lazy::new(|| Regex::new(r"^\{G\s*=\s*(\w+)\}$").unwrap());

impl GenderDefinition {
fn parse(string: &str) -> Option<GenderDefinition> {
let pat_gender = Regex::new(r"^\{G\s*=\s*(\w+)\}$").unwrap();
let caps = pat_gender.captures(string)?;
let caps = PAT_GENDER.captures(string)?;
Some(GenderDefinition {
gender: String::from(&caps[1]),
})
Expand All @@ -87,12 +90,14 @@ impl GenderDefinition {
}
}

static PAT_CHOICE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^\{([PG])(?:\s+(\d+)(?::(\d+))?)?(\s+[^\s0-9].*?)\s*\}$").unwrap());
static PAT_ITEM: Lazy<Regex> =
Lazy::new(|| Regex::new(r##"^\s+(?:([^\s"]+)|"([^"]*)")"##).unwrap());

impl ChoiceList {
fn parse(string: &str) -> Option<ChoiceList> {
let pat_choice =
Regex::new(r"^\{([PG])(?:\s+(\d+)(?::(\d+))?)?(\s+[^\s0-9].*?)\s*\}$").unwrap();
let pat_item = Regex::new(r##"^\s+(?:([^\s"]+)|"([^"]*)")"##).unwrap();
let caps = pat_choice.captures(string)?;
let caps = PAT_CHOICE.captures(string)?;
let mut result = ChoiceList {
name: String::from(&caps[1]),
indexref: caps.get(2).and_then(|v| v.as_str().parse().ok()),
Expand All @@ -101,7 +106,7 @@ impl ChoiceList {
};
let mut rest = &caps[4];
while !rest.is_empty() {
let m = pat_item.captures(rest)?;
let m = PAT_ITEM.captures(rest)?;
result
.choices
.push(String::from(m.get(1).or(m.get(2)).unwrap().as_str()));
Expand Down

0 comments on commit 0f3896b

Please sign in to comment.