-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
102 lines (86 loc) · 1.7 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
package main
import (
"encoding/binary"
"math/rand"
"strconv"
"time"
)
// BytesConcat concat bytes
func BytesConcat(slices ...[]byte) []byte {
var totalLen int
for _, s := range slices {
totalLen += len(s)
}
tmp := make([]byte, totalLen)
var i int
for _, s := range slices {
i += copy(tmp[i:], s)
}
return tmp
}
// Int64ToBytes parse int64 to bytes
func Int64ToBytes(v int64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(v))
return b
}
// BytesToInt64 parse bytes to int64
func BytesToInt64(v []byte) int64 {
if len(v) == 0 {
return 0
}
return int64(binary.BigEndian.Uint64(v))
}
// Uint64ToBytes parse uint64 to bytes
func Uint64ToBytes(v uint64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, v)
return b
}
// BytesToUint64 parse bytes to uint64
func BytesToUint64(v []byte) uint64 {
if len(v) == 0 {
return 0
}
return binary.BigEndian.Uint64(v)
}
func PrefixEndBytes(prefix []byte) []byte {
if len(prefix) == 0 {
return nil
}
end := make([]byte, len(prefix))
copy(end, prefix)
for {
if end[len(end)-1] != byte(255) {
end[len(end)-1]++
break
}
end = end[:len(end)-1]
if len(end) == 0 {
end = nil
break
}
}
return end
}
var (
defaultLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
)
func RandomString(n int, allowedChars ...[]rune) string {
rand.Seed(time.Now().UnixNano())
var letters []rune
if len(allowedChars) == 0 {
letters = defaultLetters
} else {
letters = allowedChars[0]
}
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func StringToInt64(str string) int64 {
i, _ := strconv.ParseInt(str, 10, 64)
return i
}