/
common.go
156 lines (139 loc) · 3.23 KB
/
common.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
package utils
import (
"encoding/binary"
"encoding/json"
"errors"
"regexp"
"runtime"
"sync"
"unicode"
"unicode/utf8"
log "github.com/sirupsen/logrus"
)
func GoAndWait(handlers ...func() error) (err error) {
var wg sync.WaitGroup
var once sync.Once
for _, f := range handlers {
wg.Add(1)
go func(handler func() error) {
defer func() {
if e := recover(); e != nil {
buf := make([]byte, 1024)
buf = buf[:runtime.Stack(buf, false)]
log.Errorf("[GoAndWait PANIC]%v\n%s\n", e, buf)
once.Do(func() {
err = errors.New("panic found in call handlers")
})
}
wg.Done()
}()
if e := handler(); e != nil {
once.Do(func() {
err = e
})
}
}(f)
}
wg.Wait()
return err
}
// JsonString 将任意内容转换为Json字符串
func JsonString(v interface{}) string {
res, _ := json.Marshal(v)
return string(res)
}
// StringLimit 限制字符串长度,若超出limit,返回前limit个码点+"..."
func StringLimit(s string, limit int) string {
runeSlice := []rune(s)
if len(runeSlice) <= limit {
return s
}
return string(runeSlice[:limit]) + "..."
}
// MergeStringSlices 合并多个字符串切片并去重、去除空字符串
func MergeStringSlices(slices ...[]string) (res []string) {
mp := FormSetByStrings(slices...)
for s, _ := range mp {
if len(s) == 0 {
continue
}
res = append(res, s)
}
return
}
// FormSetByStrings 将字符串切片形成Set
func FormSetByStrings(slices ...[]string) map[string]struct{} {
mp := make(map[string]struct{})
for _, slice := range slices {
for _, s := range slice {
mp[s] = struct{}{}
}
}
return mp
}
// StringSliceContain 字符串切片中是否含有指定字符串
func StringSliceContain(slices []string, substr string) bool {
for _, str := range slices {
if str == substr {
return true
}
}
return false
}
// DeleteStringInSlice 删除字符串切片中的str元素,并去重
func DeleteStringInSlice(slice []string, str ...string) []string {
slice = MergeStringSlices(slice)
str = MergeStringSlices(str)
for _, s := range str {
for i, now := range slice {
if now == s {
slice = append(slice[:i], slice[i+1:]...)
break
}
}
}
return slice
}
var letterReg = regexp.MustCompile(`^[A-Za-z]+$`)
// IsLetter 字符串是否为纯字母
func IsLetter(s string) bool {
return letterReg.MatchString(s)
}
var numberReg = regexp.MustCompile(`^\d+$`)
// IsNumber 字符串是否为纯数字
func IsNumber(s string) bool {
return numberReg.MatchString(s)
}
// StringRealLength 计算字符串的真实长度
func StringRealLength(s string) int {
return utf8.RuneCountInString(s)
}
// SplitOnSpace 按文字、空格、文字...分隔字符串
func SplitOnSpace(x string) []string {
var result []string
pi := 0
ps := false
for i, c := range x {
s := unicode.IsSpace(c)
if s != ps && i > 0 {
result = append(result, x[pi:i])
pi = i
}
ps = s
}
result = append(result, x[pi:])
return result
}
// UInt32ToBytes uint32转换为bytes
func UInt32ToBytes(n uint32) []byte {
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, n)
return b
}
// BytesToUInt32 bytes转换为uint32
func BytesToUInt32(b []byte) uint32 {
for len(b) < 4 {
b = append(b, byte(0))
}
return binary.LittleEndian.Uint32(b)
}