Skip to content

Speed up std::fmt::Write::write_str for maud::Escaper. #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions maud/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("&amp;"),
b'<' => self.0.push_str("&lt;"),
b'>' => self.0.push_str("&gt;"),
b'"' => self.0.push_str("&quot;"),
_ => unsafe { self.0.as_mut_vec().push(b) },
b'&' => push!("&amp;"),
b'<' => push!("&lt;"),
b'>' => push!("&gt;"),
b'"' => push!("&quot;"),
_ => {}
}
}
unsafe {
self.0.as_mut_vec().extend_from_slice(&bytes[last..]);
}
Ok(())
}
}
Expand Down