2019-05-22 21:16:55 +00:00
|
|
|
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.
|
2020-08-05 08:53:41 +00:00
|
|
|
func ToUnsafeBytes(s string) (b []byte) {
|
2019-05-22 21:16:55 +00:00
|
|
|
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
2020-08-05 08:53:41 +00:00
|
|
|
slh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
2019-05-22 21:16:55 +00:00
|
|
|
slh.Data = sh.Data
|
|
|
|
slh.Len = sh.Len
|
|
|
|
slh.Cap = sh.Len
|
2020-06-02 21:23:20 +00:00
|
|
|
return b
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|