diff options
author | Raymond Hettinger <python@rcn.com> | 2005-09-10 06:49:04 +0000 |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2005-09-10 06:49:04 +0000 |
commit | 9e99e35df5cb909c8c5db70df043d12abd420c90 (patch) | |
tree | d0901bf82707993827912d0e30e0633489dc2808 /Lib/urllib.py | |
parent | f736e7401e36db8e0ca26a940647259b8d6fc5db (diff) | |
download | cpython-9e99e35df5cb909c8c5db70df043d12abd420c90.tar.gz |
Simplify and speed-up unquote().
Diffstat (limited to 'Lib/urllib.py')
-rw-r--r-- | Lib/urllib.py | 25 |
1 files changed, 10 insertions, 15 deletions
diff --git a/Lib/urllib.py b/Lib/urllib.py index 2889b3dbfb..113b828dc6 100644 --- a/Lib/urllib.py +++ b/Lib/urllib.py @@ -1049,23 +1049,18 @@ def splitgophertype(selector): return selector[1], selector[2:] 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): """unquote('abc%20def') -> 'abc def'.""" - mychr = chr - myatoi = int - list = s.split('%') - res = [list[0]] - myappend = res.append - del list[0] - for item in list: - if item[1:2]: - try: - myappend(mychr(myatoi(item[:2], 16)) - + item[2:]) - except ValueError: - myappend('%' + item) - else: - myappend('%' + item) + res = s.split('%') + for i in xrange(1, len(res)): + item = res[i] + try: + res[i] = _hextochr[item[:2]] + item[2:] + except KeyError: + res[i] = '%' + item return "".join(res) def unquote_plus(s): |