-
Notifications
You must be signed in to change notification settings - Fork 1
String operations
Jacob edited this page Aug 25, 2016
·
3 revisions
ByteBuffer includes Crystal's IO module, which among other things defines many methods to write and read strings. These methods both advance the position and are limited by the limit of the buffer.
bb = ByteBuffer.new(15)
bb.puts "Hello World"
bb.position # => 12
bb.remaining # => 3
bb.to_slice # => Slice[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 10, 0, 0, 0]
Like all types that include IO, it supports encodings.
bb = ByteBuffer.new(30)
bb.set_encoding("UTF-16LE")
bb.puts "Hello World"
bb.position # => 24
bb.remaining # => 6
bb.to_slice # => Slice[72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0, 10, 0, 0, 0, 0, 0, 0, 0]
For more information take a look at IO's API