diff options
author | Guido van Rossum <guido@python.org> | 1998-06-28 23:49:35 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1998-06-28 23:49:35 (GMT) |
commit | 52e86ad05b3b0471bb56a66c29c6a8c6d864d9a2 (patch) | |
tree | e9531a1ff2d90ca553df4591cdcca6432334ec38 | |
parent | 002f7aae083b187a850818eac9045a3756b2a326 (diff) | |
download | cpython-52e86ad05b3b0471bb56a66c29c6a8c6d864d9a2.zip cpython-52e86ad05b3b0471bb56a66c29c6a8c6d864d9a2.tar.gz cpython-52e86ad05b3b0471bb56a66c29c6a8c6d864d9a2.tar.bz2 |
Speed-up unquote(), inspired by post from Daniel Walton.
-rw-r--r-- | Lib/urllib.py | 34 |
1 files changed, 16 insertions, 18 deletions
diff --git a/Lib/urllib.py b/Lib/urllib.py index e21c9a2..ecf5057 100644 --- a/Lib/urllib.py +++ b/Lib/urllib.py @@ -854,25 +854,23 @@ def splitgophertype(selector): return selector[1], selector[2:] return None, selector -_quoteprog = None def unquote(s): - global _quoteprog - if _quoteprog is None: - import re - _quoteprog = re.compile('%[0-9a-fA-F][0-9a-fA-F]') - - i = 0 - n = len(s) - res = [] - while 0 <= i < n: - match = _quoteprog.search(s, i) - if not match: - res.append(s[i:]) - break - j = match.start(0) - res.append(s[i:j] + chr(string.atoi(s[j+1:j+3], 16))) - i = j+3 - return string.joinfields(res, '') + mychr = chr + myatoi = string.atoi + list = string.split(s, '%') + 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: + myappend(item) + else: + myappend(item) + return string.join(res, "") def unquote_plus(s): if '+' in s: |