slices.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Copyright 2021 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package slices defines various functions useful with slices of string type.
  14. // The goal is to be as close as possible to
  15. // https://github.com/golang/go/issues/45955. Ideal would be if we can just
  16. // replace "stringslices" if the "slices" package becomes standard.
  17. package slices
  18. // Equal reports whether two slices are equal: the same length and all
  19. // elements equal. If the lengths are different, Equal returns false.
  20. // Otherwise, the elements are compared in index order, and the
  21. // comparison stops at the first unequal pair.
  22. func Equal(s1, s2 []string) bool {
  23. if len(s1) != len(s2) {
  24. return false
  25. }
  26. for i, n := range s1 {
  27. if n != s2[i] {
  28. return false
  29. }
  30. }
  31. return true
  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. func Filter(d, s []string, keep func(string) bool) []string {
  39. for _, n := range s {
  40. if keep(n) {
  41. d = append(d, n)
  42. }
  43. }
  44. return d
  45. }
  46. // Contains reports whether v is present in s.
  47. func Contains(s []string, v string) bool {
  48. return Index(s, v) >= 0
  49. }
  50. // Index returns the index of the first occurrence of v in s, or -1 if
  51. // not present.
  52. func Index(s []string, v string) int {
  53. // "Contains" may be replaced with "Index(s, v) >= 0":
  54. // https://github.com/golang/go/issues/45955#issuecomment-873377947
  55. for i, n := range s {
  56. if n == v {
  57. return i
  58. }
  59. }
  60. return -1
  61. }
  62. // Functions below are not in https://github.com/golang/go/issues/45955
  63. // Clone returns a new clone of s.
  64. func Clone(s []string) []string {
  65. // https://github.com/go101/go101/wiki/There-is-not-a-perfect-way-to-clone-slices-in-Go
  66. if s == nil {
  67. return nil
  68. }
  69. c := make([]string, len(s))
  70. copy(c, s)
  71. return c
  72. }