54 lines
972 B
Go
54 lines
972 B
Go
// +build unsafe
|
|
|
|
// Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
|
|
// Use of this source code is governed by a MIT license found in the LICENSE file.
|
|
|
|
package codec
|
|
|
|
import (
|
|
"runtime"
|
|
"unsafe"
|
|
)
|
|
|
|
// This file has unsafe variants of some helper methods.
|
|
// NOTE: See helper_not_unsafe.go for the usage information.
|
|
|
|
type unsafeString struct {
|
|
Data uintptr
|
|
Len int
|
|
}
|
|
|
|
type unsafeSlice struct {
|
|
Data uintptr
|
|
Len int
|
|
Cap int
|
|
}
|
|
|
|
func stringView(v []byte) string {
|
|
if len(v) == 0 {
|
|
return ""
|
|
}
|
|
|
|
bx := (*unsafeSlice)(unsafe.Pointer(&v))
|
|
sx := unsafeString{bx.Data, bx.Len}
|
|
return *(*string)(unsafe.Pointer(&sx))
|
|
}
|
|
|
|
func bytesView(v string) []byte {
|
|
if len(v) == 0 {
|
|
return zeroByteSlice
|
|
}
|
|
|
|
sx := (*unsafeString)(unsafe.Pointer(&v))
|
|
bx := unsafeSlice{sx.Data, sx.Len, sx.Len}
|
|
return *(*[]byte)(unsafe.Pointer(&bx))
|
|
}
|
|
|
|
func keepAlive4BytesView(v string) {
|
|
runtime.KeepAlive(v)
|
|
}
|
|
|
|
func keepAlive4StringView(v []byte) {
|
|
runtime.KeepAlive(v)
|
|
}
|