This repository has been archived by the owner on Dec 10, 2020. It is now read-only.
/
encode_number.go
121 lines (103 loc) · 2.51 KB
/
encode_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
package msgpack
import (
"math"
"github.com/danjac/podbaby/api/Godeps/_workspace/src/gopkg.in/vmihailenco/msgpack.v2/codes"
)
func (e *Encoder) EncodeUint(v uint) error {
return e.EncodeUint64(uint64(v))
}
func (e *Encoder) EncodeUint8(v uint8) error {
return e.EncodeUint64(uint64(v))
}
func (e *Encoder) EncodeUint16(v uint16) error {
return e.EncodeUint64(uint64(v))
}
func (e *Encoder) EncodeUint32(v uint32) error {
return e.EncodeUint64(uint64(v))
}
func (e *Encoder) EncodeUint64(v uint64) error {
if v <= math.MaxInt8 {
return e.w.WriteByte(byte(v))
}
if v <= math.MaxUint8 {
return e.write1(codes.Uint8, v)
}
if v <= math.MaxUint16 {
return e.write2(codes.Uint16, v)
}
if v <= math.MaxUint32 {
return e.write4(codes.Uint32, v)
}
return e.write8(codes.Uint64, v)
}
func (e *Encoder) EncodeInt(v int) error {
return e.EncodeInt64(int64(v))
}
func (e *Encoder) EncodeInt8(v int8) error {
return e.EncodeInt64(int64(v))
}
func (e *Encoder) EncodeInt16(v int16) error {
return e.EncodeInt64(int64(v))
}
func (e *Encoder) EncodeInt32(v int32) error {
return e.EncodeInt64(int64(v))
}
func (e *Encoder) EncodeInt64(v int64) error {
if v >= 0 {
return e.EncodeUint64(uint64(v))
}
if v >= -32 {
return e.w.WriteByte(byte(v))
}
if v >= math.MinInt8 {
return e.write1(codes.Int8, uint64(v))
}
if v >= math.MinInt16 {
return e.write2(codes.Int16, uint64(v))
}
if v >= math.MinInt32 {
return e.write4(codes.Int32, uint64(v))
}
return e.write8(codes.Int64, uint64(v))
}
func (e *Encoder) EncodeFloat32(n float32) error {
return e.write4(codes.Float, uint64(math.Float32bits(n)))
}
func (e *Encoder) EncodeFloat64(n float64) error {
return e.write8(codes.Double, math.Float64bits(n))
}
func (e *Encoder) write1(code byte, n uint64) error {
e.buf = e.buf[:2]
e.buf[0] = code
e.buf[1] = byte(n)
return e.write(e.buf)
}
func (e *Encoder) write2(code byte, n uint64) error {
e.buf = e.buf[:3]
e.buf[0] = code
e.buf[1] = byte(n >> 8)
e.buf[2] = byte(n)
return e.write(e.buf)
}
func (e *Encoder) write4(code byte, n uint64) error {
e.buf = e.buf[:5]
e.buf[0] = code
e.buf[1] = byte(n >> 24)
e.buf[2] = byte(n >> 16)
e.buf[3] = byte(n >> 8)
e.buf[4] = byte(n)
return e.write(e.buf)
}
func (e *Encoder) write8(code byte, n uint64) error {
e.buf = e.buf[:9]
e.buf[0] = code
e.buf[1] = byte(n >> 56)
e.buf[2] = byte(n >> 48)
e.buf[3] = byte(n >> 40)
e.buf[4] = byte(n >> 32)
e.buf[5] = byte(n >> 24)
e.buf[6] = byte(n >> 16)
e.buf[7] = byte(n >> 8)
e.buf[8] = byte(n)
return e.write(e.buf)
}