0

I want to write contents on one String::Builder to another, like:

str1 = String::Builder.new
str2 = String::Builder.new

str1 << "foo"
str2 << "bar"

str1.copy_somehow_another_builder(str2) #=> "foobar"

currently I just str1 << str2.to_s.

How to do it? And is to_s'ing and pushing is same as what I want from performance point of view?

ClassyPimp
  • 715
  • 7
  • 20
  • According to documentation: "You should never have to deal with this class (means String::Builder). Instead, use [String.build](https://crystal-lang.org/api/0.20.5/String.html#build%28capacity%3D64%2C%26block%29%3Aself-class-method)" – Oleh Sobchuk Feb 09 '17 at 08:06

1 Answers1

1

If any one meets with the problem, you can use IO::Memory for the same purpose like:

io = IO::Memory.new 128
io2 = IO::Memory.new 128

io << "foo"
io2 << "bar"

buffer = uninitialized UInt8[128]

io2.rewind

if (read_bytes_length = io2.read(buffer.to_slice)) > 0
  io.write( buffer.to_slice[0, read_bytes_length] )
end

p io.to_s #=> "foobar"
ClassyPimp
  • 715
  • 7
  • 20