Skip to content

Commit

Permalink
Merge pull request #38 from jamesbt365/oncelock
Browse files Browse the repository at this point in the history
Compile regex once
  • Loading branch information
InfinityGhost authored Jun 28, 2024
2 parents e218a01 + df5fe69 commit 1697b81
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
16 changes: 14 additions & 2 deletions src/commands/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::OnceLock;

use crate::{
commands::{respond_embed, respond_err, respond_ok},
structures::RepositoryDetails,
Expand Down Expand Up @@ -269,8 +271,8 @@ pub async fn add_repo(
#[description = "The owner of the repository."] owner: String,
#[description = "The respository name."] repository: String,
) -> Result<(), Error> {
let key_regex = Regex::new(r"[a-z+]+$").unwrap();
let repo_details_regex = Regex::new(r"^([a-zA-Z0-9-_.]+)*$").unwrap();
let key_regex = get_key_regex();
let repo_details_regex = get_repo_details_regex();
if !key_regex.is_match(&key) {
respond_err(
&ctx,
Expand Down Expand Up @@ -403,3 +405,13 @@ fn rm_repo(ctx: &Context<'_>, key: &str) {

rwlock_guard.issue_prefixes.remove(key);
}

fn get_key_regex() -> &'static Regex {
static REGEX: OnceLock<Regex> = OnceLock::new();
REGEX.get_or_init(|| Regex::new(r"[a-z+]+$").unwrap())
}

fn get_repo_details_regex() -> &'static Regex {
static REGEX: OnceLock<Regex> = OnceLock::new();
REGEX.get_or_init(|| Regex::new(r"^([a-zA-Z0-9-_.]+)*$").unwrap())
}
14 changes: 10 additions & 4 deletions src/events/code.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use regex::{Match, Regex};
use std::path::Path;
use std::str::FromStr;
use std::{path::Path, sync::OnceLock};

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

Expand Down Expand Up @@ -80,9 +80,7 @@ struct FileReference<'a> {

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

let files: Vec<FileReference> = r
.captures_iter(text)
Expand Down Expand Up @@ -156,3 +154,11 @@ impl FileReference<'_> {
}
}
}

fn get_file_reference_regex() -> &'static Regex {
static REGEX: OnceLock<Regex> = OnceLock::new();
REGEX.get_or_init(|| {
Regex::new(r"https://github.com/(.+?)/(.+?)/blob/(.+?)/(.+?)#L([0-9]+)(?:-L([0-9]+))?")
.unwrap()
})
}
10 changes: 7 additions & 3 deletions src/events/issues/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Duration;
use std::{sync::OnceLock, time::Duration};

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

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

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

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

Expand Down Expand Up @@ -197,3 +196,8 @@ async fn issue_embeds(data: &Data, message: &Message) -> Option<Vec<CreateEmbed>
Some(embeds)
}
}

fn get_issue_regex() -> &'static Regex {
static REGEX: OnceLock<Regex> = OnceLock::new();
REGEX.get_or_init(|| Regex::new(r" ?([a-zA-Z0-9-_.]+)?#([0-9]+) ?").unwrap())
}

0 comments on commit 1697b81

Please sign in to comment.