mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-28 14:55:23 +01:00
34 lines
854 B
Go
34 lines
854 B
Go
package bytesutil
|
|
|
|
import (
|
|
"reflect"
|
|
"unsafe"
|
|
)
|
|
|
|
// Resize resizes b to n bytes and returns b (which may be newly allocated).
|
|
func Resize(b []byte, n int) []byte {
|
|
if nn := n - cap(b); nn > 0 {
|
|
b = append(b[:cap(b)], make([]byte, nn)...)
|
|
}
|
|
return b[:n]
|
|
}
|
|
|
|
// ToUnsafeString converts b to string without memory allocations.
|
|
//
|
|
// The returned string is valid only until b is reachable and unmodified.
|
|
func ToUnsafeString(b []byte) string {
|
|
return *(*string)(unsafe.Pointer(&b))
|
|
}
|
|
|
|
// ToUnsafeBytes converts s to a byte slice without memory allocations.
|
|
//
|
|
// The returned byte slice is valid only until s is reachable and unmodified.
|
|
func ToUnsafeBytes(s string) (b []byte) {
|
|
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
|
slh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
|
slh.Data = sh.Data
|
|
slh.Len = sh.Len
|
|
slh.Cap = sh.Len
|
|
return b
|
|
}
|