blob: ff7b27c5b203749598ebc7843a6ae06d02b089d3 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001// This file will only be included to the build if neither
2// easyjson_nounsafe nor appengine build tag is set. See README notes
3// for more details.
4
5//+build !easyjson_nounsafe
6//+build !appengine
7
8package jlexer
9
10import (
11 "reflect"
12 "unsafe"
13)
14
15// bytesToStr creates a string pointing at the slice to avoid copying.
16//
17// Warning: the string returned by the function should be used with care, as the whole input data
18// chunk may be either blocked from being freed by GC because of a single string or the buffer.Data
19// may be garbage-collected even when the string exists.
20func bytesToStr(data []byte) string {
21 h := (*reflect.SliceHeader)(unsafe.Pointer(&data))
22 shdr := reflect.StringHeader{Data: h.Data, Len: h.Len}
23 return *(*string)(unsafe.Pointer(&shdr))
24}