lib/codec.rb in scale_rb-0.1.15 vs lib/codec.rb in scale_rb-0.2.0
- old
+ new
@@ -143,16 +143,16 @@
def _do_decode_compact(bytes)
case bytes[0] & 3
when 0
[bytes[0] >> 2, bytes[1..]]
when 1
- [bytes[0..1].flip.to_uint >> 2, bytes[2..]]
+ [bytes[0..1]._flip._to_uint >> 2, bytes[2..]]
when 2
- [bytes[0..3].flip.to_uint >> 2, bytes[4..]]
+ [bytes[0..3]._flip._to_uint >> 2, bytes[4..]]
when 3
length = 4 + (bytes[0] >> 2)
- [bytes[1..length].flip.to_uint, bytes[length + 1..]]
+ [bytes[1..length]._flip._to_uint, bytes[length + 1..]]
else
raise Unreachable, 'type: Compact'
end
end
@@ -218,11 +218,11 @@
raise NotImplemented, "type: #{type.inspect}"
end
def decode_bytes(bytes)
length, remaining_bytes = _do_decode_compact(bytes)
- value = remaining_bytes[0...length].to_hex
+ value = remaining_bytes[0...length]._to_hex
# debug 'length', length
# debug 'value', value
[
value,
remaining_bytes[length..]
@@ -244,11 +244,11 @@
def decode_string(bytes)
length, remaining_bytes = _do_decode_compact(bytes)
raise NotEnoughBytesError, 'type: String' if remaining_bytes.length < length
- value = remaining_bytes[0...length].to_utf8
+ value = remaining_bytes[0...length]._to_utf8
# debug 'byte length', length
# debug 'value', value.inspect
[
value,
remaining_bytes[length..]
@@ -258,11 +258,11 @@
def decode_int(type, bytes)
bit_length = type[1..].to_i
byte_length = bit_length / 8
raise NotEnoughBytesError, "type: #{type}" if bytes.length < byte_length
- value = bytes[0...byte_length].flip.to_int(bit_length)
+ value = bytes[0...byte_length]._flip._to_int(bit_length)
# debug 'value', value
[
value,
bytes[byte_length..]
]
@@ -271,11 +271,11 @@
def decode_uint(type_def, bytes)
bit_length = type_def[1..].to_i
byte_length = bit_length / 8
raise NotEnoughBytesError, "type: #{type_def}" if bytes.length < byte_length
- value = bytes[0...byte_length].flip.to_uint
+ value = bytes[0...byte_length]._flip._to_uint
# debug 'value', value
[
value,
bytes[byte_length..]
]
@@ -394,19 +394,19 @@
prefix + body
end
def encode_compact(value)
return [value << 2] if (value >= 0) && (value < 64)
- return ((value << 2) + 1).to_bytes.flip if value < 2**14
- return ((value << 2) + 2).to_bytes.flip if value < 2**30
+ return ((value << 2) + 1)._to_bytes._flip if value < 2**14
+ return ((value << 2) + 2)._to_bytes._flip if value < 2**30
- bytes = value.to_bytes.flip
+ bytes = value._to_bytes._flip
[(((bytes.length - 4) << 2) + 3)] + bytes
end
def encode_uint(type, value)
bit_length = type[1..].to_i
- value.to_bytes(bit_length).flip
+ value._to_bytes(bit_length)._flip
end
def encode_option(type, value, registry = {})
return [0x00] if value.nil?