maud_macros/
escape.rs

1// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2// !!!!!!!! PLEASE KEEP THIS IN SYNC WITH `maud/src/escape.rs` !!!!!!!!!
3// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4
5pub fn escape_to_string(input: &str, output: &mut String) {
6    for b in input.bytes() {
7        match b {
8            b'&' => output.push_str("&"),
9            b'<' => output.push_str("&lt;"),
10            b'>' => output.push_str("&gt;"),
11            b'"' => output.push_str("&quot;"),
12            _ => unsafe { output.as_mut_vec().push(b) },
13        }
14    }
15}
16
17#[cfg(test)]
18mod test {
19    use super::escape_to_string;
20
21    #[test]
22    fn it_works() {
23        let mut s = String::new();
24        escape_to_string("<script>launchMissiles()</script>", &mut s);
25        assert_eq!(s, "&lt;script&gt;launchMissiles()&lt;/script&gt;");
26    }
27}