mirror of
https://github.com/python/cpython.git
synced 2025-09-05 00:11:10 +00:00
Slightly faster (un)quoting.
This commit is contained in:
parent
f480c674b1
commit
f8abb38737
1 changed files with 8 additions and 8 deletions
|
@ -642,26 +642,26 @@ _quoteprog = regex.compile('%[0-9a-fA-F][0-9a-fA-F]')
|
||||||
def unquote(s):
|
def unquote(s):
|
||||||
i = 0
|
i = 0
|
||||||
n = len(s)
|
n = len(s)
|
||||||
res = ''
|
res = []
|
||||||
while 0 <= i < n:
|
while 0 <= i < n:
|
||||||
j = _quoteprog.search(s, i)
|
j = _quoteprog.search(s, i)
|
||||||
if j < 0:
|
if j < 0:
|
||||||
res = res + s[i:]
|
res.append(s[i:])
|
||||||
break
|
break
|
||||||
res = res + (s[i:j] + chr(string.atoi(s[j+1:j+3], 16)))
|
res.append(s[i:j] + chr(string.atoi(s[j+1:j+3], 16)))
|
||||||
i = j+3
|
i = j+3
|
||||||
return res
|
return string.joinfields(res, '')
|
||||||
|
|
||||||
always_safe = string.letters + string.digits + '_,.-'
|
always_safe = string.letters + string.digits + '_,.-'
|
||||||
def quote(s, safe = '/'):
|
def quote(s, safe = '/'):
|
||||||
safe = always_safe + safe
|
safe = always_safe + safe
|
||||||
res = ''
|
res = []
|
||||||
for c in s:
|
for c in s:
|
||||||
if c in safe:
|
if c in safe:
|
||||||
res = res + c
|
res.append(c)
|
||||||
else:
|
else:
|
||||||
res = res + '%%%02x' % ord(c)
|
res.append('%%%02x' % ord(c))
|
||||||
return res
|
return string.joinfields(res, '')
|
||||||
|
|
||||||
|
|
||||||
# Proxy handling
|
# Proxy handling
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue