Commits

Konstantin Lopuhin  committed f505dfc

fix quadratic slowness of unquote (caused by buliding string with inplace addition)

  • Participants
  • Parent commits 935bd2f
  • Branches unquote-faster

Comments (0)

Files changed (2)

File lib-python/2.7/urllib.py

     # fastpath
     if len(res) == 1:
         return s
-    s = res[0]
+    res_list = [res[0]]
     for item in res[1:]:
         try:
-            s += _hextochr[item[:2]] + item[2:]
+            x = _hextochr[item[:2]] + item[2:]
         except KeyError:
-            s += '%' + item
+            x = '%' + item
         except UnicodeDecodeError:
-            s += unichr(int(item[:2], 16)) + item[2:]
-    return s
+            x = unichr(int(item[:2], 16)) + item[2:]
+        res_list.append(x)
+    return ''.join(res_list)
 
 def unquote_plus(s):
     """unquote('%7e/abc+def') -> '~/abc def'"""

File lib-python/2.7/urlparse.py

     # fastpath
     if len(res) == 1:
         return s
-    s = res[0]
+    res_list = [res[0]]
     for item in res[1:]:
         try:
-            s += _hextochr[item[:2]] + item[2:]
+            x = _hextochr[item[:2]] + item[2:]
         except KeyError:
-            s += '%' + item
+            x = '%' + item
         except UnicodeDecodeError:
-            s += unichr(int(item[:2], 16)) + item[2:]
-    return s
+            x = unichr(int(item[:2], 16)) + item[2:]
+        res_list.append(x)
+    return ''.join(res_list)
 
 def parse_qs(qs, keep_blank_values=0, strict_parsing=0):
     """Parse a query given as a string argument.