-
Notifications
You must be signed in to change notification settings - Fork 1
/
sb.go
275 lines (247 loc) · 7.5 KB
/
sb.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* Copyright (c) 2019-present, Serhat Şevki Dinçer.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// Package sixb provides some string, slice & integer utility functions
package sixb
import (
"os"
"runtime"
"strings"
"unsafe"
)
// AnumToSixb is a bijection (without fixed points, single cycle and inverse of SixbToAnum)
// that maps 0-9: @A-Z a-z onto 6-bits
var AnumToSixb = [...]byte{208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220,
221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237,
238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254,
255, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 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, 69, 70, 71, 72, 73, 74, 0, 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, 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, 207, 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,
191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 153}
// SixbToAnum is a bijection (without fixed points, single cycle and inverse of AnumToSixb)
// that maps 6-bits onto 0-9: @A-Z a-z
var SixbToAnum = [...]byte{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, 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,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 91, 92, 93, 94, 95,
96, 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, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 255, 202, 203, 204, 205, 206,
207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240,
241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 201, 0, 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}
// Copy creates an identical copy of x
func Copy(x []byte) []byte {
r := make([]byte, len(x))
copy(r, x)
return r
}
// InsideTest returns true inside a Go test
func InsideTest() bool {
suffix := ".test"
if runtime.GOOS == "windows" {
suffix += ".exe"
}
return len(os.Args) > 1 && strings.HasSuffix(os.Args[0], suffix) &&
strings.HasPrefix(os.Args[1], "-test.")
}
// String internals from reflect
type String struct {
Data unsafe.Pointer
Len int
}
// Slice internals from reflect
type Slice struct {
Data unsafe.Pointer
Len int
Cap int
}
// BtoU4 converts byte slice to integer slice
func BtoU4(b []byte) (i []uint32) {
I := (*Slice)(unsafe.Pointer(&i))
B := (*Slice)(unsafe.Pointer(&b))
I.Data = B.Data
l := B.Len >> 2
I.Len = l
I.Cap = l
return
}
// U4toB converts integer slice to byte slice
func U4toB(i []uint32) (b []byte) {
I := (*Slice)(unsafe.Pointer(&i))
B := (*Slice)(unsafe.Pointer(&b))
B.Data = I.Data
l := I.Len << 2
B.Len = l
B.Cap = l
return
}
// U4toU8 converts uint32 slice to uint64 slice
func U4toU8(i []uint32) (k []uint64) {
I := (*Slice)(unsafe.Pointer(&i))
K := (*Slice)(unsafe.Pointer(&k))
K.Data = I.Data
l := I.Len >> 1
K.Len = l
K.Cap = l
return
}
// U8toU4 converts uint64 slice to uint32 slice
func U8toU4(i []uint64) (k []uint32) {
I := (*Slice)(unsafe.Pointer(&i))
K := (*Slice)(unsafe.Pointer(&k))
K.Data = I.Data
l := I.Len << 1
K.Len = l
K.Cap = l
return
}
// BtoU8 converts byte slice to integer slice
func BtoU8(b []byte) (i []uint64) {
I := (*Slice)(unsafe.Pointer(&i))
B := (*Slice)(unsafe.Pointer(&b))
I.Data = B.Data
l := B.Len >> 3
I.Len = l
I.Cap = l
return
}
// U8toB converts integer slice to byte slice
func U8toB(i []uint64) (b []byte) {
I := (*Slice)(unsafe.Pointer(&i))
B := (*Slice)(unsafe.Pointer(&b))
B.Data = I.Data
l := I.Len << 3
B.Len = l
B.Cap = l
return
}
// BtoS converts byte slice to string
func BtoS(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// StoB converts string to byte slice
func StoB(s string) (b []byte) {
B := (*Slice)(unsafe.Pointer(&b))
S := (*String)(unsafe.Pointer(&s))
B.Data = S.Data
l := S.Len
B.Len = l
B.Cap = l
return
}
// StoU4 converts string to integer slice
func StoU4(s string) (i []uint32) {
I := (*Slice)(unsafe.Pointer(&i))
S := (*String)(unsafe.Pointer(&s))
I.Data = S.Data
l := S.Len >> 2
I.Len = l
I.Cap = l
return
}
// U4toS converts integer slice to string
func U4toS(i []uint32) (s string) {
I := (*Slice)(unsafe.Pointer(&i))
S := (*String)(unsafe.Pointer(&s))
S.Data = I.Data
S.Len = I.Len << 2
return
}
// StoU8 converts string to integer slice
func StoU8(s string) (i []uint64) {
I := (*Slice)(unsafe.Pointer(&i))
S := (*String)(unsafe.Pointer(&s))
I.Data = S.Data
l := S.Len >> 3
I.Len = l
I.Cap = l
return
}
// U8toS converts integer slice to string
func U8toS(i []uint64) (s string) {
I := (*Slice)(unsafe.Pointer(&i))
S := (*String)(unsafe.Pointer(&s))
S.Data = I.Data
S.Len = I.Len << 3
return
}
const (
// StrSize is size of a string variable
StrSize = int(unsafe.Sizeof(""))
// SliceSize is size of a slice variable
SliceSize = int(unsafe.Sizeof([]byte{}))
)
// BtoStrs converts byte slice to String slice
func BtoStrs(b []byte) (ss []String) {
B := (*Slice)(unsafe.Pointer(&b))
S := (*Slice)(unsafe.Pointer(&ss))
S.Data = B.Data
l := B.Len / StrSize
S.Len = l
S.Cap = l
return
}
// U4toStrs converts integer slice to String slice
func U4toStrs(i []uint32) (ss []String) {
I := (*Slice)(unsafe.Pointer(&i))
S := (*Slice)(unsafe.Pointer(&ss))
S.Data = I.Data
l := 4 * I.Len / StrSize
S.Len = l
S.Cap = l
return
}
// U8toStrs converts integer slice to String slice
func U8toStrs(i []uint64) (ss []String) {
I := (*Slice)(unsafe.Pointer(&i))
S := (*Slice)(unsafe.Pointer(&ss))
S.Data = I.Data
l := 8 * I.Len / StrSize
S.Len = l
S.Cap = l
return
}
// BtoSlcs converts byte slice to Slice slice
func BtoSlcs(b []byte) (ss []Slice) {
B := (*Slice)(unsafe.Pointer(&b))
S := (*Slice)(unsafe.Pointer(&ss))
S.Data = B.Data
l := B.Len / SliceSize
S.Len = l
S.Cap = l
return
}
// U4toSlcs converts integer slice to Slice slice
func U4toSlcs(i []uint32) (ss []Slice) {
I := (*Slice)(unsafe.Pointer(&i))
S := (*Slice)(unsafe.Pointer(&ss))
S.Data = I.Data
l := 4 * I.Len / SliceSize
S.Len = l
S.Cap = l
return
}
// U8toSlcs converts integer slice to Slice slice
func U8toSlcs(i []uint64) (ss []Slice) {
I := (*Slice)(unsafe.Pointer(&i))
S := (*Slice)(unsafe.Pointer(&ss))
S.Data = I.Data
l := 8 * I.Len / SliceSize
S.Len = l
S.Cap = l
return
}