| 1 | //! Contains functions for performing XML special characters escaping. |
| 2 | |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#, markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#, fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::{Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result#, Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#}}; |
| 4 | |
| 5 | pub(craterust-analyzer cargo xml-rs 0.8.19 crate/) trait Escapesrust-analyzer cargo xml-rs 0.8.19 escape/Escapes# { |
| 6 | fn escaperust-analyzer cargo xml-rs 0.8.19 escape/Escapes#escape().(c: u8) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&'static str>; |
| 7 | |
| 8 | fn byte_needs_escapingrust-analyzer cargo xml-rs 0.8.19 escape/Escapes#byte_needs_escaping().(c: u8) -> bool { |
| 9 | Self::escaperust-analyzer cargo xml-rs 0.8.19 escape/Escapes#escape().(c).is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() |
| 10 | } |
| 11 | |
| 12 | fn str_needs_escapingrust-analyzer cargo xml-rs 0.8.19 escape/Escapes#str_needs_escaping().(s: &str) -> bool { |
| 13 | s.bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]bytes().().anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Bytes<'_>`][Iterator]any().(|c| Self::escaperust-analyzer cargo xml-rs 0.8.19 escape/Escapes#escape().(c).is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().()) |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | pub(craterust-analyzer cargo xml-rs 0.8.19 crate/) struct Escapedrust-analyzer cargo xml-rs 0.8.19 escape/Escaped#<'a, E: Escapesrust-analyzer cargo xml-rs 0.8.19 escape/Escapes#> { |
| 18 | _escape_phantomrust-analyzer cargo xml-rs 0.8.19 escape/Escaped#_escape_phantom.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<E>, |
| 19 | to_escaperust-analyzer cargo xml-rs 0.8.19 escape/Escaped#to_escape.: &'a str, |
| 20 | } |
| 21 | |
| 22 | impl<'a, E: Escapesrust-analyzer cargo xml-rs 0.8.19 escape/Escapes#> Escapedrust-analyzer cargo xml-rs 0.8.19 escape/Escaped#<'a, E> { |
| 23 | pub fn newrust-analyzer cargo xml-rs 0.8.19 escape/impl#[`Escaped<'a, E>`]new().(s: &'a str) -> Selfrust-analyzer cargo xml-rs 0.8.19 escape/impl#[`Escaped<'a, E>`] { |
| 24 | Escapedrust-analyzer cargo xml-rs 0.8.19 escape/Escaped# { |
| 25 | _escape_phantomrust-analyzer cargo xml-rs 0.8.19 escape/Escaped#_escape_phantom.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#, |
| 26 | to_escaperust-analyzer cargo xml-rs 0.8.19 escape/Escaped#to_escape.: s, |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl<'a, E: Escapesrust-analyzer cargo xml-rs 0.8.19 escape/Escapes#> Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Escapedrust-analyzer cargo xml-rs 0.8.19 escape/Escaped#<'a, E> { |
| 32 | fn fmtrust-analyzer cargo xml-rs 0.8.19 escape/impl#[`Escaped<'a, E>`][Display]fmt().(&self, f: &mut Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 33 | let mut total_remaining = self.to_escaperust-analyzer cargo xml-rs 0.8.19 escape/Escaped#to_escape.; |
| 34 | |
| 35 | // find the next occurence |
| 36 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(n) = total_remaining |
| 37 | .bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]bytes().() |
| 38 | .positionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Bytes<'_>`][Iterator]position().(E::byte_needs_escapingrust-analyzer cargo xml-rs 0.8.19 escape/Escapes#byte_needs_escaping().) |
| 39 | { |
| 40 | let (start, remaining) = total_remaining.split_atrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_at().(n); |
| 41 | |
| 42 | f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(start)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 43 | |
| 44 | // unwrap is safe because we checked is_some for position n earlier |
| 45 | let next_byte = remaining.bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]bytes().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Bytes<'_>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 46 | let replacement = E::escaperust-analyzer cargo xml-rs 0.8.19 escape/Escapes#escape().(next_byte).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 47 | f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(replacement)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 48 | |
| 49 | total_remaining = &remaining[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 50 | } |
| 51 | |
| 52 | f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(total_remaining) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | fn escape_strrust-analyzer cargo xml-rs 0.8.19 escape/escape_str().<E: Escapesrust-analyzer cargo xml-rs 0.8.19 escape/Escapes#>(s: &str) -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'_, str> { |
| 57 | if E::str_needs_escapingrust-analyzer cargo xml-rs 0.8.19 escape/Escapes#str_needs_escaping().(s) { |
| 58 | Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}", Escapedrust-analyzer cargo xml-rs 0.8.19 escape/Escaped#::<E>::newrust-analyzer cargo xml-rs 0.8.19 escape/impl#[`Escaped<'a, E>`]new().(s))) |
| 59 | } else { |
| 60 | Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(s) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | macro_rules! escapesrust-analyzer cargo xml-rs 0.8.19 escape/escapes! { |
| 65 | { |
| 66 | $name: ident, |
| 67 | $($k: expr => $v: expr),* $(,)? |
| 68 | } => { |
| 69 | pub(crate) struct $name; |
| 70 | |
| 71 | impl Escapes for $name { |
| 72 | fn escape(c: u8) -> Option<&'static str> { |
| 73 | match c { |
| 74 | $( $k => Some($v),)* |
| 75 | _ => None |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | escapesrust-analyzer cargo xml-rs 0.8.19 escape/escapes!!( |
| 83 | AttributeEscapesrust-analyzer cargo xml-rs 0.8.19 escape/AttributeEscapes#, |
| 84 | b'<' => "<", |
| 85 | b'>' => ">", |
| 86 | b'"' => """, |
| 87 | b'\'' => "'", |
| 88 | b'&' => "&", |
| 89 | b'\n' => "
", |
| 90 | b'\r' => "
", |
| 91 | ); |
| 92 | |
| 93 | escapesrust-analyzer cargo xml-rs 0.8.19 escape/escapes!!( |
| 94 | PcDataEscapesrust-analyzer cargo xml-rs 0.8.19 escape/PcDataEscapes#, |
| 95 | b'<' => "<", |
| 96 | b'&' => "&", |
| 97 | ); |
| 98 | |
| 99 | /// Performs escaping of common XML characters inside an attribute value. |
| 100 | /// |
| 101 | /// This function replaces several important markup characters with their |
| 102 | /// entity equivalents: |
| 103 | /// |
| 104 | /// * `<` → `<` |
| 105 | /// * `>` → `>` |
| 106 | /// * `"` → `"` |
| 107 | /// * `'` → `'` |
| 108 | /// * `&` → `&` |
| 109 | /// |
| 110 | /// The following characters are escaped so that attributes are printed on |
| 111 | /// a single line: |
| 112 | /// * `\n` → `
` |
| 113 | /// * `\r` → `
` |
| 114 | /// |
| 115 | /// The resulting string is safe to use inside XML attribute values or in PCDATA sections. |
| 116 | /// |
| 117 | /// Does not perform allocations if the given string does not contain escapable characters. |
| 118 | #[inline] |
| 119 | #[must_use] |
| 120 | pub fn escape_str_attributerust-analyzer cargo xml-rs 0.8.19 escape/escape_str_attribute().(s: &str) -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'_, str> { |
| 121 | escape_strrust-analyzer cargo xml-rs 0.8.19 escape/escape_str().::<AttributeEscapesrust-analyzer cargo xml-rs 0.8.19 escape/AttributeEscapes#>(s) |
| 122 | } |
| 123 | |
| 124 | /// Performs escaping of common XML characters inside PCDATA. |
| 125 | /// |
| 126 | /// This function replaces several important markup characters with their |
| 127 | /// entity equivalents: |
| 128 | /// |
| 129 | /// * `<` → `<` |
| 130 | /// * `&` → `&` |
| 131 | /// |
| 132 | /// The resulting string is safe to use inside PCDATA sections but NOT inside attribute values. |
| 133 | /// |
| 134 | /// Does not perform allocations if the given string does not contain escapable characters. |
| 135 | #[inline] |
| 136 | #[must_use] |
| 137 | pub fn escape_str_pcdatarust-analyzer cargo xml-rs 0.8.19 escape/escape_str_pcdata().(s: &str) -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'_, str> { |
| 138 | escape_strrust-analyzer cargo xml-rs 0.8.19 escape/escape_str().::<PcDataEscapesrust-analyzer cargo xml-rs 0.8.19 escape/PcDataEscapes#>(s) |
| 139 | } |
| 140 | |
| 141 | #[cfg(test)] |
| 142 | mod testsrust-analyzer cargo xml-rs 0.8.19 escape/tests/ { |
| 143 | use superrust-analyzer cargo xml-rs 0.8.19 escape/::{escape_str_attributerust-analyzer cargo xml-rs 0.8.19 escape/escape_str_attribute()., escape_str_pcdatarust-analyzer cargo xml-rs 0.8.19 escape/escape_str_pcdata().}; |
| 144 | |
| 145 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 146 | fn test_escape_str_attributerust-analyzer cargo xml-rs 0.8.19 escape/tests/test_escape_str_attribute().() { |
| 147 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escape_str_attributerust-analyzer cargo xml-rs 0.8.19 escape/escape_str_attribute().("<>'\"&\n\r"), "<>'"&

"); |
| 148 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escape_str_attributerust-analyzer cargo xml-rs 0.8.19 escape/escape_str_attribute().("no_escapes"), "no_escapes"); |
| 149 | } |
| 150 | |
| 151 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 152 | fn test_escape_str_pcdatarust-analyzer cargo xml-rs 0.8.19 escape/tests/test_escape_str_pcdata().() { |
| 153 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escape_str_pcdatarust-analyzer cargo xml-rs 0.8.19 escape/escape_str_pcdata().("<&"), "<&"); |
| 154 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escape_str_pcdatarust-analyzer cargo xml-rs 0.8.19 escape/escape_str_pcdata().("no_escapes"), "no_escapes"); |
| 155 | } |
| 156 | |
| 157 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 158 | fn test_escape_multibyte_code_pointsrust-analyzer cargo xml-rs 0.8.19 escape/tests/test_escape_multibyte_code_points().() { |
| 159 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escape_str_attributerust-analyzer cargo xml-rs 0.8.19 escape/escape_str_attribute().("☃<"), "☃<"); |
| 160 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escape_str_pcdatarust-analyzer cargo xml-rs 0.8.19 escape/escape_str_pcdata().("☃<"), "☃<"); |
| 161 | } |
| 162 | } |