Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | // 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 | |
| 8 | package jlexer |
| 9 | |
| 10 | import ( |
| 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. |
| 20 | func 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 | } |