-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsize.go
73 lines (71 loc) · 2.02 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
package binp
// Size calculates the total capacity needed to hold all the values.
func Size(values []interface{}) (int, error) {
var total int
for _, v := range values {
switch v.(type) {
case int:
total += 9
case []int:
total += len(v.([]int))*8 + 3 // b[0] slice identifier + b[1,2] slice len
case int64:
total += 9
case []int64:
total += len(v.([]int64))*8 + 3 // b[0] slice identifier + b[1,2] slice len
case int32:
total += 5
case []int32:
total += len(v.([]int32))*4 + 3 // b[0] slice identifier + b[1,2] slice len
case int16:
total += 3
case []int16:
total += len(v.([]int16))*2 + 3 // b[0] slice identifier + b[1,2] slice len
case int8:
total += 2
case []int8:
total += len(v.([]int8))*1 + 3 // b[0] slice identifier + b[1,2] slice len
case uint:
total += 9
case []uint:
total += len(v.([]uint))*8 + 3
case uint64:
total += 9
case []uint64:
total += len(v.([]uint64))*8 + 3 // b[0] slice identifier + b[1,2] slice len
case uint32:
total += 5
case []uint32:
total += len(v.([]uint32))*4 + 3 // b[0] slice identifier + b[1,2] slice len
case uint16:
total += 3
case []uint16:
total += len(v.([]uint16))*2 + 3 // b[0] slice identifier + b[1,2] slice len
case byte:
total += 2
case []byte:
total += len(v.([]byte))*1 + 5 // b[0] slice identifier + b[1,2] slice len
case bool:
total += 1
case []bool:
total += len(v.([]bool))*1 + 3 // b[0] slice identifier + b[1,2] slice len
case float64:
total += 9
case []float64:
total += len(v.([]float64))*8 + 3 // b[0] slice identifier + b[1,2] slice len
case float32:
total += 5
case []float32:
total += len(v.([]float32))*4 + 3 // b[0] slice identifier + b[1,2] slice len
case string:
total += 5 + len(v.(string))
case []string:
total += 3 // String Slice identifier + 2 bytes for slice len
for _, e := range v.([]string) {
total += len(e) + 4 // 4 is the string length identifier
}
default:
return 0, ErrUnsupportedType
}
}
return total, nil
}