Skip to content
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

Speed-up HTML escaping by using equal sized entities #55

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
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
73 changes: 50 additions & 23 deletions rinja/src/filters/escape.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::convert::Infallible;
use std::fmt::{self, Display, Formatter, Write};
use std::num::NonZeroU8;
use std::str;

/// Marks a string (or other `Display` type) as safe
Expand Down Expand Up @@ -59,37 +60,63 @@ pub fn e(text: impl fmt::Display, escaper: impl Escaper) -> Result<impl Display,

/// Escape characters in a safe way for HTML texts and attributes
///
/// * `<` => `&lt;`
/// * `>` => `&gt;`
/// * `&` => `&amp;`
/// * `"` => `&quot;`
/// * `'` => `&#x27;`
/// * `"` => `&#34;`
/// * `&` => `&#38;`
/// * `'` => `&#39;`
/// * `<` => `&#60;`
/// * `>` => `&#62;`
#[derive(Debug, Clone, Copy, Default)]
pub struct Html;

impl Escaper for Html {
fn write_escaped_str<W: Write>(&self, mut fmt: W, string: &str) -> fmt::Result {
let mut escaped_buf = *b"&#__;";
let mut last = 0;

for (index, byte) in string.bytes().enumerate() {
const MIN_CHAR: u8 = b'"';
const MAX_CHAR: u8 = b'>';
const TABLE: [Option<&&str>; (MAX_CHAR - MIN_CHAR + 1) as usize] = {
let mut table = [None; (MAX_CHAR - MIN_CHAR + 1) as usize];
table[(b'<' - MIN_CHAR) as usize] = Some(&"&lt;");
table[(b'>' - MIN_CHAR) as usize] = Some(&"&gt;");
table[(b'&' - MIN_CHAR) as usize] = Some(&"&amp;");
table[(b'"' - MIN_CHAR) as usize] = Some(&"&quot;");
table[(b'\'' - MIN_CHAR) as usize] = Some(&"&#x27;");

struct Table {
_align: [usize; 0],
lookup: [Option<[NonZeroU8; 2]>; (MAX_CHAR - MIN_CHAR + 1) as usize],
}

const TABLE: Table = {
const fn n(c: u8) -> Option<[NonZeroU8; 2]> {
let n0 = match NonZeroU8::new(c / 10 + b'0') {
Some(n) => n,
None => panic!(),
};
let n1 = match NonZeroU8::new(c % 10 + b'0') {
Some(n) => n,
None => panic!(),
};
Some([n0, n1])
}

let mut table = Table {
_align: [],
lookup: [None; (MAX_CHAR - MIN_CHAR + 1) as usize],
};

table.lookup[(b'"' - MIN_CHAR) as usize] = n(b'"');
table.lookup[(b'&' - MIN_CHAR) as usize] = n(b'&');
table.lookup[(b'\'' - MIN_CHAR) as usize] = n(b'\'');
table.lookup[(b'<' - MIN_CHAR) as usize] = n(b'<');
table.lookup[(b'>' - MIN_CHAR) as usize] = n(b'>');
table
};

let escaped = match byte {
MIN_CHAR..=MAX_CHAR => TABLE[(byte - MIN_CHAR) as usize],
MIN_CHAR..=MAX_CHAR => TABLE.lookup[(byte - MIN_CHAR) as usize],
_ => None,
};
if let Some(escaped) = escaped {
escaped_buf[2] = escaped[0].get();
escaped_buf[3] = escaped[1].get();
fmt.write_str(&string[last..index])?;
fmt.write_str(escaped)?;
fmt.write_str(unsafe { std::str::from_utf8_unchecked(escaped_buf.as_slice()) })?;
last = index + 1;
}
}
Expand All @@ -98,11 +125,11 @@ impl Escaper for Html {

fn write_escaped_char<W: Write>(&self, mut fmt: W, c: char) -> fmt::Result {
fmt.write_str(match (c.is_ascii(), c as u8) {
(true, b'<') => "&lt;",
(true, b'>') => "&gt;",
(true, b'&') => "&amp;",
(true, b'"') => "&quot;",
(true, b'\'') => "&#x27;",
(true, b'"') => "&#34;",
(true, b'&') => "&#38;",
(true, b'\'') => "&#39;",
(true, b'<') => "&#60;",
(true, b'>') => "&#62;",
_ => return fmt.write_char(c),
})
}
Expand Down Expand Up @@ -136,10 +163,10 @@ pub trait Escaper: Copy {
#[test]
fn test_escape() {
assert_eq!(escape("", Html).unwrap().to_string(), "");
assert_eq!(escape("<&>", Html).unwrap().to_string(), "&lt;&amp;&gt;");
assert_eq!(escape("bla&", Html).unwrap().to_string(), "bla&amp;");
assert_eq!(escape("<foo", Html).unwrap().to_string(), "&lt;foo");
assert_eq!(escape("bla&h", Html).unwrap().to_string(), "bla&amp;h");
assert_eq!(escape("<&>", Html).unwrap().to_string(), "&#60;&#38;&#62;");
assert_eq!(escape("bla&", Html).unwrap().to_string(), "bla&#38;");
assert_eq!(escape("<foo", Html).unwrap().to_string(), "&#60;foo");
assert_eq!(escape("bla&h", Html).unwrap().to_string(), "bla&#38;h");

assert_eq!(escape("", Text).unwrap().to_string(), "");
assert_eq!(escape("<&>", Text).unwrap().to_string(), "<&>");
Expand Down
2 changes: 1 addition & 1 deletion testing/tests/filter_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ struct D;
#[test]
fn filter_block_html_escape() {
let template = D;
assert_eq!(template.render().unwrap(), r#"&lt;block&gt;"#);
assert_eq!(template.render().unwrap(), r#"&#60;block&#62;"#);
}

// This test ensures that it is not escaped if it is not HTML.
Expand Down
14 changes: 7 additions & 7 deletions testing/tests/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ fn filter_escape() {
};
assert_eq!(
s.render().unwrap(),
"// my &lt;html&gt; is &quot;unsafe&quot; &amp; \
should be &#x27;escaped&#x27;"
"// my &#60;html&#62; is &#34;unsafe&#34; &#38; \
should be &#39;escaped&#39;"
);
}

Expand All @@ -42,7 +42,7 @@ fn filter_opt_escaper_none() {
assert_eq!(
t.render().unwrap(),
r#"<h1 class="title">Foo Bar</h1>
&lt;h1 class=&quot;title&quot;&gt;Foo Bar&lt;/h1&gt;
&#60;h1 class=&#34;title&#34;&#62;Foo Bar&#60;/h1&#62;
<h1 class="title">Foo Bar</h1>
<h1 class="title">Foo Bar</h1>
"#
Expand All @@ -67,9 +67,9 @@ fn filter_opt_escaper_html() {
assert_eq!(
t.render().unwrap(),
r#"<h1 class="title">Foo Bar</h1>
&lt;h1 class=&quot;title&quot;&gt;Foo Bar&lt;/h1&gt;
&lt;h1 class=&quot;title&quot;&gt;Foo Bar&lt;/h1&gt;
&lt;h1 class=&quot;title&quot;&gt;Foo Bar&lt;/h1&gt;
&#60;h1 class=&#34;title&#34;&#62;Foo Bar&#60;/h1&#62;
&#60;h1 class=&#34;title&#34;&#62;Foo Bar&#60;/h1&#62;
&#60;h1 class=&#34;title&#34;&#62;Foo Bar&#60;/h1&#62;
"#
);
}
Expand Down Expand Up @@ -329,7 +329,7 @@ fn test_json_attribute() {
};
assert_eq!(
t.render().unwrap(),
r#"<li data-name="&quot;\&quot;\u003e\u003cbutton\u003eHacked!\u003c/button\u003e&quot;"></li>"#
r#"<li data-name="&#34;\&#34;\u003e\u003cbutton\u003eHacked!\u003c/button\u003e&#34;"></li>"#
);
}

Expand Down
4 changes: 2 additions & 2 deletions testing/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct EscapeTemplate<'a> {
fn test_escape() {
let s = EscapeTemplate { name: "<>&\"'" };

assert_eq!(s.render().unwrap(), "Hello, &lt;&gt;&amp;&quot;&#x27;!");
assert_eq!(s.render().unwrap(), "Hello, &#60;&#62;&#38;&#34;&#39;!");
}

#[derive(Template)]
Expand Down Expand Up @@ -155,7 +155,7 @@ fn test_literals_escape() {
let s = LiteralsEscapeTemplate {};
assert_eq!(
s.render().unwrap(),
"A\n\r\t\\\0♥&#x27;&quot;&quot;\nA\n\r\t\\\0♥&#x27;&quot;&#x27;"
"A\n\r\t\\\0♥&#39;&#34;&#34;\nA\n\r\t\\\0♥&#39;&#34;&#39;"
);
}

Expand Down
2 changes: 1 addition & 1 deletion testing/tests/whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn test_extra_whitespace() {
template.nested_1.nested_2.hash.insert("key", "value");
assert_eq!(
template.render().unwrap(),
"\n0\n0\n0\n0\n\n\n\n0\n0\n0\n0\n0\n\na0\na1\nvalue\n\n\n\n\n\n[\n &quot;a0&quot;,\n &quot;a1&quot;,\n &quot;a2&quot;,\n &quot;a3&quot;\n]\n[\n &quot;a0&quot;,\n &quot;a1&quot;,\n &quot;a2&quot;,\n &quot;a3&quot;\n][\n &quot;a0&quot;,\n &quot;a1&quot;,\n &quot;a2&quot;,\n &quot;a3&quot;\n]\n[\n &quot;a1&quot;\n][\n &quot;a1&quot;\n]\n[\n &quot;a1&quot;,\n &quot;a2&quot;\n][\n &quot;a1&quot;,\n &quot;a2&quot;\n]\n[\n &quot;a1&quot;\n][\n &quot;a1&quot;\n]1-1-1\n3333 3\n2222 2\n0000 0\n3333 3\n\ntruefalse\nfalsefalsefalse\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
"\n0\n0\n0\n0\n\n\n\n0\n0\n0\n0\n0\n\na0\na1\nvalue\n\n\n\n\n\n[\n &#34;a0&#34;,\n &#34;a1&#34;,\n &#34;a2&#34;,\n &#34;a3&#34;\n]\n[\n &#34;a0&#34;,\n &#34;a1&#34;,\n &#34;a2&#34;,\n &#34;a3&#34;\n][\n &#34;a0&#34;,\n &#34;a1&#34;,\n &#34;a2&#34;,\n &#34;a3&#34;\n]\n[\n &#34;a1&#34;\n][\n &#34;a1&#34;\n]\n[\n &#34;a1&#34;,\n &#34;a2&#34;\n][\n &#34;a1&#34;,\n &#34;a2&#34;\n]\n[\n &#34;a1&#34;\n][\n &#34;a1&#34;\n]1-1-1\n3333 3\n2222 2\n0000 0\n3333 3\n\ntruefalse\nfalsefalsefalse\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
);
}

Expand Down
Loading