[pypy-commit] pypy default: use the new replace functions and kill the old ones
cfbolz
noreply at buildbot.pypy.org
Mon Jun 17 14:35:27 CEST 2013
Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch:
Changeset: r64911:e53c9a99343c
Date: 2013-06-14 18:50 +0200
http://bitbucket.org/pypy/pypy/changeset/e53c9a99343c/
Log: use the new replace functions and kill the old ones
diff --git a/pypy/objspace/std/stringobject.py b/pypy/objspace/std/stringobject.py
--- a/pypy/objspace/std/stringobject.py
+++ b/pypy/objspace/std/stringobject.py
@@ -16,7 +16,7 @@
from rpython.rlib.objectmodel import (
compute_hash, compute_unique_id, specialize)
from rpython.rlib.rarithmetic import ovfcheck
-from rpython.rlib.rstring import StringBuilder, split, rsplit
+from rpython.rlib.rstring import StringBuilder, split, rsplit, replace
class W_AbstractStringObject(W_Object):
@@ -483,75 +483,30 @@
return space.wrap(res)
-def _string_replace(space, input, sub, by, maxsplit):
- if maxsplit == 0:
- return space.wrap(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 OperationError(space.w_OverflowError,
- space.wrap("replace string is too long")
- )
- builder = StringBuilder(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 OperationError(space.w_OverflowError,
- space.wrap("replace string is too long")
- )
-
- builder = StringBuilder(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 space.wrap(builder.build())
-
def str_replace__String_ANY_ANY_ANY(space, w_self, w_sub, w_by, w_maxsplit):
- return _string_replace(space, w_self._value, space.buffer_w(w_sub).as_str(),
- space.buffer_w(w_by).as_str(),
- space.int_w(w_maxsplit))
+ sub = space.buffer_w(w_sub).as_str()
+ by = space.buffer_w(w_by).as_str()
+ maxsplit = space.int_w(w_maxsplit)
+ try:
+ res = replace(w_self._value, sub, by, maxsplit)
+ except OverflowError:
+ raise OperationError(space.w_OverflowError,
+ space.wrap("replace string is too long")
+ )
+ return space.wrap(res)
def str_replace__String_String_String_ANY(space, w_self, w_sub, w_by, w_maxsplit=-1):
- input = w_self._value
sub = w_sub._value
by = w_by._value
maxsplit = space.int_w(w_maxsplit)
- return _string_replace(space, input, sub, by, maxsplit)
+ try:
+ res = replace(w_self._value, sub, by, maxsplit)
+ except OverflowError:
+ raise OperationError(space.w_OverflowError,
+ space.wrap("replace string is too long")
+ )
+ return space.wrap(res)
def _strip(space, w_self, w_chars, left, right):
"internal function called by str_xstrip methods"
diff --git a/pypy/objspace/std/unicodeobject.py b/pypy/objspace/std/unicodeobject.py
--- a/pypy/objspace/std/unicodeobject.py
+++ b/pypy/objspace/std/unicodeobject.py
@@ -15,7 +15,7 @@
from rpython.rlib.rarithmetic import ovfcheck
from rpython.rlib.objectmodel import (
compute_hash, compute_unique_id, specialize)
-from rpython.rlib.rstring import UnicodeBuilder, split, rsplit
+from rpython.rlib.rstring import UnicodeBuilder, split, rsplit, replace
from rpython.rlib.runicode import make_unicode_escape_function
from rpython.tool.sourcetools import func_with_new_name
@@ -734,26 +734,16 @@
raise OperationError(space.w_ValueError, space.wrap("empty separator"))
return space.newlist_unicode(rsplit(value, by, maxsplit))
-def _split_into_chars(self, maxsplit):
- if maxsplit == 0:
- return [self]
- index = 0
- end = len(self)
- parts = [u'']
- maxsplit -= 1
- while maxsplit != 0:
- if index >= end:
- break
- parts.append(self[index])
- index += 1
- maxsplit -= 1
- parts.append(self[index:])
- return parts
-
def unicode_replace__Unicode_Unicode_Unicode_ANY(space, w_self, w_old,
w_new, w_maxsplit):
- return _unicode_replace(space, w_self, w_old._value, w_new._value,
- w_maxsplit)
+ maxsplit = space.int_w(w_maxsplit)
+ try:
+ return W_UnicodeObject(
+ replace(w_self._value, w_old._value, w_new._value, maxsplit))
+ except OverflowError:
+ raise OperationError(
+ space.w_OverflowError,
+ space.wrap("replace string is too long"))
def unicode_replace__Unicode_ANY_ANY_ANY(space, w_self, w_old, w_new,
w_maxsplit):
@@ -765,27 +755,14 @@
new = unicode(space.bufferstr_w(w_new))
else:
new = space.unicode_w(w_new)
- return _unicode_replace(space, w_self, old, new, w_maxsplit)
-
-def _unicode_replace(space, w_self, old, new, w_maxsplit):
- if len(old):
- parts = split(w_self._value, old, space.int_w(w_maxsplit))
- else:
- self = w_self._value
- maxsplit = space.int_w(w_maxsplit)
- parts = _split_into_chars(self, maxsplit)
-
+ maxsplit = space.int_w(w_maxsplit)
try:
- one = ovfcheck(len(parts) * len(new))
- ovfcheck(one + len(w_self._value))
+ return W_UnicodeObject(replace(w_self._value, old, new, maxsplit))
except OverflowError:
raise OperationError(
space.w_OverflowError,
space.wrap("replace string is too long"))
- return W_UnicodeObject(new.join(parts))
-
-
def unicode_encode__Unicode_ANY_ANY(space, w_unistr,
w_encoding=None,
w_errors=None):
More information about the pypy-commit
mailing list