-
Notifications
You must be signed in to change notification settings - Fork 0
/
byte_utils.go
136 lines (112 loc) · 3.38 KB
/
byte_utils.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
package sockety
import (
"encoding/binary"
"github.com/google/uuid"
"unsafe"
)
// Read bytes
var readBytes readBytesInternal
type readBytesInternal struct{}
func (readBytesInternal) Uint8(b []byte, o offset) uint8 {
return b[o]
}
func (readBytesInternal) Uint16(b []byte, o offset) uint16 {
return binary.LittleEndian.Uint16(b[o:])
}
func (readBytesInternal) Uint24(b []byte, o offset) Uint24 {
_ = b[o+2] // bounds check hint to compiler; see golang.org/issue/14808
return Uint24(b[o]) | (Uint24(b[o+1]) << 8) | (Uint24(b[o+2]) << 16)
}
func (readBytesInternal) Uint32(b []byte, o offset) uint32 {
return binary.LittleEndian.Uint32(b[o:])
}
func (readBytesInternal) Uint48(b []byte, o offset) Uint48 {
_ = b[o+5] // bounds check hint to compiler; see golang.org/issue/14808
return Uint48(b[o]) | (Uint48(b[o+1]) << 8) | (Uint48(b[o+2]) << 16) | (Uint48(b[o+3]) << 24) | (Uint48(b[o+4]) << 32) | (Uint48(b[o+5]) << 40)
}
func (readBytesInternal) UUID(b []byte, o offset) uuid.UUID {
result, err := uuid.FromBytes(b[o : o+16])
if err != nil {
panic("should not happen")
}
return result
}
func (readBytesInternal) String(b []byte, o offset, bytesLength uint32) string {
b = b[o:bytesLength]
return *(*string)(unsafe.Pointer(&b))
}
// Write bytes
var writeBytes writeBytesInternal
type writeBytesInternal struct{}
func (writeBytesInternal) Uint8(b []byte, o offset, value uint8) offset {
b[o] = value
return o + 1
}
func (writeBytesInternal) Byte(b []byte, o offset, value byte) offset {
b[o] = value
return o + 1
}
func (writeBytesInternal) Bytes(b []byte, o offset, value []byte) offset {
nextOffset := o + offset(len(value))
_ = b[nextOffset-1]
copy(b[o:], value)
return nextOffset
}
func (writeBytesInternal) Uint16(b []byte, o offset, value uint16) offset {
binary.LittleEndian.PutUint16(b[o:], value)
return o + 2
}
func (writeBytesInternal) Uint24(b []byte, o offset, value Uint24) offset {
_ = b[o+2] // bounds check hint to compiler; see golang.org/issue/14808
b[o] = byte(value)
b[o+1] = byte(value >> 8)
b[o+2] = byte(value >> 16)
return o + 3
}
func (writeBytesInternal) Uint32(b []byte, o offset, value uint32) offset {
binary.LittleEndian.PutUint32(b[o:], value)
return o + 4
}
func (writeBytesInternal) Uint48(b []byte, o offset, value Uint48) offset {
_ = b[o+5] // bounds check hint to compiler; see golang.org/issue/14808
b[o] = byte(value)
b[o+1] = byte(value >> 8)
b[o+2] = byte(value >> 16)
b[o+3] = byte(value >> 24)
b[o+4] = byte(value >> 32)
b[o+5] = byte(value >> 40)
return o + 6
}
func (w writeBytesInternal) Uint(b []byte, o offset, value Uint48, bytesLength uint8) offset {
switch bytesLength {
case 1:
return w.Uint8(b, o, uint8(value))
case 2:
return w.Uint16(b, o, uint16(value))
case 3:
return w.Uint24(b, o, Uint24(value))
case 4:
return w.Uint32(b, o, uint32(value))
case 6:
return w.Uint48(b, o, value)
default:
panic("supported uint bytes are 1-4 and 6")
}
}
func (writeBytesInternal) UUID(b []byte, o offset, value uuid.UUID) offset {
_ = b[o+15] // bounds check hint to compiler; see golang.org/issue/14808
*(*uuid.UUID)(unsafe.Pointer(&b[o])) = value
return o + 16
}
func (writeBytesInternal) String(b []byte, o offset, value string) offset {
strLen := len(value)
strOffset := o + offset(strLen)
_ = b[strOffset-1]
copy(b[o:], *(*[]byte)(unsafe.Pointer(
&struct {
string
Cap int
}{value, strLen},
)))
return strOffset
}