diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-11-05 21:02:27 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-11-05 21:03:31 +0100 |
commit | 2e028ed903169c27748e3c717ea5b1311950a615 (patch) | |
tree | 5e68455d4eb5b15a91ee1817bd6acec43a51d780 /askama_shared/src | |
parent | 99abc3e303f7c92b1395b0cdb000ea7169963f1c (diff) | |
download | askama-2e028ed903169c27748e3c717ea5b1311950a615.tar.gz askama-2e028ed903169c27748e3c717ea5b1311950a615.tar.bz2 askama-2e028ed903169c27748e3c717ea5b1311950a615.zip |
Reorder and tweak code style a little bit
Diffstat (limited to '')
-rw-r--r-- | askama_shared/src/escaping.rs | 37 |
1 files changed, 18 insertions, 19 deletions
diff --git a/askama_shared/src/escaping.rs b/askama_shared/src/escaping.rs index 6390f0c..b967f1f 100644 --- a/askama_shared/src/escaping.rs +++ b/askama_shared/src/escaping.rs @@ -43,50 +43,49 @@ where } } -const FLAG: u8 = b'>' - b'"'; - pub fn escape(s: &str) -> Escaped { Escaped { bytes: s.as_bytes(), } } -pub struct Escaped<'a> { - bytes: &'a [u8], -} - macro_rules! escaping_body { - ($state:ident, $i:ident, $fmt:ident, $_self:ident, $quote:expr) => {{ - if $state < $i { - $fmt.write_str(unsafe { str::from_utf8_unchecked(&$_self.bytes[$state..$i]) })?; + ($start:ident, $i:ident, $fmt:ident, $_self:ident, $quote:expr) => {{ + if $start < $i { + $fmt.write_str(unsafe { str::from_utf8_unchecked(&$_self.bytes[$start..$i]) })?; } $fmt.write_str($quote)?; - $state = $i + 1; + $start = $i + 1; }}; } +pub struct Escaped<'a> { + bytes: &'a [u8], +} + impl<'a> ::std::fmt::Display for Escaped<'a> { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - let mut state = 0; + let mut start = 0; for (i, b) in self.bytes.iter().enumerate() { if b.wrapping_sub(b'"') <= FLAG { match *b { - b'<' => escaping_body!(state, i, fmt, self, "<"), - b'>' => escaping_body!(state, i, fmt, self, ">"), - b'&' => escaping_body!(state, i, fmt, self, "&"), - b'"' => escaping_body!(state, i, fmt, self, """), - b'\'' => escaping_body!(state, i, fmt, self, "'"), - b'/' => escaping_body!(state, 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, "'"), + b'/' => escaping_body!(start, i, fmt, self, "/"), _ => (), } } } - - fmt.write_str(unsafe { str::from_utf8_unchecked(&self.bytes[state..]) })?; + fmt.write_str(unsafe { str::from_utf8_unchecked(&self.bytes[start..]) })?; Ok(()) } } +const FLAG: u8 = b'>' - b'"'; + #[cfg(test)] mod tests { use super::*; |