/
get.go
190 lines (164 loc) · 5.05 KB
/
get.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package slice
import (
"encoding/binary"
"math"
)
// GetBool get a bool at given position
func (b *Slice) GetBool(i int) (bool, error) {
if i < 0 || i > b.capacity-1 {
return false, ErrOutOfBound
}
return b.buf[i] != 0, nil
}
// GetByte get a byte at given position
func (b *Slice) GetByte(i int) (byte, error) {
if i < 0 || i > b.capacity-1 {
return 0, ErrOutOfBound
}
return b.buf[i], nil
}
// GetByte get a byte at given position
func (b *Slice) GetByteInt32(i int32) byte {
if i < 0 || i > int32(b.capacity)-1 {
panic(ErrOutOfBound.Error())
}
return b.buf[i]
}
// GetBytes get bytes at given position, GetBytes(p, start), GetBytes(p, start, end)
func (b *Slice) GetBytes(p []byte, start int, indexes ...int) (int, error) {
end := b.capacity
if len(indexes) >= 1 && indexes[0] < end {
end = indexes[0]
}
if start > end {
return 0, ErrOutOfBound
}
copy(p, b.buf[start:end])
return end - start, nil
}
// GetUInt8 get an uint8 at given position
func (b *Slice) GetUInt8(i int) (uint8, error) {
if i < 0 || i > b.capacity-1 {
return 0, ErrOutOfBound
}
return uint8(b.buf[i]), nil
}
// GetInt8 get an int8 at given position
func (b *Slice) GetInt8(i int) (int8, error) {
if i < 0 || i > b.capacity-1 {
return 0, ErrOutOfBound
}
return int8(b.buf[i]), nil
}
// GetUInt16BE get an uint16 in big endian at given position
func (b *Slice) GetUInt16BE(i int) (uint16, error) {
if i < 0 || i > b.capacity-2 {
return 0, ErrOutOfBound
}
return binary.BigEndian.Uint16(b.buf[i : i+2]), nil
}
// GetUInt16LE get an uint16 in little endian at given position
func (b *Slice) GetUInt16LE(i int) (uint16, error) {
if i < 0 || i > b.capacity-2 {
return 0, ErrOutOfBound
}
return binary.LittleEndian.Uint16(b.buf[i : i+2]), nil
}
// GetInt16BE get an int16 in big endian at given position
func (b *Slice) GetInt16BE(i int) (int16, error) {
if i < 0 || i > b.capacity-2 {
return 0, ErrOutOfBound
}
return int16(binary.BigEndian.Uint16(b.buf[i : i+2])), nil
}
// GetInt16LE get an int16 in little endian at given position
func (b *Slice) GetInt16LE(i int) (int16, error) {
if i < 0 || i > b.capacity-2 {
return 0, ErrOutOfBound
}
return int16(binary.LittleEndian.Uint16(b.buf[i : i+2])), nil
}
// GetUInt32BE get an uint32 in big endian at given position
func (b *Slice) GetUInt32BE(i int) (uint32, error) {
if i < 0 || i > b.capacity-4 {
return 0, ErrOutOfBound
}
return binary.BigEndian.Uint32(b.buf[i : i+4]), nil
}
// GetUInt32LE get an uint32 in little endian at given position
func (b *Slice) GetUInt32LE(i int) (uint32, error) {
if i < 0 || i > b.capacity-4 {
return 0, ErrOutOfBound
}
return binary.LittleEndian.Uint32(b.buf[i : i+4]), nil
}
// GetInt32BE get an int32 in big endian at given position
func (b *Slice) GetInt32BE(i int) (int32, error) {
if i < 0 || i > b.capacity-4 {
return 0, ErrOutOfBound
}
return int32(binary.BigEndian.Uint32(b.buf[i : i+4])), nil
}
// GetInt32LE get an int32 in little endian at given position
func (b *Slice) GetInt32LE(i int) (int32, error) {
if i < 0 || i > b.capacity-4 {
return 0, ErrOutOfBound
}
return int32(binary.LittleEndian.Uint32(b.buf[i : i+4])), nil
}
// GetUInt64BE get an uint64 in big endian at given position
func (b *Slice) GetUInt64BE(i int) (uint64, error) {
if i < 0 || i > b.capacity-8 {
return 0, ErrOutOfBound
}
return binary.BigEndian.Uint64(b.buf[i : i+8]), nil
}
// GetUInt64LE get an uint64 in little endian at given position
func (b *Slice) GetUInt64LE(i int) (uint64, error) {
if i < 0 || i > b.capacity-8 {
return 0, ErrOutOfBound
}
return binary.LittleEndian.Uint64(b.buf[i : i+8]), nil
}
// GetInt64BE get a int64 in big endian at given position
func (b *Slice) GetInt64BE(i int) (int64, error) {
if i < 0 || i > b.capacity-8 {
return 0, ErrOutOfBound
}
return int64(binary.BigEndian.Uint64(b.buf[i : i+8])), nil
}
// GetInt64LE get a int64 in little endian at given position
func (b *Slice) GetInt64LE(i int) (int64, error) {
if i < 0 || i > b.capacity-8 {
return 0, ErrOutOfBound
}
return int64(binary.LittleEndian.Uint64(b.buf[i : i+8])), nil
}
// GetFloat32BE get a float32 in big endian at given position
func (b *Slice) GetFloat32BE(i int) (float32, error) {
if i < 0 || i > b.capacity-4 {
return 0.0, ErrOutOfBound
}
return math.Float32frombits(binary.BigEndian.Uint32(b.buf[i : i+4])), nil
}
// GetFloat32LE get a float32 in little endian at given position
func (b *Slice) GetFloat32LE(i int) (float32, error) {
if i < 0 || i > b.capacity-4 {
return 0.0, ErrOutOfBound
}
return math.Float32frombits(binary.LittleEndian.Uint32(b.buf[i : i+4])), nil
}
// GetFloat64BE get a float64 in big endian at given position
func (b *Slice) GetFloat64BE(i int) (float64, error) {
if i < 0 || i > b.capacity-8 {
return 0.0, ErrOutOfBound
}
return math.Float64frombits(binary.BigEndian.Uint64(b.buf[i : i+8])), nil
}
// GetFloat64LE get a float64 in little endian at given position
func (b *Slice) GetFloat64LE(i int) (float64, error) {
if i < 0 || i > b.capacity-8 {
return 0.0, ErrOutOfBound
}
return math.Float64frombits(binary.LittleEndian.Uint64(b.buf[i : i+8])), nil
}