Class: Capnp::StringBuffer

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
SliceableBuffer
Defined in:
lib/capnp/runtime/buffer/string_buffer.rb

Instance Method Summary collapse

Constructor Details

#initialize(buffer) ⇒ StringBuffer

Returns a new instance of StringBuffer.



13
14
15
# File 'lib/capnp/runtime/buffer/string_buffer.rb', line 13

def initialize(buffer)
  @buffer = T.let(buffer.encode(Encoding::BINARY), String)
end

Instance Method Details

#read_bytes(offset, length) ⇒ Object

Raises:



33
34
35
36
37
# File 'lib/capnp/runtime/buffer/string_buffer.rb', line 33

def read_bytes(offset, length)
  slice = @buffer.byteslice(offset, length)
  raise Capnp::Error.new("Offset+length exceeds data size") if slice.nil? || slice.bytesize != length
  slice
end

#read_f32(offset) ⇒ Object



80
81
82
# File 'lib/capnp/runtime/buffer/string_buffer.rb', line 80

def read_f32(offset)
  T.cast(read_bytes(offset, 4).unpack1("e"), Float)
end

#read_f64(offset) ⇒ Object



85
86
87
# File 'lib/capnp/runtime/buffer/string_buffer.rb', line 85

def read_f64(offset)
  T.cast(read_bytes(offset, 8).unpack1("E"), Float)
end

#read_s16(offset) ⇒ Object



65
66
67
# File 'lib/capnp/runtime/buffer/string_buffer.rb', line 65

def read_s16(offset)
  T.cast(read_bytes(offset, 2).unpack1("s<"), Integer)
end

#read_s32(offset) ⇒ Object



70
71
72
# File 'lib/capnp/runtime/buffer/string_buffer.rb', line 70

def read_s32(offset)
  T.cast(read_bytes(offset, 4).unpack1("l<"), Integer)
end

#read_s64(offset) ⇒ Object



75
76
77
# File 'lib/capnp/runtime/buffer/string_buffer.rb', line 75

def read_s64(offset)
  T.cast(read_bytes(offset, 8).unpack1("q<"), Integer)
end

#read_s8(offset) ⇒ Object



60
61
62
# File 'lib/capnp/runtime/buffer/string_buffer.rb', line 60

def read_s8(offset)
  T.cast(read_bytes(offset, 1).unpack1("c"), Integer)
end

#read_string(offset, length) ⇒ Object



28
29
30
# File 'lib/capnp/runtime/buffer/string_buffer.rb', line 28

def read_string(offset, length)
  read_bytes(offset, length).encode!(Encoding::UTF_8)
end

#read_u16(offset) ⇒ Object



45
46
47
# File 'lib/capnp/runtime/buffer/string_buffer.rb', line 45

def read_u16(offset)
  T.cast(read_bytes(offset, 2).unpack1("S<"), Integer)
end

#read_u32(offset) ⇒ Object



50
51
52
# File 'lib/capnp/runtime/buffer/string_buffer.rb', line 50

def read_u32(offset)
  T.cast(read_bytes(offset, 4).unpack1("L<"), Integer)
end

#read_u64(offset) ⇒ Object



55
56
57
# File 'lib/capnp/runtime/buffer/string_buffer.rb', line 55

def read_u64(offset)
  T.cast(read_bytes(offset, 8).unpack1("Q<"), Integer)
end

#read_u8(offset) ⇒ Object



40
41
42
# File 'lib/capnp/runtime/buffer/string_buffer.rb', line 40

def read_u8(offset)
  T.cast(read_bytes(offset, 1).unpack1("C"), Integer)
end

#sizeObject



23
24
25
# File 'lib/capnp/runtime/buffer/string_buffer.rb', line 23

def size
  @buffer.bytesize
end

#slice(offset, length) ⇒ Object



18
19
20
# File 'lib/capnp/runtime/buffer/string_buffer.rb', line 18

def slice(offset, length)
  self.class.new(read_bytes(offset, length))
end