-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.int.family.go
304 lines (254 loc) · 7.66 KB
/
encode.int.family.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
package msgpack
import (
"fmt"
"math"
)
// EncodeFixedInt writes a fixed int to the current writer. The
// function will panic with ErrOutOfRange if the value is
// out of range for a msgpack fixed int encoding.
//
// The valid range for EncodeFixedInt is -32..127 (incl.)
//
// To encode an integer outside of this range use a function
// appropriate to the type you wish to encode or EncodeInt; these
// functions all select the most efficient packing for the
// value involved.
func (enc Encoder) EncodeFixedInt(i int) error {
switch {
case i < int(minFixedInt),
i > int(maxFixedInt):
panic(fmt.Errorf("EncodeFixedInt: %d: %w: -%d..%d", i, ErrValueOutOfRange, minFixedInt, maxFixedInt))
default:
return enc.Write(byte(i))
}
}
// EncodeInt8 encodes a signed 8-bit integer to the current writer.
//
// The encoder will use the most efficient format for the value
// being encoded, which may be a fixed int.
func (enc Encoder) EncodeInt8(i int8) error {
switch {
case i < minFixedInt:
_ = enc.Write(typeInt8)
return enc.Write(i)
default: // all int8 are <= maxFixedInt:
return enc.Write(byte(i)) // bypass the range check in EncodeFixedInt
}
}
// EncodeInt16 encodes a signed 16-bit integer to the current writer.
//
// The encoder will use the most efficient format for the value
// being encoded, which may not be int16.
func (enc Encoder) EncodeInt16(i int16) error {
switch {
case i < int16(math.MinInt8):
_ = enc.Write(typeInt16)
return enc.Write(int16(i))
case i < int16(minFixedInt):
_ = enc.Write(typeInt8)
return enc.Write(int8(i))
case i <= int16(maxFixedInt):
return enc.Write(byte(i)) // bypass the range check in EncodeFixedInt
case i <= math.MaxUint8:
_ = enc.Write(typeUint8)
return enc.Write(uint8(i))
default:
_ = enc.Write(typeInt16)
return enc.Write(i)
}
}
// EncodeInt32 encodes a signed 32-bit integer to the current writer.
//
// The encoder will use the most efficient format for the value
// being encoded, which may not be int32.
func (enc Encoder) EncodeInt32(i int32) error {
switch {
case i < int32(math.MinInt16):
_ = enc.Write(typeInt32)
return enc.Write(int32(i))
case i < int32(math.MinInt8):
_ = enc.Write(typeInt16)
return enc.Write(int16(i))
case i < int32(minFixedInt):
_ = enc.Write(typeInt8)
return enc.Write(int8(i))
case i <= int32(maxFixedInt):
return enc.Write(byte(i)) // bypass the range check in EncodeFixedInt
case i <= math.MaxUint8:
_ = enc.Write(typeUint8)
return enc.Write(uint8(i))
case i <= math.MaxUint16:
_ = enc.Write(typeUint16)
return enc.Write(uint16(i))
default:
_ = enc.Write(typeInt32)
return enc.Write(i)
}
}
// EncodeInt64 encodes a signed 64-bit integer to the current writer.
//
// The encoder will use the most efficient format for the value
// being encoded, which may not be int64.
func (enc Encoder) EncodeInt64(i int64) error {
switch {
case i < math.MinInt32:
_ = enc.Write(typeInt64)
return enc.Write(i)
case i < math.MinInt16:
_ = enc.Write(typeInt32)
return enc.Write(int32(i))
case i < math.MinInt8:
_ = enc.Write(typeInt16)
return enc.Write(int16(i))
case i < int64(minFixedInt):
_ = enc.Write(typeInt8)
return enc.Write(int8(i))
case i <= int64(maxFixedInt):
return enc.Write(byte(i)) // bypass the range check in EncodeFixedInt
case i <= math.MaxUint8:
_ = enc.Write(typeUint8)
return enc.Write(uint8(i))
case i <= math.MaxUint16:
_ = enc.Write(typeUint16)
return enc.Write(uint16(i))
case i <= math.MaxUint32:
_ = enc.Write(typeUint32)
return enc.Write(uint32(i))
default:
// sonarcloud: avoid duplicating the code for the case for < MinInt32
// (int64 and uint64 encoding is identical in any case)
_ = enc.Write(typeUint64)
return enc.Write(i)
}
}
// WriteUint8 encodes an unsigned 8-bit integer to the current writer.
//
// The encoder will use the most efficient format for the value
// being encoded: fixed int or uint8.
func (enc Encoder) EncodeUint8(i uint8) error {
switch {
case i <= maxFixedUint:
return enc.Write(byte(i)) // bypass the range check in EncodeFixedInt
default:
_ = enc.Write(typeUint8)
return enc.Write(i)
}
}
// EncodeUint16 encodes an unsigned 16-bit integer to the current writer.
//
// The encoder will use the most efficient format for the value
// being encoded: fixed int, uint8 or uint16.
func (enc Encoder) EncodeUint16(i uint16) error {
switch {
case i <= uint16(maxFixedUint):
return enc.Write(byte(i)) // bypass the range check in EncodeFixedInt``
case i <= math.MaxUint8:
_ = enc.Write(typeUint8)
return enc.Write(uint8(i))
default:
_ = enc.Write(typeUint16)
return enc.Write(i)
}
}
// EncodeUint32 encodes an unsigned 32-bit integer to the current writer.
//
// The encoder will use the most efficient format for the value
// being encoded: fixed int, uint8, uint16 or uint32.
func (enc Encoder) EncodeUint32(i uint32) error {
switch {
case i <= uint32(maxFixedUint):
return enc.Write(byte(i)) // bypass the range check in EncodeFixedInt
case i <= math.MaxUint8:
_ = enc.Write(typeUint8)
return enc.Write(uint8(i))
case i <= math.MaxUint16:
_ = enc.Write(typeUint16)
return enc.Write(uint16(i))
default:
_ = enc.Write(typeUint32)
return enc.Write(i)
}
}
// EncodeUint64 encodes an unsigned 64-bit integer to the current writer.
//
// The encoder will use the most efficient format for the value
// being encoded: fixed int, uint8, uint16, uint32 or uint64.
func (enc Encoder) EncodeUint64(i uint64) error {
switch {
case i <= uint64(maxFixedUint):
return enc.Write(byte(i)) // bypass the range check in EncodeFixedInt
case i <= math.MaxUint8:
_ = enc.Write(typeUint8)
return enc.Write(uint8(i))
case i <= math.MaxUint16:
_ = enc.Write(typeUint16)
return enc.Write(uint16(i))
case i <= math.MaxUint32:
_ = enc.Write(typeUint32)
return enc.Write(uint32(i))
default:
_ = enc.Write(typeUint64)
return enc.Write(i)
}
}
// EncodeInt encodes a signed integer to the current writer.
//
// The encoder packs using the smallest possible integer
// type for the value involved.
//
// To write values that exceed the MaxInt/MinInt range on a 32-bit
// platform you must explicitly use WriteInt64/WriteUint64.
func (enc Encoder) EncodeInt(i int) error {
switch {
case i < math.MinInt32:
_ = enc.Write(typeInt64)
return enc.Write(int64(i))
case i < math.MinInt16:
_ = enc.Write(typeInt32)
return enc.Write(int32(i))
case i < math.MinInt8:
_ = enc.Write(typeInt16)
return enc.Write(int16(i))
case i < int(minFixedInt):
_ = enc.Write(typeInt8)
return enc.Write(int8(i))
case i <= int(maxFixedInt):
return enc.Write(byte(i)) // bypass the range check in EncodeFixedInt
case i <= math.MaxUint8:
_ = enc.Write(typeUint8)
return enc.Write(uint8(i))
case i <= math.MaxUint16:
_ = enc.Write(typeUint16)
return enc.Write(uint16(i))
case i <= math.MaxUint32:
_ = enc.Write(typeUint32)
return enc.Write(uint32(i))
default:
// sonarcloud: avoid duplicating the code for the case for < MinInt32
// (int64 and uint64 encoding is identical in any case)
_ = enc.Write(typeUint64)
return enc.Write(int64(i))
}
}
// EncodeUint encodes an unsigned integer to the current writer.
//
// The encoder packs using the smallest possible integer
// type for the value involved.
func (enc Encoder) EncodeUint(i uint) error {
switch {
case i <= uint(maxFixedUint):
return enc.Write(byte(i)) // bypass the range check in EncodeFixedInt
case i <= math.MaxUint8:
_ = enc.Write(typeUint8)
return enc.Write(uint8(i))
case i <= math.MaxUint16:
_ = enc.Write(typeUint16)
return enc.Write(uint16(i))
case i <= math.MaxUint32:
_ = enc.Write(typeUint32)
return enc.Write(uint32(i))
default:
_ = enc.Write(typeUint64)
return enc.Write(uint64(i))
}
}