Skip to content

Commit

Permalink
switch from lazy_static to std::sync::OnceLock
Browse files Browse the repository at this point in the history
  • Loading branch information
digama0 committed Nov 28, 2023
1 parent 0cc6dda commit 1428ea9
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 57 deletions.
5 changes: 2 additions & 3 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions metamath-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ categories.workspace = true
edition = "2021"

[dependencies]
lazy_static = "1.4"
itertools = "0.10"
itertools = "0.12"
filetime = "0.2"
fnv = "1.0"
regex = { version = "1.5", default-features = false, features = ["std", "perf"] }
Expand Down
5 changes: 5 additions & 0 deletions metamath-rs/src/bit_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ impl Bitset {
Some(bx) => bx.iter().all(|&word| word == 0),
}
}

/// Returns an iterator over the indices of set bits in the bitset.
pub fn iter(&self) -> BitsetIter<'_> {
self.into_iter()
}
}

impl<'a> BitOrAssign<&'a Bitset> for Bitset {
Expand Down
28 changes: 14 additions & 14 deletions metamath-rs/src/comment_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
//! corresponding byte string in the file has to be unescaped before
//! interpretation, using the [`CommentParser::unescape_text`] and
//! [`CommentParser::unescape_math`] functions.
use std::fmt::Display;
use std::{fmt::Display, sync::OnceLock};

use lazy_static::lazy_static;
use regex::bytes::{CaptureMatches, Match, Regex, RegexSet};

use crate::{statement::unescape, Span};
Expand Down Expand Up @@ -473,14 +472,15 @@ impl Discouragements {
/// is discouraged.
#[must_use]
pub fn new(buf: &[u8]) -> Self {
lazy_static! {
static ref MODIFICATION: RegexSet = RegexSet::new([
static MODIFICATION: OnceLock<RegexSet> = OnceLock::new();
let modification = MODIFICATION.get_or_init(|| {
RegexSet::new([
r"\(Proof[ \n]+modification[ \n]+is[ \n]+discouraged\.\)",
r"\(New[ \n]+usage[ \n]+is[ \n]+discouraged\.\)"
r"\(New[ \n]+usage[ \n]+is[ \n]+discouraged\.\)",
])
.unwrap();
}
let m = MODIFICATION.matches(buf);
.unwrap()
});
let m = modification.matches(buf);
Self {
modification_discouraged: m.matched(0),
usage_discouraged: m.matched(1),
Expand Down Expand Up @@ -532,17 +532,17 @@ impl<'a> ParentheticalIter<'a> {
/// Construct a new parenthetical iterator given a segment buffer and a span in it.
#[must_use]
pub fn new(buf: &'a [u8], span: Span) -> Self {
lazy_static! {
static ref PARENTHETICALS: Regex = Regex::new(concat!(
static PARENTHETICALS: OnceLock<Regex> = OnceLock::new();
let parentheticals = PARENTHETICALS.get_or_init(|| {
Regex::new(concat!(
r"\((Contributed|Revised|Proof[ \r\n]+shortened)",
r"[ \r\n]+by[ \r\n]+([^,)]+),[ \r\n]+([0-9]{1,2}-[A-Z][a-z]{2}-[0-9]{4})\.\)|",
r"\((Proof[ \r\n]+modification|New[ \r\n]+usage)[ \r\n]+is[ \r\n]+discouraged\.\)",
))
.unwrap();
}

.unwrap()
});
Self {
matches: PARENTHETICALS.captures_iter(span.as_ref(buf)),
matches: parentheticals.captures_iter(span.as_ref(buf)),
off: span.start,
}
}
Expand Down
9 changes: 5 additions & 4 deletions metamath-rs/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::fmt;
use std::io;
use std::io::Write;
use std::str;
use std::sync::OnceLock;

/// The error type for [`Database::export_mmp`].
#[derive(Debug)]
Expand Down Expand Up @@ -77,13 +78,13 @@ impl Database {
as_str(thm_label)
)?;
if let Some(comment) = stmt.associated_comment() {
lazy_static::lazy_static! {
static ref LEADING_WHITESPACE: Regex = Regex::new(r"\n +").unwrap();
}
static LEADING_WHITESPACE: OnceLock<Regex> = OnceLock::new();
let leading_whitespace =
LEADING_WHITESPACE.get_or_init(|| Regex::new(r"\n +").unwrap());
let mut span = comment.span();
span.start += 2;
span.end -= 3;
let cstr = LEADING_WHITESPACE.replace_all(
let cstr = leading_whitespace.replace_all(
as_str(span.as_ref(&comment.segment().segment.buffer)),
"\n ",
);
Expand Down
39 changes: 20 additions & 19 deletions metamath-rs/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use std::collections::hash_map::Entry;
use std::fmt::Debug;
use std::mem;
use std::str;
use std::sync::Arc;
use std::sync::{Arc, OnceLock};

/// State used by the scanning process
#[derive(Default)]
Expand Down Expand Up @@ -1108,21 +1108,23 @@ impl HeadingComment {
/// or it is malformed.
#[must_use]
pub fn parse(buf: &[u8], lvl: HeadingLevel, sp: Span) -> Option<Self> {
lazy_static::lazy_static! {
static ref MAJOR_PART: Regex =
Regex::new(r"^[ \r\n]+#{4,}\r?\n *([^\n]*)\r?\n#{4,}\r?\n").unwrap();
static ref SECTION: Regex =
Regex::new(r"^[ \r\n]+(?:#\*){2,}#?\r?\n *([^\n]*)\r?\n(?:#\*){2,}#?\r?\n").unwrap();
static ref SUBSECTION: Regex =
Regex::new(r"^[ \r\n]+(?:=-){2,}=?\r?\n *([^\n]*)\r?\n(?:=-){2,}=?\r?\n").unwrap();
static ref SUBSUBSECTION: Regex =
Regex::new(r"^[ \r\n]+(?:-\.){2,}-?\r?\n *([^\n]*)\r?\n(?:-\.){2,}-?\r?\n").unwrap();
}
static MAJOR_PART: OnceLock<Regex> = OnceLock::new();
static SECTION: OnceLock<Regex> = OnceLock::new();
static SUBSECTION: OnceLock<Regex> = OnceLock::new();
static SUBSUBSECTION: OnceLock<Regex> = OnceLock::new();
let regex = match lvl {
HeadingLevel::MajorPart => &*MAJOR_PART,
HeadingLevel::Section => &*SECTION,
HeadingLevel::SubSection => &*SUBSECTION,
HeadingLevel::SubSubSection => &*SUBSUBSECTION,
HeadingLevel::MajorPart => MAJOR_PART.get_or_init(|| {
Regex::new(r"^[ \r\n]+#{4,}\r?\n *([^\n]*)\r?\n#{4,}\r?\n").unwrap()
}),
HeadingLevel::Section => SECTION.get_or_init(|| {
Regex::new(r"^[ \r\n]+(?:#\*){2,}#?\r?\n *([^\n]*)\r?\n(?:#\*){2,}#?\r?\n").unwrap()
}),
HeadingLevel::SubSection => SUBSECTION.get_or_init(|| {
Regex::new(r"^[ \r\n]+(?:=-){2,}=?\r?\n *([^\n]*)\r?\n(?:=-){2,}=?\r?\n").unwrap()
}),
HeadingLevel::SubSubSection => SUBSUBSECTION.get_or_init(|| {
Regex::new(r"^[ \r\n]+(?:-\.){2,}-?\r?\n *([^\n]*)\r?\n(?:-\.){2,}-?\r?\n").unwrap()
}),
_ => unreachable!(),
};
let groups = regex.captures(sp.as_ref(buf))?;
Expand All @@ -1136,10 +1138,9 @@ impl HeadingComment {
/// Parses a mathbox heading comment, returning the span of the author name.
#[must_use]
pub fn parse_mathbox_header(&self, buf: &[u8]) -> Option<Span> {
lazy_static::lazy_static! {
static ref MATHBOX_FOR: Regex = Regex::new(r"^Mathbox for (.*)$").unwrap();
}
let m = MATHBOX_FOR.captures(self.header.as_ref(buf))?.get(1)?;
static MATHBOX_FOR: OnceLock<Regex> = OnceLock::new();
let mathbox_for = MATHBOX_FOR.get_or_init(|| Regex::new(r"^Mathbox for (.*)$").unwrap());
let m = mathbox_for.captures(self.header.as_ref(buf))?.get(1)?;
Some(Span::new2(
self.header.start + m.start() as u32,
self.header.start + m.end() as u32,
Expand Down
31 changes: 16 additions & 15 deletions metamath-rs/src/verify_markup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ use crate::util::{HashMap, HashSet};
use crate::{Database, Span, StatementRef, StatementType};
use regex::bytes::Regex;
use std::ops::Range;
use std::sync::OnceLock;

lazy_static::lazy_static! {
static ref WINDOWS_RESERVED_NAMES: Regex =
Regex::new("(?i-u)^(?:CON|PRN|AUX|NUL|(?:COM|LPT)[1-9])$").unwrap();
fn windows_reserved_names() -> &'static Regex {
static WINDOWS_RESERVED_NAMES: OnceLock<Regex> = OnceLock::new();
WINDOWS_RESERVED_NAMES
.get_or_init(|| Regex::new("(?i-u)^(?:CON|PRN|AUX|NUL|(?:COM|LPT)[1-9])$").unwrap())
}

impl Database {
Expand Down Expand Up @@ -69,7 +71,7 @@ impl Database {
stmt.address(),
Diagnostic::MMReservedLabel(stmt.label_span()),
))
} else if WINDOWS_RESERVED_NAMES.is_match(stmt.label()) {
} else if windows_reserved_names().is_match(stmt.label()) {
diags.push((
stmt.address(),
Diagnostic::WindowsReservedLabel(stmt.label_span()),
Expand Down Expand Up @@ -421,12 +423,11 @@ fn verify_markup_comment(
}

fn check_uninterpreted_html(buf: &[u8], sp: Span, diag: &mut impl FnMut(Diagnostic)) {
lazy_static::lazy_static! {
static ref HTML: Regex = Regex::new("(?i-u)</?HTML>").unwrap();
}
static HTML: OnceLock<Regex> = OnceLock::new();
let html = HTML.get_or_init(|| Regex::new("(?i-u)</?HTML>").unwrap());
let text = sp.as_ref(buf);
if HTML.is_match(text) {
if let Some(m) = HTML.find(text) {
if html.is_match(text) {
if let Some(m) = html.find(text) {
diag(Diagnostic::UninterpretedHtml(Span::new2(
sp.start + m.start() as u32,
sp.start + m.end() as u32,
Expand Down Expand Up @@ -542,13 +543,13 @@ impl Bibliography {
/// Parse bibliography file data out of the given [`SourceInfo`], and put
/// any parse errors in `diags`.
pub fn parse<'a>(source: &'a SourceInfo, diags: &mut Vec<(&'a SourceInfo, BibError)>) -> Self {
lazy_static::lazy_static! {
static ref A_NAME: Regex =
#[allow(clippy::invalid_regex)] // https://github.com/rust-lang/rust-clippy/issues/10825
Regex::new("(?i-u)<a[[:space:]]name=['\"]?([^&>]*?)['\"]?>").unwrap();
}
static A_NAME: OnceLock<Regex> = OnceLock::new();
let a_name = A_NAME.get_or_init(|| {
#[allow(clippy::invalid_regex)] // https://github.com/rust-lang/rust-clippy/issues/10825
Regex::new("(?i-u)<a[[:space:]]name=['\"]?([^&>]*?)['\"]?>").unwrap()
});
let mut bib = HashMap::default();
for captures in A_NAME.captures_iter(&source.text) {
for captures in a_name.captures_iter(&source.text) {
let m = captures.get(0).unwrap();
let sp = Span::new(m.start(), m.end());
if let Some(sp2) = bib.insert(captures.get(1).unwrap().as_bytes().into(), sp) {
Expand Down

0 comments on commit 1428ea9

Please sign in to comment.