Skip to content

Commit

Permalink
Use std::sync::OnceLock instead of once_cell crate in parse_display_d…
Browse files Browse the repository at this point in the history
…erive.
  • Loading branch information
frozenlib committed Dec 1, 2023
1 parent e60715d commit a155e9a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
1 change: 0 additions & 1 deletion parse-display-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ quote = "1.0.29"
proc-macro2 = "1.0.64"
regex = "1.9.1"
regex-syntax = "0.7.4"
once_cell = "1.18.0"
structmeta = "0.2.0"
10 changes: 4 additions & 6 deletions parse-display-derive/src/format_syntax.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use once_cell::sync::Lazy;
use proc_macro2::Span;
use regex::*;

use std::fmt::{Display, Formatter};
use std::str::FromStr;

use proc_macro2::Span;

#[derive(Debug, PartialEq, Eq)]
pub enum Sign {
Plus,
Expand Down Expand Up @@ -96,7 +94,7 @@ impl<'a> FormatSpec<'a> {
}

pub fn parse(s: &'a str) -> std::result::Result<Self, FormatParseError> {
static RE: Lazy<Regex> = lazy_regex!(
let re = regex!(
"^\
((?<fill>.)?\
(?<align>[<>^]))??\
Expand All @@ -116,7 +114,7 @@ impl<'a> FormatSpec<'a> {
$"
);

let c = RE.captures(s).ok_or(FormatParseError)?;
let c = re.captures(s).ok_or(FormatParseError)?;
let fill = c.name("fill").map(|m| m.as_str().chars().next().unwrap());
let align = c.name("align").map(|m| m.as_str().parse().unwrap());
let sign = c.name("sign").map(|m| match m.as_str() {
Expand Down
20 changes: 9 additions & 11 deletions parse-display-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ mod syn_utils;
mod format_syntax;

use crate::{format_syntax::*, regex_utils::*, syn_utils::*};
use once_cell::sync::Lazy;
use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote};
use regex::{Captures, Regex};
Expand Down Expand Up @@ -329,9 +328,8 @@ impl<'a> ParserBuilder<'a> {
}

fn push_regex(&mut self, s: &LitStr, context: &DisplayContext) -> Result<()> {
static REGEX_NUMBER: Lazy<Regex> = lazy_regex!("^[0-9]+$");
static REGEX_CAPTURE: Lazy<Regex> =
lazy_regex!(r"(?<esc>\\*)\(\?(?<p>P?)<(?<key>[_0-9a-zA-Z.]*)>");
let regex_number = regex!("^[0-9]+$");
let regex_capture = regex!(r"(?<esc>\\*)\(\?(?<p>P?)<(?<key>[_0-9a-zA-Z.]*)>");
const IDX_ESC: usize = 1;
const IDX_P: usize = 2;
const IDX_KEY: usize = 3;
Expand All @@ -340,7 +338,7 @@ impl<'a> ParserBuilder<'a> {
}

let text = s.value();
let text_debug = REGEX_CAPTURE.replace_all(&text, |c: &Captures| {
let text_debug = regex_capture.replace_all(&text, |c: &Captures| {
let esc = &c[IDX_ESC];
if is_escaped(esc) {
return c[0].to_owned();
Expand All @@ -351,7 +349,7 @@ impl<'a> ParserBuilder<'a> {
} else {
key.replace('.', "_")
};
let key = REGEX_NUMBER.replace(&key, "_$0");
let key = regex_number.replace(&key, "_$0");
format!("{esc}(?<{key}>")
});
if let Err(e) = regex_syntax::ast::parse::Parser::new().parse(&text_debug) {
Expand All @@ -361,7 +359,7 @@ impl<'a> ParserBuilder<'a> {
let mut has_capture = false;
let mut has_capture_empty = false;
let mut p = "";
let mut text = try_replace_all(&REGEX_CAPTURE, &text, |c: &Captures| -> Result<String> {
let mut text = try_replace_all(regex_capture, &text, |c: &Captures| -> Result<String> {
let esc = &c[IDX_ESC];
if is_escaped(esc) {
return Ok(c[0].to_owned());
Expand Down Expand Up @@ -989,8 +987,8 @@ impl DisplayFormat {
Self::parse(&s.value(), s.span())
}
fn parse(mut s: &str, span: Span) -> Result<DisplayFormat> {
static REGEX_STR: Lazy<Regex> = lazy_regex!(r"^[^{}]+");
static REGEX_VAR: Lazy<Regex> = lazy_regex!(r"^\{([^:{}]*)(?::([^}]*))?\}");
let regex_str = regex!(r"^[^{}]+");
let regex_var = regex!(r"^\{([^:{}]*)(?::([^}]*))?\}");
let mut parts = Vec::new();
while !s.is_empty() {
if s.starts_with("{{") {
Expand All @@ -1003,12 +1001,12 @@ impl DisplayFormat {
s = &s[2..];
continue;
}
if let Some(m) = REGEX_STR.find(s) {
if let Some(m) = regex_str.find(s) {
parts.push(DisplayFormatPart::Str(m.as_str().into()));
s = &s[m.end()..];
continue;
}
if let Some(c) = REGEX_VAR.captures(s) {
if let Some(c) = regex_var.captures(s) {
let arg = c.get(1).unwrap().as_str().into();
let format_spec = c.get(2).map_or("", |x| x.as_str()).into();
parts.push(DisplayFormatPart::Var { arg, format_spec });
Expand Down
9 changes: 5 additions & 4 deletions parse-display-derive/src/regex_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ pub fn try_replace_all<R: AsRef<str>, E>(
Ok(s)
}

macro_rules! lazy_regex {
($re:expr) => {
once_cell::sync::Lazy::new(|| regex::Regex::new($re).unwrap())
};
macro_rules! regex {
($s:expr) => {{
static RE: ::std::sync::OnceLock<regex::Regex> = ::std::sync::OnceLock::new();
RE.get_or_init(|| ::regex::Regex::new($s).unwrap())
}};
}

0 comments on commit a155e9a

Please sign in to comment.