File: number.go

package info (click to toggle)
golang-github-tinylib-msgp 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 1,344 kB
  • sloc: makefile: 48
file content (390 lines) | stat: -rw-r--r-- 8,949 bytes parent folder | download
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package msgp

import (
	"math"
	"math/bits"
	"strconv"
)

// The portable parts of the Number implementation

// Number can be
// an int64, uint64, float32,
// or float64 internally.
// It can decode itself
// from any of the native
// messagepack number types.
// The zero-value of Number
// is Int(0). Using the equality
// operator with Number compares
// both the type and the value
// of the number.
type Number struct {
	// internally, this
	// is just a tagged union.
	// the raw bits of the number
	// are stored the same way regardless.
	bits uint64
	typ  Type
}

// AsInt sets the number to an int64.
func (n *Number) AsInt(i int64) {
	// we always store int(0)
	// as {0, InvalidType} in
	// order to preserve
	// the behavior of the == operator
	if i == 0 {
		n.typ = InvalidType
		n.bits = 0
		return
	}

	n.typ = IntType
	n.bits = uint64(i)
}

// AsUint sets the number to a uint64.
func (n *Number) AsUint(u uint64) {
	n.typ = UintType
	n.bits = u
}

// AsFloat32 sets the value of the number
// to a float32.
func (n *Number) AsFloat32(f float32) {
	n.typ = Float32Type
	n.bits = uint64(math.Float32bits(f))
}

// AsFloat64 sets the value of the
// number to a float64.
func (n *Number) AsFloat64(f float64) {
	n.typ = Float64Type
	n.bits = math.Float64bits(f)
}

// Int casts the number as an int64, and
// returns whether or not that was the
// underlying type.
func (n *Number) Int() (int64, bool) {
	return int64(n.bits), n.typ == IntType || n.typ == InvalidType
}

// Uint casts the number as a uint64, and returns
// whether or not that was the underlying type.
func (n *Number) Uint() (uint64, bool) {
	return n.bits, n.typ == UintType
}

// Float casts the number to a float64, and
// returns whether that was the underlying
// type (either a float64 or a float32).
func (n *Number) Float() (float64, bool) {
	switch n.typ {
	case Float32Type:
		return float64(math.Float32frombits(uint32(n.bits))), true
	case Float64Type:
		return math.Float64frombits(n.bits), true
	default:
		return 0.0, false
	}
}

// Type will return one of:
// Float64Type, Float32Type, UintType, or IntType.
func (n *Number) Type() Type {
	if n.typ == InvalidType {
		return IntType
	}
	return n.typ
}

// DecodeMsg implements msgp.Decodable
func (n *Number) DecodeMsg(r *Reader) error {
	typ, err := r.NextType()
	if err != nil {
		return err
	}
	switch typ {
	case Float32Type:
		f, err := r.ReadFloat32()
		if err != nil {
			return err
		}
		n.AsFloat32(f)
		return nil
	case Float64Type:
		f, err := r.ReadFloat64()
		if err != nil {
			return err
		}
		n.AsFloat64(f)
		return nil
	case IntType:
		i, err := r.ReadInt64()
		if err != nil {
			return err
		}
		n.AsInt(i)
		return nil
	case UintType:
		u, err := r.ReadUint64()
		if err != nil {
			return err
		}
		n.AsUint(u)
		return nil
	default:
		return TypeError{Encoded: typ, Method: IntType}
	}
}

// UnmarshalMsg implements msgp.Unmarshaler
func (n *Number) UnmarshalMsg(b []byte) ([]byte, error) {
	typ := NextType(b)
	switch typ {
	case IntType:
		i, o, err := ReadInt64Bytes(b)
		if err != nil {
			return b, err
		}
		n.AsInt(i)
		return o, nil
	case UintType:
		u, o, err := ReadUint64Bytes(b)
		if err != nil {
			return b, err
		}
		n.AsUint(u)
		return o, nil
	case Float64Type:
		f, o, err := ReadFloat64Bytes(b)
		if err != nil {
			return b, err
		}
		n.AsFloat64(f)
		return o, nil
	case Float32Type:
		f, o, err := ReadFloat32Bytes(b)
		if err != nil {
			return b, err
		}
		n.AsFloat32(f)
		return o, nil
	default:
		return b, TypeError{Method: IntType, Encoded: typ}
	}
}

// MarshalMsg implements msgp.Marshaler
func (n *Number) MarshalMsg(b []byte) ([]byte, error) {
	switch n.typ {
	case IntType:
		return AppendInt64(b, int64(n.bits)), nil
	case UintType:
		return AppendUint64(b, uint64(n.bits)), nil
	case Float64Type:
		return AppendFloat64(b, math.Float64frombits(n.bits)), nil
	case Float32Type:
		return AppendFloat32(b, math.Float32frombits(uint32(n.bits))), nil
	default:
		return AppendInt64(b, 0), nil
	}
}

// EncodeMsg implements msgp.Encodable
func (n *Number) EncodeMsg(w *Writer) error {
	switch n.typ {
	case IntType:
		return w.WriteInt64(int64(n.bits))
	case UintType:
		return w.WriteUint64(n.bits)
	case Float64Type:
		return w.WriteFloat64(math.Float64frombits(n.bits))
	case Float32Type:
		return w.WriteFloat32(math.Float32frombits(uint32(n.bits)))
	default:
		return w.WriteInt64(0)
	}
}

// CoerceInt attempts to coerce the value of
// the number into a signed integer and returns
// whether it was successful.
// "Success" implies that no precision in the value of
// the number was lost, which means that the number was an integer or
// a floating point that mapped exactly to an integer without rounding.
func (n *Number) CoerceInt() (int64, bool) {
	switch n.typ {
	case InvalidType, IntType:
		// InvalidType just means un-initialized.
		return int64(n.bits), true
	case UintType:
		return int64(n.bits), n.bits <= math.MaxInt64
	case Float32Type:
		f := math.Float32frombits(uint32(n.bits))
		if n.isExactInt() && f <= math.MaxInt64 && f >= math.MinInt64 {
			return int64(f), true
		}
		if n.bits == 0 || n.bits == 1<<31 {
			return 0, true
		}
	case Float64Type:
		f := math.Float64frombits(n.bits)
		if n.isExactInt() && f <= math.MaxInt64 && f >= math.MinInt64 {
			return int64(f), true
		}
		return 0, n.bits == 0 || n.bits == 1<<63
	}
	return 0, false
}

// CoerceUInt attempts to coerce the value of
// the number into an unsigned integer and returns
// whether it was successful.
// "Success" implies that no precision in the value of
// the number was lost, which means that the number was an integer or
// a floating point that mapped exactly to an integer without rounding.
func (n *Number) CoerceUInt() (uint64, bool) {
	switch n.typ {
	case InvalidType, IntType:
		// InvalidType just means un-initialized.
		if int64(n.bits) >= 0 {
			return n.bits, true
		}
	case UintType:
		return n.bits, true
	case Float32Type:
		f := math.Float32frombits(uint32(n.bits))
		if f >= 0 && f <= math.MaxUint64 && n.isExactInt() {
			return uint64(f), true
		}
		if n.bits == 0 || n.bits == 1<<31 {
			return 0, true
		}
	case Float64Type:
		f := math.Float64frombits(n.bits)
		if f >= 0 && f <= math.MaxUint64 && n.isExactInt() {
			return uint64(f), true
		}
		return 0, n.bits == 0 || n.bits == 1<<63
	}
	return 0, false
}

// isExactInt will return true if the number represents an integer value.
// NaN, Inf returns false.
func (n *Number) isExactInt() bool {
	var eBits int // Exponent bits
	var mBits int // Mantissa bits

	switch n.typ {
	case InvalidType, IntType, UintType:
		return true
	case Float32Type:
		eBits = 8
		mBits = 23
	case Float64Type:
		eBits = 11
		mBits = 52
	default:
		return false
	}
	// Calculate float parts
	exp := int(n.bits>>mBits) & ((1 << eBits) - 1)
	mant := n.bits & ((1 << mBits) - 1)
	if exp == 0 && mant == 0 {
		// Handle zero value.
		return true
	}

	exp -= (1 << (eBits - 1)) - 1
	if exp < 0 || exp == 1<<(eBits-1) {
		// Negative exponent is never integer (except zero handled above)
		// Handles NaN (exp all 1s)
		return false
	}

	if exp >= mBits {
		// If we have more exponent than mantissa bits it is always an integer.
		return true
	}
	// Check if all bits below the exponent are zero.
	return bits.TrailingZeros64(mant) >= mBits-exp
}

// CoerceFloat returns the number as a float64.
// If the number is an integer, it will be
// converted to a float64 with the closest representation.
func (n *Number) CoerceFloat() float64 {
	switch n.typ {
	case IntType:
		return float64(int64(n.bits))
	case UintType:
		return float64(n.bits)
	case Float32Type:
		return float64(math.Float32frombits(uint32(n.bits)))
	case Float64Type:
		return math.Float64frombits(n.bits)
	default:
		return 0.0
	}
}

// Msgsize implements msgp.Sizer
func (n *Number) Msgsize() int {
	switch n.typ {
	case Float32Type:
		return Float32Size
	case Float64Type:
		return Float64Size
	case IntType:
		return Int64Size
	case UintType:
		return Uint64Size
	default:
		return 1 // fixint(0)
	}
}

// MarshalJSON implements json.Marshaler
func (n *Number) MarshalJSON() ([]byte, error) {
	t := n.Type()
	if t == InvalidType {
		return []byte{'0'}, nil
	}
	out := make([]byte, 0, 32)
	switch t {
	case Float32Type, Float64Type:
		f, _ := n.Float()
		return strconv.AppendFloat(out, f, 'f', -1, 64), nil
	case IntType:
		i, _ := n.Int()
		return strconv.AppendInt(out, i, 10), nil
	case UintType:
		u, _ := n.Uint()
		return strconv.AppendUint(out, u, 10), nil
	default:
		panic("(*Number).typ is invalid")
	}
}

// String implements fmt.Stringer
func (n *Number) String() string {
	switch n.typ {
	case InvalidType:
		return "0"
	case Float32Type, Float64Type:
		f, _ := n.Float()
		return strconv.FormatFloat(f, 'f', -1, 64)
	case IntType:
		i, _ := n.Int()
		return strconv.FormatInt(i, 10)
	case UintType:
		u, _ := n.Uint()
		return strconv.FormatUint(u, 10)
	default:
		panic("(*Number).typ is invalid")
	}
}