diff options
| author | Tomasz Dabrowski <[email protected]> | 2017-02-10 12:58:41 +0100 |
|---|---|---|
| committer | Tomasz Dabrowski <[email protected]> | 2017-02-10 12:59:28 +0100 |
| commit | 981105b3e6758455646e9834b1c2695bf774a401 (patch) | |
| tree | d8d5fbeeecb1af277c3060074619ec9d7c4229fd /mrbgems/mruby-string-ext/mrblib | |
| parent | d0ecf862d9d2e7aed461bc9360686449f56c5d25 (diff) | |
| download | mruby-981105b3e6758455646e9834b1c2695bf774a401.tar.gz mruby-981105b3e6758455646e9834b1c2695bf774a401.zip | |
String#ljust and String#rjust reimplementation (fix #3445)
- String#ljust and String#rjust are now C functions to improve performance
- infinite loop because of an empty padding argument is now prevented (ArgumentError is raised)
- extra tests for ljust/rjust added
Diffstat (limited to 'mrbgems/mruby-string-ext/mrblib')
| -rw-r--r-- | mrbgems/mruby-string-ext/mrblib/string.rb | 46 |
1 files changed, 0 insertions, 46 deletions
diff --git a/mrbgems/mruby-string-ext/mrblib/string.rb b/mrbgems/mruby-string-ext/mrblib/string.rb index 6e5f3c73d..610a462a7 100644 --- a/mrbgems/mruby-string-ext/mrblib/string.rb +++ b/mrbgems/mruby-string-ext/mrblib/string.rb @@ -263,52 +263,6 @@ class String self end - ## - # call-seq: - # str.ljust(integer, padstr=' ') -> new_str - # - # If <i>integer</i> is greater than the length of <i>str</i>, returns a new - # <code>String</code> of length <i>integer</i> with <i>str</i> left justified - # and padded with <i>padstr</i>; otherwise, returns <i>str</i>. - # - # "hello".ljust(4) #=> "hello" - # "hello".ljust(20) #=> "hello " - # "hello".ljust(20, '1234') #=> "hello123412341234123" - def ljust(idx, padstr = ' ') - if idx <= self.size - return self - end - newstr = self.dup - newstr << padstr - while newstr.size <= idx - newstr << padstr - end - return newstr.slice(0,idx) - end - - ## - # call-seq: - # str.rjust(integer, padstr=' ') -> new_str - # - # If <i>integer</i> is greater than the length of <i>str</i>, returns a new - # <code>String</code> of length <i>integer</i> with <i>str</i> right justified - # and padded with <i>padstr</i>; otherwise, returns <i>str</i>. - # - # "hello".rjust(4) #=> "hello" - # "hello".rjust(20) #=> " hello" - # "hello".rjust(20, '1234') #=> "123412341234123hello" - def rjust(idx, padstr = ' ') - if idx <= self.size - return self - end - padsize = idx - self.size - newstr = padstr.dup - while newstr.size <= padsize - newstr << padstr - end - return newstr.slice(0,padsize) + self - end - # str.upto(other_str, exclusive=false) {|s| block } -> str # str.upto(other_str, exclusive=false) -> an_enumerator # |
