Skip to content

Commit

Permalink
Use static LazyLocks for all constant regexes (#22225)
Browse files Browse the repository at this point in the history
Release Notes:

- N/A
  • Loading branch information
mgsloan authored Dec 19, 2024
1 parent 837bbc8 commit b93cee8
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 33 deletions.
13 changes: 9 additions & 4 deletions crates/assistant/src/context_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use paths::contexts_dir;
use project::Project;
use regex::Regex;
use rpc::AnyProtoClient;
use std::sync::LazyLock;
use std::{
cmp::Reverse,
ffi::OsStr,
Expand Down Expand Up @@ -753,8 +754,8 @@ impl ContextStore {
continue;
}

let pattern = r" - \d+.zed.json$";
let re = Regex::new(pattern).unwrap();
static ASSISTANT_CONTEXT_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r" - \d+.zed.json$").unwrap());

let metadata = fs.metadata(&path).await?;
if let Some((file_name, metadata)) = path
Expand All @@ -763,11 +764,15 @@ impl ContextStore {
.zip(metadata)
{
// This is used to filter out contexts saved by the new assistant.
if !re.is_match(file_name) {
if !ASSISTANT_CONTEXT_REGEX.is_match(file_name) {
continue;
}

if let Some(title) = re.replace(file_name, "").lines().next() {
if let Some(title) = ASSISTANT_CONTEXT_REGEX
.replace(file_name, "")
.lines()
.next()
{
contexts.push(SavedContextMetadata {
title: title.to_string(),
path,
Expand Down
11 changes: 8 additions & 3 deletions crates/feedback/src/feedback_modal.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{ops::RangeInclusive, sync::Arc, time::Duration};
use std::{
ops::RangeInclusive,
sync::{Arc, LazyLock},
time::Duration,
};

use anyhow::{anyhow, bail};
use bitflags::bitflags;
Expand Down Expand Up @@ -34,7 +38,8 @@ const DEV_MODE: bool = true;
const DEV_MODE: bool = false;

const DATABASE_KEY_NAME: &str = "email_address";
const EMAIL_REGEX: &str = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b";
static EMAIL_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b").unwrap());
const FEEDBACK_CHAR_LIMIT: RangeInclusive<i32> = 10..=5000;
const FEEDBACK_SUBMISSION_ERROR_TEXT: &str =
"Feedback failed to submit, see error log for details.";
Expand Down Expand Up @@ -320,7 +325,7 @@ impl FeedbackModal {
let mut invalid_state_flags = InvalidStateFlags::empty();

let valid_email_address = match self.email_address_editor.read(cx).text_option(cx) {
Some(email_address) => Regex::new(EMAIL_REGEX).unwrap().is_match(&email_address),
Some(email_address) => EMAIL_REGEX.is_match(&email_address),
None => true,
};

Expand Down
8 changes: 4 additions & 4 deletions crates/git_hosting_providers/src/providers/github.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::str::FromStr;
use std::sync::{Arc, OnceLock};
use std::sync::{Arc, LazyLock};

use anyhow::{bail, Context, Result};
use async_trait::async_trait;
Expand All @@ -15,9 +15,9 @@ use git::{
};

fn pull_request_number_regex() -> &'static Regex {
static PULL_REQUEST_NUMBER_REGEX: OnceLock<Regex> = OnceLock::new();

PULL_REQUEST_NUMBER_REGEX.get_or_init(|| Regex::new(r"\(#(\d+)\)$").unwrap())
static PULL_REQUEST_NUMBER_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\(#(\d+)\)$").unwrap());
&PULL_REQUEST_NUMBER_REGEX
}

#[derive(Debug, Deserialize)]
Expand Down
12 changes: 6 additions & 6 deletions crates/html_to_markdown/src/markdown_writer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;
use std::sync::OnceLock;
use std::{cell::RefCell, sync::LazyLock};

use anyhow::Result;
use markup5ever_rcdom::{Handle, NodeData};
Expand All @@ -10,13 +9,14 @@ use regex::Regex;
use crate::html_element::HtmlElement;

fn empty_line_regex() -> &'static Regex {
static REGEX: OnceLock<Regex> = OnceLock::new();
REGEX.get_or_init(|| Regex::new(r"^\s*$").unwrap())
static REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^\s*$").expect("Failed to create empty_line_regex"));
&REGEX
}

fn more_than_three_newlines_regex() -> &'static Regex {
static REGEX: OnceLock<Regex> = OnceLock::new();
REGEX.get_or_init(|| Regex::new(r"\n{3,}").unwrap())
static REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\n{3,}").unwrap());
&REGEX
}

pub enum StartTagOutcome {
Expand Down
25 changes: 12 additions & 13 deletions crates/project/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ use std::{
io::{BufRead, BufReader, Read},
ops::Range,
path::Path,
sync::{Arc, LazyLock, OnceLock},
sync::{Arc, LazyLock},
};
use text::Anchor;
use util::paths::PathMatcher;

static TEXT_REPLACEMENT_SPECIAL_CHARACTERS_REGEX: OnceLock<Regex> = OnceLock::new();

pub enum SearchResult {
Buffer {
buffer: Model<Buffer>,
Expand Down Expand Up @@ -265,16 +263,17 @@ impl SearchQuery {
regex, replacement, ..
} => {
if let Some(replacement) = replacement {
let replacement = TEXT_REPLACEMENT_SPECIAL_CHARACTERS_REGEX
.get_or_init(|| Regex::new(r"\\\\|\\n|\\t").unwrap())
.replace_all(replacement, |c: &Captures| {
match c.get(0).unwrap().as_str() {
r"\\" => "\\",
r"\n" => "\n",
r"\t" => "\t",
x => unreachable!("Unexpected escape sequence: {}", x),
}
});
static TEXT_REPLACEMENT_SPECIAL_CHARACTERS_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\\\\|\\n|\\t").unwrap());
let replacement = TEXT_REPLACEMENT_SPECIAL_CHARACTERS_REGEX.replace_all(
replacement,
|c: &Captures| match c.get(0).unwrap().as_str() {
r"\\" => "\\",
r"\n" => "\n",
r"\t" => "\t",
x => unreachable!("Unexpected escape sequence: {}", x),
},
);
Some(regex.replace(text, replacement))
} else {
None
Expand Down
7 changes: 4 additions & 3 deletions crates/util/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod test;
use futures::Future;

use regex::Regex;
use std::sync::OnceLock;
use std::sync::{LazyLock, OnceLock};
use std::{
borrow::Cow,
cmp::{self, Ordering},
Expand Down Expand Up @@ -567,8 +567,9 @@ impl<'a> PartialOrd for NumericPrefixWithSuffix<'a> {
}

fn emoji_regex() -> &'static Regex {
static EMOJI_REGEX: OnceLock<Regex> = OnceLock::new();
EMOJI_REGEX.get_or_init(|| Regex::new("(\\p{Emoji}|\u{200D})").unwrap())
static EMOJI_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new("(\\p{Emoji}|\u{200D})").unwrap());
&EMOJI_REGEX
}

/// Returns true if the given string consists of emojis only.
Expand Down

0 comments on commit b93cee8

Please sign in to comment.