forked from TrueCloudLab/frostfs-api-go
Add stable marshaller helper for bool
Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
parent
311b76b75b
commit
89bd8f3915
4 changed files with 147 additions and 22 deletions
|
@ -16,10 +16,10 @@ func BytesMarshal(field int, buf, v []byte) (int, error) {
|
|||
return 0, nil
|
||||
}
|
||||
|
||||
prefix := field<<3 | 0x2
|
||||
|
||||
// buf length check can prevent panic at PutUvarint, but it will make
|
||||
// marshaller a bit slower.
|
||||
|
||||
prefix := field<<3 | 0x2
|
||||
i := binary.PutUvarint(buf, uint64(prefix))
|
||||
i += binary.PutUvarint(buf[i:], uint64(len(v)))
|
||||
i += copy(buf[i:], v)
|
||||
|
@ -46,6 +46,30 @@ func StringSize(field int, v string) int {
|
|||
return BytesSize(field, []byte(v))
|
||||
}
|
||||
|
||||
func BoolMarshal(field int, buf []byte, v bool) (int, error) {
|
||||
if !v {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
prefix := field << 3
|
||||
|
||||
// buf length check can prevent panic at PutUvarint, but it will make
|
||||
// marshaller a bit slower.
|
||||
i := binary.PutUvarint(buf, uint64(prefix))
|
||||
buf[i] = 0x1
|
||||
|
||||
return i + 1, nil
|
||||
}
|
||||
|
||||
func BoolSize(field int, v bool) int {
|
||||
if !v {
|
||||
return 0
|
||||
}
|
||||
|
||||
prefix := field << 3
|
||||
return VarUIntSize(uint64(prefix)) + 1 // bool is always 1 byte long
|
||||
}
|
||||
|
||||
// varUIntSize returns length of varint byte sequence for uint64 value 'x'.
|
||||
func VarUIntSize(x uint64) int {
|
||||
return (bits.Len64(x|1) + 6) / 7
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue