]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/vmihailenco/msgpack/encode_number.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / vmihailenco / msgpack / encode_number.go
1 package msgpack
2
3 import (
4 "math"
5 "reflect"
6
7 "github.com/vmihailenco/msgpack/codes"
8 )
9
10 // EncodeUint8 encodes an uint8 in 2 bytes preserving type of the number.
11 func (e *Encoder) EncodeUint8(n uint8) error {
12 return e.write1(codes.Uint8, n)
13 }
14
15 func (e *Encoder) encodeUint8Cond(n uint8) error {
16 if e.useCompact {
17 return e.EncodeUint(uint64(n))
18 }
19 return e.EncodeUint8(n)
20 }
21
22 // EncodeUint16 encodes an uint16 in 3 bytes preserving type of the number.
23 func (e *Encoder) EncodeUint16(n uint16) error {
24 return e.write2(codes.Uint16, n)
25 }
26
27 func (e *Encoder) encodeUint16Cond(n uint16) error {
28 if e.useCompact {
29 return e.EncodeUint(uint64(n))
30 }
31 return e.EncodeUint16(n)
32 }
33
34 // EncodeUint32 encodes an uint16 in 5 bytes preserving type of the number.
35 func (e *Encoder) EncodeUint32(n uint32) error {
36 return e.write4(codes.Uint32, n)
37 }
38
39 func (e *Encoder) encodeUint32Cond(n uint32) error {
40 if e.useCompact {
41 return e.EncodeUint(uint64(n))
42 }
43 return e.EncodeUint32(n)
44 }
45
46 // EncodeUint64 encodes an uint16 in 9 bytes preserving type of the number.
47 func (e *Encoder) EncodeUint64(n uint64) error {
48 return e.write8(codes.Uint64, n)
49 }
50
51 func (e *Encoder) encodeUint64Cond(n uint64) error {
52 if e.useCompact {
53 return e.EncodeUint(n)
54 }
55 return e.EncodeUint64(n)
56 }
57
58 // EncodeInt8 encodes an int8 in 2 bytes preserving type of the number.
59 func (e *Encoder) EncodeInt8(n int8) error {
60 return e.write1(codes.Int8, uint8(n))
61 }
62
63 func (e *Encoder) encodeInt8Cond(n int8) error {
64 if e.useCompact {
65 return e.EncodeInt(int64(n))
66 }
67 return e.EncodeInt8(n)
68 }
69
70 // EncodeInt16 encodes an int16 in 3 bytes preserving type of the number.
71 func (e *Encoder) EncodeInt16(n int16) error {
72 return e.write2(codes.Int16, uint16(n))
73 }
74
75 func (e *Encoder) encodeInt16Cond(n int16) error {
76 if e.useCompact {
77 return e.EncodeInt(int64(n))
78 }
79 return e.EncodeInt16(n)
80 }
81
82 // EncodeInt32 encodes an int32 in 5 bytes preserving type of the number.
83 func (e *Encoder) EncodeInt32(n int32) error {
84 return e.write4(codes.Int32, uint32(n))
85 }
86
87 func (e *Encoder) encodeInt32Cond(n int32) error {
88 if e.useCompact {
89 return e.EncodeInt(int64(n))
90 }
91 return e.EncodeInt32(n)
92 }
93
94 // EncodeInt64 encodes an int64 in 9 bytes preserving type of the number.
95 func (e *Encoder) EncodeInt64(n int64) error {
96 return e.write8(codes.Int64, uint64(n))
97 }
98
99 func (e *Encoder) encodeInt64Cond(n int64) error {
100 if e.useCompact {
101 return e.EncodeInt(n)
102 }
103 return e.EncodeInt64(n)
104 }
105
106 // EncodeUnsignedNumber encodes an uint64 in 1, 2, 3, 5, or 9 bytes.
107 // Type of the number is lost during encoding.
108 func (e *Encoder) EncodeUint(n uint64) error {
109 if n <= math.MaxInt8 {
110 return e.w.WriteByte(byte(n))
111 }
112 if n <= math.MaxUint8 {
113 return e.EncodeUint8(uint8(n))
114 }
115 if n <= math.MaxUint16 {
116 return e.EncodeUint16(uint16(n))
117 }
118 if n <= math.MaxUint32 {
119 return e.EncodeUint32(uint32(n))
120 }
121 return e.EncodeUint64(uint64(n))
122 }
123
124 // EncodeNumber encodes an int64 in 1, 2, 3, 5, or 9 bytes.
125 // Type of number is lost during encoding.
126 func (e *Encoder) EncodeInt(n int64) error {
127 if n >= 0 {
128 return e.EncodeUint(uint64(n))
129 }
130 if n >= int64(int8(codes.NegFixedNumLow)) {
131 return e.w.WriteByte(byte(n))
132 }
133 if n >= math.MinInt8 {
134 return e.EncodeInt8(int8(n))
135 }
136 if n >= math.MinInt16 {
137 return e.EncodeInt16(int16(n))
138 }
139 if n >= math.MinInt32 {
140 return e.EncodeInt32(int32(n))
141 }
142 return e.EncodeInt64(int64(n))
143 }
144
145 func (e *Encoder) EncodeFloat32(n float32) error {
146 return e.write4(codes.Float, math.Float32bits(n))
147 }
148
149 func (e *Encoder) EncodeFloat64(n float64) error {
150 return e.write8(codes.Double, math.Float64bits(n))
151 }
152
153 func (e *Encoder) write1(code codes.Code, n uint8) error {
154 e.buf = e.buf[:2]
155 e.buf[0] = byte(code)
156 e.buf[1] = byte(n)
157 return e.write(e.buf)
158 }
159
160 func (e *Encoder) write2(code codes.Code, n uint16) error {
161 e.buf = e.buf[:3]
162 e.buf[0] = byte(code)
163 e.buf[1] = byte(n >> 8)
164 e.buf[2] = byte(n)
165 return e.write(e.buf)
166 }
167
168 func (e *Encoder) write4(code codes.Code, n uint32) error {
169 e.buf = e.buf[:5]
170 e.buf[0] = byte(code)
171 e.buf[1] = byte(n >> 24)
172 e.buf[2] = byte(n >> 16)
173 e.buf[3] = byte(n >> 8)
174 e.buf[4] = byte(n)
175 return e.write(e.buf)
176 }
177
178 func (e *Encoder) write8(code codes.Code, n uint64) error {
179 e.buf = e.buf[:9]
180 e.buf[0] = byte(code)
181 e.buf[1] = byte(n >> 56)
182 e.buf[2] = byte(n >> 48)
183 e.buf[3] = byte(n >> 40)
184 e.buf[4] = byte(n >> 32)
185 e.buf[5] = byte(n >> 24)
186 e.buf[6] = byte(n >> 16)
187 e.buf[7] = byte(n >> 8)
188 e.buf[8] = byte(n)
189 return e.write(e.buf)
190 }
191
192 func encodeUint8CondValue(e *Encoder, v reflect.Value) error {
193 return e.encodeUint8Cond(uint8(v.Uint()))
194 }
195
196 func encodeUint16CondValue(e *Encoder, v reflect.Value) error {
197 return e.encodeUint16Cond(uint16(v.Uint()))
198 }
199
200 func encodeUint32CondValue(e *Encoder, v reflect.Value) error {
201 return e.encodeUint32Cond(uint32(v.Uint()))
202 }
203
204 func encodeUint64CondValue(e *Encoder, v reflect.Value) error {
205 return e.encodeUint64Cond(v.Uint())
206 }
207
208 func encodeInt8CondValue(e *Encoder, v reflect.Value) error {
209 return e.encodeInt8Cond(int8(v.Int()))
210 }
211
212 func encodeInt16CondValue(e *Encoder, v reflect.Value) error {
213 return e.encodeInt16Cond(int16(v.Int()))
214 }
215
216 func encodeInt32CondValue(e *Encoder, v reflect.Value) error {
217 return e.encodeInt32Cond(int32(v.Int()))
218 }
219
220 func encodeInt64CondValue(e *Encoder, v reflect.Value) error {
221 return e.encodeInt64Cond(v.Int())
222 }
223
224 func encodeFloat32Value(e *Encoder, v reflect.Value) error {
225 return e.EncodeFloat32(float32(v.Float()))
226 }
227
228 func encodeFloat64Value(e *Encoder, v reflect.Value) error {
229 return e.EncodeFloat64(v.Float())
230 }