-
Notifications
You must be signed in to change notification settings - Fork 1
/
strings.go
130 lines (117 loc) · 2.96 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
package util
import (
"encoding/hex"
"math/rand"
"regexp"
"sort"
"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
}
func StringArrayIndex(array []string, value string) int {
for i, v := range array {
if v == value {
return i
}
}
return -1
}
// 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
}
// 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
}