1
#[must_use]
2
12
pub fn html_escape(s: &str) -> String {
3
12
    let mut out = String::with_capacity(s.len());
4
12
    for c in s.chars() {
5
186
        match c {
6
5
            '&' => out.push_str("&"),
7
7
            '<' => out.push_str("&lt;"),
8
7
            '>' => out.push_str("&gt;"),
9
6
            '"' => out.push_str("&quot;"),
10
6
            '\'' => out.push_str("&#39;"),
11
155
            _ => out.push(c),
12
        }
13
    }
14
12
    out
15
12
}
16

            
17
/// Inverse of [`html_escape`]; `&amp;` goes last so `&amp;lt;` stays `&lt;`.
18
#[must_use]
19
4
pub fn html_unescape(s: &str) -> String {
20
4
    s.replace("&lt;", "<")
21
4
        .replace("&gt;", ">")
22
4
        .replace("&quot;", "\"")
23
4
        .replace("&#39;", "'")
24
4
        .replace("&amp;", "&")
25
4
}
26

            
27
#[cfg(test)]
28
mod tests;