diff options
author | René Kijewski <rene.kijewski@fu-berlin.de> | 2023-03-30 14:12:15 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-03-30 14:49:15 +0200 |
commit | 3b830b0fee51306f0100bb27292fd8dd09bf08f3 (patch) | |
tree | 38a0fd09256bd9f266464979f58dabd086af117a /askama_escape/src | |
parent | 6daf343e8b1b99cb776e244f9f8e658e1887e633 (diff) | |
download | askama-3b830b0fee51306f0100bb27292fd8dd09bf08f3.tar.gz askama-3b830b0fee51306f0100bb27292fd8dd09bf08f3.tar.bz2 askama-3b830b0fee51306f0100bb27292fd8dd09bf08f3.zip |
Use lookup table
Diffstat (limited to 'askama_escape/src')
-rw-r--r-- | askama_escape/src/lib.rs | 18 |
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(&"<"); + table[(b'>' - MIN_CHAR) as usize] = Some(&">"); + table[(b'&' - MIN_CHAR) as usize] = Some(&"&"); + table[(b'"' - MIN_CHAR) as usize] = Some(&"""); + table[(b'\'' - MIN_CHAR) as usize] = Some(&"'"); + table + }; + let escaped = match byte { - b'<' => Some(&"<"), - b'>' => Some(&">"), - b'&' => Some(&"&"), - b'"' => Some(&"""), - b'\'' => Some(&"'"), + MIN_CHAR..=MAX_CHAR => TABLE[(byte - MIN_CHAR) as usize], _ => None, }; if let Some(escaped) = escaped { |