From 67ca69c1423162945814ffbcb25f8be808775035 Mon Sep 17 00:00:00 2001 From: YouKnow Date: Wed, 3 Apr 2024 04:43:41 +0330 Subject: [PATCH 1/2] Changed escape functions in both html and markdown utils to do less allocations and also be slightly faster --- crates/teloxide/src/utils/html.rs | 10 +++++- crates/teloxide/src/utils/markdown.rs | 45 +++++++++++++++------------ 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/crates/teloxide/src/utils/html.rs b/crates/teloxide/src/utils/html.rs index 37f6f034..5fa2ee91 100644 --- a/crates/teloxide/src/utils/html.rs +++ b/crates/teloxide/src/utils/html.rs @@ -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('&', "&").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 \ diff --git a/crates/teloxide/src/utils/markdown.rs b/crates/teloxide/src/utils/markdown.rs index 4c0fa475..4d49707d 100644 --- a/crates/teloxide/src/utils/markdown.rs +++ b/crates/teloxide/src/utils/markdown.rs @@ -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 \ From 95ebe2a764e680696ae719c5560a94d4fcd2fe7d Mon Sep 17 00:00:00 2001 From: YouKnow Date: Wed, 3 Apr 2024 04:44:55 +0330 Subject: [PATCH 2/2] format the code --- crates/teloxide/src/utils/markdown.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/teloxide/src/utils/markdown.rs b/crates/teloxide/src/utils/markdown.rs index 4d49707d..ad6eb26c 100644 --- a/crates/teloxide/src/utils/markdown.rs +++ b/crates/teloxide/src/utils/markdown.rs @@ -109,9 +109,8 @@ 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 { - const CHARS: [char; 18] = [ - '_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!', - ]; + const CHARS: [char; 18] = + ['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!']; s.chars().fold(String::with_capacity(s.len()), |mut s, c| { if CHARS.contains(&c) {