forked from jenkins-x/jx
/
strings.go
85 lines (77 loc) · 1.82 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
package util
import (
"regexp"
"sort"
"strings"
)
// 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
}