2024-05-14 13:07:09 +00:00
|
|
|
// This file will only be included to the build if neither
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
// easyjson_nounsafe nor appengine build tag is set. See README notes
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
// for more details.
|
|
|
|
|
2024-06-14 08:41:36 +00:00
|
|
|
//go:build !easyjson_nounsafe && !appengine
|
|
|
|
// +build !easyjson_nounsafe,!appengine
|
2024-05-14 13:07:09 +00:00
|
|
|
|
|
|
|
package jlexer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// bytesToStr creates a string pointing at the slice to avoid copying.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
//
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
// Warning: the string returned by the function should be used with care, as the whole input data
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
// chunk may be either blocked from being freed by GC because of a single string or the buffer.Data
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
// may be garbage-collected even when the string exists.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
func bytesToStr(data []byte) string {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&data))
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
shdr := reflect.StringHeader{Data: h.Data, Len: h.Len}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
return *(*string)(unsafe.Pointer(&shdr))
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-14 13:07:09 +00:00
|
|
|
}
|