forked from NVIDIA/aistore
/
slice.go
49 lines (45 loc) · 1.04 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
// Package cos provides common low-level types and utilities for all aistore projects
/*
* Copyright (c) 2018-2021, NVIDIA CORPORATION. All rights reserved.
*/
package cos
import "strings"
func StringInSlice(s string, arr []string) bool {
for _, el := range arr {
if el == s {
return true
}
}
return false
}
// StrSlicesEqual compares content of two string slices. It is replacement for
// reflect.DeepEqual because the latter returns false if slices have the same
// values but in different order.
func StrSlicesEqual(lhs, rhs []string) bool {
if len(lhs) == 0 && len(rhs) == 0 {
return true
}
if len(lhs) != len(rhs) {
return false
}
total := make(map[string]bool, len(lhs))
for _, item := range lhs {
total[item] = true
}
for _, item := range rhs {
if _, ok := total[item]; ok {
delete(total, item)
continue
}
total[item] = true
}
return len(total) == 0
}
func AnyHasPrefixInSlice(prefix string, arr []string) bool {
for _, el := range arr {
if strings.HasPrefix(el, prefix) {
return true
}
}
return false
}