Class: Bytepack::SingleTypeArray
- Defined in:
- lib/bytepack/compound/single_type_array.rb
Constant Summary collapse
- LENGTH_TYPE =
AnyType
Class Method Summary collapse
- .autodetect_dataType(array) ⇒ Object
-
.pack(val = []) ⇒ Object
First element is a data type indicator (Integer, Struct, String/Symbol).
- .unpack(bytes, offset = 0) ⇒ Object
Methods inherited from Struct
classifyDataType, config, packingDataType, single_type_array?, testpacking
Class Method Details
.autodetect_dataType(array) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/bytepack/compound/single_type_array.rb', line 30 def autodetect_dataType(array) if array[0].is_a?(::Integer) max_int = array.select {|e| e.is_a?(::Integer)}.max packingDataType(max_int) else packingDataType(array[0]) end end |
.pack(val = []) ⇒ Object
First element is a data type indicator (Integer, Struct, String/Symbol)
6 7 8 9 10 11 12 13 14 |
# File 'lib/bytepack/compound/single_type_array.rb', line 6 def pack(val = []) # First element is a data type indicator (Integer, Struct, String/Symbol) array = val[1..-1] dataType = classifyDataType(val[0]) unless dataType # No indicator recognized array = val dataType = autodetect_dataType(array) end TypeInfo.pack(dataType) + self::LENGTH_TYPE.pack(array.size) + array.map {|e| dataType.pack(e)}.join end |
.unpack(bytes, offset = 0) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/bytepack/compound/single_type_array.rb', line 16 def unpack(bytes, offset = 0) dataType, offset = *TypeInfo.unpack(bytes, offset) if dataType.nil? array = nil else array_size, offset = *self::LENGTH_TYPE.unpack(bytes, offset) array = array_size.times.map do element, offset = *dataType.unpack(bytes, offset) element end end [array, offset] end |