mirror of
https://github.com/gravitational/teleport.git
synced 2026-11-02 23:31:08 +08:00
108 lines
2.4 KiB
Go
108 lines
2.4 KiB
Go
/*
|
|
Copyright 2021 Gravitational, Inc.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package utils
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
// CopyByteSlice returns a copy of the byte slice.
|
|
func CopyByteSlice(in []byte) []byte {
|
|
if in == nil {
|
|
return nil
|
|
}
|
|
out := make([]byte, len(in))
|
|
copy(out, in)
|
|
return out
|
|
}
|
|
|
|
// CopyByteSlices returns a copy of the byte slices.
|
|
func CopyByteSlices(in [][]byte) [][]byte {
|
|
if in == nil {
|
|
return nil
|
|
}
|
|
out := make([][]byte, len(in))
|
|
for i := range in {
|
|
out[i] = CopyByteSlice(in[i])
|
|
}
|
|
return out
|
|
}
|
|
|
|
// StringSlicesEqual returns true if string slices equal
|
|
func StringSlicesEqual(a, b []string) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
for i := range a {
|
|
if a[i] != b[i] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// SliceContainsStr returns 'true' if the slice contains the given value
|
|
func SliceContainsStr[T ~string](slice []T, value T) bool {
|
|
for i := range slice {
|
|
if slice[i] == value {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// JoinStrings returns a string that is all the elements in the slice `T[]` joined by `sep`
|
|
// This being generic allows for the usage of custom string times, without having to convert
|
|
// the elements to a string to be passed into `strings.Join`.
|
|
func JoinStrings[T ~string](elems []T, sep string) T {
|
|
switch len(elems) {
|
|
case 0:
|
|
return ""
|
|
case 1:
|
|
return elems[0]
|
|
}
|
|
n := len(sep) * (len(elems) - 1)
|
|
for i := 0; i < len(elems); i++ {
|
|
n += len(elems[i])
|
|
}
|
|
|
|
var b strings.Builder
|
|
b.Grow(n)
|
|
b.WriteString(string(elems[0]))
|
|
for _, s := range elems[1:] {
|
|
b.WriteString(sep)
|
|
b.WriteString(string(s))
|
|
}
|
|
return T(b.String())
|
|
}
|
|
|
|
// Deduplicate deduplicates list of strings
|
|
func Deduplicate(in []string) []string {
|
|
if len(in) == 0 {
|
|
return in
|
|
}
|
|
out := make([]string, 0, len(in))
|
|
seen := make(map[string]bool, len(in))
|
|
for _, val := range in {
|
|
if _, ok := seen[val]; !ok {
|
|
out = append(out, val)
|
|
seen[val] = true
|
|
}
|
|
}
|
|
return out
|
|
}
|