1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
//! Escapes special characters in `subject`. use split; /// Escapes HTML special characters < > & ' " ` in `subject`. /// /// # Arguments /// /// * `subject` - The string to escape. /// /// # Example /// /// ``` /// use voca_rs::*; /// escape::escape_html("<p>wonderful world</p>"); /// // => <p>wonderful world</p> // TODO: check for optimizations #10 // https://lise-henry.github.io/articles/optimising_strings.html pub fn escape_html(subject: &str) -> String { match subject.len() { 0 => "".to_string(), _ => subject .replace("&", "&") .replace("<", "<") .replace(">", ">") .replace("\"", """) .replace("'", "'") .replace("`", "`"), } } /// Escapes the regular expression special characters - [ ] / { } ( ) * + ? . \ ^ $ | in `subject`. /// /// # Arguments /// /// * `subject` - The string to escape. /// /// # Example /// /// ``` /// use voca_rs::*; /// escape::escape_regexp("(hours)[minutes]{seconds}"); /// // => \(hours\)\[minutes\]\{seconds\} pub fn escape_regexp(subject: &str) -> String { let key = "-[]/{}()*+?.\\^$|"; match subject.len() { 0 => "".to_string(), _ => { let mut res = String::new(); for c in split::chars(&subject) { let push_char = if key.contains(&c) { format!("\\{}", &c) } else { c.to_string() }; res.push_str(&push_char); } res } } } /// Unescapes HTML special characters from < > & " ' ` to corresponding < > & " ' ` in `subject`. /// /// # Arguments /// /// * `subject` - The string to unescape. /// /// # Example /// /// ``` /// use voca_rs::*; /// escape::unescape_html("<p>wonderful world</p>"); /// // => <p>wonderful world</p> pub fn unescape_html(subject: &str) -> String { match subject.len() { 0 => "".to_string(), _ => subject .replace("<", "<") .replace(">", ">") .replace("&", "&") .replace(""", "\"") .replace("'", "'") .replace("`", "`"), } }