lfml_escape/
lib.rs

1extern crate alloc;
2use alloc::string::String;
3
4pub fn escape_to_string(input: &str, output: &mut String) {
5    for b in input.bytes() {
6        match b {
7            b'&' => output.push_str("&"),
8            b'<' => output.push_str("&lt;"),
9            b'>' => output.push_str("&gt;"),
10            b'"' => output.push_str("&quot;"),
11            // Safety: `input` is valid utf-8.
12            _ => unsafe { output.as_mut_vec().push(b) },
13        }
14    }
15}
16
17pub fn escape_string(input: &str) -> String {
18    let mut s = String::new();
19    escape_to_string(input, &mut s);
20    s
21}
22
23#[cfg(test)]
24mod test {
25    use super::*;
26
27    #[test]
28    fn escape_works_as_expected() {
29        let mut s = String::new();
30        escape_to_string("<script>BadThings()</script>", &mut s);
31        assert_eq!(s, "&lt;script&gt;BadThings()&lt;/script&gt;");
32    }
33}