@@ -765,6 +765,56 @@ def upto(stop, exclusive=false)
765765 self
766766 end
767767
768+ def center ( width , padding = " " )
769+ padding = StringValue ( padding )
770+ raise ArgumentError , "zero width padding" if padding . size == 0
771+
772+ enc = Rubinius ::Type . compatible_encoding self , padding
773+
774+ width = Rubinius ::Type . coerce_to width , Fixnum , :to_int
775+ return dup if width <= size
776+
777+ width -= size
778+ left = width / 2
779+
780+ bs = bytesize
781+ pbs = padding . bytesize
782+
783+ if pbs > 1
784+ ps = padding . size
785+ pm = Rubinius ::Mirror . reflect padding
786+
787+ x = left / ps
788+ y = left % ps
789+
790+ lpbi = pm . byte_index ( y )
791+ lbytes = x * pbs + lpbi
792+
793+ right = left + ( width & 0x1 )
794+
795+ x = right / ps
796+ y = right % ps
797+
798+ rpbi = pm . byte_index ( y )
799+ rbytes = x * pbs + rpbi
800+
801+ pad = self . class . pattern rbytes , padding
802+ str = self . class . pattern lbytes + bs + rbytes , ""
803+ m = Rubinius ::Mirror . reflect str
804+
805+ m . copy_from self , 0 , bs , lbytes
806+ m . copy_from pad , 0 , lbytes , 0
807+ m . copy_from pad , 0 , rbytes , lbytes + bs
808+ else
809+ str = self . class . pattern width + bs , padding
810+ m = Rubinius ::Mirror . reflect str
811+ m . copy_from self , 0 , bs , left
812+ end
813+
814+ str . taint if tainted? or padding . tainted?
815+ str . force_encoding enc
816+ end
817+
768818 def ljust ( width , padding = " " )
769819 padding = StringValue ( padding )
770820 raise ArgumentError , "zero width padding" if padding . size == 0
0 commit comments