From a2bdf3b138c22e057a75bfe3b0a96f946327adc8 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 10 Jan 2019 08:53:04 +0100 Subject: Slightly simplify escaping code --- askama_escape/src/lib.rs | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) (limited to 'askama_escape') 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 { - 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'"'; -- cgit