Debian Codegraph @

api/internal/utils/stringslice.go

raw
1 // Copyright 2020 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package utils
5
6 // StringSliceIndex returns the index of the str, else -1.
7 func StringSliceIndex(slice []string, str string) int {
8 for i := range slice {
9 if slice[i] == str {
10 return i
11 }
12 }
13 return -1
14 }
15
16 // StringSliceContains returns true if the slice has the string.
17 func StringSliceContains(slice []string, str string) bool {
18 for _, s := range slice {
19 if s == str {
20 return true
21 }
22 }
23 return false
24 }
25
26 // SameEndingSubSlice returns true if the slices end the same way, e.g.
27 // {"a", "b", "c"}, {"b", "c"} => true
28 // {"a", "b", "c"}, {"a", "b"} => false
29 // If one slice is empty and the other is not, return false.
30 func SameEndingSubSlice(shortest, longest []string) bool {
31 if len(shortest) > len(longest) {
32 longest, shortest = shortest, longest
33 }
34 diff := len(longest) - len(shortest)
35 if len(shortest) == 0 {
36 return diff == 0
37 }
38 for i := len(shortest) - 1; i >= 0; i-- {
39 if longest[i+diff] != shortest[i] {
40 return false
41 }
42 }
43 return true
44 }