Debian Codegraph @

bytes_unsafe.go

raw symbols SCIP
1 // +build !appengine,!appenginevm, !tinygo
2
3 // SYS-REQ-001, SYS-REQ-013, SYS-REQ-014: unsafe build-tag byte utilities
4 package jsonparser
5
6 import (
7 "reflect"
8 "strconv"
9 "unsafe"
10 "runtime"
11 )
12
13 //
14 // The reason for using *[]byte rather than []byte in parameters is an optimization. As of Go 1.6,
15 // the compiler cannot perfectly inline the function when using a non-pointer slice. That is,
16 // the non-pointer []byte parameter version is slower than if its function body is manually
17 // inlined, whereas the pointer []byte version is equally fast to the manually inlined
18 // version. Instruction count in assembly taken from "go tool compile" confirms this difference.
19 //
20 // TODO: Remove hack after Go 1.7 release
21 //
22 func equalStr(b *[]byte, s string) bool {
23 return *(*string)(unsafe.Pointer(b)) == s
24 }
25
26 func parseFloat(b *[]byte) (float64, error) {
27 return strconv.ParseFloat(*(*string)(unsafe.Pointer(b)), 64)
28 }
29
30 // A hack until issue golang/go#2632 is fixed.
31 // See: https://github.com/golang/go/issues/2632
32 func bytesToString(b *[]byte) string {
33 return *(*string)(unsafe.Pointer(b))
34 }
35
36 func StringToBytes(s string) []byte {
37 b := make([]byte, 0, 0)
38 bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
39 sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
40 bh.Data = sh.Data
41 bh.Cap = sh.Len
42 bh.Len = sh.Len
43 runtime.KeepAlive(s)
44 return b
45 }