-
Notifications
You must be signed in to change notification settings - Fork 0
/
id.go
158 lines (128 loc) · 3.29 KB
/
id.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
package uid
import (
"encoding/binary"
"errors"
)
const ErrorMsgSizeDivisible2 = "size of data must be divisible by 2"
const ErrorMsgSizeDivisible4 = "size of data must be divisible by 4"
const ErrorMsgSizeDivisible8 = "size of data must be divisible by 8"
type Identifier interface {
String() string
Byte() []byte
Int16() ([]int16, error)
Uint16() ([]uint16, error)
Int32() ([]int32, error)
Uint32() ([]uint32, error)
Int64() ([]int64, error)
Uint64() ([]uint64, error)
}
type ID struct {
data []byte
enc Encoder
}
func NewID(data []byte, enc Encoder) Identifier {
return &ID{
data: data,
enc: enc,
}
}
func NewIDStdBase32(data []byte) Identifier {
return NewID(data, NewEncoder())
}
func (id ID) String() string {
return id.enc.EncodeToString(id.data)
}
func (id ID) Byte() []byte {
return id.data
}
func (id ID) Int16() ([]int16, error) {
if len(id.data)%SizeInt16 != 0 {
return nil, &WrongSizeError{errors.New(ErrorMsgSizeDivisible2)}
}
size := sizeNewArray(id.data, SizeInt16)
dst := make([]int16, size)
bs := readBytes(id.data, size, SizeInt16)
for i, b := range bs {
dst[i] = int16(binary.LittleEndian.Uint16(b))
}
return dst, nil
}
func (id ID) Uint16() ([]uint16, error) {
if len(id.data)%SizeUint16 != 0 {
return nil, &WrongSizeError{errors.New(ErrorMsgSizeDivisible2)}
}
size := sizeNewArray(id.data, SizeUint16)
dst := make([]uint16, size)
bs := readBytes(id.data, size, SizeUint16)
for i, b := range bs {
dst[i] = binary.LittleEndian.Uint16(b)
}
return dst, nil
}
func (id ID) Int32() ([]int32, error) {
if len(id.data)%SizeInt32 != 0 {
return nil, &WrongSizeError{errors.New(ErrorMsgSizeDivisible4)}
}
size := sizeNewArray(id.data, SizeInt32)
dst := make([]int32, size)
bs := readBytes(id.data, size, SizeInt32)
for i, b := range bs {
dst[i] = int32(binary.LittleEndian.Uint32(b))
}
return dst, nil
}
func (id ID) Uint32() ([]uint32, error) {
if len(id.data)%SizeUint32 != 0 {
return nil, &WrongSizeError{errors.New(ErrorMsgSizeDivisible4)}
}
size := sizeNewArray(id.data, SizeUint32)
dst := make([]uint32, size)
bs := readBytes(id.data, size, SizeUint32)
for i, b := range bs {
dst[i] = binary.LittleEndian.Uint32(b)
}
return dst, nil
}
func (id ID) Int64() ([]int64, error) {
if len(id.data)%SizeInt64 != 0 {
return nil, &WrongSizeError{errors.New(ErrorMsgSizeDivisible8)}
}
size := sizeNewArray(id.data, SizeInt64)
dst := make([]int64, size)
bs := readBytes(id.data, size, SizeInt64)
for i, b := range bs {
dst[i] = int64(binary.LittleEndian.Uint64(b))
}
return dst, nil
}
func (id ID) Uint64() ([]uint64, error) {
if len(id.data)%SizeUint64 != 0 {
return nil, &WrongSizeError{errors.New(ErrorMsgSizeDivisible8)}
}
size := sizeNewArray(id.data, SizeUint64)
dst := make([]uint64, size)
bs := readBytes(id.data, size, SizeInt64)
for i, b := range bs {
dst[i] = binary.LittleEndian.Uint64(b)
}
return dst, nil
}
func sizeNewArray(src []byte, size int) int {
res := len(src) / size
if len(src)%size != 0 {
res += 1
}
return res
}
func readBytes(src []byte, dstParts int, dstSize int) [][]byte {
if len(src) == 0 || len(src)%dstSize != 0 {
return [][]byte{}
}
bs := make([][]byte, 0, dstSize)
for i := 0; i < dstParts; i++ {
start := i * dstSize
end := (i + 1) * dstSize
bs = append(bs, src[start:end])
}
return bs
}