if last_error != null then return 0
var typ = read_byte
- if typ == null then
+ if typ < 0 then
# Error, return default `null`
return null
- else if typ & 0b1000_0000u8 == 0u8 or typ & 0b1110_0000u8 == 0b1110_0000u8 then
+ else if typ & 0b1000_0000 == 0 or typ & 0b1110_0000 == 0b1110_0000 then
# fixint
var bytes = new Bytes.with_capacity(1)
bytes.add typ
return bytes.to_i(signed=true)
- else if typ & 0b1111_0000u8 == 0b1000_0000u8 then
+ else if typ & 0b1111_0000 == 0b1000_0000 then
# fixmap
- var len = typ & 0b0000_1111u8
+ var len = typ & 0b0000_1111
return read_msgpack_map_data(len.to_i)
- else if typ & 0b1111_0000u8 == 0b1001_0000u8 then
+ else if typ & 0b1111_0000 == 0b1001_0000 then
# fixarray
- var len = typ & 0b0000_1111u8
+ var len = typ & 0b0000_1111
return read_msgpack_array_data(len.to_i)
- else if typ & 0b1110_0000u8 == 0b1010_0000u8 then
+ else if typ & 0b1110_0000 == 0b1010_0000 then
# fixstr
- var len = typ & 0b0001_1111u8
+ var len = typ & 0b0001_1111
return read_bytes(len.to_i).to_s
- else if typ == 0xC0u8 then
+ else if typ == 0xC0 then
return null
- else if typ == 0xC2u8 then
+ else if typ == 0xC2 then
return false
- else if typ == 0xC3u8 then
+ else if typ == 0xC3 then
return true
- else if typ == 0xCCu8 then
- # uint8
- return (read_byte or else 0u8).to_i
- else if typ == 0xCDu8 then
- # uint16
- return read_bytes(2).to_i
- else if typ == 0xCEu8 then
- # uint32
- return read_bytes(4).to_i
- else if typ == 0xCFu8 then
- # uint64
- return read_bytes(8).to_i
- else if typ == 0xD0u8 then
- # int8
- return read_bytes(1).to_i(true)
- else if typ == 0xD1u8 then
- # int16
- return read_bytes(2).to_i(true)
- else if typ == 0xD2u8 then
- # int32
- return read_bytes(4).to_i(true)
- else if typ == 0xD3u8 then
- # int64
- return read_int64
-
- else if typ == 0xCAu8 then
- return read_float
- else if typ == 0xCBu8 then
- return read_double
-
- else if typ == 0xD9u8 then
- # str8
- var len = read_byte
- if len == null then return null
- return read_bytes(len.to_i).to_s
- else if typ == 0xDAu8 then
- # str16
- var len = read_bytes(2)
- return read_bytes(len.to_i).to_s
- else if typ == 0xDBu8 then
- # str32
- var len = read_bytes(4)
- return read_bytes(len.to_i).to_s
+ else if typ >= 0xCC and typ <= 0xCF then
+ # uint8, 16, 32 and 64
+ var len = 1 << (typ - 0xCC)
+ return read_bytes(len).to_i
- else if typ == 0xC4u8 then
- # bin8
- var len = read_byte
- if len == null then return null
- return read_bytes(len.to_i)
- else if typ == 0xC5u8 then
- # bin16
- var len = read_bytes(2)
- return read_bytes(len.to_i)
- else if typ == 0xC6u8 then
- # bin32
- var len = read_bytes(4)
- return read_bytes(len.to_i)
-
- else if typ == 0xDCu8 then
- # array16
- var len = read_bytes(2)
- return read_msgpack_array_data(len.to_i)
- else if typ == 0xDDu8 then
- # array32
- var len = read_bytes(4)
- return read_msgpack_array_data(len.to_i)
+ else if typ >= 0xD0 and typ <= 0xD3 then
+ # int8, 16, 32 and 64
+ var len = 1 << (typ - 0xD0)
+ return read_bytes(len).to_i(true)
- else if typ == 0xDEu8 then
- # map16
- var len = read_bytes(2)
- return read_msgpack_map_data(len.to_i)
- else if typ == 0xDFu8 then
- # map32
- var len = read_bytes(4)
- return read_msgpack_map_data(len.to_i)
+ else if typ == 0xCA then
+ return read_float
+ else if typ == 0xCB then
+ return read_double
- else if typ == 0xD4u8 then
+ else if typ >= 0xD9 and typ <= 0xDB then
+ # str8, 16 and 32
+ var len_ln = 1 << (typ - 0xD9)
+ var bf = read_bytes(len_ln)
+ var len = bf.to_i
+ if len < 0 then return null
+ var rd_buf = read_bytes(len)
+ if rd_buf.length != len then
+ # Bad formatted message.
+ return null
+ end
+ return rd_buf.to_s
+
+ else if typ >= 0xC4 and typ <= 0xC6 then
+ # bin8, 16 or 32
+ var len_ln = 1 << (typ - 0xC4)
+ var bf = read_bytes(len_ln)
+ var len = bf.to_i
+ if len < 0 then return null
+ var rd_buf = read_bytes(len)
+ if rd_buf.length != len then
+ # Bad formatted message.
+ return null
+ end
+ return rd_buf
+
+ else if typ == 0xDC or typ == 0xDD then
+ # array16 and array32
+ var len_ln = 2 << (typ - 0xDC)
+ var lenbuf = read_bytes(len_ln)
+ return read_msgpack_array_data(lenbuf.to_i)
+
+ else if typ == 0xDE or typ == 0xDF then
+ # map16 and map32
+ var len_ln = 2 << (typ - 0xDE)
+ var lenbuf = read_bytes(len_ln)
+ return read_msgpack_map_data(lenbuf.to_i)
+
+ else if typ == 0xD4 then
# fixext1
return read_msgpack_fixext_data(1)
- else if typ == 0xD5u8 then
+ else if typ == 0xD5 then
# fixext2
return read_msgpack_fixext_data(2)
- else if typ == 0xD6u8 then
+ else if typ == 0xD6 then
# fixext4
return read_msgpack_fixext_data(4)
- else if typ == 0xD7u8 then
+ else if typ == 0xD7 then
# fixext8
return read_msgpack_fixext_data(8)
- else if typ == 0xD8u8 then
+ else if typ == 0xD8 then
# fixext16
return read_msgpack_fixext_data(16)
- else if typ == 0xC7u8 then
+ else if typ == 0xC7 then
# ext1
return read_msgpack_ext_data(1)
- else if typ == 0xC8u8 then
+ else if typ == 0xC8 then
# ext2
return read_msgpack_ext_data(2)
- else if typ == 0xC9u8 then
+ else if typ == 0xC9 then
# ext4
return read_msgpack_ext_data(4)
end
- print_error "MessagePack Warning: Found no match for typ {typ} / 0b{typ.to_i.to_base(2)}"
+ print_error "MessagePack Warning: Found no match for typ {typ.to_base(16)} / 0b{typ.to_base(2)}"
return null
end
# var reader = new BytesReader(b"\xC7\x03\x0A\x0B\x0C\x0D")
# var ext = reader.read_msgpack
# assert ext isa MsgPackExt
- # assert ext.typ == 0x0Au8
+ # assert ext.typ == 0x0a
# assert ext.data == b"\x0B\x0C\x0D"
# ~~~
private fun read_msgpack_fixext_data(len: Int): MsgPackExt
do
- var exttyp = read_byte or else 0u8
+ var exttyp = read_byte
+ if exttyp < 0 then exttyp = 0
var data = read_bytes(len)
return new MsgPackExt(exttyp, data)
end