mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
leb128: make decoding return the number of decoded bytes, in addition to the decoded value (#17912)
This commit is contained in:
parent
abe0ad886b
commit
b772731b98
@ -66,8 +66,8 @@ pub fn encode_u32(value u32) []u8 {
|
||||
return result
|
||||
}
|
||||
|
||||
// decode_i32 decodes an i32 from the given leb128 encoded array `value`
|
||||
pub fn decode_i32(value []u8) i32 {
|
||||
// decode_i32 decodes an i32 and returns the number of bytes used from the given leb128 encoded array `value`
|
||||
pub fn decode_i32(value []u8) (i32, int) {
|
||||
mut result := int(0)
|
||||
mut shift := 0
|
||||
for b in value {
|
||||
@ -80,11 +80,11 @@ pub fn decode_i32(value []u8) i32 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return result
|
||||
return result, shift / 7
|
||||
}
|
||||
|
||||
// decode_i64 decodes an i64 from the given leb128 encoded array `value`
|
||||
pub fn decode_i64(value []u8) i64 {
|
||||
// decode_i64 decodes an i64 and returns the number of bytes used from the given leb128 encoded array `value`
|
||||
pub fn decode_i64(value []u8) (i64, int) {
|
||||
mut result := u64(0)
|
||||
mut shift := 0
|
||||
for b in value {
|
||||
@ -97,11 +97,11 @@ pub fn decode_i64(value []u8) i64 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return i64(result)
|
||||
return i64(result), shift / 7
|
||||
}
|
||||
|
||||
// decode_u64 decodes an u64 from the given leb128 encoded array `value`
|
||||
pub fn decode_u64(value []u8) u64 {
|
||||
// decode_u64 decodes an u64 and returns the number of bytes used from the given leb128 encoded array `value`
|
||||
pub fn decode_u64(value []u8) (u64, int) {
|
||||
mut result := u64(0)
|
||||
mut shift := 0
|
||||
for b in value {
|
||||
@ -111,11 +111,11 @@ pub fn decode_u64(value []u8) u64 {
|
||||
}
|
||||
shift += 7
|
||||
}
|
||||
return result
|
||||
return result, shift / 7 + 1
|
||||
}
|
||||
|
||||
// decode_u32 decodes an u32 from the given leb128 encoded array `value`
|
||||
pub fn decode_u32(value []u8) u32 {
|
||||
// decode_u32 decodes an u32 and returns the number of bytes used from the given leb128 encoded array `value`
|
||||
pub fn decode_u32(value []u8) (u32, int) {
|
||||
mut result := u32(0)
|
||||
mut shift := 0
|
||||
for b in value {
|
||||
@ -125,5 +125,5 @@ pub fn decode_u32(value []u8) u32 {
|
||||
}
|
||||
shift += 7
|
||||
}
|
||||
return result
|
||||
return result, shift / 7 + 1
|
||||
}
|
||||
|
@ -13,9 +13,13 @@ struct PairI {
|
||||
|
||||
fn test_basic() {
|
||||
assert leb128.encode_u64(624485) == [u8(0xe5), 0x8e, 0x26]
|
||||
assert leb128.decode_u64([u8(0xe5), 0x8e, 0x26]) == 624485
|
||||
uval, ulen := leb128.decode_u64([u8(0xe5), 0x8e, 0x26])
|
||||
assert uval == 624485, 'val, _ := leb128.decode_u64([u8(0xe5), 0x8e, 0x26]) == 624486'
|
||||
assert ulen == 3, '_, len := leb128.decode_u64([u8(0xe5), 0x8e, 0x26]) == 3'
|
||||
assert leb128.encode_i64(-123456) == [u8(0xc0), 0xbb, 0x78]
|
||||
assert leb128.decode_i64([u8(0xc0), 0xbb, 0x78]) == -123456
|
||||
sval, slen := leb128.decode_i64([u8(0xc0), 0xbb, 0x78])
|
||||
assert sval == -123456, 'val, _ := leb128.decode_i64([u8(0xc0), 0xbb, 0x78]) == -123456'
|
||||
assert slen == 3, '_, len := leb128.decode_i64([u8(0xc0), 0xbb, 0x78]) == 3'
|
||||
}
|
||||
|
||||
fn test_unsigned_data() {
|
||||
@ -23,7 +27,9 @@ fn test_unsigned_data() {
|
||||
assert leb128.encode_u64(x.value).hex() == x.encoded, 'leb128.encode_u64( ${x.value} ) == ${x.encoded}'
|
||||
bytes := hex.decode(x.encoded)!
|
||||
// eprintln('>> bytes: ${bytes} | pair: ${x}')
|
||||
assert leb128.decode_u64(bytes) == x.value, 'leb128.decode_u64( ${x.encoded} ) == ${x.value}'
|
||||
val, len := leb128.decode_u64(bytes)
|
||||
assert val == x.value, 'val, _ := leb128.decode_u64( ${x.encoded} ) == ${x.value}'
|
||||
assert len == bytes.len, '_, len := leb128.decode_u64( ${x.encoded} ) = ${bytes.len}'
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,7 +38,9 @@ fn test_signed_data() {
|
||||
assert leb128.encode_i64(x.value).hex() == x.encoded, 'k: ${x.value} | v: ${x.encoded}'
|
||||
bytes := hex.decode(x.encoded)!
|
||||
// eprintln('>> bytes: ${bytes} | pair: ${x}')
|
||||
assert leb128.decode_i64(bytes) == x.value, 'leb128.decode_i64( ${x.encoded} ) == ${x.value}'
|
||||
val, len := leb128.decode_i64(bytes)
|
||||
assert val == x.value, 'val, _ := leb128.decode_i64( ${x.encoded} ) == ${x.value}'
|
||||
assert len == bytes.len, '_, len := leb128.decode_i64( ${x.encoded} ) == ${x.value}'
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user