Skip to content

Commit

Permalink
Merge pull request #762 from kachick/implement-string-rjust
Browse files Browse the repository at this point in the history
Implement String#rjust
  • Loading branch information
alex committed Jun 24, 2013
2 parents 2ea4c02 + bdb99ac commit 1932525
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
8 changes: 0 additions & 8 deletions spec/tags/core/string/rjust_tags.txt
@@ -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
16 changes: 16 additions & 0 deletions topaz/objects/stringobject.py
Expand Up @@ -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)
Expand Down

0 comments on commit 1932525

Please sign in to comment.