Repository navigation
Expand file tree
/
Copy pathstringutils.go
More file actions
366 lines (323 loc) · 8.12 KB
/
Copy pathstringutils.go
File metadata and controls
366 lines (323 loc) · 8.12 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Package stringutils provides utilities for working with strings.
package stringutils
import (
"crypto/rand"
"fmt"
"math/big"
"strings"
)
// Contains string in slice
func Contains(src string, inSlice []string) bool {
for _, a := range inSlice {
if a == src {
return true
}
}
return false
}
// ContainsAnySubstring checks if string contains any of provided substring
func ContainsAnySubstring(s string, subStrings []string) bool {
for _, mx := range subStrings {
if mx == "" {
continue // skip empty substrings
}
if strings.Contains(s, mx) {
return true
}
}
return false
}
// DeDup remove duplicates from slice.
// This function is stable - it preserves the order of first occurrences.
func DeDup(keys []string) []string {
if len(keys) == 0 {
return nil
}
result := make([]string, 0, len(keys))
visited := make(map[string]struct{}, len(keys))
for _, k := range keys {
if _, found := visited[k]; !found {
visited[k] = struct{}{}
result = append(result, k)
}
}
return result
}
// DeDupBig remove duplicates from slice.
//
// Deprecated: Use DeDup instead. This function now just calls DeDup for backwards compatibility.
func DeDupBig(keys []string) []string {
return DeDup(keys)
}
// SliceToString converts slice of any to slice of string
func SliceToString(s []any) []string {
if len(s) == 0 {
return nil
}
strSlice := make([]string, len(s))
for i, v := range s {
if vb, ok := v.([]byte); ok {
strSlice[i] = string(vb) // safe conversion
continue
}
strSlice[i] = fmt.Sprintf("%v", v)
}
return strSlice
}
// HasCommonElement checks if any element of the second slice is in the first slice
func HasCommonElement(a, b []string) bool {
if len(a) == 0 || len(b) == 0 {
return false
}
// build set from smaller slice for better performance
if len(a) > len(b) {
a, b = b, a
}
set := make(map[string]struct{}, len(a))
for _, x := range a {
set[x] = struct{}{}
}
for _, y := range b {
if _, ok := set[y]; ok {
return true
}
}
return false
}
// HasPrefixSlice checks if any string in the slice starts with the given prefix
func HasPrefixSlice(prefix string, slice []string) bool {
for _, v := range slice {
if strings.HasPrefix(v, prefix) {
return true
}
}
return false
}
// HasSuffixSlice checks if any string in the slice ends with the given suffix
func HasSuffixSlice(suffix string, slice []string) bool {
for _, v := range slice {
if strings.HasSuffix(v, suffix) {
return true
}
}
return false
}
// Truncate cuts string to the given length (in runes) and adds ellipsis if it was truncated
// if maxLen is less than 4 (3 chars for ellipsis + 1 rune from string), returns empty string
func Truncate(s string, maxLen int) string {
if maxLen < 4 {
return ""
}
runes := []rune(s)
if len(runes) <= maxLen {
return s
}
return string(runes[:maxLen-3]) + "..."
}
// TruncateWords cuts string to the given number of words and adds ellipsis if it was truncated
// returns empty string if maxWords is 0
func TruncateWords(s string, maxWords int) string {
if maxWords == 0 {
return ""
}
words := strings.Fields(s)
if len(words) <= maxWords {
return s
}
return strings.Join(words[:maxWords], " ") + "..."
}
// RandomWord generates pronounceable random word with length between minLen and maxLen
func RandomWord(minLen, maxLen int) string {
if minLen < 2 {
minLen = 2
}
if maxLen < minLen {
maxLen = minLen
}
vowels := []rune("aeiou")
consonants := []rune("bcdfghjklmnpqrstvwxyz")
// make a random length between min and max
n, err := rand.Int(rand.Reader, big.NewInt(int64(maxLen-minLen+1)))
length := minLen
if err == nil {
length += int(n.Int64())
}
var result strings.Builder
// decide to start with vowel or consonant
n, _ = rand.Int(rand.Reader, big.NewInt(2))
startWithVowel := n.Int64() == 0
for i := 0; i < length; i++ {
isVowel := (i%2 == 0) == startWithVowel
if isVowel {
n, _ = rand.Int(rand.Reader, big.NewInt(int64(len(vowels))))
result.WriteRune(vowels[n.Int64()])
} else {
n, _ = rand.Int(rand.Reader, big.NewInt(int64(len(consonants))))
result.WriteRune(consonants[n.Int64()])
}
}
return result.String()
}
// Filter returns a new slice containing only elements that match the predicate
func Filter(slice []string, predicate func(string) bool) []string {
if len(slice) == 0 || predicate == nil {
return nil
}
result := make([]string, 0, len(slice))
for _, s := range slice {
if predicate(s) {
result = append(result, s)
}
}
if len(result) == 0 {
return nil
}
return result
}
// Map applies transform function to each element and returns new slice
func Map(slice []string, transform func(string) string) []string {
if len(slice) == 0 || transform == nil {
return nil
}
result := make([]string, len(slice))
for i, s := range slice {
result[i] = transform(s)
}
return result
}
// Reverse returns a new slice with elements in reversed order
func Reverse(slice []string) []string {
if len(slice) == 0 {
return nil
}
result := make([]string, len(slice))
for i, j := 0, len(slice)-1; i <= j; i, j = i+1, j-1 {
result[i], result[j] = slice[j], slice[i]
}
return result
}
// IndexOf returns the index of the first occurrence of element in slice, or -1 if not found
func IndexOf(slice []string, element string) int {
for i, s := range slice {
if s == element {
return i
}
}
return -1
}
// LastIndexOf returns the index of the last occurrence of element in slice, or -1 if not found
func LastIndexOf(slice []string, element string) int {
for i := len(slice) - 1; i >= 0; i-- {
if slice[i] == element {
return i
}
}
return -1
}
// Difference returns elements that are in the first slice but not in the second
func Difference(a, b []string) []string {
if len(a) == 0 {
return nil
}
if len(b) == 0 {
return a
}
// build set from b for O(1) lookups
bSet := make(map[string]struct{}, len(b))
for _, s := range b {
bSet[s] = struct{}{}
}
result := make([]string, 0, len(a))
for _, s := range a {
if _, found := bSet[s]; !found {
result = append(result, s)
}
}
if len(result) == 0 {
return nil
}
return result
}
// Union combines multiple slices and removes duplicates, preserving order
func Union(slices ...[]string) []string {
if len(slices) == 0 {
return nil
}
// estimate capacity
totalLen := 0
for _, slice := range slices {
totalLen += len(slice)
}
if totalLen == 0 {
return nil
}
seen := make(map[string]struct{}, totalLen)
result := make([]string, 0, totalLen)
for _, slice := range slices {
for _, s := range slice {
if _, found := seen[s]; !found {
seen[s] = struct{}{}
result = append(result, s)
}
}
}
if len(result) == 0 {
return nil
}
return result
}
// Intersection returns elements that are present in both slices, preserving order from first slice
func Intersection(a, b []string) []string {
if len(a) == 0 || len(b) == 0 {
return nil
}
// build set from b for O(1) lookups
bSet := make(map[string]struct{}, len(b))
for _, s := range b {
bSet[s] = struct{}{}
}
result := make([]string, 0, len(a))
seen := make(map[string]struct{}, len(a))
for _, s := range a {
if _, inB := bSet[s]; inB {
if _, alreadyAdded := seen[s]; !alreadyAdded {
seen[s] = struct{}{}
result = append(result, s)
}
}
}
if len(result) == 0 {
return nil
}
return result
}
// NormalizeWhitespace replaces multiple whitespace characters with single space and trims
func NormalizeWhitespace(s string) string {
if s == "" {
return ""
}
// use Fields to split on any whitespace, then rejoin with single space
fields := strings.Fields(s)
if len(fields) == 0 {
return ""
}
return strings.Join(fields, " ")
}
// IsBlank returns true if string is empty or contains only whitespace
func IsBlank(s string) bool {
return strings.TrimSpace(s) == ""
}
// RemovePrefix removes the prefix from s if present, otherwise returns s unchanged
func RemovePrefix(s, prefix string) string {
if strings.HasPrefix(s, prefix) {
return s[len(prefix):]
}
return s
}
// RemoveSuffix removes the suffix from s if present, otherwise returns s unchanged
func RemoveSuffix(s, suffix string) string {
if strings.HasSuffix(s, suffix) {
return s[:len(s)-len(suffix)]
}
return s
}