forked from kelindar/binary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.go
33 lines (25 loc) · 789 Bytes
/
convert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//go:build !js
// +build !js
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package binary
import (
"reflect"
"unsafe"
)
// ToString converts byte slice to a string without allocating.
func ToString(b *[]byte) string {
return *(*string)(unsafe.Pointer(b))
}
// ToBytes converts a string to a byte slice without allocating.
func ToBytes(v string) []byte {
strHeader := (*reflect.StringHeader)(unsafe.Pointer(&v))
bytesData := unsafe.Slice((*byte)(unsafe.Pointer(strHeader.Data)), len(v))
return bytesData
}
func binaryToBools(b *[]byte) []bool {
return *(*[]bool)(unsafe.Pointer(b))
}
func boolsToBinary(v *[]bool) []byte {
return *(*[]byte)(unsafe.Pointer(v))
}