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_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_center__RopeUnicode_ANY_ANY(space, w_self, w_width, w_fillchar): self = w_self._node length = self.length() width = space.int_w(w_width) fillchar = _to_unichar_w(space, w_fillchar) padding = width - length if padding < 0: return w_self.create_if_subclassed() offset = padding // 2 pre = rope.multiply(fillchar, offset) post = rope.multiply(fillchar, (padding - offset)) centered = rope.rebalance([pre, self, post]) return W_RopeUnicodeObject(centered)
def unicode_expandtabs__RopeUnicode_ANY(space, w_self, w_tabsize): from pypy.objspace.std.ropeobject import _tabindent self = w_self._node tabsize = space.int_w(w_tabsize) splitted = rope.split(self, rope.LiteralStringNode.PREBUILT[ord('\t')]) last = splitted[0] expanded = [last] for i in range(1, len(splitted)): expanded.append(rope.multiply(rope.LiteralStringNode.PREBUILT[ord(" ")], _tabindent(last, tabsize))) last = splitted[i] expanded.append(last) return W_RopeUnicodeObject(rope.rebalance(expanded))
def unicode_expandtabs__RopeUnicode_ANY(space, w_self, w_tabsize): from pypy.objspace.std.ropeobject import _tabindent self = w_self._node tabsize = space.int_w(w_tabsize) splitted = rope.split(self, rope.LiteralStringNode.PREBUILT[ord('\t')]) last = splitted[0] expanded = [last] for i in range(1, len(splitted)): expanded.append( rope.multiply(rope.LiteralStringNode.PREBUILT[ord(" ")], _tabindent(last, tabsize))) last = splitted[i] expanded.append(last) return W_RopeUnicodeObject(rope.rebalance(expanded))
def str_expandtabs__Rope_ANY(space, w_self, w_tabsize): node = w_self._node length = node.length() if length == 0: return W_RopeObject.EMPTY tabsize = space.int_w(w_tabsize) splitted = rope.split(node, rope.LiteralStringNode.PREBUILT[ord("\t")]) last = splitted[0] expanded = [last] for i in range(1, len(splitted)): expanded.append(rope.multiply(rope.LiteralStringNode.PREBUILT[ord(" ")], _tabindent(last, tabsize))) last = splitted[i] expanded.append(last) return W_RopeObject(rope.rebalance(expanded))
def unicode_expandtabs__RopeUnicode_ANY(space, w_self, w_tabsize): from pypy.objspace.std.ropeobject import _tabindent self = w_self._node tabsize = space.int_w(w_tabsize) splitted = rope.split(self, rope.LiteralStringNode.PREBUILT[ord("\t")]) last = splitted[0] expanded = [last] for i in range(1, len(splitted)): expanded.append(rope.multiply(rope.LiteralStringNode.PREBUILT[ord(" ")], _tabindent(last, tabsize))) last = splitted[i] expanded.append(last) try: return W_RopeUnicodeObject(rope.rebalance(expanded)) except OverflowError: raise OperationError(space.w_OverflowError, space.wrap("string too long"))
def unicode_expandtabs__RopeUnicode_ANY(space, w_self, w_tabsize): from pypy.objspace.std.ropeobject import _tabindent self = w_self._node tabsize = space.int_w(w_tabsize) splitted = rope.split(self, rope.LiteralStringNode.PREBUILT[ord('\t')]) last = splitted[0] expanded = [last] for i in range(1, len(splitted)): expanded.append(rope.multiply(rope.LiteralStringNode.PREBUILT[ord(" ")], _tabindent(last, tabsize))) last = splitted[i] expanded.append(last) try: return W_RopeUnicodeObject(rope.rebalance(expanded)) except OverflowError: raise OperationError(space.w_OverflowError, space.wrap("string too long"))
def str_expandtabs__Rope_ANY(space, w_self, w_tabsize): node = w_self._node length = node.length() if length == 0: return W_RopeObject.EMPTY tabsize = space.int_w(w_tabsize) splitted = rope.split(node, rope.LiteralStringNode.PREBUILT[ord("\t")]) last = splitted[0] expanded = [last] for i in range(1, len(splitted)): expanded.append(rope.multiply(rope.LiteralStringNode.PREBUILT[ord(" ")], _tabindent(last, tabsize))) last = splitted[i] expanded.append(last) try: return W_RopeObject(rope.rebalance(expanded)) except OverflowError: raise OperationError(space.w_OverflowError, space.wrap("new string is too long"))
def str_center__Rope_ANY_ANY(space, w_self, w_arg, w_fillchar): node = w_self._node length = node.length() arg = space.int_w(w_arg) fillchar = space.str_w(w_fillchar) if len(fillchar) != 1: raise OperationError(space.w_TypeError, space.wrap("center() argument 2 must be a single character")) d = arg - length if d > 0: offset = d // 2 + (d & arg & 1) fillcharnode = rope.LiteralStringNode.PREBUILT[ord(fillchar)] pre = rope.multiply(fillcharnode, offset) post = rope.multiply(fillcharnode, (d - offset)) centered = rope.rebalance([pre, node, post]) return W_RopeObject(centered) else: return w_self.create_if_subclassed()
def str_center__Rope_ANY_ANY(space, w_self, w_arg, w_fillchar): node = w_self._node length = node.length() arg = space.int_w(w_arg) fillchar = space.str_w(w_fillchar) if len(fillchar) != 1: raise OperationError(space.w_TypeError, space.wrap("center() argument 2 must be a single character")) d = arg - length if d>0: offset = d//2 fillcharnode = rope.LiteralStringNode.PREBUILT[ord(fillchar)] pre = rope.multiply(fillcharnode, offset) post = rope.multiply(fillcharnode, (d - offset)) centered = rope.rebalance([pre, node, post]) return W_RopeObject(centered) else: return w_self.create_if_subclassed()
def str_expandtabs__Rope_ANY(space, w_self, w_tabsize): node = w_self._node length = node.length() if length == 0: return W_RopeObject.EMPTY tabsize = space.int_w(w_tabsize) splitted = rope.split(node, rope.LiteralStringNode.PREBUILT[ord("\t")]) last = splitted[0] expanded = [last] for i in range(1, len(splitted)): expanded.append(rope.multiply(rope.LiteralStringNode.PREBUILT[ord(" ")], _tabindent(last, tabsize))) last = splitted[i] expanded.append(last) try: return W_RopeObject(rope.rebalance(expanded)) except OverflowError: raise OperationError(space.w_OverflowError, space.wrap('new string is too long'))
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))