github.com/safing/portbase@v0.19.5/utils/slices.go (about) 1 package utils 2 3 // IndexOfString returns the index of given string and -1 if its not part of the slice. 4 func IndexOfString(a []string, s string) int { 5 for i, entry := range a { 6 if entry == s { 7 return i 8 } 9 } 10 return -1 11 } 12 13 // StringInSlice returns whether the given string is in the string slice. 14 func StringInSlice(a []string, s string) bool { 15 return IndexOfString(a, s) >= 0 16 } 17 18 // RemoveFromStringSlice removes the given string from the slice and returns a new slice. 19 func RemoveFromStringSlice(a []string, s string) []string { 20 i := IndexOfString(a, s) 21 if i > 0 { 22 a = append(a[:i], a[i+1:]...) 23 } 24 return a 25 } 26 27 // DuplicateStrings returns a new copy of the given string slice. 28 func DuplicateStrings(a []string) []string { 29 b := make([]string, len(a)) 30 copy(b, a) 31 return b 32 } 33 34 // StringSliceEqual returns whether the given string slices are equal. 35 func StringSliceEqual(a []string, b []string) bool { 36 if len(a) != len(b) { 37 return false 38 } 39 for i, v := range a { 40 if v != b[i] { 41 return false 42 } 43 } 44 return true 45 } 46 47 // DuplicateBytes returns a new copy of the given byte slice. 48 func DuplicateBytes(a []byte) []byte { 49 b := make([]byte, len(a)) 50 copy(b, a) 51 return b 52 }