diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2019-01-10 08:53:04 +0100 |
---|---|---|
committer | Juan Aguilar <mhpoin@gmail.com> | 2019-01-12 09:16:20 +0100 |
commit | a2bdf3b138c22e057a75bfe3b0a96f946327adc8 (patch) | |
tree | 0c5c3ebbdeacf43ef641a8d812b548a5f2c03bf7 /askama_escape | |
parent | 0a68c39d126d7478566f07521b9e232d271edd59 (diff) | |
download | askama-a2bdf3b138c22e057a75bfe3b0a96f946327adc8.tar.gz askama-a2bdf3b138c22e057a75bfe3b0a96f946327adc8.tar.bz2 askama-a2bdf3b138c22e057a75bfe3b0a96f946327adc8.zip |
Slightly simplify escaping code
Diffstat (limited to '')
-rw-r--r-- | askama_escape/src/lib.rs | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/askama_escape/src/lib.rs b/askama_escape/src/lib.rs index a591a9c..48d43ca 100644 --- a/askama_escape/src/lib.rs +++ b/askama_escape/src/lib.rs @@ -53,9 +53,7 @@ pub struct EscapeWriter<'a, 'b: 'a> { impl io::Write for EscapeWriter<'_, '_> { fn write(&mut self, bytes: &[u8]) -> io::Result<usize> { - let escaped = Escaped { bytes }; - escaped - .fmt(self.fmt) + write_escaped_str(self.fmt, bytes) .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?; Ok(bytes.len()) } @@ -72,9 +70,9 @@ pub fn escape(s: &str) -> Escaped<'_> { } macro_rules! escaping_body { - ($start:ident, $i:ident, $fmt:ident, $_self:ident, $quote:expr) => {{ + ($start:ident, $i:ident, $fmt:ident, $bytes:ident, $quote:expr) => {{ if $start < $i { - $fmt.write_str(unsafe { str::from_utf8_unchecked(&$_self.bytes[$start..$i]) })?; + $fmt.write_str(unsafe { str::from_utf8_unchecked(&$bytes[$start..$i]) })?; } $fmt.write_str($quote)?; $start = $i + 1; @@ -87,23 +85,27 @@ pub struct Escaped<'a> { impl<'a> ::std::fmt::Display for Escaped<'a> { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut start = 0; - for (i, b) in self.bytes.iter().enumerate() { - if b.wrapping_sub(b'"') <= FLAG { - match *b { - b'<' => escaping_body!(start, i, fmt, self, "<"), - b'>' => escaping_body!(start, i, fmt, self, ">"), - b'&' => escaping_body!(start, i, fmt, self, "&"), - b'"' => escaping_body!(start, i, fmt, self, """), - b'\'' => escaping_body!(start, i, fmt, self, "'"), - b'/' => escaping_body!(start, i, fmt, self, "/"), - _ => (), - } + write_escaped_str(fmt, self.bytes) + } +} + +fn write_escaped_str(fmt: &mut fmt::Formatter<'_>, bytes: &[u8]) -> fmt::Result { + let mut start = 0; + for (i, b) in bytes.iter().enumerate() { + if b.wrapping_sub(b'"') <= FLAG { + match *b { + b'<' => escaping_body!(start, i, fmt, bytes, "<"), + b'>' => escaping_body!(start, i, fmt, bytes, ">"), + b'&' => escaping_body!(start, i, fmt, bytes, "&"), + b'"' => escaping_body!(start, i, fmt, bytes, """), + b'\'' => escaping_body!(start, i, fmt, bytes, "'"), + b'/' => escaping_body!(start, i, fmt, bytes, "/"), + _ => (), } } - fmt.write_str(unsafe { str::from_utf8_unchecked(&self.bytes[start..]) })?; - Ok(()) } + fmt.write_str(unsafe { str::from_utf8_unchecked(&bytes[start..]) })?; + Ok(()) } const FLAG: u8 = b'>' - b'"'; |