/
slices.go
52 lines (46 loc) · 1.16 KB
/
slices.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
package utils
// IndexOfString returns the index of given string and -1 if its not part of the slice.
func IndexOfString(a []string, s string) int {
for i, entry := range a {
if entry == s {
return i
}
}
return -1
}
// StringInSlice returns whether the given string is in the string slice.
func StringInSlice(a []string, s string) bool {
return IndexOfString(a, s) >= 0
}
// RemoveFromStringSlice removes the given string from the slice and returns a new slice.
func RemoveFromStringSlice(a []string, s string) []string {
i := IndexOfString(a, s)
if i > 0 {
a = append(a[:i], a[i+1:]...)
}
return a
}
// DuplicateStrings returns a new copy of the given string slice.
func DuplicateStrings(a []string) []string {
b := make([]string, len(a))
copy(b, a)
return b
}
// StringSliceEqual returns whether the given string slices are equal.
func StringSliceEqual(a []string, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
// DuplicateBytes returns a new copy of the given byte slice.
func DuplicateBytes(a []byte) []byte {
b := make([]byte, len(a))
copy(b, a)
return b
}