Debian Codegraph @

strings/slices/slices.go

raw
1 /*
2 Copyright 2021 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 // Package slices defines various functions useful with slices of string type.
18 // The goal is to be as close as possible to
19 // https://github.com/golang/go/issues/45955. Ideal would be if we can just
20 // replace "stringslices" if the "slices" package becomes standard.
21 package slices
22
23 import goslices "slices"
24
25 // Equal reports whether two slices are equal: the same length and all
26 // elements equal. If the lengths are different, Equal returns false.
27 // Otherwise, the elements are compared in index order, and the
28 // comparison stops at the first unequal pair.
29 //
30 // Deprecated: use stdlib slices.Equal instead.
31 var Equal = goslices.Equal[[]string]
32
33 // Filter appends to d each element e of s for which keep(e) returns true.
34 // It returns the modified d. d may be s[:0], in which case the kept
35 // elements will be stored in the same slice.
36 // if the slices overlap in some other way, the results are unspecified.
37 // To create a new slice with the filtered results, pass nil for d.
38 //
39 // Deprecated: use stdlib slices.DeleteFunc instead.
40 func Filter(d, s []string, keep func(string) bool) []string {
41 for _, n := range s {
42 if keep(n) {
43 d = append(d, n)
44 }
45 }
46 return d
47 }
48
49 // Contains reports whether v is present in s.
50 //
51 // Deprecated: use stdlib slices.Contains instead.
52 var Contains = goslices.Contains[[]string]
53
54 // Index returns the index of the first occurrence of v in s, or -1 if
55 // not present.
56 //
57 // Deprecated: use stdlib slices.Index instead.
58 var Index = goslices.Index[[]string]
59
60 // Clone returns a new clone of s.
61 //
62 // Deprecated: use stdlib slices.Clone instead.
63 var Clone = goslices.Clone[[]string]