-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Cow in printf rewrite rule (#7986)
Small thing that bothered me when looking into the regex update.
- Loading branch information
1 parent
7da4e28
commit 84f7391
Showing
2 changed files
with
31 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,21 @@ | ||
use once_cell::sync::Lazy; | ||
use regex::{Captures, Regex}; | ||
use std::borrow::Cow; | ||
|
||
static CURLY_BRACES: Lazy<Regex> = Lazy::new(|| Regex::new(r"(\\N\{[^}]+})|([{}])").unwrap()); | ||
|
||
pub(super) fn curly_escape(text: &str) -> String { | ||
pub(super) fn curly_escape(text: &str) -> Cow<'_, str> { | ||
// Match all curly braces. This will include named unicode escapes (like | ||
// \N{SNOWMAN}), which we _don't_ want to escape, so take care to preserve them. | ||
CURLY_BRACES | ||
.replace_all(text, |caps: &Captures| { | ||
if let Some(match_) = caps.get(1) { | ||
match_.as_str().to_string() | ||
CURLY_BRACES.replace_all(text, |caps: &Captures| { | ||
if let Some(match_) = caps.get(1) { | ||
match_.as_str().to_string() | ||
} else { | ||
if &caps[2] == "{" { | ||
"{{".to_string() | ||
} else { | ||
if &caps[2] == "{" { | ||
"{{".to_string() | ||
} else { | ||
"}}".to_string() | ||
} | ||
"}}".to_string() | ||
} | ||
}) | ||
.to_string() | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters