[pypy-svn] r78823 - pypy/trunk/pypy/objspace/std

arigo at codespeak.net arigo at codespeak.net
Sun Nov 7 16:52:35 CET 2010


Author: arigo
Date: Sun Nov  7 16:52:29 2010
New Revision: 78823

Modified:
   pypy/trunk/pypy/objspace/std/stringobject.py
Log:
Optimize the code for  str.translate(..., deletechars).


Modified: pypy/trunk/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/stringobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/stringobject.py	Sun Nov  7 16:52:29 2010
@@ -937,10 +937,17 @@
 
     string = w_string._value
     chars = []
-    for char in string:
-        w_char = W_StringObject.PREBUILT[ord(char)]
-        if not space.is_true(space.contains(w_deletechars, w_char)):
-             chars.append(table[ord(char)])
+    deletechars = space.str_w(w_deletechars)
+    if len(deletechars) == 0:
+        for char in string:
+            chars.append(table[ord(char)])
+    else:
+        deletion_table = [False] * 256
+        for c in deletechars:
+            deletion_table[ord(c)] = True
+        for char in string:
+            if not deletion_table[ord(char)]:
+                chars.append(table[ord(char)])
     return W_StringObject(''.join(chars))
 
 def str_decode__String_ANY_ANY(space, w_string, w_encoding=None, w_errors=None):



More information about the Pypy-commit mailing list