qbolt/util.go

41 lines
862 B
Go

package main
import (
"strconv"
)
func getDisplayName(qba []byte) string {
if len(qba) ==0 {
return "<empty>"
}
ret := strconv.Quote(string(qba))
return ret[1 : len(ret)-1]
}
// ReverseSlice reverses a slice.
// @ref https://stackoverflow.com/a/28058324
func ReverseSlice[S ~[]E, E any](s S) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
// CopySliceAdd copies a slice and adds one more thing on the end.
func CopySliceAdd[T comparable](base []T, and T) []T {
ret := make([]T, len(base)+1)
copy(ret, base)
ret[len(ret)-1] = and
return ret
}
// Apply creates a new slice where every element of the input arr is transformed.
func Apply[T comparable](arr []T, transform func(T) T) []T {
ret := make([]T, len(arr))
for i := 0; i < len(arr); i++ {
ret[i] = transform(arr[i])
}
return ret
}