-
Notifications
You must be signed in to change notification settings - Fork 438
/
slice.go
91 lines (79 loc) · 1.61 KB
/
slice.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
package util
func DiffStringSlice(slice1, slice2 []string) []string {
var diff []string
// Loop two times, first to find slice1 strings not in slice2,
// second loop to find slice2 strings not in slice1
for i := 0; i < 2; i++ {
for _, s1 := range slice1 {
found := false
for _, s2 := range slice2 {
if s1 == s2 {
found = true
break
}
}
// String not found. We add it to return slice
if !found {
diff = append(diff, s1)
}
}
// Swap the slices, only if it was the first loop
if i == 0 {
slice1, slice2 = slice2, slice1
}
}
return diff
}
// UniqString creates an array of string with unique values.
func UniqString(a []string) []string {
length := len(a)
seen := make(map[string]struct{}, length)
j := 0
for i := 0; i < length; i++ {
v := a[i]
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
a[j] = v
j++
}
return a[0:j]
}
// IsStringsOverlap check if two string slices are overlapped
func IsStringsOverlap(a, b []string) bool {
for _, sa := range a {
for _, sb := range b {
if sa == sb {
return true
}
}
}
return false
}
func IsStringIn(str string, slice []string) bool {
for _, s := range slice {
if s == str {
return true
}
}
return false
}
// Helper functions to check and remove string from a slice of strings.
func ContainsString(slice []string, s string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}
func RemoveString(slice []string, s string) (result []string) {
for _, item := range slice {
if item == s {
continue
}
result = append(result, item)
}
return
}