forked from wapc/tinygo-msgpack
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsizer.go
More file actions
139 lines (123 loc) · 2.42 KB
/
sizer.go
File metadata and controls
139 lines (123 loc) · 2.42 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package msgpack
import "math"
type Sizer struct {
length uint32
}
func NewSizer() Sizer {
return Sizer{}
}
func (s *Sizer) CheckError() error {
return nil
}
func (s *Sizer) Len() uint32 {
return s.length
}
func (s *Sizer) WriteNil() {
s.length++
}
func (s *Sizer) WriteString(value string) {
buf := []byte(value)
length := uint32(len(buf))
s.writeStringLength(length)
s.length += length
}
func (s *Sizer) writeStringLength(length uint32) {
if length < 32 {
s.length++
} else if length <= math.MaxUint8 {
s.length += 2
} else if length <= math.MaxUint16 {
s.length += 3
} else {
s.length += 5
}
}
func (s *Sizer) WriteBool(value bool) {
s.length++
}
func (s *Sizer) WriteArraySize(length uint32) {
if length < 16 {
s.length++
} else if length <= math.MaxUint16 {
s.length += 3
} else {
s.length += 5
}
}
func (s *Sizer) writeBinLength(length uint32) {
if length < math.MaxUint8 {
s.length += 1
} else if length <= math.MaxUint16 {
s.length += 2
} else {
s.length += 4
}
}
func (s *Sizer) WriteByteArray(value []byte) {
length := uint32(len(value))
if length == 0 {
s.length++
return
}
s.writeBinLength(length)
s.length += length + 1
}
func (s *Sizer) WriteMapSize(length uint32) {
if length < 16 {
s.length++
} else if length <= math.MaxUint16 {
s.length += 3
} else {
s.length += 5
}
}
func (s *Sizer) WriteInt8(value int8) {
s.WriteInt64(int64(value))
}
func (s *Sizer) WriteInt16(value int16) {
s.WriteInt64(int64(value))
}
func (s *Sizer) WriteInt32(value int32) {
s.WriteInt64(int64(value))
}
func (s *Sizer) WriteInt64(value int64) {
if value >= -(1<<5) && value < 1<<7 {
s.length++
} else if value < 1<<7 && value >= -(1<<7) {
s.length += 2
} else if value < 1<<15 && value >= -(1<<15) {
s.length += 3
} else if value < 1<<31 && value >= -(1<<31) {
s.length += 5
} else {
s.length += 9
}
}
func (s *Sizer) WriteUint8(value uint8) {
s.WriteUint64(uint64(value))
}
func (s *Sizer) WriteUint16(value uint16) {
s.WriteUint64(uint64(value))
}
func (s *Sizer) WriteUint32(value uint32) {
s.WriteUint64(uint64(value))
}
func (s *Sizer) WriteUint64(value uint64) {
if value < 1<<7 {
s.length++
} else if value < 1<<8 {
s.length += 2
} else if value < 1<<16 {
s.length += 3
} else if value < 1<<32 {
s.length += 5
} else {
s.length += 9
}
}
func (s *Sizer) WriteFloat32(value float32) {
s.length += 5
}
func (s *Sizer) WriteFloat64(value float64) {
s.length += 9
}