-
Notifications
You must be signed in to change notification settings - Fork 0
/
str_util.go
128 lines (112 loc) · 2.56 KB
/
str_util.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
package text
import (
"strings"
"unicode/utf8"
validator "github.com/nine-monsters/go-hutool/hutool-core/validator"
)
// IsBlank return str is all blank
func IsBlank(str string) bool {
if len(str) == 0 {
return true
}
return IsBlankRunes([]rune(str))
}
// IsEmpty return str is empty
func IsEmpty(str string) bool {
if len(str) == 0 {
return true
}
return false
}
// Trim returns a slice of the string s, with all leading
// and trailing white space removed, as defined by Unicode.
func Trim(str string) string {
if len(str) == 0 {
return EMPTY
}
return strings.TrimSpace(str)
}
// Trims returns a slice of the string s, with all leading
// and trailing white space removed, as defined by Unicode.
func Trims(str []string) []string {
if len(str) == 0 {
return []string{}
}
// slice size = 0, cap = str length
res := make([]string, 0, len(str))
for _, s := range str {
res = append(res, Trim(s))
}
return res
}
// Equals return seq1 equal to seq2
func Equals(seq1 string, seq2 string) bool {
return EqualsIgnoreCase(seq1, seq2, false)
}
func EqualsIgnoreCase(seq1 string, seq2 string, ignoreCase bool) bool {
if seq1 == "" {
return seq2 == ""
}
if seq2 == "" {
return false
}
if ignoreCase {
return seq1 == seq2
} else {
return strings.EqualFold(seq1, seq2)
}
}
// Creates an slice of slice values not included in the other given slice.
func Diff(base, exclude []string) (result []string) {
excludeMap := make(map[string]bool)
for _, s := range exclude {
excludeMap[s] = true
}
for _, s := range base {
if !excludeMap[s] {
result = append(result, s)
}
}
return result
}
func Unique(ss []string) (result []string) {
smap := make(map[string]bool)
for _, s := range ss {
smap[s] = true
}
for s := range smap {
result = append(result, s)
}
return result
}
func CamelCaseToUnderscore(str string) string {
return validator.CamelCaseToUnderscore(str)
}
func UnderscoreToCamelCase(str string) string {
return validator.UnderscoreToCamelCase(str)
}
func FindString(array []string, str string) int {
for index, s := range array {
if str == s {
return index
}
}
return -1
}
func StringIn(str string, array []string) bool {
return FindString(array, str) > -1
}
func Reverse(s string) string {
size := len(s)
buf := make([]byte, size)
for start := 0; start < size; {
r, n := utf8.DecodeRuneInString(s[start:])
start += n
utf8.EncodeRune(buf[size-start:], r)
}
return string(buf)
}
// Contains checks if the string contains the substring.
func Contains(str, substring string) bool {
return strings.Contains(str, substring)
}