Skip to content

Commit df5fe69

Browse files
committed
Compile regex once
1 parent e218a01 commit df5fe69

3 files changed

Lines changed: 31 additions & 9 deletions

File tree

src/commands/utils.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::OnceLock;
2+
13
use crate::{
24
commands::{respond_embed, respond_err, respond_ok},
35
structures::RepositoryDetails,
@@ -269,8 +271,8 @@ pub async fn add_repo(
269271
#[description = "The owner of the repository."] owner: String,
270272
#[description = "The respository name."] repository: String,
271273
) -> Result<(), Error> {
272-
let key_regex = Regex::new(r"[a-z+]+$").unwrap();
273-
let repo_details_regex = Regex::new(r"^([a-zA-Z0-9-_.]+)*$").unwrap();
274+
let key_regex = get_key_regex();
275+
let repo_details_regex = get_repo_details_regex();
274276
if !key_regex.is_match(&key) {
275277
respond_err(
276278
&ctx,
@@ -403,3 +405,13 @@ fn rm_repo(ctx: &Context<'_>, key: &str) {
403405

404406
rwlock_guard.issue_prefixes.remove(key);
405407
}
408+
409+
fn get_key_regex() -> &'static Regex {
410+
static REGEX: OnceLock<Regex> = OnceLock::new();
411+
REGEX.get_or_init(|| Regex::new(r"[a-z+]+$").unwrap())
412+
}
413+
414+
fn get_repo_details_regex() -> &'static Regex {
415+
static REGEX: OnceLock<Regex> = OnceLock::new();
416+
REGEX.get_or_init(|| Regex::new(r"^([a-zA-Z0-9-_.]+)*$").unwrap())
417+
}

src/events/code.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use regex::{Match, Regex};
2-
use std::path::Path;
32
use std::str::FromStr;
3+
use std::{path::Path, sync::OnceLock};
44

55
use poise::serenity_prelude::{self as serenity, Colour, Context, CreateEmbed, Message};
66

@@ -80,9 +80,7 @@ struct FileReference<'a> {
8080

8181
impl FileReference<'_> {
8282
pub fn try_from_str(text: &str) -> Option<Vec<FileReference>> {
83-
let r =
84-
Regex::new(r"https://github.com/(.+?)/(.+?)/blob/(.+?)/(.+?)#L([0-9]+)(?:-L([0-9]+))?")
85-
.expect("Expected url regex");
83+
let r = get_file_reference_regex();
8684

8785
let files: Vec<FileReference> = r
8886
.captures_iter(text)
@@ -156,3 +154,11 @@ impl FileReference<'_> {
156154
}
157155
}
158156
}
157+
158+
fn get_file_reference_regex() -> &'static Regex {
159+
static REGEX: OnceLock<Regex> = OnceLock::new();
160+
REGEX.get_or_init(|| {
161+
Regex::new(r"https://github.com/(.+?)/(.+?)/blob/(.+?)/(.+?)#L([0-9]+)(?:-L([0-9]+))?")
162+
.unwrap()
163+
})
164+
}

src/events/issues/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::time::Duration;
1+
use std::{sync::OnceLock, time::Duration};
22

33
use crate::{commands::interaction_err, structures::Embeddable, Data};
44

@@ -152,8 +152,7 @@ async fn issue_embeds(data: &Data, message: &Message) -> Option<Vec<CreateEmbed>
152152
let client = octocrab::instance();
153153
let ratelimit = client.ratelimit();
154154

155-
// TODO: stop compiling this every time.
156-
let regex = Regex::new(r" ?([a-zA-Z0-9-_.]+)?#([0-9]+) ?").expect("Expected numbers regex");
155+
let regex = get_issue_regex();
157156

158157
let custom_repos = { data.state.read().unwrap().issue_prefixes.clone() };
159158

@@ -197,3 +196,8 @@ async fn issue_embeds(data: &Data, message: &Message) -> Option<Vec<CreateEmbed>
197196
Some(embeds)
198197
}
199198
}
199+
200+
fn get_issue_regex() -> &'static Regex {
201+
static REGEX: OnceLock<Regex> = OnceLock::new();
202+
REGEX.get_or_init(|| Regex::new(r" ?([a-zA-Z0-9-_.]+)?#([0-9]+) ?").unwrap())
203+
}

0 commit comments

Comments
 (0)