-
Notifications
You must be signed in to change notification settings - Fork 22
/
decoder.go
93 lines (83 loc) · 2.17 KB
/
decoder.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
package gotiny
import (
"reflect"
"unsafe"
)
type Decoder struct {
buf []byte //buf
index int //下一个要使用的字节在buf中的下标
boolPos byte //下一次要读取的bool在buf中的下标,即buf[boolPos]
boolBit byte //下一次要读取的bool的buf[boolPos]中的bit位
engines []decEng //解码器集合
length int //解码器数量
}
func Unmarshal(buf []byte, is ...any) int {
return NewDecoderWithPtr(is...).decode(buf, is...)
}
func NewDecoderWithPtr(is ...any) *Decoder {
l := len(is)
engines := make([]decEng, l)
for i := 0; i < l; i++ {
rt := reflect.TypeOf(is[i])
if rt.Kind() != reflect.Ptr {
panic("the argument must be a pointer type!")
}
engines[i] = getDecEngine(rt.Elem())
}
return &Decoder{
length: l,
engines: engines,
}
}
func NewDecoder(is ...any) *Decoder {
l := len(is)
engines := make([]decEng, l)
for i := 0; i < l; i++ {
engines[i] = getDecEngine(reflect.TypeOf(is[i]))
}
return &Decoder{
length: l,
engines: engines,
}
}
func NewDecoderWithType(ts ...reflect.Type) *Decoder {
l := len(ts)
des := make([]decEng, l)
for i := 0; i < l; i++ {
des[i] = getDecEngine(ts[i])
}
return &Decoder{
length: l,
engines: des,
}
}
func (d *Decoder) reset() int {
index := d.index
d.index = 0
d.boolPos = 0
d.boolBit = 0
return index
}
// Decode takes a byte slice and a variable number of pointers to variables.
// It decodes the byte slice into the variables.
// the arguments must be a pointer type
// The return value is the number of bytes that were decoded.
func (d *Decoder) decode(buf []byte, is ...any) int {
d.buf = buf
engines := d.engines
for i := 0; i < len(engines) && i < len(is); i++ {
engines[i](d, reflect.ValueOf(is[i]).UnsafePointer())
}
return d.reset()
}
// DecodeValue takes a byte slice and a variable number of reflect.Values.
// It decodes the byte slice into the reflect.Values.
// The return value is the number of bytes that were decoded.
func (d *Decoder) decodeValue(buf []byte, vs ...reflect.Value) int {
d.buf = buf
engines := d.engines
for i := 0; i < len(engines) && i < len(vs); i++ {
engines[i](d, unsafe.Pointer(vs[i].UnsafeAddr()))
}
return d.reset()
}