Skip to content

Commit 01e27dd

Browse files
committed
Deprecate strings/slices functions covered by stdlib slices
As suggested by Tim in [1], deprecate the functions in the k8s.io/utils/strings/slices package that are already covered by generic functions in the Go standard library slices package. Also reimplement them in terms of slices functions. [1] kubernetes/kubernetes#126715 (review)
1 parent 18e509b commit 01e27dd

File tree

1 file changed

+10
-35
lines changed

1 file changed

+10
-35
lines changed

strings/slices/slices.go

+10-35
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,14 @@ limitations under the License.
2020
// replace "stringslices" if the "slices" package becomes standard.
2121
package slices
2222

23+
import "slices"
24+
2325
// Equal reports whether two slices are equal: the same length and all
2426
// elements equal. If the lengths are different, Equal returns false.
2527
// Otherwise, the elements are compared in index order, and the
2628
// comparison stops at the first unequal pair.
27-
func Equal(s1, s2 []string) bool {
28-
if len(s1) != len(s2) {
29-
return false
30-
}
31-
for i, n := range s1 {
32-
if n != s2[i] {
33-
return false
34-
}
35-
}
36-
return true
37-
}
29+
// Deprecated: use [slices.Equal] instead.
30+
var Equal = slices.Equal[[]string, string]
3831

3932
// Filter appends to d each element e of s for which keep(e) returns true.
4033
// It returns the modified d. d may be s[:0], in which case the kept
@@ -51,32 +44,14 @@ func Filter(d, s []string, keep func(string) bool) []string {
5144
}
5245

5346
// Contains reports whether v is present in s.
54-
func Contains(s []string, v string) bool {
55-
return Index(s, v) >= 0
56-
}
47+
// Deprecated: use [slices.Contains] instead.
48+
var Contains = slices.Contains[[]string, string]
5749

5850
// Index returns the index of the first occurrence of v in s, or -1 if
5951
// not present.
60-
func Index(s []string, v string) int {
61-
// "Contains" may be replaced with "Index(s, v) >= 0":
62-
// https://github.com/golang/go/issues/45955#issuecomment-873377947
63-
for i, n := range s {
64-
if n == v {
65-
return i
66-
}
67-
}
68-
return -1
69-
}
70-
71-
// Functions below are not in https://github.com/golang/go/issues/45955
52+
// Deprecated: use [slices.Index] instead.
53+
var Index = slices.Index[[]string, string]
7254

7355
// Clone returns a new clone of s.
74-
func Clone(s []string) []string {
75-
// https://github.com/go101/go101/wiki/There-is-not-a-perfect-way-to-clone-slices-in-Go
76-
if s == nil {
77-
return nil
78-
}
79-
c := make([]string, len(s))
80-
copy(c, s)
81-
return c
82-
}
56+
// Deprecated: use [slices.Clone] instead.
57+
var Clone = slices.Clone[[]string, string]

0 commit comments

Comments
 (0)