aboutsummaryrefslogtreecommitdiffstats
path: root/askama_escape/src
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2019-01-10 08:53:04 +0100
committerLibravatar Juan Aguilar <mhpoin@gmail.com>2019-01-12 09:16:20 +0100
commita2bdf3b138c22e057a75bfe3b0a96f946327adc8 (patch)
tree0c5c3ebbdeacf43ef641a8d812b548a5f2c03bf7 /askama_escape/src
parent0a68c39d126d7478566f07521b9e232d271edd59 (diff)
downloadaskama-a2bdf3b138c22e057a75bfe3b0a96f946327adc8.tar.gz
askama-a2bdf3b138c22e057a75bfe3b0a96f946327adc8.tar.bz2
askama-a2bdf3b138c22e057a75bfe3b0a96f946327adc8.zip
Slightly simplify escaping code
Diffstat (limited to 'askama_escape/src')
-rw-r--r--askama_escape/src/lib.rs40
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, "&lt;"),
- b'>' => escaping_body!(start, i, fmt, self, "&gt;"),
- b'&' => escaping_body!(start, i, fmt, self, "&amp;"),
- b'"' => escaping_body!(start, i, fmt, self, "&quot;"),
- b'\'' => escaping_body!(start, i, fmt, self, "&#x27;"),
- b'/' => escaping_body!(start, i, fmt, self, "&#x2f;"),
- _ => (),
- }
+ 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, "&lt;"),
+ b'>' => escaping_body!(start, i, fmt, bytes, "&gt;"),
+ b'&' => escaping_body!(start, i, fmt, bytes, "&amp;"),
+ b'"' => escaping_body!(start, i, fmt, bytes, "&quot;"),
+ b'\'' => escaping_body!(start, i, fmt, bytes, "&#x27;"),
+ b'/' => escaping_body!(start, i, fmt, bytes, "&#x2f;"),
+ _ => (),
}
}
- 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'"';