Skip to content

Commit

Permalink
Merge pull request #315 from tklauser/deprecated-strings-slices
Browse files Browse the repository at this point in the history
Deprecate strings/slices functions covered by stdlib slices
  • Loading branch information
k8s-ci-robot committed Aug 21, 2024
2 parents 18e509b + 4965b0f commit f90d014
Showing 1 changed file with 10 additions and 35 deletions.
45 changes: 10 additions & 35 deletions strings/slices/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,14 @@ limitations under the License.
// replace "stringslices" if the "slices" package becomes standard.
package slices

import goslices "slices"

Check failure on line 23 in strings/slices/slices.go

View workflow job for this annotation

GitHub Actions / build (1.20.x, windows-latest)

package slices is not in GOROOT (C:\hostedtoolcache\windows\go\1.20.14\x64\src\slices)

Check failure on line 23 in strings/slices/slices.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.14/x64/src/slices)

Check failure on line 23 in strings/slices/slices.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, macos-latest)

package slices is not in GOROOT (/Users/runner/hostedtoolcache/go/1.20.14/arm64/src/slices)

// Equal reports whether two slices are equal: the same length and all
// elements equal. If the lengths are different, Equal returns false.
// Otherwise, the elements are compared in index order, and the
// comparison stops at the first unequal pair.
func Equal(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
}
for i, n := range s1 {
if n != s2[i] {
return false
}
}
return true
}
// Deprecated: use [slices.Equal] instead.
var Equal = goslices.Equal[[]string, string]

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

// Contains reports whether v is present in s.
func Contains(s []string, v string) bool {
return Index(s, v) >= 0
}
// Deprecated: use [slices.Contains] instead.
var Contains = goslices.Contains[[]string, string]

// Index returns the index of the first occurrence of v in s, or -1 if
// not present.
func Index(s []string, v string) int {
// "Contains" may be replaced with "Index(s, v) >= 0":
// https://github.com/golang/go/issues/45955#issuecomment-873377947
for i, n := range s {
if n == v {
return i
}
}
return -1
}

// Functions below are not in https://github.com/golang/go/issues/45955
// Deprecated: use [slices.Index] instead.
var Index = goslices.Index[[]string, string]

// Clone returns a new clone of s.
func Clone(s []string) []string {
// https://github.com/go101/go101/wiki/There-is-not-a-perfect-way-to-clone-slices-in-Go
if s == nil {
return nil
}
c := make([]string, len(s))
copy(c, s)
return c
}
// Deprecated: use [slices.Clone] instead.
var Clone = goslices.Clone[[]string, string]

0 comments on commit f90d014

Please sign in to comment.