Skip to content

Commit

Permalink
WIP: chore: Replace once_cell dependency by std lib
Browse files Browse the repository at this point in the history
Open question:
- Do we want to wait for LazyLock? rust-lang/rust#109736
  • Loading branch information
caspermeijn committed May 22, 2024
1 parent b925a4f commit e3517c5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
1 change: 0 additions & 1 deletion prost-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ petgraph = { version = "0.6", default-features = false }
prost = { version = "0.12.6", path = "../prost", default-features = false }
prost-types = { version = "0.12.6", path = "../prost-types", default-features = false }
tempfile = "3"
once_cell = "1.17.1"
regex = { version = "1.8.1", default-features = false, features = ["std", "unicode-bool"] }

# feature: format
Expand Down
14 changes: 8 additions & 6 deletions prost-build/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use once_cell::sync::Lazy;
use prost_types::source_code_info::Location;
#[cfg(feature = "cleanup-markdown")]
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag};
use regex::Regex;
use std::sync::OnceLock;

/// Comments on a Protobuf item.
#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -110,12 +110,14 @@ impl Comments {
/// - escape urls as <http://foo.com>
/// - escape `[` & `]` if not already escaped and not followed by a parenthesis or bracket
fn sanitize_line(line: &str) -> String {
static RULE_URL: Lazy<Regex> = Lazy::new(|| Regex::new(r"https?://[^\s)]+").unwrap());
static RULE_BRACKETS: Lazy<Regex> =
Lazy::new(|| Regex::new(r"(^|[^\]\\])\[(([^\]]*[^\\])?)\]([^(\[]|$)").unwrap());
static RULE_URL: OnceLock<Regex> = OnceLock::new();
static RULE_BRACKETS: OnceLock<Regex> = OnceLock::new();
let rule_url = RULE_URL.get_or_init(|| Regex::new(r"https?://[^\s)]+").unwrap());
let rule_brackets = RULE_BRACKETS
.get_or_init(|| Regex::new(r"(^|[^\]\\])\[(([^\]]*[^\\])?)\]([^(\[]|$)").unwrap());

let mut s = RULE_URL.replace_all(line, r"<$0>").to_string();
s = RULE_BRACKETS.replace_all(&s, r"$1\[$2\]$4").to_string();
let mut s = rule_url.replace_all(line, r"<$0>").to_string();
s = rule_brackets.replace_all(&s, r"$1\[$2\]$4").to_string();
if Self::should_indent(&s) {
s.insert(0, ' ');
}
Expand Down

0 comments on commit e3517c5

Please sign in to comment.