Class: Nl::Encoder
- Inherits:
-
Object
- Object
- Nl::Encoder
- Defined in:
- lib/nl/encoder.rb
Instance Attribute Summary collapse
-
#position ⇒ Object
readonly
Returns the value of attribute position.
Instance Method Summary collapse
- #align_to(alignment) ⇒ Object
- #buffer ⇒ Object
-
#initialize(capacity = 4096) ⇒ Encoder
constructor
A new instance of Encoder.
- #measure(type, offset = 0) ⇒ Object
- #put_string(value) ⇒ Object
- #put_value(type, value) ⇒ Object
- #put_value_at(offset, type, value) ⇒ Object
- #put_values(types, values) ⇒ Object
- #put_values_at(offset, types, values) ⇒ Object
- #put_zstring(value) ⇒ Object
- #reserve(size) ⇒ Object
Constructor Details
#initialize(capacity = 4096) ⇒ Encoder
Returns a new instance of Encoder.
5 6 7 8 |
# File 'lib/nl/encoder.rb', line 5 def initialize(capacity = 4096) @buffer = IO::Buffer.new(capacity) @position = 0 end |
Instance Attribute Details
#position ⇒ Object (readonly)
Returns the value of attribute position.
3 4 5 |
# File 'lib/nl/encoder.rb', line 3 def position @position end |
Instance Method Details
#align_to(alignment) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/nl/encoder.rb', line 38 def align_to(alignment) if alignment > 1 nposition = (@position + alignment - 1) & ~(alignment - 1) reserve(nposition - @position) @position = nposition end end |
#buffer ⇒ Object
54 55 56 |
# File 'lib/nl/encoder.rb', line 54 def buffer @buffer.slice(0, @position) end |
#measure(type, offset = 0) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/nl/encoder.rb', line 58 def measure(type, offset = 0) before = @position yield after = @position put_value_at(before + offset, type, after - before) end |
#put_string(value) ⇒ Object
17 18 19 20 |
# File 'lib/nl/encoder.rb', line 17 def put_string(value) reserve(value.bytesize) @position += @buffer.set_string(value, @position) end |
#put_value(type, value) ⇒ Object
28 29 30 31 |
# File 'lib/nl/encoder.rb', line 28 def put_value(type, value) reserve(IO::Buffer.size_of(type)) @position = @buffer.set_value(type, @position, value) end |
#put_value_at(offset, type, value) ⇒ Object
46 47 48 |
# File 'lib/nl/encoder.rb', line 46 def put_value_at(offset, type, value) @buffer.set_value(type, offset, value) end |
#put_values(types, values) ⇒ Object
33 34 35 36 |
# File 'lib/nl/encoder.rb', line 33 def put_values(types, values) reserve(IO::Buffer.size_of(types)) @position = @buffer.set_values(types, @position, values) end |
#put_values_at(offset, types, values) ⇒ Object
50 51 52 |
# File 'lib/nl/encoder.rb', line 50 def put_values_at(offset, types, values) @buffer.set_values(types, offset, values) end |
#put_zstring(value) ⇒ Object
22 23 24 25 26 |
# File 'lib/nl/encoder.rb', line 22 def put_zstring(value) reserve(value.bytesize + 1) @position += @buffer.set_string(value, @position) @position = @buffer.set_value(:U8, @position, 0) end |
#reserve(size) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/nl/encoder.rb', line 10 def reserve(size) nposition = @position + size if @buffer.size < nposition @buffer.resize([@buffer.size * 2, nposition].max) end end |