def descr_replace(self, space, w_old, w_new, count=-1): from rpython.rlib.rstring import replace old_is_unicode = space.isinstance_w(w_old, space.w_unicode) new_is_unicode = space.isinstance_w(w_new, space.w_unicode) if old_is_unicode or new_is_unicode: self_as_uni = unicode_from_encoded_object(space, self, None, None) return self_as_uni.descr_replace(space, w_old, w_new, count) # almost copy of StringMethods.descr_replace :-( input = self._value sub = self._op_val(space, w_old) by = self._op_val(space, w_new) # the following two lines are for being bug-to-bug compatible # with CPython: see issue #2448 if count >= 0 and len(input) == 0: return self._empty() try: res = replace(input, sub, by, count) except OverflowError: raise oefmt(space.w_OverflowError, "replace string is too long") # difference: reuse self if no replacement was done if type(self) is W_BytesObject and res is input: return self return self._new(res)
def descr_replace(self, space, w_old, w_new, count=-1): old_is_unicode = space.isinstance_w(w_old, space.w_unicode) new_is_unicode = space.isinstance_w(w_new, space.w_unicode) if old_is_unicode or new_is_unicode: self_as_uni = unicode_from_encoded_object(space, self, None, None) return self_as_uni.descr_replace(space, w_old, w_new, count) return self._StringMethods_descr_replace(space, w_old, w_new, count)
def descr_contains(self, space, w_sub): if space.isinstance_w(w_sub, space.w_unicode): from pypy.objspace.std.unicodeobject import W_UnicodeObject assert isinstance(w_sub, W_UnicodeObject) self_as_unicode = unicode_from_encoded_object( space, self, None, None) return space.newbool(self_as_unicode._utf8.find(w_sub._utf8) >= 0) return self._StringMethods_descr_contains(space, w_sub)
def _endswith(self, space, value, w_suffix, start, end): if space.isinstance_w(w_suffix, space.w_unicode): self_as_unicode = unicode_from_encoded_object(space, self, None, None) return self_as_unicode._endswith(space, self_as_unicode._value, w_suffix, start, end) return self._StringMethods__endswith(space, value, w_suffix, start, end)
def descr_contains(self, space, w_sub): if space.isinstance_w(w_sub, space.w_unicode): from pypy.objspace.std.unicodeobject import W_UnicodeObject assert isinstance(w_sub, W_UnicodeObject) self_as_unicode = unicode_from_encoded_object(space, self, None, None) return space.newbool(self_as_unicode._value.find(w_sub._value) >= 0) return self._StringMethods_descr_contains(space, w_sub)
def _endswith(self, space, value, w_suffix, start, end): if space.isinstance_w(w_suffix, space.w_unicode): self_as_unicode = unicode_from_encoded_object( space, self, None, None) return self_as_unicode._endswith(space, self_as_unicode._utf8, w_suffix, start, end) return self._StringMethods__endswith(space, value, w_suffix, start, end)
def descr_replace(self, space, w_old, w_new, count=-1): old_is_unicode = space.isinstance_w(w_old, space.w_unicode) new_is_unicode = space.isinstance_w(w_new, space.w_unicode) if old_is_unicode or new_is_unicode: self_as_uni = unicode_from_encoded_object(space, self, None, None) if not old_is_unicode: w_old = unicode_from_encoded_object(space, w_old, None, None) if not new_is_unicode: w_new = unicode_from_encoded_object(space, w_new, None, None) input = self_as_uni._val(space) sub = self_as_uni._op_val(space, w_old) by = self_as_uni._op_val(space, w_new) try: res = replace(input, sub, by, count) except OverflowError: raise oefmt(space.w_OverflowError, "replace string is too long") return self_as_uni._new(res) return self._StringMethods_descr_replace(space, w_old, w_new, count)
def descr_add(self, space, w_other): if space.isinstance_w(w_other, space.w_unicode): self_as_unicode = unicode_from_encoded_object( space, self, None, None) return self_as_unicode.descr_add(space, w_other) elif space.isinstance_w(w_other, space.w_bytearray): # XXX: eliminate double-copy from .bytearrayobject import W_BytearrayObject, _make_data self_as_bytearray = W_BytearrayObject(_make_data(self._value)) return space.add(self_as_bytearray, w_other) return self._StringMethods_descr_add(space, w_other)
def descr_add(self, space, w_other): if space.isinstance_w(w_other, space.w_unicode): self_as_unicode = unicode_from_encoded_object(space, self, None, None) return space.add(self_as_unicode, w_other) elif space.isinstance_w(w_other, space.w_bytearray): # XXX: eliminate double-copy from .bytearrayobject import W_BytearrayObject, _make_data self_as_bytearray = W_BytearrayObject(_make_data(self._value)) return space.add(self_as_bytearray, w_other) if space.config.objspace.std.withstrbuf: from pypy.objspace.std.strbufobject import W_StringBufferObject try: other = self._op_val(space, w_other) except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise builder = StringBuilder() builder.append(self._value) builder.append(other) return W_StringBufferObject(builder) return self._StringMethods_descr_add(space, w_other)
def descr_rsplit(self, space, w_sep=None, maxsplit=-1): if w_sep is not None and space.isinstance_w(w_sep, space.w_unicode): self_as_uni = unicode_from_encoded_object(space, self, None, None) return self_as_uni.descr_rsplit(space, w_sep, maxsplit) return self._StringMethods_descr_rsplit(space, w_sep, maxsplit)
def descr_rpartition(self, space, w_sub): if space.isinstance_w(w_sub, space.w_unicode): self_as_uni = unicode_from_encoded_object(space, self, None, None) return self_as_uni.descr_rpartition(space, w_sub) return self._StringMethods_descr_rpartition(space, w_sub)
def descr_rindex(self, space, w_sub, w_start=None, w_end=None): if space.isinstance_w(w_sub, space.w_unicode): self_as_uni = unicode_from_encoded_object(space, self, None, None) return self_as_uni.descr_rindex(space, w_sub, w_start, w_end) return self._StringMethods_descr_rindex(space, w_sub, w_start, w_end)
def descr_rstrip(self, space, w_chars=None): if w_chars is not None and space.isinstance_w(w_chars, space.w_unicode): self_as_uni = unicode_from_encoded_object(space, self, None, None) return self_as_uni.descr_rstrip(space, w_chars) return self._StringMethods_descr_rstrip(space, w_chars)