mirror of
https://github.com/vlang/v.git
synced 2025-08-04 02:07:28 -04:00
parent
1c61b6f85e
commit
e146ee600f
@ -127,7 +127,7 @@ fn encode_primitive[T](mut s EncodeState, value T) ! {
|
|||||||
s.put_u64(u64(value))
|
s.put_u64(u64(value))
|
||||||
} $else {
|
} $else {
|
||||||
// TODO: `any` type support?
|
// TODO: `any` type support?
|
||||||
return error('${@FN}unsupported type ${typeof(value).name}')
|
return error('${@FN}(): unsupported type ${typeof(value).name}')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -309,15 +309,15 @@ fn decode_primitive[T](mut s DecodeState, value T) !T {
|
|||||||
return T(s.get_u64()!)
|
return T(s.get_u64()!)
|
||||||
} $else {
|
} $else {
|
||||||
// TODO: `any` type support?
|
// TODO: `any` type support?
|
||||||
return error('${@FN}unsupported type ${typeof(value).name}')
|
return error('${@FN}(): unsupported type ${typeof(value).name}')
|
||||||
}
|
}
|
||||||
return error('impossiable error')
|
return error('${@FN}(): impossible error')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decode_array[T](mut s DecodeState, _ []T) ![]T {
|
fn decode_array[T](mut s DecodeState, _ []T) ![]T {
|
||||||
len := int(s.get_u64()!)
|
len := int(s.get_u64()!)
|
||||||
if len <= 0 || s.offset + len > s.b.len {
|
if len <= 0 || s.offset + len > s.b.len {
|
||||||
return error('invalid array length decode from stream')
|
return error('${@FN}(): invalid array length decode from stream')
|
||||||
}
|
}
|
||||||
mut arr := []T{cap: len}
|
mut arr := []T{cap: len}
|
||||||
$if T is u8 {
|
$if T is u8 {
|
||||||
@ -327,7 +327,7 @@ fn decode_array[T](mut s DecodeState, _ []T) ![]T {
|
|||||||
} $else {
|
} $else {
|
||||||
for _ in 0 .. len {
|
for _ in 0 .. len {
|
||||||
if s.offset >= s.b.len {
|
if s.offset >= s.b.len {
|
||||||
return error('unexpected end of data')
|
return error('${@FN}(): unexpected end of data')
|
||||||
}
|
}
|
||||||
$if T is $array {
|
$if T is $array {
|
||||||
arr << decode_array(mut s, T{})!
|
arr << decode_array(mut s, T{})!
|
||||||
@ -348,7 +348,7 @@ fn decode_array[T](mut s DecodeState, _ []T) ![]T {
|
|||||||
fn decode_string(mut s DecodeState) !string {
|
fn decode_string(mut s DecodeState) !string {
|
||||||
len := int(s.get_u64()!)
|
len := int(s.get_u64()!)
|
||||||
if len <= 0 || s.offset + len > s.b.len {
|
if len <= 0 || s.offset + len > s.b.len {
|
||||||
return error('invalid string length decode from stream')
|
return error('${@FN}(): invalid string length decode from stream')
|
||||||
}
|
}
|
||||||
str := unsafe { s.b[s.offset..s.offset + len].bytestr() }
|
str := unsafe { s.b[s.offset..s.offset + len].bytestr() }
|
||||||
s.offset += len
|
s.offset += len
|
||||||
@ -379,7 +379,7 @@ type Any = int
|
|||||||
fn decode_map[K, V](mut s DecodeState, _ map[K]V) !map[K]V {
|
fn decode_map[K, V](mut s DecodeState, _ map[K]V) !map[K]V {
|
||||||
len := int(s.get_u64()!)
|
len := int(s.get_u64()!)
|
||||||
if len <= 0 || s.offset + len > s.b.len {
|
if len <= 0 || s.offset + len > s.b.len {
|
||||||
return error('invalid map length decode from stream')
|
return error('${@FN}(): invalid map length decode from stream')
|
||||||
}
|
}
|
||||||
|
|
||||||
mut m := map[K]V{}
|
mut m := map[K]V{}
|
||||||
@ -395,17 +395,19 @@ fn decode_map[K, V](mut s DecodeState, _ map[K]V) !map[K]V {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// decode value
|
// decode value
|
||||||
mut v := Any(0)
|
$if V is $struct {
|
||||||
$if V is $string {
|
v := decode_struct(mut s, V{})!
|
||||||
v = decode_string(mut s)!
|
m[k as K] = v
|
||||||
} $else $if V is $struct {
|
|
||||||
v = decode_struct(mut s, V{})!
|
|
||||||
} $else $if V is $map {
|
} $else $if V is $map {
|
||||||
v = decode_map(mut s, V{})!
|
v := decode_map(mut s, V{})!
|
||||||
|
m[k as K] = v
|
||||||
|
} $else $if V is $string {
|
||||||
|
v := decode_string(mut s)!
|
||||||
|
m[k as K] = v
|
||||||
} $else {
|
} $else {
|
||||||
v = decode_primitive(mut s, unsafe { V(0) })!
|
v := decode_primitive(mut s, unsafe { V(0) })!
|
||||||
|
m[k as K] = v
|
||||||
}
|
}
|
||||||
m[k as K] = v as V
|
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
@ -413,7 +415,7 @@ fn decode_map[K, V](mut s DecodeState, _ map[K]V) !map[K]V {
|
|||||||
@[inline]
|
@[inline]
|
||||||
fn (mut s DecodeState) get_u64() !u64 {
|
fn (mut s DecodeState) get_u64() !u64 {
|
||||||
if s.offset + 8 > s.b.len {
|
if s.offset + 8 > s.b.len {
|
||||||
return error('bytes length is not enough for u64')
|
return error('${@FN}(): bytes length is not enough for u64')
|
||||||
}
|
}
|
||||||
defer {
|
defer {
|
||||||
s.offset += 8
|
s.offset += 8
|
||||||
@ -428,7 +430,7 @@ fn (mut s DecodeState) get_u64() !u64 {
|
|||||||
@[inline]
|
@[inline]
|
||||||
fn (mut s DecodeState) get_u32() !u32 {
|
fn (mut s DecodeState) get_u32() !u32 {
|
||||||
if s.offset + 4 > s.b.len {
|
if s.offset + 4 > s.b.len {
|
||||||
return error('bytes length is not enough for u32')
|
return error('${@FN}(): bytes length is not enough for u32')
|
||||||
}
|
}
|
||||||
defer {
|
defer {
|
||||||
s.offset += 4
|
s.offset += 4
|
||||||
@ -443,7 +445,7 @@ fn (mut s DecodeState) get_u32() !u32 {
|
|||||||
@[inline]
|
@[inline]
|
||||||
fn (mut s DecodeState) get_u16() !u16 {
|
fn (mut s DecodeState) get_u16() !u16 {
|
||||||
if s.offset + 2 > s.b.len {
|
if s.offset + 2 > s.b.len {
|
||||||
return error('bytes length is not enough for u16')
|
return error('${@FN}(): bytes length is not enough for u16')
|
||||||
}
|
}
|
||||||
defer {
|
defer {
|
||||||
s.offset += 2
|
s.offset += 2
|
||||||
@ -458,7 +460,7 @@ fn (mut s DecodeState) get_u16() !u16 {
|
|||||||
@[inline]
|
@[inline]
|
||||||
fn (mut s DecodeState) get_u8() !u8 {
|
fn (mut s DecodeState) get_u8() !u8 {
|
||||||
if s.offset + 1 > s.b.len {
|
if s.offset + 1 > s.b.len {
|
||||||
return error('bytes length is not enough for u8')
|
return error('${@FN}(): bytes length is not enough for u8')
|
||||||
}
|
}
|
||||||
defer {
|
defer {
|
||||||
s.offset += 1
|
s.offset += 1
|
||||||
|
@ -192,6 +192,10 @@ fn test_encode_decode_array() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct St {
|
||||||
|
i int
|
||||||
|
}
|
||||||
|
|
||||||
fn test_encode_decode_map() {
|
fn test_encode_decode_map() {
|
||||||
a_map_string_string := {
|
a_map_string_string := {
|
||||||
'abc': 'def'
|
'abc': 'def'
|
||||||
@ -242,6 +246,10 @@ fn test_encode_decode_map() {
|
|||||||
'abc': usize(432211)
|
'abc': usize(432211)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
a_map_string_struct := {
|
||||||
|
's': St{1}
|
||||||
|
}
|
||||||
|
|
||||||
b_map_string_string := encode_binary(a_map_string_string)!
|
b_map_string_string := encode_binary(a_map_string_string)!
|
||||||
b_map_string_int := encode_binary(a_map_string_int)!
|
b_map_string_int := encode_binary(a_map_string_int)!
|
||||||
b_map_string_u8 := encode_binary(a_map_string_u8)!
|
b_map_string_u8 := encode_binary(a_map_string_u8)!
|
||||||
@ -258,6 +266,7 @@ fn test_encode_decode_map() {
|
|||||||
b_map_string_rune := encode_binary(a_map_string_rune)!
|
b_map_string_rune := encode_binary(a_map_string_rune)!
|
||||||
b_map_string_isize := encode_binary(a_map_string_isize)!
|
b_map_string_isize := encode_binary(a_map_string_isize)!
|
||||||
b_map_string_usize := encode_binary(a_map_string_usize)!
|
b_map_string_usize := encode_binary(a_map_string_usize)!
|
||||||
|
b_map_string_struct := encode_binary(a_map_string_struct)!
|
||||||
|
|
||||||
c_map_string_string := decode_binary[map[string]string](b_map_string_string)!
|
c_map_string_string := decode_binary[map[string]string](b_map_string_string)!
|
||||||
c_map_string_int := decode_binary[map[string]int](b_map_string_int)!
|
c_map_string_int := decode_binary[map[string]int](b_map_string_int)!
|
||||||
@ -275,6 +284,7 @@ fn test_encode_decode_map() {
|
|||||||
c_map_string_rune := decode_binary[map[string]rune](b_map_string_rune)!
|
c_map_string_rune := decode_binary[map[string]rune](b_map_string_rune)!
|
||||||
c_map_string_isize := decode_binary[map[string]isize](b_map_string_isize)!
|
c_map_string_isize := decode_binary[map[string]isize](b_map_string_isize)!
|
||||||
c_map_string_usize := decode_binary[map[string]usize](b_map_string_usize)!
|
c_map_string_usize := decode_binary[map[string]usize](b_map_string_usize)!
|
||||||
|
c_map_string_struct := decode_binary[map[string]St](b_map_string_struct)!
|
||||||
|
|
||||||
assert a_map_string_string == c_map_string_string
|
assert a_map_string_string == c_map_string_string
|
||||||
assert a_map_string_int == c_map_string_int
|
assert a_map_string_int == c_map_string_int
|
||||||
@ -291,6 +301,7 @@ fn test_encode_decode_map() {
|
|||||||
assert a_map_string_rune == c_map_string_rune
|
assert a_map_string_rune == c_map_string_rune
|
||||||
assert a_map_string_isize == c_map_string_isize
|
assert a_map_string_isize == c_map_string_isize
|
||||||
assert a_map_string_usize == c_map_string_usize
|
assert a_map_string_usize == c_map_string_usize
|
||||||
|
assert a_map_string_struct == c_map_string_struct
|
||||||
|
|
||||||
assert b_map_string_string == [u8(1), 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 97, 98,
|
assert b_map_string_string == [u8(1), 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 97, 98,
|
||||||
99, 3, 0, 0, 0, 0, 0, 0, 0, 100, 101, 102]
|
99, 3, 0, 0, 0, 0, 0, 0, 0, 100, 101, 102]
|
||||||
@ -331,6 +342,8 @@ fn test_encode_decode_map() {
|
|||||||
assert b_map_string_usize == [u8(1), 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 97, 98,
|
assert b_map_string_usize == [u8(1), 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 97, 98,
|
||||||
99, 83, 152, 6, 0]
|
99, 83, 152, 6, 0]
|
||||||
}
|
}
|
||||||
|
assert b_map_string_struct == [u8(1), 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 115, 1,
|
||||||
|
0, 0, 0, 0, 0, 0, 0]
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MyStruct {
|
struct MyStruct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user