summaryrefslogtreecommitdiff
path: root/Lib/html
diff options
context:
space:
mode:
authorEzio Melotti <ezio.melotti@gmail.com>2013-07-07 11:11:24 +0200
committerEzio Melotti <ezio.melotti@gmail.com>2013-07-07 11:11:24 +0200
commit4603487dc941f2e6de04664058689f21a2ff349f (patch)
tree7c2b70480473914ea303a94940dab94a5a2b0c9d /Lib/html
parent071029fac6c074c801d5dc292d250c77c189ea3f (diff)
downloadcpython-git-4603487dc941f2e6de04664058689f21a2ff349f.tar.gz
#18020: improve html.escape speed by an order of magnitude. Patch by Matt Bryant.
Diffstat (limited to 'Lib/html')
-rw-r--r--Lib/html/__init__.py13
1 files changed, 6 insertions, 7 deletions
diff --git a/Lib/html/__init__.py b/Lib/html/__init__.py
index 02652ef73c..2ad167f16a 100644
--- a/Lib/html/__init__.py
+++ b/Lib/html/__init__.py
@@ -2,11 +2,6 @@
General functions for HTML manipulation.
"""
-
-_escape_map = {ord('&'): '&amp;', ord('<'): '&lt;', ord('>'): '&gt;'}
-_escape_map_full = {ord('&'): '&amp;', ord('<'): '&lt;', ord('>'): '&gt;',
- ord('"'): '&quot;', ord('\''): '&#x27;'}
-
# NB: this is a candidate for a bytes/string polymorphic interface
def escape(s, quote=True):
@@ -16,6 +11,10 @@ def escape(s, quote=True):
characters, both double quote (") and single quote (') characters are also
translated.
"""
+ s = s.replace("&", "&amp;") # Must be done first!
+ s = s.replace("<", "&lt;")
+ s = s.replace(">", "&gt;")
if quote:
- return s.translate(_escape_map_full)
- return s.translate(_escape_map)
+ s = s.replace('"', "&quot;")
+ s = s.replace('\'', "&#x27;")
+ return s