Changed escape functions in both html and markdown utils to do less allocations and also be slightly faster

This commit is contained in:
YouKnow 2024-04-03 04:43:41 +03:30
parent fb2865b7cc
commit 67ca69c142
2 changed files with 34 additions and 21 deletions

View file

@ -101,7 +101,15 @@ pub fn code_inline(s: &str) -> String {
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn escape(s: &str) -> String {
s.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;")
s.chars().fold(String::with_capacity(s.len()), |mut s, c| {
match c {
'&' => s.push_str("&amp;"),
'<' => s.push_str("&lt;"),
'>' => s.push_str("&gt;"),
c => s.push(c),
}
s
})
}
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \

View file

@ -109,24 +109,17 @@ pub fn code_inline(s: &str) -> String {
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn escape(s: &str) -> String {
s.replace('_', r"\_")
.replace('*', r"\*")
.replace('[', r"\[")
.replace(']', r"\]")
.replace('(', r"\(")
.replace(')', r"\)")
.replace('~', r"\~")
.replace('`', r"\`")
.replace('>', r"\>")
.replace('#', r"\#")
.replace('+', r"\+")
.replace('-', r"\-")
.replace('=', r"\=")
.replace('|', r"\|")
.replace('{', r"\{")
.replace('}', r"\}")
.replace('.', r"\.")
.replace('!', r"\!")
const CHARS: [char; 18] = [
'_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!',
];
s.chars().fold(String::with_capacity(s.len()), |mut s, c| {
if CHARS.contains(&c) {
s.push('\\');
}
s.push(c);
s
})
}
/// Escapes all markdown special characters specific for the inline link URL
@ -134,7 +127,13 @@ pub fn escape(s: &str) -> String {
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn escape_link_url(s: &str) -> String {
s.replace('`', r"\`").replace(')', r"\)")
s.chars().fold(String::with_capacity(s.len()), |mut s, c| {
if ['`', ')'].contains(&c) {
s.push('\\');
}
s.push(c);
s
})
}
/// Escapes all markdown special characters specific for the code block (``` and
@ -142,7 +141,13 @@ pub fn escape_link_url(s: &str) -> String {
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn escape_code(s: &str) -> String {
s.replace('\\', r"\\").replace('`', r"\`")
s.chars().fold(String::with_capacity(s.len()), |mut s, c| {
if ['`', '\\'].contains(&c) {
s.push('\\');
}
s.push(c);
s
})
}
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \