forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
size.go
93 lines (78 loc) · 1.63 KB
/
size.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package protocol
import (
"math/bits"
"reflect"
)
func sizeOf(typ reflect.Type) int {
switch typ.Kind() {
case reflect.Bool, reflect.Int8:
return 1
case reflect.Int16:
return 2
case reflect.Int32:
return 4
case reflect.Int64:
return 8
default:
return 0
}
}
func sizeOfString(s string) int {
return 2 + len(s)
}
func sizeOfBytes(b []byte) int {
return 4 + len(b)
}
func sizeOfVarString(s string) int {
return sizeOfVarInt(int64(len(s))) + len(s)
}
func sizeOfCompactString(s string) int {
return sizeOfUnsignedVarInt(uint64(len(s)+1)) + len(s)
}
func sizeOfVarBytes(b []byte) int {
return sizeOfVarInt(int64(len(b))) + len(b)
}
func sizeOfCompactBytes(b []byte) int {
return sizeOfUnsignedVarInt(uint64(len(b)+1)) + len(b)
}
func sizeOfVarNullString(s string) int {
n := len(s)
if n == 0 {
return sizeOfVarInt(-1)
}
return sizeOfVarInt(int64(n)) + n
}
func sizeOfCompactNullString(s string) int {
n := len(s)
if n == 0 {
return sizeOfUnsignedVarInt(0)
}
return sizeOfUnsignedVarInt(uint64(n)+1) + n
}
func sizeOfVarNullBytes(b []byte) int {
if b == nil {
return sizeOfVarInt(-1)
}
n := len(b)
return sizeOfVarInt(int64(n)) + n
}
func sizeOfVarNullBytesIface(b Bytes) int {
if b == nil {
return sizeOfVarInt(-1)
}
n := b.Len()
return sizeOfVarInt(int64(n)) + n
}
func sizeOfCompactNullBytes(b []byte) int {
if b == nil {
return sizeOfUnsignedVarInt(0)
}
n := len(b)
return sizeOfUnsignedVarInt(uint64(n)+1) + n
}
func sizeOfVarInt(i int64) int {
return sizeOfUnsignedVarInt(uint64((i << 1) ^ (i >> 63))) // zig-zag encoding
}
func sizeOfUnsignedVarInt(i uint64) int {
return (bits.Len64(i|1) + 6) / 7
}