-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathstringutil.go
168 lines (145 loc) · 3.44 KB
/
stringutil.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
// Package stringutil provides generl string
// utility functions.
package stringutil
import (
"fmt"
"regexp"
"strings"
)
var (
rxNumber = regexp.MustCompile(`^-?\d+$`)
)
// IsInteger returns true if the passed string is
// a valid number.
func IsInteger(str string) bool {
return rxNumber.MatchString(str)
}
// EnsureNotEmpty returns def if str is empty.
func EnsureNotEmpty(str, def string) string {
if str == "" {
return def
}
return str
}
// FromBool returns ifTrue if cond is true
// else returns ifFalse.
func FromBool(cond bool, ifTrue, ifFalse string) string {
if cond {
return ifTrue
}
return ifFalse
}
// IndexOf returns the index of the
// passed str in the passed arr. If str is not
// in arr, -1 is returned.
func IndexOf(str string, arr []string) int {
for i, v := range arr {
if v == str {
return i
}
}
return -1
}
// ContainsAny returns true if str is contained
// in arr.
func ContainsAny(str string, arr []string) bool {
return IndexOf(str, arr) > -1
}
// Contained returns an array of items which
// contains all items of subset which are also
// present in arr.
func Contained(subset, arr []string) (contained []string) {
if len(subset) == 0 || len(arr) == 0 {
return []string{}
}
contained = make([]string, 0, len(arr))
for _, s := range subset {
if ContainsAny(s, arr) {
contained = append(contained, s)
}
}
return
}
// NotContained returns an array of items which
// contains all items of must not present in arr.
func NotContained(must, arr []string) (notContained []string) {
if len(must) == 0 {
return []string{}
}
if len(arr) == 0 {
return must
}
notContained = make([]string, 0, len(must))
for _, m := range must {
if !ContainsAny(m, arr) {
notContained = append(notContained, m)
}
}
return
}
// HasPrefixAny returns true if the given str has
// any of the given prefixes.
func HasPrefixAny(str string, prefixes ...string) bool {
for _, p := range prefixes {
if strings.HasPrefix(str, p) {
return true
}
}
return false
}
// HasSuffixAny returns true if the given str has
// any of the given suffixes.
func HasSuffixAny(str string, suffixes ...string) bool {
for _, s := range suffixes {
if strings.HasSuffix(str, s) {
return true
}
}
return false
}
// Splice returns arr without the element at index
// exclude.
func Splice(arr []string, exclude int) []string {
if exclude < 0 || exclude >= len(arr) {
return arr
}
if exclude == 0 {
return arr[1:]
}
if exclude == len(arr)-1 {
return arr[:len(arr)-1]
}
return append(arr[:exclude], arr[exclude+1:]...)
}
// Capitalize uppercases the first character of the
// given string.
//
// If all is true, all starting characters of all
// words in the string are capitalized.
func Capitalize(v string, all bool) string {
if v == "" {
return ""
}
if all {
split := strings.Split(v, " ")
for i, v := range split {
split[i] = Capitalize(v, false)
}
return strings.Join(split, " ")
} else {
return strings.ToUpper(string(v[0])) + v[1:]
}
}
// Cap cuts the string to the given max len if it exceeds
// it ending the string with '…' if it does.
//
// If max is smaller than 0, v is returned as is.
func Cap(v string, max int) string {
if max == 0 {
return ""
}
if max < 0 || len(v) <= max {
return v
}
return fmt.Sprintf("%s…", v[:max-1])
}