diff --git a/maud/src/lib.rs b/maud/src/lib.rs index 23223c53..b9c61f57 100644 --- a/maud/src/lib.rs +++ b/maud/src/lib.rs @@ -176,15 +176,29 @@ impl<'a> Escaper<'a> { impl<'a> fmt::Write for Escaper<'a> { fn write_str(&mut self, s: &str) -> fmt::Result { - for b in s.bytes() { + let bytes = s.as_bytes(); + let mut last = 0; + for (i, &b) in bytes.iter().enumerate() { + macro_rules! push { + ($str:expr) => {{ + unsafe { + self.0.as_mut_vec().extend_from_slice(&bytes[last..i]); + } + self.0.push_str($str); + last = i + 1; + }} + } match b { - b'&' => self.0.push_str("&"), - b'<' => self.0.push_str("<"), - b'>' => self.0.push_str(">"), - b'"' => self.0.push_str("""), - _ => unsafe { self.0.as_mut_vec().push(b) }, + b'&' => push!("&"), + b'<' => push!("<"), + b'>' => push!(">"), + b'"' => push!("""), + _ => {} } } + unsafe { + self.0.as_mut_vec().extend_from_slice(&bytes[last..]); + } Ok(()) } }