]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/escape.rs
Merge commit 'c4416f20dcaec5d93077f72470e83e150fb923b1' into sync-rustfmt
[rust.git] / src / librustdoc / html / escape.rs
1 //! HTML escaping.
2 //!
3 //! This module contains one unit struct, which can be used to HTML-escape a
4 //! string of text (for use in a format string).
5
6 use std::fmt;
7
8 /// Wrapper struct which will emit the HTML-escaped version of the contained
9 /// string when passed to a format string.
10 pub(crate) struct Escape<'a>(pub &'a str);
11
12 impl<'a> fmt::Display for Escape<'a> {
13     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
14         // Because the internet is always right, turns out there's not that many
15         // characters to escape: http://stackoverflow.com/questions/7381974
16         let Escape(s) = *self;
17         let pile_o_bits = s;
18         let mut last = 0;
19         for (i, ch) in s.char_indices() {
20             let s = match ch {
21                 '>' => "&gt;",
22                 '<' => "&lt;",
23                 '&' => "&amp;",
24                 '\'' => "&#39;",
25                 '"' => "&quot;",
26                 _ => continue,
27             };
28             fmt.write_str(&pile_o_bits[last..i])?;
29             fmt.write_str(s)?;
30             // NOTE: we only expect single byte characters here - which is fine as long as we
31             // only match single byte characters
32             last = i + 1;
33         }
34
35         if last < s.len() {
36             fmt.write_str(&pile_o_bits[last..])?;
37         }
38         Ok(())
39     }
40 }