-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.go
More file actions
353 lines (318 loc) · 7.82 KB
/
encoder.go
File metadata and controls
353 lines (318 loc) · 7.82 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package msgpck
import (
"encoding/binary"
"math"
"unsafe"
)
// Encoder writes MessagePack data to a byte buffer.
type Encoder struct {
buf []byte
pos int
}
// NewEncoder creates a new Encoder with the given initial capacity.
func NewEncoder(capacity int) *Encoder {
return &Encoder{
buf: make([]byte, 0, capacity),
}
}
// NewEncoderBuffer creates an Encoder that writes to an existing buffer.
// The buffer will be grown as needed.
func NewEncoderBuffer(buf []byte) *Encoder {
return &Encoder{
buf: buf[:0], // reset length but keep capacity
}
}
// Reset resets the encoder for reuse.
func (e *Encoder) Reset() {
e.buf = e.buf[:0]
e.pos = 0
}
// Bytes returns the encoded bytes.
func (e *Encoder) Bytes() []byte {
return e.buf
}
// Len returns the length of encoded data.
func (e *Encoder) Len() int {
return len(e.buf)
}
// grow ensures there's space for n more bytes
func (e *Encoder) grow(n int) {
if cap(e.buf)-len(e.buf) >= n {
return
}
// Double capacity or add n, whichever is larger
newCap := cap(e.buf) * 2
if newCap < len(e.buf)+n {
newCap = len(e.buf) + n
}
newBuf := make([]byte, len(e.buf), newCap)
copy(newBuf, e.buf)
e.buf = newBuf
}
// writeByte writes a single byte
func (e *Encoder) writeByte(b byte) {
e.grow(1)
e.buf = append(e.buf, b)
}
// writeBytes writes multiple bytes
func (e *Encoder) writeBytes(b []byte) {
e.grow(len(b))
e.buf = append(e.buf, b...)
}
// writeUint16 writes a big-endian uint16
func (e *Encoder) writeUint16(v uint16) {
e.grow(2)
e.buf = append(e.buf, 0, 0)
binary.BigEndian.PutUint16(e.buf[len(e.buf)-2:], v)
}
// writeUint32 writes a big-endian uint32
func (e *Encoder) writeUint32(v uint32) {
e.grow(4)
e.buf = append(e.buf, 0, 0, 0, 0)
binary.BigEndian.PutUint32(e.buf[len(e.buf)-4:], v)
}
// writeUint64 writes a big-endian uint64
func (e *Encoder) writeUint64(v uint64) {
e.grow(8)
e.buf = append(e.buf, 0, 0, 0, 0, 0, 0, 0, 0)
binary.BigEndian.PutUint64(e.buf[len(e.buf)-8:], v)
}
// EncodeNil writes a nil value
func (e *Encoder) EncodeNil() {
e.writeByte(formatNil)
}
// EncodeBool writes a boolean value
func (e *Encoder) EncodeBool(v bool) {
if v {
e.writeByte(formatTrue)
} else {
e.writeByte(formatFalse)
}
}
// EncodeInt writes an int64 using the most compact format
func (e *Encoder) EncodeInt(v int64) {
if v >= 0 {
e.EncodeUint(uint64(v))
return
}
// Negative values
if v >= -32 {
// Negative fixint
e.writeByte(byte(v))
} else if v >= -128 {
e.writeByte(formatInt8)
e.writeByte(byte(v))
} else if v >= -32768 {
e.writeByte(formatInt16)
e.writeUint16(uint16(v))
} else if v >= -2147483648 {
e.writeByte(formatInt32)
e.writeUint32(uint32(v))
} else {
e.writeByte(formatInt64)
e.writeUint64(uint64(v))
}
}
// EncodeUint writes a uint64 using the most compact format
func (e *Encoder) EncodeUint(v uint64) {
if v <= 127 {
// Positive fixint
e.writeByte(byte(v))
} else if v <= 255 {
e.writeByte(formatUint8)
e.writeByte(byte(v))
} else if v <= 65535 {
e.writeByte(formatUint16)
e.writeUint16(uint16(v))
} else if v <= 4294967295 {
e.writeByte(formatUint32)
e.writeUint32(uint32(v))
} else {
e.writeByte(formatUint64)
e.writeUint64(v)
}
}
// EncodeFloat32 writes a float32 value
func (e *Encoder) EncodeFloat32(v float32) {
e.writeByte(formatFloat32)
e.writeUint32(math.Float32bits(v))
}
// EncodeFloat64 writes a float64 value
func (e *Encoder) EncodeFloat64(v float64) {
e.writeByte(formatFloat64)
e.writeUint64(math.Float64bits(v))
}
// EncodeString writes a string value (zero-copy using unsafe)
func (e *Encoder) EncodeString(v string) {
length := len(v)
if length <= 31 {
e.writeByte(fixstrPrefix | byte(length))
} else if length <= 255 {
e.writeByte(formatStr8)
e.writeByte(byte(length))
} else if length <= 65535 {
e.writeByte(formatStr16)
e.writeUint16(uint16(length))
} else {
e.writeByte(formatStr32)
e.writeUint32(uint32(length))
}
// Zero-copy string to bytes using unsafe
e.writeBytes(unsafe.Slice(unsafe.StringData(v), length))
}
// EncodeStringBytes writes a string from bytes
func (e *Encoder) EncodeStringBytes(v []byte) {
length := len(v)
if length <= 31 {
// Fixstr
e.writeByte(fixstrPrefix | byte(length))
} else if length <= 255 {
e.writeByte(formatStr8)
e.writeByte(byte(length))
} else if length <= 65535 {
e.writeByte(formatStr16)
e.writeUint16(uint16(length))
} else {
e.writeByte(formatStr32)
e.writeUint32(uint32(length))
}
e.writeBytes(v)
}
// EncodeBinary writes binary data
func (e *Encoder) EncodeBinary(v []byte) {
length := len(v)
if length <= 255 {
e.writeByte(formatBin8)
e.writeByte(byte(length))
} else if length <= 65535 {
e.writeByte(formatBin16)
e.writeUint16(uint16(length))
} else {
e.writeByte(formatBin32)
e.writeUint32(uint32(length))
}
e.writeBytes(v)
}
// EncodeArrayHeader writes the header for an array of the given length.
// Call this, then encode each element.
func (e *Encoder) EncodeArrayHeader(length int) {
if length <= 15 {
// Fixarray
e.writeByte(fixarrayPrefix | byte(length))
} else if length <= 65535 {
e.writeByte(formatArray16)
e.writeUint16(uint16(length))
} else {
e.writeByte(formatArray32)
e.writeUint32(uint32(length))
}
}
// EncodeMapHeader writes the header for a map of the given length.
// Call this, then encode each key-value pair.
func (e *Encoder) EncodeMapHeader(length int) {
if length <= 15 {
// Fixmap
e.writeByte(fixmapPrefix | byte(length))
} else if length <= 65535 {
e.writeByte(formatMap16)
e.writeUint16(uint16(length))
} else {
e.writeByte(formatMap32)
e.writeUint32(uint32(length))
}
}
// EncodeExt writes extension data
func (e *Encoder) EncodeExt(extType int8, data []byte) {
length := len(data)
switch length {
case 1:
e.writeByte(formatFixExt1)
case 2:
e.writeByte(formatFixExt2)
case 4:
e.writeByte(formatFixExt4)
case 8:
e.writeByte(formatFixExt8)
case 16:
e.writeByte(formatFixExt16)
default:
if length <= 255 {
e.writeByte(formatExt8)
e.writeByte(byte(length))
} else if length <= 65535 {
e.writeByte(formatExt16)
e.writeUint16(uint16(length))
} else {
e.writeByte(formatExt32)
e.writeUint32(uint32(length))
}
}
e.writeByte(byte(extType))
e.writeBytes(data)
}
// EncodeValue writes a Value
func (e *Encoder) EncodeValue(v *Value) {
switch v.Type {
case TypeNil:
e.EncodeNil()
case TypeBool:
e.EncodeBool(v.Bool)
case TypeInt:
e.EncodeInt(v.Int)
case TypeUint:
e.EncodeUint(v.Uint)
case TypeFloat32:
e.EncodeFloat32(v.Float32)
case TypeFloat64:
e.EncodeFloat64(v.Float64)
case TypeString:
e.EncodeStringBytes(v.Bytes)
case TypeBinary:
e.EncodeBinary(v.Bytes)
case TypeArray:
e.EncodeArrayHeader(len(v.Array))
for i := range v.Array {
e.EncodeValue(&v.Array[i])
}
case TypeMap:
e.EncodeMapHeader(len(v.Map))
for i := range v.Map {
e.EncodeStringBytes(v.Map[i].Key)
e.EncodeValue(&v.Map[i].Value)
}
case TypeExt:
e.EncodeExt(v.Ext.Type, v.Ext.Data)
}
}
// EncodeInt64Array encodes a slice of int64 values.
// More efficient than encoding elements individually.
func (e *Encoder) EncodeInt64Array(arr []int64) {
e.EncodeArrayHeader(len(arr))
for _, v := range arr {
e.EncodeInt(v)
}
}
// EncodeUint64Array encodes a slice of uint64 values.
// More efficient than encoding elements individually.
func (e *Encoder) EncodeUint64Array(arr []uint64) {
e.EncodeArrayHeader(len(arr))
for _, v := range arr {
e.EncodeUint(v)
}
}
// EncodeFloat64Array encodes a slice of float64 values.
// More efficient than encoding elements individually.
func (e *Encoder) EncodeFloat64Array(arr []float64) {
e.EncodeArrayHeader(len(arr))
for _, v := range arr {
e.EncodeFloat64(v)
}
}
// EncodeStringArray encodes a slice of strings.
// More efficient than encoding elements individually.
func (e *Encoder) EncodeStringArray(arr []string) {
e.EncodeArrayHeader(len(arr))
for _, v := range arr {
e.EncodeString(v)
}
}