-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprimitives.go
161 lines (142 loc) · 3.48 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package vpack
import (
"encoding/binary"
"math"
)
var BigEndian = binary.BigEndian
// FUInt64 implements fixed size serialization of uint64. It writes data in big
// endian, making it suitable for int keys to bolt.
func FUInt64(n *uint64, buf *Buffer) {
if buf.Writing {
buf.Data = BigEndian.AppendUint64(buf.Data, *n)
} else {
slice := buf.ReadBytes(8)
*n = BigEndian.Uint64(slice)
}
}
// FUInt32 implements fixed size serialization of uint32. It writes data in big
// endian, making it suitable for int keys to bolt.
func FUInt32(n *uint32, buf *Buffer) {
if buf.Writing {
buf.Data = BigEndian.AppendUint32(buf.Data, *n)
} else {
slice := buf.ReadBytes(2)
*n = BigEndian.Uint32(slice)
}
}
// FUInt16 implements fixed size serialization of uint16. It writes data in big
// endian, making it suitable for int keys to bolt.
func FUInt16(n *uint16, buf *Buffer) {
if buf.Writing {
buf.Data = BigEndian.AppendUint16(buf.Data, *n)
} else {
slice := buf.ReadBytes(2)
*n = BigEndian.Uint16(slice)
}
}
// FInt64 implements fixed size serialization of int64. It writes data in big
// endian, making it suitable for int keys to bolt.
func FInt64(n *int64, buf *Buffer) {
var u = uint64(*n)
FUInt64(&u, buf)
*n = int64(u)
}
// FInt implements fixed size serialization of int (as 64 bits). It writes data
// in big endian, making it suitable for int keys to bolt.
func FInt(n *int, buf *Buffer) {
var u = uint64(*n)
FUInt64(&u, buf)
*n = int(u)
}
func Float64(n *float64, buf *Buffer) {
// Flaot64bit and Float64frombits are just transmute casts that should cost nothing
var u = math.Float64bits(*n)
FUInt64(&u, buf)
*n = math.Float64frombits(u)
}
/*
switch buf.Mode {
case Serialize:
case Deserialize:
}
*/
// Byte implements serialization for a single byte
func Byte(b *byte, buf *Buffer) {
if buf.Writing {
buf.WriteBytes(*b)
} else {
*b = buf.ReadBytes(1)[0]
}
}
// Bool implements serialization for a bool
func Bool(b *bool, buf *Buffer) {
var bt byte
if *b {
bt = 1
}
Byte(&bt, buf)
*b = bt != 0
}
// VInt64 implements varint encoding for int64. Varint users fewer bytes for
// small values.
func VInt64(n *int64, buf *Buffer) {
if buf.Writing {
buf.Data = binary.AppendVarint(buf.Data, *n)
} else {
var err error
*n, err = binary.ReadVarint(buf)
if err != nil {
buf.Error = true
}
}
}
// VUInt64 implements varint encoding for uin64. Varint users fewer bytes for
// small values.
func VUInt64(n *uint64, buf *Buffer) {
if buf.Writing {
buf.Data = binary.AppendUvarint(buf.Data, *n)
} else {
var err error
*n, err = binary.ReadUvarint(buf)
if err != nil {
buf.Error = true
}
}
}
// Int implements varint encoding for int (as int64). Varint users fewer bytes for
// small values.
func Int(n *int, buf *Buffer) {
var n64 = int64(*n)
VInt64(&n64, buf)
*n = int(n64)
}
// UInt implements varint encoding for uint (as uint64). Varint users fewer
// bytes for small values.
func UInt(n *uint, buf *Buffer) {
var n64 = uint64(*n)
VUInt64(&n64, buf)
*n = uint(n64)
}
type IntBased interface {
~int | ~int64
}
// IntEnum implements varint encoding for an int (or int64) based enum types
func IntEnum[T IntBased](n *T, buf *Buffer) {
var n64 = int64(*n)
VInt64(&n64, buf)
*n = T(n64)
}
// Rune implements serialization for a single rune as a varint.
func Rune(r *rune, buf *Buffer) {
var n64 = int64(*r)
VInt64(&n64, buf)
*r = rune(n64)
}
func Version(max int, buf *Buffer) int {
var v = max
Int(&v, buf)
if v > max {
buf.Error = true
}
return v
}