slice.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Copyright 2015 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 slice provides utility methods for common operations on slices.
  14. package slice
  15. import (
  16. "sort"
  17. utilrand "k8s.io/apimachinery/pkg/util/rand"
  18. )
  19. // CopyStrings copies the contents of the specified string slice
  20. // into a new slice.
  21. func CopyStrings(s []string) []string {
  22. if s == nil {
  23. return nil
  24. }
  25. c := make([]string, len(s))
  26. copy(c, s)
  27. return c
  28. }
  29. // SortStrings sorts the specified string slice in place. It returns the same
  30. // slice that was provided in order to facilitate method chaining.
  31. func SortStrings(s []string) []string {
  32. sort.Strings(s)
  33. return s
  34. }
  35. // ShuffleStrings copies strings from the specified slice into a copy in random
  36. // order. It returns a new slice.
  37. func ShuffleStrings(s []string) []string {
  38. if s == nil {
  39. return nil
  40. }
  41. shuffled := make([]string, len(s))
  42. perm := utilrand.Perm(len(s))
  43. for i, j := range perm {
  44. shuffled[j] = s[i]
  45. }
  46. return shuffled
  47. }
  48. // ContainsString checks if a given slice of strings contains the provided string.
  49. // If a modifier func is provided, it is called with the slice item before the comparation.
  50. func ContainsString(slice []string, s string, modifier func(s string) string) bool {
  51. for _, item := range slice {
  52. if item == s {
  53. return true
  54. }
  55. if modifier != nil && modifier(item) == s {
  56. return true
  57. }
  58. }
  59. return false
  60. }
  61. // RemoveString returns a newly created []string that contains all items from slice that
  62. // are not equal to s and modifier(s) in case modifier func is provided.
  63. func RemoveString(slice []string, s string, modifier func(s string) string) []string {
  64. newSlice := make([]string, 0)
  65. for _, item := range slice {
  66. if item == s {
  67. continue
  68. }
  69. if modifier != nil && modifier(item) == s {
  70. continue
  71. }
  72. newSlice = append(newSlice, item)
  73. }
  74. if len(newSlice) == 0 {
  75. // Sanitize for unit tests so we don't need to distinguish empty array
  76. // and nil.
  77. newSlice = nil
  78. }
  79. return newSlice
  80. }