-
Notifications
You must be signed in to change notification settings - Fork 785
/
strings.go
212 lines (191 loc) · 5.12 KB
/
strings.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
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
204
205
206
207
208
209
210
211
212
package util
import (
"encoding/hex"
"math/rand"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
// RegexpSplit splits a string into an array using the regexSep as a separator
func RegexpSplit(text string, regexSeperator string) []string {
reg := regexp.MustCompile(regexSeperator)
indexes := reg.FindAllStringIndex(text, -1)
lastIdx := 0
result := make([]string, len(indexes)+1)
for i, element := range indexes {
result[i] = text[lastIdx:element[0]]
lastIdx = element[1]
}
result[len(indexes)] = text[lastIdx:]
return result
}
// StringIndexes returns all the indices where the value occurs in the given string
func StringIndexes(text string, value string) []int {
answer := []int{}
t := text
valueLen := len(value)
offset := 0
for {
idx := strings.Index(t, value)
if idx < 0 {
break
}
answer = append(answer, idx+offset)
offset += valueLen
t = t[idx+valueLen:]
}
return answer
}
// StringArrayIndex returns the index in the slice which equals the given value
func StringArrayIndex(array []string, value string) int {
for i, v := range array {
if v == value {
return i
}
}
return -1
}
// StringArrayHasPrefixIndex returns the index in the slice which the value has the given prefix
func StringArrayHasPrefixIndex(array []string, prefix string) int {
for i, v := range array {
if strings.HasPrefix(v, prefix) {
return i
}
}
return -1
}
// StringArraysEqual returns true if the two string slices are equal
func StringArraysEqual(a1 []string, a2 []string) bool {
if len(a1) != len(a2) {
return false
}
for i := 0; i < len(a1); i++ {
if a1[i] != a2[i] {
return false
}
}
return true
}
// FirstNotEmptyString returns the first non empty string or the empty string if none can be found
func FirstNotEmptyString(values ...string) string {
if values != nil {
for _, v := range values {
if v != "" {
return v
}
}
}
return ""
}
// SortedMapKeys returns the sorted keys of the given map
func SortedMapKeys(m map[string]string) []string {
answer := []string{}
for k, _ := range m {
answer = append(answer, k)
}
sort.Strings(answer)
return answer
}
func ReverseStrings(a []string) {
for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
a[i], a[j] = a[j], a[i]
}
}
// StringArrayToLower returns a string slice with all the values converted to lower case
func StringArrayToLower(values []string) []string {
answer := []string{}
for _, v := range values {
answer = append(answer, strings.ToLower(v))
}
return answer
}
// StringMatches returns true if the given text matches the includes/excludes lists
func StringMatchesAny(text string, includes []string, excludes []string) bool {
for _, x := range excludes {
if StringMatchesPattern(text, x) {
return false
}
}
if len(includes) == 0 {
return true
}
for _, inc := range includes {
if StringMatchesPattern(text, inc) {
return true
}
}
return false
}
// StringMatchesPattern returns true if the given text matches the includes/excludes lists
func StringMatchesPattern(text string, pattern string) bool {
if pattern == "*" {
return true
}
if strings.HasSuffix(pattern, "*") {
prefix := strings.TrimSuffix(pattern, "*")
return strings.HasPrefix(text, prefix)
}
return text == pattern
}
// StringsContaining if the filter is not empty return all the strings which contain the text
func StringsContaining(slice []string, filter string) []string {
if filter == "" {
return slice
}
answer := []string{}
for _, text := range slice {
if strings.Contains(text, filter) {
answer = append(answer, text)
}
}
return answer
}
// RandStringBytesMaskImprSrc returns a random hexadecimal string of length n.
func RandStringBytesMaskImprSrc(n int) (string, error) {
src := rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, (n+1)/2) // can be simplified to n/2 if n is always even
if _, err := src.Read(b); err != nil {
return "", err
}
return hex.EncodeToString(b)[:n], nil
}
// DiffSlices compares the two slices and returns an array of items to delete from the old slice and a slice of
// new values to add to
func DiffSlices(oldSlice []string, newSlice []string) ([]string, []string) {
toDelete := []string{}
toInsert := []string{}
for _, name := range oldSlice {
if StringArrayIndex(newSlice, name) < 0 {
toDelete = append(toInsert, name)
}
}
for _, name := range newSlice {
if StringArrayIndex(oldSlice, name) < 0 {
toInsert = append(toInsert, name)
}
}
return toDelete, toInsert
}
// ParseBool parses the boolean string. Returns false if the string is empty
func ParseBool(text string) (bool, error) {
if text == "" {
return false, nil
}
return strconv.ParseBool(text)
}
// CheckMark returns the check mark unicode character.
// We could configure this to use no color or avoid unicode using platform, env vars or config?
func CheckMark() string {
return "\u2705"
}
// RemoveStringFromSlice removes the first occurence of the specified string from a slice, if it exists and returns the result
func RemoveStringFromSlice(strings []string, toRemove string) []string {
for i, str := range strings {
if str == toRemove {
return append(strings[:i], strings[i+1:]...)
}
}
return strings
}