mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-24 07:39:27 +01:00
Merge pull request #1037 from YouKnow-sys/master
Changed `escape` functions in both html and markdown utils to do less allocations and also be slightly faster
This commit is contained in:
commit
f811061566
2 changed files with 33 additions and 21 deletions
|
@ -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 \
|
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
|
||||||
without using its output does nothing useful"]
|
without using its output does nothing useful"]
|
||||||
pub fn escape(s: &str) -> String {
|
pub fn escape(s: &str) -> String {
|
||||||
s.replace('&', "&").replace('<', "<").replace('>', ">")
|
s.chars().fold(String::with_capacity(s.len()), |mut s, c| {
|
||||||
|
match c {
|
||||||
|
'&' => s.push_str("&"),
|
||||||
|
'<' => s.push_str("<"),
|
||||||
|
'>' => s.push_str(">"),
|
||||||
|
c => s.push(c),
|
||||||
|
}
|
||||||
|
s
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
|
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
|
||||||
|
|
|
@ -109,24 +109,16 @@ pub fn code_inline(s: &str) -> String {
|
||||||
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
|
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
|
||||||
without using its output does nothing useful"]
|
without using its output does nothing useful"]
|
||||||
pub fn escape(s: &str) -> String {
|
pub fn escape(s: &str) -> String {
|
||||||
s.replace('_', r"\_")
|
const CHARS: [char; 18] =
|
||||||
.replace('*', r"\*")
|
['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'];
|
||||||
.replace('[', r"\[")
|
|
||||||
.replace(']', r"\]")
|
s.chars().fold(String::with_capacity(s.len()), |mut s, c| {
|
||||||
.replace('(', r"\(")
|
if CHARS.contains(&c) {
|
||||||
.replace(')', r"\)")
|
s.push('\\');
|
||||||
.replace('~', r"\~")
|
}
|
||||||
.replace('`', r"\`")
|
s.push(c);
|
||||||
.replace('>', r"\>")
|
s
|
||||||
.replace('#', r"\#")
|
})
|
||||||
.replace('+', r"\+")
|
|
||||||
.replace('-', r"\-")
|
|
||||||
.replace('=', r"\=")
|
|
||||||
.replace('|', r"\|")
|
|
||||||
.replace('{', r"\{")
|
|
||||||
.replace('}', r"\}")
|
|
||||||
.replace('.', r"\.")
|
|
||||||
.replace('!', r"\!")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Escapes all markdown special characters specific for the inline link URL
|
/// Escapes all markdown special characters specific for the inline link URL
|
||||||
|
@ -134,7 +126,13 @@ pub fn escape(s: &str) -> String {
|
||||||
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
|
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
|
||||||
without using its output does nothing useful"]
|
without using its output does nothing useful"]
|
||||||
pub fn escape_link_url(s: &str) -> String {
|
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
|
/// Escapes all markdown special characters specific for the code block (``` and
|
||||||
|
@ -142,7 +140,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 \
|
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
|
||||||
without using its output does nothing useful"]
|
without using its output does nothing useful"]
|
||||||
pub fn escape_code(s: &str) -> String {
|
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 \
|
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
|
||||||
|
|
Loading…
Add table
Reference in a new issue