-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.go
More file actions
310 lines (275 loc) · 7.58 KB
/
string.go
File metadata and controls
310 lines (275 loc) · 7.58 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package agstring
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/mozillazg/go-unidecode"
"github.com/pkg/errors"
funk "github.com/thoas/go-funk"
)
var stripper = regexp.MustCompile(" +")
// ReplaceMultispace replaces multiple spaces with one space and
// also trims space from both ends
func ReplaceMultispace(s string) string {
return strings.TrimSpace(stripper.ReplaceAllString(s, " "))
}
// FirstStr returns the first element of given list or empty string when the list is empty.
func FirstStr(strings []string) string {
if len(strings) != 0 {
return strings[0]
}
return ""
}
// TrimSuffixes returns s without any of the provided trailing suffixes strings.
func TrimSuffixes(s string, suffixes ...string) string {
s = strings.TrimSpace(s)
for _, suffix := range suffixes {
if strings.HasSuffix(s, suffix) {
return strings.TrimSpace(strings.TrimSuffix(s, suffix))
}
}
return s
}
// TrimPrefixesAndSpace returns a string without any of the provided leading prefixes at word
// boundaries or spaces. See test for examples.
func TrimPrefixesAndSpace(s string, prefixes []string) string {
if prefixes == nil || s == "" {
return s
}
trimAgain := true
rePre := make([]*regexp.Regexp, len(prefixes))
for trimAgain {
trimAgain = false
for i, prefix := range prefixes {
if prefix == "" {
continue
}
if rePre[i] == nil {
reE := fmt.Sprintf("^\\s*%s\\b(?P<rest>.*)", prefix)
rePre[i] = regexp.MustCompile(reE)
}
if matches, ok := RegexpGroups(rePre[i], strings.TrimSpace(s)); ok {
s = matches["rest"]
trimAgain = true
}
}
}
return strings.TrimSpace(s)
}
var nonAlphanumRegexp = regexp.MustCompile("[^[:alnum:]]")
// RemoveNonAlnum removes non-alphanumeric characters from string
func RemoveNonAlnum(s string) string {
return nonAlphanumRegexp.ReplaceAllLiteralString(s, "")
}
// ContainsAll checks if given slice contains all searched strings
func ContainsAll(holder, searched []string) bool {
for _, s := range searched {
if !funk.ContainsString(holder, s) {
return false
}
}
return true
}
// StringContainsAll checks if given string contains all searched strings
func StringContainsAll(holder string, searched []string) bool {
for _, s := range searched {
if !strings.Contains(holder, s) {
return false
}
}
return true
}
// ContainsAny checks if source slice contains any of given strings
func ContainsAny(src, qs []string) bool {
for _, q := range qs {
if funk.ContainsString(src, q) {
return true
}
}
return false
}
// StringContainsAny is similar to ContainsAny but source is a string
func StringContainsAny(s string, ls []string) bool {
for _, e := range ls {
if strings.Contains(s, e) {
return true
}
}
return false
}
// MatchesPrefixes checks if given string has a prefix from given prefix list
func MatchesPrefixes(s string, prefixes []string) bool {
for _, prefix := range prefixes {
if strings.HasPrefix(s, prefix) {
return true
}
}
return false
}
// RemoveDiacritics removes diacritics from a string. If non-alphanumeric character is
// encountered diacritics are removed from it. If removing diacritics is not possible, character
// is removed.
func RemoveDiacritics(s string) string { return unidecode.Unidecode(s) }
// Normalize tries to remove the diacritics, removes remaining non-alphanumeric characters and
// then changes case to lower
func Normalize(s string) string {
s = RemoveDiacritics(s)
s = RemoveNonAlnum(s)
return strings.ToLower(s)
}
// EmptyIf returns empty string if given string equals to one
// of the strings in empty list. Otherwise, given string is returned as it is.
func EmptyIf(s string, emptyList ...string) string {
return ConvertIf(s, "", emptyList...)
}
// ConvertIf returns converted string if given string is one of the strings in the list
func ConvertIf(val, converted string, list ...string) string {
for _, t := range list {
if val == t {
return converted
}
}
return val
}
// ValueIfExists returns value from map for given key if exists, else returns the given key
func ValueIfExists(k string, m map[string]string) string {
v, ok := m[k]
if ok {
return v
}
return k
}
// ReplaceWholeWord replaces old into new only if old occurs as a whole word.
func ReplaceWholeWord(s, old, replacement string) string {
s = " " + s + " "
old = " " + old + " "
replacement = " " + replacement + " "
s = strings.Replace(s, old, replacement, -1)
return s[1 : len(s)-1]
}
// StringIterator provides a generator of names / strings
type StringIterator interface {
Get() string
HasNext() bool
}
// TrimSpace trims spaces in the given slice
func TrimSpace(ls []string) []string {
for i := range ls {
ls[i] = strings.TrimSpace(ls[i])
}
return ls
}
// ToLower makes lowercase strings in the given slice
func ToLower(ls []string) []string {
for i := range ls {
ls[i] = strings.ToLower(ls[i])
}
return ls
}
// Title ensures title formatting for given string
func Title(s string) string { return strings.Title(strings.ToLower(s)) }
// HasPrefix checks string has any one of given prefixes
func HasPrefix(s string, prefixes ...string) bool {
for _, prefix := range prefixes {
if strings.HasPrefix(s, prefix) {
return true
}
}
return false
}
// NonEmpty filters nonempty strings from given slice
func NonEmpty(ls []string) []string {
var nonempty []string
for _, s := range ls {
if s != "" {
nonempty = append(nonempty, s)
}
}
return nonempty
}
// IsEmpty checks if slice contains only empty strings
func IsEmpty(ls []string) bool {
for _, s := range ls {
if s != "" {
return false
}
}
return true
}
// RemoveAllDiacritics removes diacritics from all strings in slice
func RemoveAllDiacritics(ls []string) []string {
res := make([]string, len(ls))
for i, s := range ls {
res[i] = RemoveDiacritics(s)
}
return res
}
// SafeAtoi converts string, including empty string, to int
func SafeAtoi(s string) (int, error) {
if s == "" {
return 0, nil
}
n, err := strconv.Atoi(s)
return n, errors.Wrap(err, "can't convert to int")
}
// RegexpGroups checks if regex matches to given string
// If so, returns named groups with matches in a map
func RegexpGroups(exp *regexp.Regexp, input string) (map[string]string, bool) {
if !exp.MatchString(input) {
return nil, false
}
match := exp.FindStringSubmatch(input)
result := make(map[string]string)
for i, name := range exp.SubexpNames() {
if i != 0 && name != "" {
result[name] = match[i]
}
}
return result, true
}
// TakeTo truncates each string in the input slice up to `n` characters.
func TakeTo(ls []string, n int) []string {
out := make([]string, 0, len(ls))
for _, s := range ls {
rs := []rune(s)
o := string(rs[:min(len(rs), n)])
out = append(out, o)
}
return out
}
// TakeFrom removes the first `n` characters from each string in the input slice
func TakeFrom(ls []string, n int) []string {
out := make([]string, 0, len(ls))
for _, s := range ls {
rs := []rune(s)
o := string(rs[min(len(rs), n):])
out = append(out, o)
}
return out
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
// ReplaceDayOrdinal replaces day ordinals (`st`, `nd`, `rd`, `th`)
// Default replaces with empty string.
func ReplaceDayOrdinal(s string, replacements ...string) string {
var rep string
if len(replacements) > 0 {
rep = replacements[0]
}
ordinal := strings.NewReplacer("st", rep, "nd", rep, "th", rep, "rd", rep)
return ordinal.Replace(s)
}
// ReplaceNewline replaces the newline character `\n`
// Default replaces with empty string.
func ReplaceNewline(s string, replacements ...string) string {
var rep string
if len(replacements) > 0 {
rep = replacements[0]
}
return strings.Replace(s, "\n", rep, -1)
}