Commit 803ce801 authored by Raymond Hettinger's avatar Raymond Hettinger

Simplify and speed-up unquote().

parent 957b1266
...@@ -1049,23 +1049,18 @@ def splitgophertype(selector): ...@@ -1049,23 +1049,18 @@ def splitgophertype(selector):
return selector[1], selector[2:] return selector[1], selector[2:]
return None, selector return None, selector
_hextochr = dict(('%02x' % i, chr(i)) for i in range(256))
_hextochr.update(('%02X' % i, chr(i)) for i in range(256))
def unquote(s): def unquote(s):
"""unquote('abc%20def') -> 'abc def'.""" """unquote('abc%20def') -> 'abc def'."""
mychr = chr res = s.split('%')
myatoi = int for i in xrange(1, len(res)):
list = s.split('%') item = res[i]
res = [list[0]]
myappend = res.append
del list[0]
for item in list:
if item[1:2]:
try: try:
myappend(mychr(myatoi(item[:2], 16)) res[i] = _hextochr[item[:2]] + item[2:]
+ item[2:]) except KeyError:
except ValueError: res[i] = '%' + item
myappend('%' + item)
else:
myappend('%' + item)
return "".join(res) return "".join(res)
def unquote_plus(s): def unquote_plus(s):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment