]> Devi Nivas Git - chevron.git/commitdiff
Faster html_escape
authorDaniel Morrison <dan@morrison.ph>
Sun, 16 Nov 2014 05:28:21 +0000 (00:28 -0500)
committerDaniel Morrison <dan@morrison.ph>
Sun, 16 Nov 2014 05:36:20 +0000 (00:36 -0500)
- uses string specific speedups
- requires & be handled first
- 33.7% overall speedup… we need this

entei.py

index d1bafe352bfa42e6033f3219665f07f4f9e6c139..c53716ff18fd8f376b89a43c83bec740d08ae249 100755 (executable)
--- a/entei.py
+++ b/entei.py
@@ -207,16 +207,16 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache',
 
         html_codes = {
             '"': '&quot;',
-            '&': '&amp;',
             '<': '&lt;',
             '>': '&gt;',
         }
 
-        def escape_char(char):
-            return html_codes.get(char, char)
-
         try:
-            return ''.join(map(escape_char, string))
+            # & must be handled first
+            string = string.replace('&', '&amp;')
+            for char in html_codes:
+                string = string.replace(char, html_codes[char])
+            return string
         except TypeError:
             return ''