[pypy-commit] pypy default: don't really need to use such heavy machinery

cfbolz noreply at buildbot.pypy.org
Mon Jun 17 14:35:28 CEST 2013


Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch: 
Changeset: r64912:cc52d477c5f3
Date: 2013-06-14 18:51 +0200
http://bitbucket.org/pypy/pypy/changeset/cc52d477c5f3/

Log:	don't really need to use such heavy machinery

diff --git a/rpython/rlib/rstring.py b/rpython/rlib/rstring.py
--- a/rpython/rlib/rstring.py
+++ b/rpython/rlib/rstring.py
@@ -83,6 +83,67 @@
     return res
 
 
+ at specialize.argtype(0)
+def replace(input, sub, by, maxsplit=-1):
+    if isinstance(input, str):
+        assert isinstance(sub, str)
+        assert isinstance(by, str)
+        Builder = StringBuilder
+    else:
+        assert isinstance(sub, unicode)
+        assert isinstance(by, unicode)
+        Builder = UnicodeBuilder
+    if maxsplit == 0:
+        return input
+
+    if not sub:
+        upper = len(input)
+        if maxsplit > 0 and maxsplit < upper + 2:
+            upper = maxsplit - 1
+            assert upper >= 0
+
+        try:
+            result_size = ovfcheck(upper * len(by))
+            result_size = ovfcheck(result_size + upper)
+            result_size = ovfcheck(result_size + len(by))
+            remaining_size = len(input) - upper
+            result_size = ovfcheck(result_size + remaining_size)
+        except OverflowError:
+            raise
+        builder = Builder(result_size)
+        for i in range(upper):
+            builder.append(by)
+            builder.append(input[i])
+        builder.append(by)
+        builder.append_slice(input, upper, len(input))
+    else:
+        # First compute the exact result size
+        count = input.count(sub)
+        if count > maxsplit and maxsplit > 0:
+            count = maxsplit
+        diff_len = len(by) - len(sub)
+        try:
+            result_size = ovfcheck(diff_len * count)
+            result_size = ovfcheck(result_size + len(input))
+        except OverflowError:
+            raise
+
+        builder = Builder(result_size)
+        start = 0
+        sublen = len(sub)
+
+        while maxsplit != 0:
+            next = input.find(sub, start)
+            if next < 0:
+                break
+            builder.append_slice(input, start, next)
+            builder.append(by)
+            start = next + sublen
+            maxsplit -= 1   # NB. if it's already < 0, it stays < 0
+
+        builder.append_slice(input, start, len(input))
+
+    return builder.build()
 
 
 # -------------- public API ---------------------------------
@@ -304,73 +365,3 @@
         hop.exception_cannot_occur()
 
 
-
-def make_replace(func_name, Builder):
-    def replace(input, sub, by, maxsplit=-1):
-        if maxsplit == 0:
-            return input
-
-        if not sub:
-            upper = len(input)
-            if maxsplit > 0 and maxsplit < upper + 2:
-                upper = maxsplit - 1
-                assert upper >= 0
-
-            try:
-                result_size = ovfcheck(upper * len(by))
-                result_size = ovfcheck(result_size + upper)
-                result_size = ovfcheck(result_size + len(by))
-                remaining_size = len(input) - upper
-                result_size = ovfcheck(result_size + remaining_size)
-            except OverflowError:
-                raise
-            builder = Builder(result_size)
-            for i in range(upper):
-                builder.append(by)
-                builder.append(input[i])
-            builder.append(by)
-            builder.append_slice(input, upper, len(input))
-        else:
-            # First compute the exact result size
-            count = input.count(sub)
-            if count > maxsplit and maxsplit > 0:
-                count = maxsplit
-            diff_len = len(by) - len(sub)
-            try:
-                result_size = ovfcheck(diff_len * count)
-                result_size = ovfcheck(result_size + len(input))
-            except OverflowError:
-                raise
-
-            builder = Builder(result_size)
-            start = 0
-            sublen = len(sub)
-
-            while maxsplit != 0:
-                next = input.find(sub, start)
-                if next < 0:
-                    break
-                builder.append_slice(input, start, next)
-                builder.append(by)
-                start = next + sublen
-                maxsplit -= 1   # NB. if it's already < 0, it stays < 0
-
-            builder.append_slice(input, start, len(input))
-
-        return builder.build()
-    replace.func_name = func_name
-    return replace
-
-_string_replace = make_replace("_string_replace", StringBuilder)
-_unicode_replace = make_replace("_unicode_replace", UnicodeBuilder)
-
- at specialize.argtype(0)
-def replace(input, sub, by, maxsplit=-1):
-    if isinstance(input, str):
-        assert isinstance(sub, str)
-        assert isinstance(by, str)
-        return _string_replace(input, sub, by, maxsplit)
-    else:
-        assert isinstance(sub, unicode)
-        assert isinstance(by, unicode)
-        return _unicode_replace(input, sub, by, maxsplit)


More information about the pypy-commit mailing list