/
number.go
300 lines (272 loc) · 8.55 KB
/
number.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
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package codec
import (
"encoding/binary"
"fmt"
"math"
"github.com/juju/errors"
)
// EncodeInt encodes the int64 value with variable length format.
// The encoded bytes format is: length flag(1 byte) + encoded data.
// The length flag is calculated with following way
// flag Value Range
// 8 -> [MinInt64, MinInt32)
// 12 -> [MinInt32, MinInt16)
// 14 -> [MinInt16, MinInt8)
// 15 -> [MinInt8, 0)
// 16 -> 0
// 17 -> (0, MaxInt8]
// 18 -> (MaxInt8, MaxInt16]
// 20 -> (MaxInt16, MaxInt32]
// 24 -> (MaxInt32, MaxInt64]
//
// EncodeInt appends the encoded value to slice b and returns the appended slice.
// EncodeInt guarantees that the encoded value is in ascending order for comparison.
func EncodeInt(b []byte, v int64) []byte {
if v < 0 {
switch {
case v >= math.MinInt8:
return append(b, 15, byte(v))
case v >= math.MinInt16:
return append(b, 14, byte(v>>8), byte(v))
case v >= math.MinInt32:
return append(b, 12, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
default:
return append(b, 8, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32),
byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}
}
return EncodeUint(b, uint64(v))
}
// EncodeIntDesc encodes the int64 value with variable length format.
// The encoded bytes format is: length flag(1 byte) + encoded data.
// The length flag is calculated with following way
// flag Value Range
// 24 -> [MinInt64, MinInt32)
// 20 -> [MinInt32, MinInt16)
// 18 -> [MinInt16, MinInt8)
// 17 -> [MinInt8, 0)
// 16 -> 0
// 15 -> (0, MaxInt8]
// 14 -> (MaxInt8, MaxInt16]
// 12 -> (MaxInt16, MaxInt32]
// 8 -> (MaxInt32, MaxInt64]
//
// EncodeIntDesc appends the encoded value to slice b and returns the appended slice.
// EncodeIntDesc guarantees that the encoded value is in descending order for comparison.
func EncodeIntDesc(b []byte, v int64) []byte {
if v < 0 {
switch {
case v >= math.MinInt8:
v = ^v
return append(b, 17, byte(v))
case v >= math.MinInt16:
v = ^v
return append(b, 18, byte(v>>8), byte(v))
case v >= math.MinInt32:
v = ^v
return append(b, 20, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
default:
v = ^v
return append(b, 24, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32),
byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}
}
return EncodeUintDesc(b, uint64(v))
}
// DecodeInt decodes value encoded by EncodeInt before.
// It returns the leftover un-decoded slice, decoded value if no error.
func DecodeInt(b []byte) ([]byte, int64, error) {
if len(b) == 0 {
return nil, 0, errors.Errorf("empty bytes to decode value")
}
flag := b[0]
length := int(flag) - 16
if length < 0 {
length = -length
leftover := b[1:]
if len(leftover) < length {
return nil, 0, errors.Errorf("insufficient bytes to decode value, need %d, but only %d", length, len(leftover))
}
var v int64
switch length {
case 1:
v = int64(int8(leftover[0]))
case 2:
v = int64(int16(binary.BigEndian.Uint16(leftover[:length])))
case 4:
v = int64(int32(binary.BigEndian.Uint32(leftover[:length])))
case 8:
v = int64(binary.BigEndian.Uint64(leftover[:length]))
default:
return nil, 0, errors.Errorf("invalid encoded length flag %d", flag)
}
return leftover[length:], v, nil
}
leftover, v, err := DecodeUint(b)
if v > math.MaxInt64 {
return nil, 0, fmt.Errorf("decoded value %d overflow int64", v)
}
return leftover, int64(v), err
}
// DecodeIntDesc decodes value encoded by EncodeInt before.
// It returns the leftover un-decoded slice, decoded value if no error.
func DecodeIntDesc(b []byte) ([]byte, int64, error) {
if len(b) == 0 {
return nil, 0, errors.Errorf("empty bytes to decode value")
}
flag := b[0]
length := int(flag) - 16
if length > 0 {
leftover := b[1:]
if len(leftover) < length {
return nil, 0, errors.Errorf("insufficient bytes to decode value, need %d, but only %d", length, len(leftover))
}
var v int64
switch length {
case 1:
v = int64(int8(leftover[0]))
case 2:
v = int64(int16(binary.BigEndian.Uint16(leftover[:length])))
case 4:
v = int64(int32(binary.BigEndian.Uint32(leftover[:length])))
case 8:
v = int64(binary.BigEndian.Uint64(leftover[:length]))
default:
return nil, 0, errors.Errorf("invalid encoded length flag %d", flag)
}
return leftover[length:], ^v, nil
}
leftover, v, err := DecodeUintDesc(b)
if v > math.MaxInt64 {
return nil, 0, fmt.Errorf("decoded value %d overflow int64", v)
}
return leftover, int64(v), err
}
// EncodeUint encodes the uint64 value with variable length format.
// The encoded bytes format is: length flag(1 byte) + encoded data.
// The length flag is calculated with following way:
// flag Value Range
// 16 -> 0
// 17 -> (0, MaxUint8]
// 18 -> (MaxUint8, MaxUint16]
// 20 -> (MaxUint16, MaxUint32]
// 24 -> (MaxUint32, MaxUint64]
//
// EncodeUint appends the encoded value to slice b and returns the appended slice.
// EncodeUint guarantees that the encoded value is in ascending order for comparison.
func EncodeUint(b []byte, v uint64) []byte {
switch {
case v == 0:
return append(b, 16)
case v <= math.MaxUint8:
return append(b, 17, byte(v))
case v <= math.MaxUint16:
return append(b, 18, byte(v>>8), byte(v))
case v <= math.MaxUint32:
return append(b, 20, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
default:
return append(b, 24, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32),
byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}
}
// EncodeUintDesc encodes the int64 value with variable length format.
// The encoded bytes format is: length flag(1 byte) + encoded data.
// The length flag is calculated with following way
// flag Value Range
// 16 -> 0
// 15 -> (0, MaxUint8]
// 14 -> (MaxUint8, MaxUint16]
// 12 -> (MaxUint16, MaxUint32]
// 8 -> (MaxUint32, MaxUint64]
//
// EncodeUintDesc appends the encoded value to slice b and returns the appended slice.
// EncodeUintDesc guarantees that the encoded value is in descending order for comparison.
func EncodeUintDesc(b []byte, v uint64) []byte {
switch {
case v == 0:
return append(b, 16)
case v <= math.MaxInt8:
v = ^v
return append(b, 15, byte(v))
case v <= math.MaxUint16:
v = ^v
return append(b, 14, byte(v>>8), byte(v))
case v <= math.MaxUint32:
v = ^v
return append(b, 12, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
default:
v = ^v
return append(b, 8, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32),
byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}
}
// DecodeUint decodes value encoded by EncodeUint before.
// It returns the leftover un-decoded slice, decoded value if no error.
func DecodeUint(b []byte) ([]byte, uint64, error) {
if len(b) == 0 {
return nil, 0, errors.Errorf("empty bytes to decode value")
}
flag := b[0]
length := int(flag) - 16
leftover := b[1:]
if len(leftover) < length {
return nil, 0, errors.Errorf("insufficient bytes to decode value, need %d, but only %d", length, len(leftover))
}
var v uint64
switch length {
case 0:
v = 0
case 1:
v = uint64(leftover[0])
case 2:
v = uint64(binary.BigEndian.Uint16(leftover[:length]))
case 4:
v = uint64(binary.BigEndian.Uint32(leftover[:length]))
case 8:
v = uint64(binary.BigEndian.Uint64(leftover[:length]))
default:
return nil, 0, errors.Errorf("invalid encoded length flag %d", flag)
}
return leftover[length:], v, nil
}
// DecodeUintDesc decodes value encoded by EncodeInt before.
// It returns the leftover un-decoded slice, decoded value if no error.
func DecodeUintDesc(b []byte) ([]byte, uint64, error) {
if len(b) == 0 {
return nil, 0, errors.Errorf("empty bytes to decode value")
}
flag := b[0]
length := 16 - int(flag)
leftover := b[1:]
if len(leftover) < length {
return nil, 0, errors.Errorf("insufficient bytes to decode value, need %d, but only %d", length, len(leftover))
}
var v uint64
switch length {
case 0:
v = 0
case 1:
v = uint64(^leftover[0])
case 2:
v = uint64(^binary.BigEndian.Uint16(leftover[:length]))
case 4:
v = uint64(^binary.BigEndian.Uint32(leftover[:length]))
case 8:
v = uint64(^binary.BigEndian.Uint64(leftover[:length]))
default:
return nil, 0, errors.Errorf("invalid encoded length flag %d", flag)
}
return leftover[length:], v, nil
}