-
Notifications
You must be signed in to change notification settings - Fork 3
/
primitives.go
133 lines (125 loc) · 2.94 KB
/
primitives.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
package ikea
import (
"encoding/binary"
"math"
"reflect"
)
var _ fixedReadWriter = (*primitiveReadWriter)(nil)
type primitiveReadWriter struct {
fixed
size int
reader func([]byte, reflect.Value)
writer func([]byte, reflect.Value)
}
func (p *primitiveReadWriter) length() int {
return p.size
}
func (p *primitiveReadWriter) readFixed(data []byte, v reflect.Value) {
p.reader(data, v)
}
func (p *primitiveReadWriter) writeFixed(data []byte, v reflect.Value) {
p.writer(data, v)
}
var primitiveIndex = map[reflect.Kind]*primitiveReadWriter{
reflect.Bool: {
size: 1,
reader: func(b []byte, v reflect.Value) {
v.SetBool(b[0] != 0)
},
writer: func(b []byte, v reflect.Value) {
if v.Bool() {
b[0] = 1
}
},
},
reflect.Int8: {
size: 1,
reader: func(b []byte, v reflect.Value) {
v.SetInt(int64(b[0]))
},
writer: func(b []byte, v reflect.Value) {
b[0] = byte(v.Int())
},
},
reflect.Int16: {
size: 2,
reader: func(b []byte, v reflect.Value) {
v.SetInt(int64(binary.BigEndian.Uint16(b)))
},
writer: func(b []byte, v reflect.Value) {
binary.BigEndian.PutUint16(b, uint16(v.Int()))
},
},
reflect.Int32: {
size: 4,
reader: func(b []byte, v reflect.Value) {
v.SetInt(int64(binary.BigEndian.Uint32(b)))
},
writer: func(b []byte, v reflect.Value) {
binary.BigEndian.PutUint32(b, uint32(v.Int()))
},
},
reflect.Int64: {
size: 8,
reader: func(b []byte, v reflect.Value) {
v.SetInt(int64(binary.BigEndian.Uint64(b)))
},
writer: func(b []byte, v reflect.Value) {
binary.BigEndian.PutUint64(b, uint64(v.Int()))
},
},
reflect.Uint8: {
size: 1,
reader: func(b []byte, v reflect.Value) {
v.SetUint(uint64(b[0]))
},
writer: func(b []byte, v reflect.Value) {
b[0] = byte(v.Uint())
},
},
reflect.Uint16: {
size: 2,
reader: func(b []byte, v reflect.Value) {
v.SetUint(uint64(binary.BigEndian.Uint16(b)))
},
writer: func(b []byte, v reflect.Value) {
binary.BigEndian.PutUint16(b, uint16(v.Uint()))
},
},
reflect.Uint32: {
size: 4,
reader: func(b []byte, v reflect.Value) {
v.SetUint(uint64(binary.BigEndian.Uint32(b)))
},
writer: func(b []byte, v reflect.Value) {
binary.BigEndian.PutUint32(b, uint32(v.Uint()))
},
},
reflect.Uint64: {
size: 8,
reader: func(b []byte, v reflect.Value) {
v.SetUint(binary.BigEndian.Uint64(b))
},
writer: func(b []byte, v reflect.Value) {
binary.BigEndian.PutUint64(b, v.Uint())
},
},
reflect.Float32: {
size: 4,
reader: func(b []byte, v reflect.Value) {
v.SetFloat(float64(math.Float32frombits(binary.BigEndian.Uint32(b))))
},
writer: func(b []byte, v reflect.Value) {
binary.BigEndian.PutUint32(b, math.Float32bits(float32(v.Float())))
},
},
reflect.Float64: {
size: 8,
reader: func(b []byte, v reflect.Value) {
v.SetFloat(math.Float64frombits(binary.BigEndian.Uint64(b)))
},
writer: func(b []byte, v reflect.Value) {
binary.BigEndian.PutUint64(b, math.Float64bits(v.Float()))
},
},
}