Exemplo n.º 1
0
def add__RopeUnicode_RopeUnicode(space, w_left, w_right):
    right = w_right._node
    left = w_left._node
    try:
        return W_RopeUnicodeObject(rope.concatenate(left, right))
    except OverflowError:
        raise OperationError(space.w_OverflowError, space.wrap("string too long"))
Exemplo n.º 2
0
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))
Exemplo n.º 3
0
def add__Rope_Rope(space, w_left, w_right):
    right = w_right._node
    left = w_left._node
    try:
        return W_RopeObject(rope.concatenate(left, right))
    except OverflowError:
        raise OperationError(space.w_OverflowError,
                             space.wrap("string too long"))
Exemplo n.º 4
0
def unicode_rjust__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()
    resultnode = rope.concatenate(rope.multiply(fillchar, padding), self)
    return W_RopeUnicodeObject(resultnode)
Exemplo n.º 5
0
def unicode_rjust__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()
    resultnode = rope.concatenate(rope.multiply(fillchar, padding), self)
    return W_RopeUnicodeObject(resultnode)
Exemplo n.º 6
0
def str_ljust__Rope_ANY_ANY(space, w_self, w_arg, w_fillchar):
    u_arg = space.int_w(w_arg)
    selfnode = w_self._node
    fillchar = space.str_w(w_fillchar)
    if len(fillchar) != 1:
        raise OperationError(space.w_TypeError, space.wrap("rjust() argument 2 must be a single character"))

    d = u_arg - selfnode.length()
    if d > 0:
        fillchar = fillchar[0]  # annotator hint: it's a single character
        resultnode = rope.concatenate(selfnode, rope.multiply(rope.LiteralStringNode.PREBUILT[ord(fillchar)], d))
        return W_RopeObject(resultnode)
    else:
        return W_RopeObject(selfnode)
Exemplo n.º 7
0
def str_ljust__Rope_ANY_ANY(space, w_self, w_arg, w_fillchar):
    u_arg = space.int_w(w_arg)
    selfnode = w_self._node
    fillchar = space.str_w(w_fillchar)
    if len(fillchar) != 1:
        raise OperationError(space.w_TypeError,
            space.wrap("rjust() argument 2 must be a single character"))
    
    d = u_arg - selfnode.length()
    if d > 0:
        fillchar = fillchar[0]    # annotator hint: it's a single character
        resultnode = rope.concatenate(
                selfnode,
                rope.multiply(rope.LiteralStringNode.PREBUILT[ord(fillchar)],
                              d))
        return W_RopeObject(resultnode)
    else:
        return W_RopeObject(selfnode)
Exemplo n.º 8
0
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))
Exemplo n.º 9
0
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))
Exemplo n.º 10
0
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))