Skip to content
Permalink
Browse files

Merge pull request #762 from kachick/implement-string-rjust

Implement String#rjust
  • Loading branch information
alex committed Jun 24, 2013
2 parents 2ea4c02 + bdb99ac commit 193252531b914a2025ef6c6250a1339e6421add7
Showing with 16 additions and 8 deletions.
  1. +0 −8 spec/tags/core/string/rjust_tags.txt
  2. +16 −0 topaz/objects/stringobject.py
@@ -1,11 +1,3 @@
fails:String#rjust with length, padding returns a new string of specified length with self right justified and padded with padstr
fails:String#rjust with length, padding pads with whitespace if no padstr is given
fails:String#rjust with length, padding returns self if it's longer than or as long as the specified length
fails:String#rjust with length, padding taints result when self or padstr is tainted
fails:String#rjust with length, padding tries to convert length to an integer using to_int
fails:String#rjust with length, padding raises a TypeError when length can't be converted to an integer
fails:String#rjust with length, padding tries to convert padstr to a string using to_str
fails:String#rjust with length, padding raises a TypeError when padstr can't be converted
fails:String#rjust with length, padding raises an ArgumentError when padstr is empty
fails:String#rjust with length, padding returns subclass instances when called on subclasses
fails:String#rjust with length, padding when padding is tainted and self is untainted returns a tainted string if and only if length is longer than self
@@ -554,6 +554,22 @@ def method_ljust(self, space, integer, padstr=" "):
chars += padstr[:pad_len % len(padstr) + 1]
return space.newstr_fromchars(chars)

@classdef.method("rjust", integer="int", padstr="str")
def method_rjust(self, space, integer, padstr=" "):
if not padstr:
raise space.error(space.w_ArgumentError, "zero width padding")
elif integer <= self.length():
return self.copy(space)
else:
pad_len = integer - self.length() - 1
assert pad_len >= 0
chars = []
for i in xrange(pad_len / len(padstr)):
chars += padstr
chars += padstr[:pad_len % len(padstr) + 1]
chars += space.str_w(self)
return space.newstr_fromchars(chars)

def search_context(self, space, ctx):
try:
return rsre_core.search_context(ctx)

0 comments on commit 1932525

Please sign in to comment.