forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sizeof.go
80 lines (66 loc) · 1.16 KB
/
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
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
package kafka
import "fmt"
type sizable interface {
size() int32
}
func sizeof(a interface{}) int32 {
switch v := a.(type) {
case int8:
return 1
case int16:
return 2
case int32:
return 4
case int64:
return 8
case string:
return sizeofString(v)
case bool:
return 1
case []byte:
return sizeofBytes(v)
case sizable:
return v.size()
}
panic(fmt.Sprintf("unsupported type: %T", a))
}
func sizeofInt8(_ int8) int32 {
return 1
}
func sizeofInt16(_ int16) int32 {
return 2
}
func sizeofInt32(_ int32) int32 {
return 4
}
func sizeofInt64(_ int64) int32 {
return 8
}
func sizeofString(s string) int32 {
return 2 + int32(len(s))
}
func sizeofNullableString(s *string) int32 {
if s == nil {
return 2
}
return sizeofString(*s)
}
func sizeofBool(_ bool) int32 {
return 1
}
func sizeofBytes(b []byte) int32 {
return 4 + int32(len(b))
}
func sizeofArray(n int, f func(int) int32) int32 {
s := int32(4)
for i := 0; i != n; i++ {
s += f(i)
}
return s
}
func sizeofInt32Array(a []int32) int32 {
return 4 + (4 * int32(len(a)))
}
func sizeofStringArray(a []string) int32 {
return sizeofArray(len(a), func(i int) int32 { return sizeofString(a[i]) })
}