github.com/qxnw/lib4go@v0.0.0-20180426074627-c80c7e84b925/influxdb/models/inline_strconv_parse.go (about) 1 package models 2 3 import ( 4 "reflect" 5 "strconv" 6 "unsafe" 7 ) 8 9 // parseIntBytes is a zero-alloc wrapper around strconv.ParseInt. 10 func parseIntBytes(b []byte, base int, bitSize int) (i int64, err error) { 11 s := unsafeBytesToString(b) 12 return strconv.ParseInt(s, base, bitSize) 13 } 14 15 // parseFloatBytes is a zero-alloc wrapper around strconv.ParseFloat. 16 func parseFloatBytes(b []byte, bitSize int) (float64, error) { 17 s := unsafeBytesToString(b) 18 return strconv.ParseFloat(s, bitSize) 19 } 20 21 // parseBoolBytes is a zero-alloc wrapper around strconv.ParseBool. 22 func parseBoolBytes(b []byte) (bool, error) { 23 return strconv.ParseBool(unsafeBytesToString(b)) 24 } 25 26 // unsafeBytesToString converts a []byte to a string without a heap allocation. 27 // 28 // It is unsafe, and is intended to prepare input to short-lived functions 29 // that require strings. 30 func unsafeBytesToString(in []byte) string { 31 src := *(*reflect.SliceHeader)(unsafe.Pointer(&in)) 32 dst := reflect.StringHeader{ 33 Data: src.Data, 34 Len: src.Len, 35 } 36 s := *(*string)(unsafe.Pointer(&dst)) 37 return s 38 }