Debian Codegraph @

internal/slices/slices.go

raw
1 // Copyright The Notary Project Authors.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package slices
15
16 // Contains reports whether v is present in s.
17 func Contains[E comparable](s []E, v E) bool {
18 for _, vs := range s {
19 if v == vs {
20 return true
21 }
22 }
23 return false
24 }
25
26 // ContainsAny reports whether v is present in s
27 func ContainsAny(s []any, v any) bool {
28 for _, vs := range s {
29 if vs == v {
30 return true
31 }
32 }
33 return false
34 }
35
36 // Delete removes element at index i from slice s and
37 // returns the modified slice.
38 func Delete[T any](s []T, i int) []T {
39 return append(s[:i], s[i+1:]...)
40 }
41
42 type isser interface {
43 Is(string) bool
44 }
45
46 // IndexIsser returns the index of the first occurrence of name in s,
47 // or -1 if not present.
48 func IndexIsser[E isser](s []E, name string) int {
49 for i, v := range s {
50 if v.Is(name) {
51 return i
52 }
53 }
54 return -1
55 }
56
57 // ContainsIsser reports whether name is present in s.
58 func ContainsIsser[E isser](s []E, name string) bool {
59 return IndexIsser(s, name) >= 0
60 }