-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
encode.go
107 lines (89 loc) · 2.28 KB
/
encode.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
package bin
import (
"encoding/binary"
"math"
)
// PutID serializes type definition id, like a8509bda.
func (b *Buffer) PutID(id uint32) {
b.PutUint32(id)
}
// Put appends raw bytes to buffer.
//
// Buffer does not retain raw.
func (b *Buffer) Put(raw []byte) {
b.Buf = append(b.Buf, raw...)
}
// PutString serializes bare string.
func (b *Buffer) PutString(s string) {
b.Buf = encodeString(b.Buf, s)
}
// PutBytes serializes bare byte string.
func (b *Buffer) PutBytes(v []byte) {
b.Buf = encodeBytes(b.Buf, v)
}
// PutVectorHeader serializes vector header with provided length.
func (b *Buffer) PutVectorHeader(length int) {
b.PutID(TypeVector)
b.PutInt32(int32(length))
}
// PutInt serializes v as signed 32-bit integer.
//
// If v is bigger than 32-bit, `behavior` is undefined.
func (b *Buffer) PutInt(v int) {
b.PutInt32(int32(v))
}
// PutBool serializes bare boolean.
func (b *Buffer) PutBool(v bool) {
switch v {
case true:
b.PutID(TypeTrue)
case false:
b.PutID(TypeFalse)
}
}
// PutUint16 serializes unsigned 16-bit integer.
func (b *Buffer) PutUint16(v uint16) {
t := make([]byte, 2)
binary.LittleEndian.PutUint16(t, v)
b.Buf = append(b.Buf, t...)
}
// PutInt32 serializes signed 32-bit integer.
func (b *Buffer) PutInt32(v int32) {
b.PutUint32(uint32(v))
}
// PutUint32 serializes unsigned 32-bit integer.
func (b *Buffer) PutUint32(v uint32) {
t := make([]byte, Word)
binary.LittleEndian.PutUint32(t, v)
b.Buf = append(b.Buf, t...)
}
// PutInt53 serializes v as signed integer.
func (b *Buffer) PutInt53(v int64) {
b.PutLong(v)
}
// PutLong serializes v as signed integer.
func (b *Buffer) PutLong(v int64) {
b.PutUint64(uint64(v))
}
// PutUint64 serializes v as unsigned 64-bit integer.
func (b *Buffer) PutUint64(v uint64) {
t := make([]byte, Word*2)
binary.LittleEndian.PutUint64(t, v)
b.Buf = append(b.Buf, t...)
}
// PutDouble serializes v as 64-bit floating point.
func (b *Buffer) PutDouble(v float64) {
b.PutUint64(math.Float64bits(v))
}
// PutInt128 serializes v as 128-bit signed integer.
func (b *Buffer) PutInt128(v Int128) {
b.Buf = append(b.Buf, v[:]...)
}
// PutInt256 serializes v as 256-bit signed integer.
func (b *Buffer) PutInt256(v Int256) {
b.Buf = append(b.Buf, v[:]...)
}
// Reset buffer to zero length.
func (b *Buffer) Reset() {
b.Buf = b.Buf[:0]
}