github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/cmn/cos/slice.go (about) 1 // Package cos provides common low-level types and utilities for all aistore projects 2 /* 3 * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. 4 */ 5 package cos 6 7 import "strings" 8 9 func StringInSlice(s string, arr []string) bool { 10 for _, el := range arr { 11 if el == s { 12 return true 13 } 14 } 15 return false 16 } 17 18 // StrSlicesEqual compares content of two string slices. It is replacement for 19 // reflect.DeepEqual because the latter returns false if slices have the same 20 // values but in different order. 21 func StrSlicesEqual(lhs, rhs []string) bool { 22 if len(lhs) == 0 && len(rhs) == 0 { 23 return true 24 } 25 if len(lhs) != len(rhs) { 26 return false 27 } 28 total := make(map[string]bool, len(lhs)) 29 for _, item := range lhs { 30 total[item] = true 31 } 32 for _, item := range rhs { 33 if _, ok := total[item]; ok { 34 delete(total, item) 35 continue 36 } 37 total[item] = true 38 } 39 return len(total) == 0 40 } 41 42 func AnyHasPrefixInSlice(prefix string, arr []string) bool { 43 for _, el := range arr { 44 if strings.HasPrefix(el, prefix) { 45 return true 46 } 47 } 48 return false 49 }