aboutsummaryrefslogtreecommitdiffstats
path: root/askama_escape/src
diff options
context:
space:
mode:
authorLibravatar René Kijewski <rene.kijewski@fu-berlin.de>2023-03-30 14:12:15 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-03-30 14:49:15 +0200
commit3b830b0fee51306f0100bb27292fd8dd09bf08f3 (patch)
tree38a0fd09256bd9f266464979f58dabd086af117a /askama_escape/src
parent6daf343e8b1b99cb776e244f9f8e658e1887e633 (diff)
downloadaskama-3b830b0fee51306f0100bb27292fd8dd09bf08f3.tar.gz
askama-3b830b0fee51306f0100bb27292fd8dd09bf08f3.tar.bz2
askama-3b830b0fee51306f0100bb27292fd8dd09bf08f3.zip
Use lookup table
Diffstat (limited to '')
-rw-r--r--askama_escape/src/lib.rs18
1 files changed, 13 insertions, 5 deletions
diff --git a/askama_escape/src/lib.rs b/askama_escape/src/lib.rs
index ef6c1f9..e09a32d 100644
--- a/askama_escape/src/lib.rs
+++ b/askama_escape/src/lib.rs
@@ -113,12 +113,20 @@ impl Escaper for Html {
{
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;");
+ table
+ };
+
let escaped = match byte {
- b'<' => Some(&"&lt;"),
- b'>' => Some(&"&gt;"),
- b'&' => Some(&"&amp;"),
- b'"' => Some(&"&quot;"),
- b'\'' => Some(&"&#x27;"),
+ MIN_CHAR..=MAX_CHAR => TABLE[(byte - MIN_CHAR) as usize],
_ => None,
};
if let Some(escaped) = escaped {