-
Notifications
You must be signed in to change notification settings - Fork 77
/
encoding.go
104 lines (83 loc) · 1.84 KB
/
encoding.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
package mandodb
import (
"encoding/binary"
"errors"
"unsafe"
)
// Encode Buffer
type encbuf struct {
B []byte
C [binary.MaxVarintLen64]byte
}
func newEncbuf() *encbuf {
return &encbuf{}
}
func (e *encbuf) Reset() { e.B = e.B[:0] }
func (e *encbuf) Bytes() []byte { return e.B }
func (e *encbuf) Len() int { return len(e.B) }
func (e *encbuf) MarshalUint8(b uint8) {
e.B = append(e.B, b)
}
func (e *encbuf) MarshalUint16(u ...uint16) {
for _, num := range u {
binary.LittleEndian.PutUint16(e.C[:], num)
e.B = append(e.B, e.C[:uint16Size]...)
}
}
func (e *encbuf) MarshalUint32(u ...uint32) {
for _, num := range u {
binary.LittleEndian.PutUint32(e.C[:], num)
e.B = append(e.B, e.C[:uint32Size]...)
}
}
func (e *encbuf) MarshalUint64(u ...uint64) {
for _, num := range u {
binary.LittleEndian.PutUint64(e.C[:], num)
e.B = append(e.B, e.C[:uint64Size]...)
}
}
func (e *encbuf) MarshalBytes(b []byte) {
e.B = append(e.B, b...)
}
func (e *encbuf) MarshalString(s string) {
e.B = append(e.B, s...)
}
var ErrInvalidSize = errors.New("invalid size")
// Decode Buffer
type decbuf struct {
err error
}
func newDecbuf() *decbuf {
return &decbuf{}
}
func (d *decbuf) UnmarshalUint16(b []byte) uint16 {
if len(b) < uint16Size {
d.err = ErrInvalidSize
return 0
}
return binary.LittleEndian.Uint16(b)
}
func (d *decbuf) UnmarshalUint32(b []byte) uint32 {
if len(b) < uint32Size {
d.err = ErrInvalidSize
return 0
}
return binary.LittleEndian.Uint32(b)
}
func (d *decbuf) UnmarshalUint64(b []byte) uint64 {
if len(b) < uint64Size {
d.err = ErrInvalidSize
return 0
}
return binary.LittleEndian.Uint64(b)
}
func (d *decbuf) UnmarshalString(b []byte) string {
return yoloString(b)
}
func (d *decbuf) Err() error {
return d.err
}
// 骚操作
func yoloString(b []byte) string {
return *((*string)(unsafe.Pointer(&b)))
}