def str_replace__Rope_Rope_Rope_ANY(space, w_self, w_sub, w_by, w_maxsplit=-1): node = w_self._node length = node.length() sub = w_sub._node by = w_by._node maxsplit = space.int_w(w_maxsplit) if maxsplit == 0: return w_self.create_if_subclassed() if not sub.length(): upper = node.length() if maxsplit > 0 and maxsplit < upper + 2: upper = maxsplit - 1 assert upper >= 0 substrings = [by] iter = rope.ItemIterator(node) for i in range(upper): substrings.append(iter.nextrope()) substrings.append(by) substrings.append(rope.getslice_one(node, upper, length)) try: return W_RopeObject(rope.rebalance(substrings)) except OverflowError: raise OperationError(space.w_OverflowError, space.wrap("string too long")) substrings = rope.split(node, sub, maxsplit) if not substrings: return w_self.create_if_subclassed() try: return W_RopeObject(rope.join(by, substrings)) except OverflowError: raise OperationError(space.w_OverflowError, space.wrap("string too long"))
def unicode_join__RopeUnicode_ANY(space, w_self, w_list): l_w = space.listview(w_list) delim = w_self._node totlen = 0 if len(l_w) == 0: return W_RopeUnicodeObject.EMPTY if (len(l_w) == 1 and space.is_w(space.type(l_w[0]), space.w_unicode)): return l_w[0] values_list = [] for i in range(len(l_w)): w_item = l_w[i] if isinstance(w_item, W_RopeUnicodeObject): # shortcut for performane item = w_item._node elif space.isinstance_w(w_item, space.w_str): item = unicode_from_string(space, w_item)._node else: msg = 'sequence item %d: expected string or Unicode' raise operationerrfmt(space.w_TypeError, msg, i) values_list.append(item) try: return W_RopeUnicodeObject(rope.join(w_self._node, values_list)) except OverflowError: raise OperationError(space.w_OverflowError, space.wrap("string too long"))
def str_join__Rope_ANY(space, w_self, w_list): list_w = space.unpackiterable(w_list) if list_w: self = w_self._node l = [] for i in range(len(list_w)): w_s = list_w[i] if not space.is_true(space.isinstance(w_s, space.w_str)): if space.is_true(space.isinstance(w_s, space.w_unicode)): w_u = space.call_function(space.w_unicode, w_self) return space.call_method(w_u, "join", space.newlist(list_w)) raise OperationError( space.w_TypeError, space.wrap("sequence item %d: expected string, %s " "found" % (i, space.type(w_s).name))) assert isinstance(w_s, W_RopeObject) node = w_s._node l.append(node) try: return W_RopeObject(rope.join(self, l)) except OverflowError: raise OperationError(space.w_OverflowError, space.wrap("string too long")) else: return W_RopeObject.EMPTY
def str_join__Rope_ANY(space, w_self, w_list): list_w = space.unpackiterable(w_list) size = len(list_w) if size == 0: return W_RopeObject.EMPTY if size == 1: w_s = list_w[0] # only one item, return it if it's not a subclass of str if (space.is_w(space.type(w_s), space.w_str) or space.is_w(space.type(w_s), space.w_unicode)): return w_s self = w_self._node l = [] for i in range(size): w_s = list_w[i] if not space.isinstance_w(w_s, space.w_str): if space.isinstance_w(w_s, space.w_unicode): w_u = space.call_function(space.w_unicode, w_self) return space.call_method(w_u, "join", space.newlist(list_w)) raise operationerrfmt( space.w_TypeError, "sequence item %d: expected string, %s " "found", i, space.type(w_s).getname(space)) assert isinstance(w_s, W_RopeObject) node = w_s._node l.append(node) try: return W_RopeObject(rope.join(self, l)) except OverflowError: raise OperationError(space.w_OverflowError, space.wrap("string too long"))
def str_join__Rope_ANY(space, w_self, w_list): list_w = space.unpackiterable(w_list) size = len(list_w) if size == 0: return W_RopeObject.EMPTY if size == 1: w_s = list_w[0] # only one item, return it if it's not a subclass of str if (space.is_w(space.type(w_s), space.w_str) or space.is_w(space.type(w_s), space.w_unicode)): return w_s self = w_self._node l = [] for i in range(size): w_s = list_w[i] if not space.is_true(space.isinstance(w_s, space.w_str)): if space.is_true(space.isinstance(w_s, space.w_unicode)): w_u = space.call_function(space.w_unicode, w_self) return space.call_method(w_u, "join", space.newlist(list_w)) raise operationerrfmt( space.w_TypeError, "sequence item %d: expected string, %s " "found", i, space.type(w_s).getname(space)) assert isinstance(w_s, W_RopeObject) node = w_s._node l.append(node) try: return W_RopeObject(rope.join(self, l)) except OverflowError: raise OperationError(space.w_OverflowError, space.wrap("string too long"))
def unicode_replace__RopeUnicode_RopeUnicode_RopeUnicode_ANY( space, w_self, w_old, w_new, w_maxsplit): self = w_self._node old = w_old._node maxsplit = space.int_w(w_maxsplit) oldlength = old.length() if not oldlength: parts = _split_into_chars(self, maxsplit) return W_RopeUnicodeObject(rope.join(w_new._node, parts)) substrings = rope.split(self, old, maxsplit) if not substrings: return w_self.create_if_subclassed() try: return W_RopeUnicodeObject(rope.join(w_new._node, substrings)) except OverflowError: raise OperationError(space.w_OverflowError, space.wrap("string too long"))
if space.is_w(w_newval, space.w_None): continue elif space.isinstance_w(w_newval, space.w_int): newval = space.int_w(w_newval) if newval < 0 or newval > maxunicode: raise OperationError( space.w_TypeError, space.wrap("character mapping must be in range(0x%x)" % (maxunicode + 1,))) result.append(rope.rope_from_unichar(unichr(newval))) elif space.isinstance_w(w_newval, space.w_unicode): result.append(ropeunicode_w(space, w_newval)) else: raise OperationError( space.w_TypeError, space.wrap("character mapping must return integer, None or unicode")) return W_RopeUnicodeObject(rope.join(rope.LiteralStringNode.EMPTY, result)) # Move this into the _codecs module as 'unicodeescape_string (Remember to cater for quotes)' def repr__RopeUnicode(space, w_unicode): hexdigits = "0123456789abcdef" node = w_unicode._node size = node.length() singlequote = doublequote = False iter = rope.ItemIterator(node) for i in range(size): c = iter.nextunichar() if singlequote and doublequote: break if c == u'\'': singlequote = True
newval = space.int_w(w_newval) if newval < 0 or newval > maxunicode: raise OperationError( space.w_TypeError, space.wrap("character mapping must be in range(0x%x)" % (maxunicode + 1, ))) result.append(rope.rope_from_unichar(unichr(newval))) elif space.isinstance_w(w_newval, space.w_unicode): result.append(ropeunicode_w(space, w_newval)) else: raise OperationError( space.w_TypeError, space.wrap( "character mapping must return integer, None or unicode" )) return W_RopeUnicodeObject(rope.join(rope.LiteralStringNode.EMPTY, result)) # Move this into the _codecs module as 'unicodeescape_string (Remember to cater for quotes)' def repr__RopeUnicode(space, w_unicode): hexdigits = "0123456789abcdef" node = w_unicode._node size = node.length() singlequote = doublequote = False iter = rope.ItemIterator(node) for i in range(size): c = iter.nextunichar() if singlequote and doublequote: break if c == u'\'':