-
Notifications
You must be signed in to change notification settings - Fork 1
/
u_string.go
68 lines (60 loc) · 1.61 KB
/
u_string.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
package kerbalwzygo
import (
"crypto/hmac"
"crypto/md5"
"encoding/hex"
"errors"
"unicode"
)
// Get the MD5 hash value of bytes
func BytesMD5Hash(data []byte) string {
mac := hmac.New(md5.New, nil)
mac.Write(data)
return hex.EncodeToString(mac.Sum(nil))
}
// Get the MD5 hash value of string
func StringMD5Hash(data string) string {
return BytesMD5Hash([]byte(data))
}
// Get the MD5 hash value of multi string
func MultiStringMD5Hash(data ...string) string {
var temp string
for _, item := range data {
temp += item
}
return StringMD5Hash(temp)
}
// 检查字符串是否包含中文字符
func StringContainsHan(data string) bool {
temp := []rune(data)
for _, r := range temp {
if unicode.Is(unicode.Han, r) {
return true
}
}
return false
}
// 检查字符串是否包含空白字符
func StringContainsSpace(data string) bool {
temp := []rune(data)
for _, r := range temp {
if unicode.IsSpace(r) {
return true
}
}
return false
}
var ErrStartLargeThenEnd = errors.New("the 'start' index value is over then 'end'")
var ErrIndexOutOfRange = errors.New("the 'start' or 'end' index value is out of range")
// 安全的切割字符串: data原字符串, start切片起点, end切片终点+1
// Direct cutting of string may cause character scrambling, because some character could be 3 or 4 byte.
// Translate the 'string' into 'rune' before cutting could make safe
func SafeSliceString(data string, start, end int) (string, error) {
if start > end {
return "", ErrStartLargeThenEnd
}
if start < 0 || end > len(data) {
return "", ErrIndexOutOfRange
}
return string([]rune(data)[start:end]), nil
}