...

Source file src/k8s.io/utils/strings/slices/slices.go

Documentation: k8s.io/utils/strings/slices

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package slices defines various functions useful with slices of string type.
    18  // The goal is to be as close as possible to
    19  // https://github.com/golang/go/issues/45955. Ideal would be if we can just
    20  // replace "stringslices" if the "slices" package becomes standard.
    21  package slices
    22  
    23  // Equal reports whether two slices are equal: the same length and all
    24  // elements equal. If the lengths are different, Equal returns false.
    25  // Otherwise, the elements are compared in index order, and the
    26  // 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  }
    38  
    39  // Filter appends to d each element e of s for which keep(e) returns true.
    40  // It returns the modified d. d may be s[:0], in which case the kept
    41  // elements will be stored in the same slice.
    42  // if the slices overlap in some other way, the results are unspecified.
    43  // To create a new slice with the filtered results, pass nil for d.
    44  func Filter(d, s []string, keep func(string) bool) []string {
    45  	for _, n := range s {
    46  		if keep(n) {
    47  			d = append(d, n)
    48  		}
    49  	}
    50  	return d
    51  }
    52  
    53  // Contains reports whether v is present in s.
    54  func Contains(s []string, v string) bool {
    55  	return Index(s, v) >= 0
    56  }
    57  
    58  // Index returns the index of the first occurrence of v in s, or -1 if
    59  // 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
    72  
    73  // 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  }
    83  

View as plain text