2021-07-18 12:55:37 +00:00
|
|
|
/*
|
|
|
|
Package slice contains byte slice helpers.
|
|
|
|
*/
|
|
|
|
package slice
|
|
|
|
|
|
|
|
// CopyReverse returns a new byte slice containing reversed version of the
|
|
|
|
// original.
|
|
|
|
func CopyReverse(b []byte) []byte {
|
|
|
|
dest := make([]byte, len(b))
|
2021-07-18 13:08:23 +00:00
|
|
|
reverse(dest, b)
|
2021-07-18 12:55:37 +00:00
|
|
|
return dest
|
|
|
|
}
|
2021-07-18 13:08:23 +00:00
|
|
|
|
|
|
|
// Reverse does in-place reversing of byte slice.
|
|
|
|
func Reverse(b []byte) {
|
|
|
|
reverse(b, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func reverse(dst []byte, src []byte) {
|
|
|
|
for i, j := 0, len(src)-1; i <= j; i, j = i+1, j-1 {
|
|
|
|
dst[i], dst[j] = src[j], src[i]
|
|
|
|
}
|
|
|
|
}
|
2021-07-18 13:32:10 +00:00
|
|
|
|
|
|
|
// Copy copies the byte slice into new slice (make/copy).
|
|
|
|
func Copy(b []byte) []byte {
|
|
|
|
d := make([]byte, len(b))
|
|
|
|
copy(d, b)
|
|
|
|
return d
|
|
|
|
}
|
2022-09-01 19:05:34 +00:00
|
|
|
|
|
|
|
// Clean wipes the data in b by filling it with zeros.
|
|
|
|
func Clean(b []byte) {
|
|
|
|
for i := range b {
|
|
|
|
b[i] = 0
|
|
|
|
}
|
|
|
|
}
|