-
Notifications
You must be signed in to change notification settings - Fork 2
/
simple.go
88 lines (72 loc) · 1.76 KB
/
simple.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
package cboring
import (
"fmt"
"io"
"math"
)
const (
simpleFalse byte = 20
simpleTrue byte = 21
)
// ReadBoolean reads a bool value from the Reader.
func ReadBoolean(r io.Reader) (b bool, err error) {
var buff [1]byte
if _, dataErr := r.Read(buff[:1]); dataErr != nil {
err = dataErr
return
}
major, adds := readMajorType(buff[0])
if major != SimpleData {
err = fmt.Errorf("ReadBoolean: Expected major 0x%x, got 0x%x", SimpleData, major)
return
}
switch adds {
case simpleFalse:
b = false
case simpleTrue:
b = true
default:
err = fmt.Errorf("ReadBoolean: Unknown additional 0x%x", adds)
}
return
}
// WriteBoolean writes a bool into the Writer.
func WriteBoolean(b bool, w io.Writer) (err error) {
var adds byte
if b {
adds = simpleTrue
} else {
adds = simpleFalse
}
payload := writeMajorType(SimpleData, adds)
_, err = w.Write([]byte{payload})
return
}
// ReadFloat32 reads a float32 value from the Reader.
func ReadFloat32(r io.Reader) (f float32, err error) {
if fbits, fbitsErr := ReadExpectMajors(SimpleData, r); fbitsErr != nil {
err = fbitsErr
} else {
f = math.Float32frombits(uint32(fbits))
}
return
}
// WriteFloat32 writes a float32 into the Writer.
func WriteFloat32(f float32, w io.Writer) (err error) {
fbits := math.Float32bits(f)
return WriteMajors(SimpleData, uint64(fbits), w)
}
// ReadFloat64 reads a float64 value from the Reader.
func ReadFloat64(r io.Reader) (f float64, err error) {
if fbits, fbitsErr := ReadExpectMajors(SimpleData, r); fbitsErr != nil {
err = fbitsErr
} else {
f = math.Float64frombits(fbits)
}
return
}
// WriteFloat64 writes a float64 into the Writer.
func WriteFloat64(f float64, w io.Writer) (err error) {
fbits := math.Float64bits(f)
return WriteMajors(SimpleData, fbits, w)
}