mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
SF #1285086: urllib.quote is too slow
Simplify and speed-up quote() function.
This commit is contained in:
parent
0ee9ba258e
commit
199d2f7997
1 changed files with 12 additions and 24 deletions
|
@ -1076,22 +1076,7 @@ def unquote_plus(s):
|
||||||
always_safe = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
always_safe = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||||
'abcdefghijklmnopqrstuvwxyz'
|
'abcdefghijklmnopqrstuvwxyz'
|
||||||
'0123456789' '_.-')
|
'0123456789' '_.-')
|
||||||
|
_safemaps = {}
|
||||||
_fast_safe_test = always_safe + '/'
|
|
||||||
_fast_safe = None
|
|
||||||
|
|
||||||
def _fast_quote(s):
|
|
||||||
global _fast_safe
|
|
||||||
if _fast_safe is None:
|
|
||||||
_fast_safe = {}
|
|
||||||
for c in _fast_safe_test:
|
|
||||||
_fast_safe[c] = c
|
|
||||||
res = list(s)
|
|
||||||
for i in range(len(res)):
|
|
||||||
c = res[i]
|
|
||||||
if not c in _fast_safe:
|
|
||||||
res[i] = '%%%02X' % ord(c)
|
|
||||||
return ''.join(res)
|
|
||||||
|
|
||||||
def quote(s, safe = '/'):
|
def quote(s, safe = '/'):
|
||||||
"""quote('abc def') -> 'abc%20def'
|
"""quote('abc def') -> 'abc%20def'
|
||||||
|
@ -1114,14 +1099,17 @@ def quote(s, safe = '/'):
|
||||||
called on a path where the existing slash characters are used as
|
called on a path where the existing slash characters are used as
|
||||||
reserved characters.
|
reserved characters.
|
||||||
"""
|
"""
|
||||||
safe = always_safe + safe
|
cachekey = (safe, always_safe)
|
||||||
if _fast_safe_test == safe:
|
try:
|
||||||
return _fast_quote(s)
|
safe_map = _safemaps[cachekey]
|
||||||
res = list(s)
|
except KeyError:
|
||||||
for i in range(len(res)):
|
safe += always_safe
|
||||||
c = res[i]
|
safe_map = {}
|
||||||
if c not in safe:
|
for i in range(256):
|
||||||
res[i] = '%%%02X' % ord(c)
|
c = chr(i)
|
||||||
|
safe_map[c] = (c in safe) and c or ('%%%02X' % i)
|
||||||
|
_safemaps[cachekey] = safe_map
|
||||||
|
res = map(safe_map.__getitem__, s)
|
||||||
return ''.join(res)
|
return ''.join(res)
|
||||||
|
|
||||||
def quote_plus(s, safe = ''):
|
def quote_plus(s, safe = ''):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue