-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringutils.go
More file actions
203 lines (178 loc) · 3.94 KB
/
Copy pathstringutils.go
File metadata and controls
203 lines (178 loc) · 3.94 KB
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
package stringutils
import (
"crypto/md5"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"math"
r "math/rand"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"unicode/utf16"
)
func GetAfter(src string, find string) (string, bool) {
index := strings.Index(src, find)
if index > -1 && index+len(find) < len(src) {
return src[(index + len(find)):], true
}
return src, false
}
func GetBefore(src string, find string) (string, bool) {
index := strings.Index(src, find)
if index > -1 && index < len(src) {
return src[:index], true
}
return src, false
}
func UnicodeDecode(text string) string {
regex, err := regexp.Compile(`(\\u[a-fA-F0-9]{4})`)
if err != nil {
return text
}
text = regex.ReplaceAllStringFunc(text, func(match string) string {
_txt := match[2:]
char, err := strconv.ParseInt(_txt, 16, 32)
if err != nil {
return match
}
return string(rune(int(char)))
})
regex, err = regexp.Compile(`(&#[\d]{2,6})`)
if err != nil {
return text
}
text = regex.ReplaceAllStringFunc(text, func(match string) string {
_txt := match[2:]
char, err := strconv.ParseInt(_txt, 10, 32)
if err != nil {
return match
}
return string(rune(int(char)))
})
return text
}
func UnicodeEncode(str string) (js, html string) {
rs := []rune(str)
js = ""
html = ""
for _, r := range rs {
rint := int(r)
if rint < 128 {
js += string(r)
html += string(r)
} else {
js += `\u` + strconv.FormatInt(int64(rint), 16) // json
html += `&#` + strconv.Itoa(int(r)) + ";" // 网页
}
}
fmt.Printf("JSON: %s\n", js)
fmt.Printf("HTML: %s\n", html)
return
}
//比较是否在切片中存在,不区分大小写
func ExistSliceFold(srcSlice []string, elem string) bool {
for _, v := range srcSlice {
if strings.EqualFold(v, elem) {
return true
}
}
return false
}
//比较是否在切片中存在,区分大小写
func ExistSlice(srcSlice []string, elem string) bool {
for _, v := range srcSlice {
if v == elem {
return true
}
}
return false
}
//字符串转换uint16
func StringToUTF16(s string) []uint16 {
return utf16.Encode([]rune(s + "\x00"))
}
func Float64IsZero(s float64) bool {
if math.Abs(s) < 0.0001 {
return true
}
return false
}
//取子串
func Substr(s string, start, length int) string {
bt := []rune(s)
if start < 0 {
start = 0
}
if start > len(bt) {
start = start % len(bt)
}
var end int
if (start + length) > (len(bt) - 1) {
end = len(bt)
} else {
end = start + length
}
return string(bt[start:end])
}
//删除slice中的元素
func RemoveSliceElement(val interface{}, index int) interface{} {
if reflect.TypeOf(val).Kind() != reflect.Slice {
fmt.Println("val类型非slice")
return nil
}
s := reflect.ValueOf(val)
if index < 0 || index >= s.Len() {
fmt.Println("传入参数有误")
return nil
}
prev := s.Index(index)
for i := index + 1; i < s.Len(); i++ {
value := s.Index(i)
prev.Set(value)
prev = value
}
return s.Slice(0, s.Len()-1).Interface()
}
// RandomCreateBytes generate random []byte by specify chars.
func RandomCreateBytes(n int, alphabets ...byte) []byte {
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, n)
var randby bool
if num, err := rand.Read(bytes); num != n || err != nil {
r.Seed(time.Now().UnixNano())
randby = true
}
for i, b := range bytes {
if len(alphabets) == 0 {
if randby {
bytes[i] = alphanum[r.Intn(len(alphanum))]
} else {
bytes[i] = alphanum[b%byte(len(alphanum))]
}
} else {
if randby {
bytes[i] = alphabets[r.Intn(len(alphabets))]
} else {
bytes[i] = alphabets[b%byte(len(alphabets))]
}
}
}
return bytes
}
func SumMd5(txtInput string) string {
h := md5.New()
io.WriteString(h, txtInput)
return fmt.Sprintf("%x", h.Sum(nil))
}
//生成Guid字串
func GetGuid() string {
b := make([]byte, 48)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
return ""
}
return SumMd5(base64.URLEncoding.EncodeToString(b))
}