-
Notifications
You must be signed in to change notification settings - Fork 13
/
sizeof.go
39 lines (30 loc) · 877 Bytes
/
sizeof.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package csproto
import (
"math/bits"
"google.golang.org/protobuf/proto"
)
// SizeOfTagKey returns the number of bytes required to hold the Protobuf varint encoding of k.
func SizeOfTagKey(k int) int {
return SizeOfVarint(uint64(uint(k) << 3))
}
// SizeOfVarint returns the number of bytes required to hold the Protobuf varint encoding of v.
func SizeOfVarint(v uint64) int {
return (bits.Len64(v|1) + 6) / 7
}
// SizeOfZigZag returns the number of bytes required to hold the zig zag encoding of v.
func SizeOfZigZag(v uint64) int {
return SizeOfVarint((v << 1) ^ uint64((int64(v) >> 63)))
}
// Size returns the encoded size of msg.
func Size(msg interface{}) int {
if pm, ok := msg.(Sizer); ok {
return pm.Size()
}
if pm, ok := msg.(ProtoV1Sizer); ok {
return pm.XXX_Size()
}
if pm, ok := msg.(proto.Message); ok {
return proto.Size(pm)
}
return 0
}