aidoku/helpers/
string.rs

1extern crate alloc;
2
3use alloc::string::String;
4
5pub trait StripPrefixOrSelf {
6	fn strip_prefix_or_self<P: AsRef<str>>(&self, prefix: P) -> &str;
7}
8
9impl StripPrefixOrSelf for str {
10	fn strip_prefix_or_self<P: AsRef<str>>(&self, prefix: P) -> &str {
11		self.strip_prefix(prefix.as_ref()).unwrap_or(self)
12	}
13}
14
15pub trait PlainText {
16	fn escape_markdown(&self) -> String;
17}
18
19impl<S: AsRef<str>> PlainText for S {
20	fn escape_markdown(&self) -> String {
21		let mut markdown = String::new();
22		for char in self.as_ref().chars() {
23			if char.is_ascii_punctuation() {
24				markdown.push('\\');
25			}
26			markdown.push(char);
27		}
28		markdown.replace("\r\n", "\n").replace('\n', "  \n")
29	}
30}