]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Simplify and speed-up unquote().
authorRaymond Hettinger <python@rcn.com>
Sat, 10 Sep 2005 06:49:04 +0000 (06:49 +0000)
committerRaymond Hettinger <python@rcn.com>
Sat, 10 Sep 2005 06:49:04 +0000 (06:49 +0000)
Lib/urllib.py

index 2889b3dbfb0d9fe9703f9bd7cc29219d436b45c5..113b828dc69e3ca14ef6f366e5190a34b7ea47a6 100644 (file)
@@ -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):