Skip to content

Commit

Permalink
[Truffle] Pulled in String#center from Rubinius.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvdrum committed Mar 11, 2015
1 parent 6259dff commit 25a64b4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
14 changes: 0 additions & 14 deletions spec/truffle/tags/core/string/center_tags.txt

This file was deleted.

50 changes: 50 additions & 0 deletions truffle/src/main/ruby/core/rubinius/common/string.rb
Expand Up @@ -765,6 +765,56 @@ def upto(stop, exclusive=false)
self
end

def center(width, padding=" ")
padding = StringValue(padding)
raise ArgumentError, "zero width padding" if padding.size == 0

enc = Rubinius::Type.compatible_encoding self, padding

width = Rubinius::Type.coerce_to width, Fixnum, :to_int
return dup if width <= size

width -= size
left = width / 2

bs = bytesize
pbs = padding.bytesize

if pbs > 1
ps = padding.size
pm = Rubinius::Mirror.reflect padding

x = left / ps
y = left % ps

lpbi = pm.byte_index(y)
lbytes = x * pbs + lpbi

right = left + (width & 0x1)

x = right / ps
y = right % ps

rpbi = pm.byte_index(y)
rbytes = x * pbs + rpbi

pad = self.class.pattern rbytes, padding
str = self.class.pattern lbytes + bs + rbytes, ""
m = Rubinius::Mirror.reflect str

m.copy_from self, 0, bs, lbytes
m.copy_from pad, 0, lbytes, 0
m.copy_from pad, 0, rbytes, lbytes + bs
else
str = self.class.pattern width + bs, padding
m = Rubinius::Mirror.reflect str
m.copy_from self, 0, bs, left
end

str.taint if tainted? or padding.tainted?
str.force_encoding enc
end

def ljust(width, padding=" ")
padding = StringValue(padding)
raise ArgumentError, "zero width padding" if padding.size == 0
Expand Down

0 comments on commit 25a64b4

Please sign in to comment.