Properly use static instead of const for global types that need a single init (#35955)

Release Notes:

- N/A
This commit is contained in:
Kirill Bulatov 2025-08-10 21:01:54 +03:00 committed by GitHub
parent 9cd5c3656e
commit 95e302fa68
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 55 additions and 41 deletions

View file

@ -8,7 +8,7 @@ use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::io::{self, Read};
use std::process;
use std::sync::LazyLock;
use std::sync::{LazyLock, OnceLock};
use util::paths::PathExt;
static KEYMAP_MACOS: LazyLock<KeymapFile> = LazyLock::new(|| {
@ -388,7 +388,7 @@ fn handle_postprocessing() -> Result<()> {
let meta_title = format!("{} | {}", page_title, meta_title);
zlog::trace!(logger => "Updating {:?}", pretty_path(&file, &root_dir));
let contents = contents.replace("#description#", meta_description);
let contents = TITLE_REGEX
let contents = title_regex()
.replace(&contents, |_: &regex::Captures| {
format!("<title>{}</title>", meta_title)
})
@ -404,10 +404,8 @@ fn handle_postprocessing() -> Result<()> {
) -> &'a std::path::Path {
&path.strip_prefix(&root).unwrap_or(&path)
}
const TITLE_REGEX: std::cell::LazyCell<Regex> =
std::cell::LazyCell::new(|| Regex::new(r"<title>\s*(.*?)\s*</title>").unwrap());
fn extract_title_from_page(contents: &str, pretty_path: &std::path::Path) -> String {
let title_tag_contents = &TITLE_REGEX
let title_tag_contents = &title_regex()
.captures(&contents)
.with_context(|| format!("Failed to find title in {:?}", pretty_path))
.expect("Page has <title> element")[1];
@ -420,3 +418,8 @@ fn handle_postprocessing() -> Result<()> {
title
}
}
fn title_regex() -> &'static Regex {
static TITLE_REGEX: OnceLock<Regex> = OnceLock::new();
TITLE_REGEX.get_or_init(|| Regex::new(r"<title>\s*(.*?)\s*</title>").unwrap())
}