summaryrefslogtreecommitdiff
path: root/utils/convert.go
diff options
context:
space:
mode:
Diffstat (limited to 'utils/convert.go')
-rw-r--r--utils/convert.go25
1 files changed, 20 insertions, 5 deletions
diff --git a/utils/convert.go b/utils/convert.go
index 8728ad1f..d96acfbb 100644
--- a/utils/convert.go
+++ b/utils/convert.go
@@ -5,6 +5,24 @@ import (
"unsafe"
)
+// AsBytes returns a slice that refers to the data backing the string s.
+func AsBytes(s string) []byte {
+ // get the pointer to the data of the string
+ p := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data)
+
+ var b []byte
+ hdr := (*reflect.SliceHeader)(unsafe.Pointer(&b))
+ hdr.Data = uintptr(p)
+ // we need to set the cap and len for the string to byte convert
+ // because string is shorter than []bytes
+ hdr.Cap = len(s)
+ hdr.Len = len(s)
+
+ // checker to check mutable access to the data
+ SetChecker(b)
+ return b
+}
+
// AsString returns a string that refers to the data backing the slice s.
func AsString(b []byte) string {
p := unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&b)).Data)
@@ -14,10 +32,7 @@ func AsString(b []byte) string {
hdr.Data = uintptr(p)
hdr.Len = len(b)
+ // checker to check mutable access to the data
+ SetChecker(b)
return s
}
-
-// Uint64 returns a pointer value for the uint64 value passed in.
-func Uint64(v uint64) *uint64 {
- return &v
-}