Skip to content

Commit 383b50a

Browse files
authored
Merge pull request #340 from skitt/delegate-to-go-slices
Delegate strings/slices functions to stdlib
2 parents 9d40a56 + 5814405 commit 383b50a

1 file changed

Lines changed: 16 additions & 35 deletions

File tree

strings/slices/slices.go

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,23 @@ limitations under the License.
2020
// replace "stringslices" if the "slices" package becomes standard.
2121
package slices
2222

23+
import goslices "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+
//
30+
// Deprecated: use stdlib slices.Equal instead.
31+
var Equal = goslices.Equal[[]string]
3832

3933
// Filter appends to d each element e of s for which keep(e) returns true.
4034
// It returns the modified d. d may be s[:0], in which case the kept
4135
// elements will be stored in the same slice.
4236
// if the slices overlap in some other way, the results are unspecified.
4337
// To create a new slice with the filtered results, pass nil for d.
38+
//
39+
// Deprecated: use stdlib slices.DeleteFunc instead.
4440
func Filter(d, s []string, keep func(string) bool) []string {
4541
for _, n := range s {
4642
if keep(n) {
@@ -51,32 +47,17 @@ func Filter(d, s []string, keep func(string) bool) []string {
5147
}
5248

5349
// Contains reports whether v is present in s.
54-
func Contains(s []string, v string) bool {
55-
return Index(s, v) >= 0
56-
}
50+
//
51+
// Deprecated: use stdlib slices.Contains instead.
52+
var Contains = goslices.Contains[[]string]
5753

5854
// Index returns the index of the first occurrence of v in s, or -1 if
5955
// 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
56+
//
57+
// Deprecated: use stdlib slices.Index instead.
58+
var Index = goslices.Index[[]string]
7259

7360
// 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-
}
61+
//
62+
// Deprecated: use stdlib slices.Clone instead.
63+
var Clone = goslices.Clone[[]string]

0 commit comments

Comments
 (0)