def unicode_rsplit__RopeUnicode_RopeUnicode_ANY(space, w_self, w_delim, w_maxsplit): # XXX works but flattens self = w_self._node.flatten_unicode() delim = w_delim._node.flatten_unicode() maxsplit = space.int_w(w_maxsplit) delim_len = len(delim) if delim_len == 0: raise OperationError(space.w_ValueError, space.wrap('empty separator')) parts = [] if len(self) == 0: return space.newlist([]) start = 0 end = len(self) while maxsplit != 0: index = self.rfind(delim, 0, end) if index < 0: break parts.append(W_RopeUnicodeObject( rope.getslice_one(w_self._node, index+delim_len, end))) end = index maxsplit -= 1 parts.append(W_RopeUnicodeObject( rope.getslice_one(w_self._node, 0, end))) parts.reverse() return space.newlist(parts)
def unicode_rsplit__RopeUnicode_RopeUnicode_ANY(space, w_self, w_delim, w_maxsplit): # XXX works but flattens self = w_self._node.flatten_unicode() delim = w_delim._node.flatten_unicode() maxsplit = space.int_w(w_maxsplit) delim_len = len(delim) if delim_len == 0: raise OperationError(space.w_ValueError, space.wrap('empty separator')) parts = [] if len(self) == 0: return space.newlist([]) start = 0 end = len(self) while maxsplit != 0: index = self.rfind(delim, 0, end) if index < 0: break parts.append( W_RopeUnicodeObject( rope.getslice_one(w_self._node, index + delim_len, end))) end = index maxsplit -= 1 parts.append(W_RopeUnicodeObject(rope.getslice_one(w_self._node, 0, end))) parts.reverse() return space.newlist(parts)
def str_partition__Rope_Rope(space, w_self, w_sub): self = w_self._node sub = w_sub._node if not sub.length(): raise OperationError(space.w_ValueError, space.wrap("empty separator")) pos = rope.find(self, sub) if pos == -1: return space.newtuple([w_self, W_RopeObject.EMPTY, W_RopeObject.EMPTY]) else: return space.newtuple([ W_RopeObject(rope.getslice_one(self, 0, pos)), w_sub, W_RopeObject( rope.getslice_one(self, pos + sub.length(), self.length())) ])
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_partition__RopeUnicode_RopeUnicode(space, w_unistr, w_unisub): self = w_unistr._node sub = w_unisub._node if not sub.length(): raise OperationError(space.w_ValueError, space.wrap("empty separator")) pos = rope.find(self, sub) if pos == -1: return space.newtuple([w_unistr, W_RopeUnicodeObject.EMPTY, W_RopeUnicodeObject.EMPTY]) else: return space.newtuple( [W_RopeUnicodeObject(rope.getslice_one(self, 0, pos)), w_unisub, W_RopeUnicodeObject(rope.getslice_one(self, pos + sub.length(), self.length()))])
def str_zfill__Rope_ANY(space, w_self, w_width): node = w_self._node length = node.length() width = space.int_w(w_width) if length >= width: return w_self.create_if_subclassed() zero = rope.LiteralStringNode.PREBUILT[ord("0")] if length == 0: return W_RopeObject(rope.multiply(zero, width)) middle = width - length firstchar = node.getchar(0) if length > 0 and (firstchar == "+" or firstchar == "-"): return W_RopeObject( rope.rebalance( [ rope.LiteralStringNode.PREBUILT[ord(firstchar)], rope.multiply(zero, middle), rope.getslice_one(node, 1, length), ] ) ) else: middle = width - length return W_RopeObject(rope.concatenate(rope.multiply(zero, middle), node))
def str_rpartition__Rope_Rope(space, w_self, w_sub): # XXX works but flattens self = w_self._node sub = w_sub._node if not sub.length(): raise OperationError(space.w_ValueError, space.wrap("empty separator")) flattened_self = self.flatten_string() flattened_sub = sub.flatten_string() pos = flattened_self.rfind(flattened_sub) if pos == -1: return space.newtuple([W_RopeObject.EMPTY, W_RopeObject.EMPTY, w_self]) else: return space.newtuple([ W_RopeObject(rope.getslice_one(self, 0, pos)), w_sub, W_RopeObject( rope.getslice_one(self, pos + sub.length(), self.length())) ])
def str_rpartition__Rope_Rope(space, w_self, w_sub): # XXX works but flattens self = w_self._node sub = w_sub._node if not sub.length(): raise OperationError(space.w_ValueError, space.wrap("empty separator")) flattened_self = self.flatten_string() flattened_sub = sub.flatten_string() pos = flattened_self.rfind(flattened_sub) if pos == -1: return space.newtuple([W_RopeObject.EMPTY, W_RopeObject.EMPTY, w_self]) else: return space.newtuple( [W_RopeObject(rope.getslice_one(self, 0, pos)), w_sub, W_RopeObject(rope.getslice_one(self, pos + sub.length(), self.length()))])
def _split_into_chars(self, maxsplit): if maxsplit == 0: return [self] index = 0 end = self.length() parts = [rope.LiteralStringNode.EMPTY] maxsplit -= 1 while maxsplit != 0: if index >= end: break parts.append(self.getrope(index)) index += 1 maxsplit -= 1 parts.append(rope.getslice_one(self, index, self.length())) return parts
def unicode_zfill__RopeUnicode_ANY(space, w_self, w_width): self = w_self._node length = self.length() width = space.int_w(w_width) zero = rope.LiteralStringNode.PREBUILT[ord("0")] if self.length() == 0: return W_RopeUnicodeObject( rope.multiply(zero, width)) padding = width - length if padding <= 0: return w_self.create_if_subclassed() firstchar = self.getunichar(0) if firstchar in (u'+', u'-'): return W_RopeUnicodeObject(rope.rebalance( [rope.LiteralStringNode.PREBUILT[ord(firstchar)], rope.multiply(zero, padding), rope.getslice_one(self, 1, length)])) else: return W_RopeUnicodeObject(rope.concatenate( rope.multiply(zero, padding), self))
def str_zfill__Rope_ANY(space, w_self, w_width): node = w_self._node length = node.length() width = space.int_w(w_width) if length >= width: return w_self.create_if_subclassed() zero = rope.LiteralStringNode.PREBUILT[ord("0")] if length == 0: return W_RopeObject(rope.multiply(zero, width)) middle = width - length firstchar = node.getchar(0) if length > 0 and (firstchar == '+' or firstchar == '-'): return W_RopeObject(rope.rebalance( [rope.LiteralStringNode.PREBUILT[ord(firstchar)], rope.multiply(zero, middle), rope.getslice_one(node, 1, length)])) else: middle = width - length return W_RopeObject(rope.concatenate( rope.multiply(zero, middle), node))