forked from alecthomas/go_serialization_benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Colfer.go
349 lines (304 loc) · 6.07 KB
/
Colfer.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
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
package goserbench
// This file was generated by colf(1); DO NOT EDIT
import (
"fmt"
"io"
"math"
"time"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = math.E
var _ = time.RFC3339
// ColferContinue signals a data continuation as a byte index.
type ColferContinue int
func (i ColferContinue) Error() string {
return fmt.Sprintf("colfer: data continuation at byte %d", i)
}
// ColferError signals a data mismatch as as a byte index.
type ColferError int
func (i ColferError) Error() string {
return fmt.Sprintf("colfer: unknown header at byte %d", i)
}
type ColferA struct {
Name string
BirthDay time.Time
Phone string
Siblings int32
Spouse bool
Money float64
}
// MarshalTo encodes o as Colfer into buf and returns the number of bytes written.
// If the buffer is too small, MarshalTo will panic.
func (o *ColferA) MarshalTo(buf []byte) int {
if o == nil {
return 0
}
buf[0] = 0x80
i := 1
if v := o.Name; len(v) != 0 {
buf[i] = 0
i++
x := uint(len(v))
for x >= 0x80 {
buf[i] = byte(x | 0x80)
x >>= 7
i++
}
buf[i] = byte(x)
i++
to := i + len(v)
copy(buf[i:], v)
i = to
}
if v := o.BirthDay; !v.IsZero() {
buf[i] = 1
s, ns := v.Unix(), v.Nanosecond()
buf[i+1], buf[i+2], buf[i+3], buf[i+4] = byte(s>>56), byte(s>>48), byte(s>>40), byte(s>>32)
buf[i+5], buf[i+6], buf[i+7], buf[i+8] = byte(s>>24), byte(s>>16), byte(s>>8), byte(s)
if ns == 0 {
i += 9
} else {
buf[i] |= 0x80
buf[i+9], buf[i+10], buf[i+11], buf[i+12] = byte(ns>>24), byte(ns>>16), byte(ns>>8), byte(ns)
i += 13
}
}
if v := o.Phone; len(v) != 0 {
buf[i] = 2
i++
x := uint(len(v))
for x >= 0x80 {
buf[i] = byte(x | 0x80)
x >>= 7
i++
}
buf[i] = byte(x)
i++
to := i + len(v)
copy(buf[i:], v)
i = to
}
if v := o.Siblings; v != 0 {
x := uint32(v)
if v >= 0 {
buf[i] = 3
} else {
x = ^x + 1
buf[i] = 3 | 0x80
}
i++
for x >= 0x80 {
buf[i] = byte(x | 0x80)
x >>= 7
i++
}
buf[i] = byte(x)
i++
}
if o.Spouse {
buf[i] = 4
i++
}
if v := o.Money; v != 0.0 {
buf[i] = 5
x := math.Float64bits(v)
buf[i+1], buf[i+2], buf[i+3], buf[i+4] = byte(x>>56), byte(x>>48), byte(x>>40), byte(x>>32)
buf[i+5], buf[i+6], buf[i+7], buf[i+8] = byte(x>>24), byte(x>>16), byte(x>>8), byte(x)
i += 9
}
buf[i] = 0x7f
i++
return i
}
// MarshalLen returns the Colfer serial byte size.
func (o *ColferA) MarshalLen() int {
if o == nil {
return 0
}
l := 2
if x := len(o.Name); x != 0 {
l += x
for x >= 0x80 {
x >>= 7
l++
}
l += 2
}
if v := o.BirthDay; !v.IsZero() {
if v.Nanosecond() == 0 {
l += 9
} else {
l += 13
}
}
if x := len(o.Phone); x != 0 {
l += x
for x >= 0x80 {
x >>= 7
l++
}
l += 2
}
if v := o.Siblings; v != 0 {
x := uint32(v)
if v < 0 {
x = ^x + 1
}
for x >= 0x80 {
x >>= 7
l++
}
l += 2
}
if o.Spouse {
l++
}
if o.Money != 0.0 {
l += 9
}
return l
}
// MarshalBinary encodes o as Colfer conform encoding.BinaryMarshaler.
// The error return is always nil.
func (o *ColferA) MarshalBinary() (data []byte, err error) {
data = make([]byte, o.MarshalLen())
o.MarshalTo(data)
return data, nil
}
// UnmarshalBinary decodes data as Colfer conform encoding.BinaryUnmarshaler.
// The error return options are io.EOF, goserbench.ColferError, and goserbench.ColferContinue.
func (o *ColferA) UnmarshalBinary(data []byte) error {
if len(data) == 0 {
return io.EOF
}
if data[0] != 0x80 {
return ColferError(0)
}
if len(data) == 1 {
return io.EOF
}
header := data[1]
i := 2
if header == 0 {
var x uint32
for shift := uint(0); ; shift += 7 {
if i == len(data) {
return io.EOF
}
b := data[i]
i++
if shift == 28 {
x |= uint32(b) << 28
break
}
x |= (uint32(b) & 0x7f) << shift
if b < 0x80 {
break
}
}
to := i + int(x)
if to >= len(data) {
return io.EOF
}
o.Name = string(data[i:to])
header = data[to]
i = to + 1
}
if header == 1 {
if i+8 >= len(data) {
return io.EOF
}
sec := uint64(data[i])<<56 | uint64(data[i+1])<<48 | uint64(data[i+2])<<40 | uint64(data[i+3])<<32
sec |= uint64(data[i+4])<<24 | uint64(data[i+5])<<16 | uint64(data[i+6])<<8 | uint64(data[i+7])
o.BirthDay = time.Unix(int64(sec), 0)
header = data[i+8]
i += 9
} else if header == 1|0x80 {
if i+12 >= len(data) {
return io.EOF
}
sec := uint64(data[i])<<56 | uint64(data[i+1])<<48 | uint64(data[i+2])<<40 | uint64(data[i+3])<<32
sec |= uint64(data[i+4])<<24 | uint64(data[i+5])<<16 | uint64(data[i+6])<<8 | uint64(data[i+7])
nsec := int64(uint(data[i+8])<<24 | uint(data[i+9])<<16 | uint(data[i+10])<<8 | uint(data[i+11]))
o.BirthDay = time.Unix(int64(sec), nsec)
header = data[i+12]
i += 13
}
if header == 2 {
var x uint32
for shift := uint(0); ; shift += 7 {
if i == len(data) {
return io.EOF
}
b := data[i]
i++
if shift == 28 {
x |= uint32(b) << 28
break
}
x |= (uint32(b) & 0x7f) << shift
if b < 0x80 {
break
}
}
to := i + int(x)
if to >= len(data) {
return io.EOF
}
o.Phone = string(data[i:to])
header = data[to]
i = to + 1
}
if header == 3 || header == 3|0x80 {
var x uint32
for shift := uint(0); ; shift += 7 {
if i == len(data) {
return io.EOF
}
b := data[i]
i++
if shift == 28 {
x |= uint32(b) << 28
break
}
x |= (uint32(b) & 0x7f) << shift
if b < 0x80 {
break
}
}
if header&0x80 != 0 {
x = ^x + 1
}
o.Siblings = int32(x)
if i == len(data) {
return io.EOF
}
header = data[i]
i++
}
if header == 4 {
o.Spouse = true
if i == len(data) {
return io.EOF
}
header = data[i]
i++
}
if header == 5 {
if i+8 >= len(data) {
return io.EOF
}
x := uint64(data[i])<<56 | uint64(data[i+1])<<48 | uint64(data[i+2])<<40 | uint64(data[i+3])<<32
x |= uint64(data[i+4])<<24 | uint64(data[i+5])<<16 | uint64(data[i+6])<<8 | uint64(data[i+7])
o.Money = math.Float64frombits(x)
header = data[i+8]
i += 9
}
if header != 0x7f {
return ColferError(i - 1)
}
if i != len(data) {
return ColferContinue(i)
}
return nil
}