-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
237 lines (207 loc) · 4.36 KB
/
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
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
package utils
import (
"bytes"
"encoding/binary"
"encoding/gob"
"io/fs"
"os"
"path/filepath"
"regexp"
"time"
)
func ExecTimeWithNanoseconds(fn func()) float64 {
start := time.Now()
fn()
return float64(time.Since(start).Nanoseconds()) / 1e6
}
func ExecTimeWithError(fn func() error) (float64, error) {
start := time.Now()
err := fn()
return float64(time.Since(start).Nanoseconds()) / 1e6, err
}
func Encoder(data interface{}) ([]byte, error) {
if data == nil {
return nil, nil
}
buffer := new(bytes.Buffer)
encoder := gob.NewEncoder(buffer)
err := encoder.Encode(data)
return buffer.Bytes(), err
}
func Decoder(data []byte, v interface{}) error {
if data == nil {
return nil
}
buffer := bytes.NewBuffer(data)
decoder := gob.NewDecoder(buffer)
err := decoder.Decode(v)
return err
}
const (
c1 = 0xcc9e2d51
c2 = 0x1b873593
c3 = 0x85ebca6b
c4 = 0xc2b2ae35
r1 = 15
r2 = 13
m = 5
n = 0xe6546b64
)
var (
Seed = uint32(1)
)
func Murmur3(key []byte) (hash uint32) {
hash = Seed
iByte := 0
for ; iByte+4 <= len(key); iByte += 4 {
k := uint32(key[iByte]) | uint32(key[iByte+1])<<8 | uint32(key[iByte+2])<<16 | uint32(key[iByte+3])<<24
k *= c1
k = (k << r1) | (k >> (32 - r1))
k *= c2
hash ^= k
hash = (hash << r2) | (hash >> (32 - r2))
hash = hash*m + n
}
var remainingBytes uint32
switch len(key) - iByte {
case 3:
remainingBytes += uint32(key[iByte+2]) << 16
fallthrough
case 2:
remainingBytes += uint32(key[iByte+1]) << 8
fallthrough
case 1:
remainingBytes += uint32(key[iByte])
remainingBytes *= c1
remainingBytes = (remainingBytes << r1) | (remainingBytes >> (32 - r1))
remainingBytes = remainingBytes * c2
hash ^= remainingBytes
}
hash ^= uint32(len(key))
hash ^= hash >> 16
hash *= c3
hash ^= hash >> 13
hash *= c4
hash ^= hash >> 16
// 出发吧,狗嬷嬷!
return
}
// StringToInt 字符串转整数
func StringToInt(value string) uint32 {
return Murmur3([]byte(value))
}
func Uint32Comparator(a, b interface{}) int {
aAsserted := a.(uint32)
bAsserted := b.(uint32)
switch {
case aAsserted > bAsserted:
return 1
case aAsserted < bAsserted:
return -1
default:
return 0
}
}
func Uint32ToBytes(i uint32) []byte {
var buf = make([]byte, 4)
binary.BigEndian.PutUint32(buf, i)
return buf
}
// QuickSortAsc 快速排序
func QuickSortAsc(arr []int, start, end int, cmp func(int, int)) {
if start < end {
i, j := start, end
key := arr[(start+end)/2]
for i <= j {
for arr[i] < key {
i++
}
for arr[j] > key {
j--
}
if i <= j {
arr[i], arr[j] = arr[j], arr[i]
if cmp != nil {
cmp(i, j)
}
i++
j--
}
}
if start < j {
QuickSortAsc(arr, start, j, cmp)
}
if end > i {
QuickSortAsc(arr, i, end, cmp)
}
}
}
func DeleteArray(array []uint32, index int) []uint32 {
return append(array[:index], array[index+1:]...)
}
func ReleaseAssets(file fs.File, out string) {
if file == nil {
return
}
if out == "" {
panic("out is empty")
}
// 判断out文件是否存在
if _, err := os.Stat(out); os.IsNotExist(err) {
// 读取文件信息
fileInfo, err := file.Stat()
if err != nil {
panic(err)
}
buffer := make([]byte, fileInfo.Size())
_, err = file.Read(buffer)
if err != nil {
panic(err)
}
// 读取输出文件目录
outDir := filepath.Dir(out)
err = os.MkdirAll(outDir, os.ModePerm)
if err != nil {
panic(err)
}
// 创建文件
outFile, _ := os.Create(out)
defer func(outFile *os.File) {
err := outFile.Close()
if err != nil {
panic(err)
}
}(outFile)
err = os.WriteFile(out, buffer, os.ModePerm)
if err != nil {
panic(err)
}
}
}
// DirSizeB DirSizeMB getFileSize get file size by path(B)
func DirSizeB(path string) int64 {
var size int64
_ = filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if !info.IsDir() {
size += info.Size()
}
return err
})
return size
}
// RemovePunctuation 移除所有的标点符号
func RemovePunctuation(str string) string {
reg := regexp.MustCompile(`\p{P}+`)
return reg.ReplaceAllString(str, "")
}
// RemoveSpace 移除所有的空格
func RemoveSpace(str string) string {
reg := regexp.MustCompile(`\s+`)
return reg.ReplaceAllString(str, "")
}
// init 注册数据类型
// 防止 gob: type not registered for interface: map[string]interface {}
func init() {
gob.Register(map[string]interface{}{})
gob.Register([]interface{}{})
}