-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgocx.go
More file actions
37 lines (32 loc) · 788 Bytes
/
gocx.go
File metadata and controls
37 lines (32 loc) · 788 Bytes
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
34
35
36
37
// Copyright 2023 CloudXaaS.
//
// Licensed under the MIT License.
// The most frequently used hacks which may not be compatible with future golang. benchmark it, it's faster
package cx
import (
// "reflect"
"unsafe"
)
/* old
//Credit : Ian Lance Taylor
//https://groups.google.com/g/golang-nuts/c/Zsfk-VMd_fU/m/O1ru4fO-BgAJ
func S2b(s string) (b []byte) {
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh.Data = sh.Data
bh.Cap = sh.Len
bh.Len = sh.Len
return b
}
*/
func S2b(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}
/*
func B2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
*/
func B2s(b []byte) string {
return unsafe.String(unsafe.SliceData(b), len(b))
}