inline_strconv_parse.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package models // import "github.com/influxdata/platform/models"
  2. import (
  3. "reflect"
  4. "strconv"
  5. "unsafe"
  6. )
  7. // parseIntBytes is a zero-alloc wrapper around strconv.ParseInt.
  8. func parseIntBytes(b []byte, base int, bitSize int) (i int64, err error) {
  9. s := unsafeBytesToString(b)
  10. return strconv.ParseInt(s, base, bitSize)
  11. }
  12. // parseUintBytes is a zero-alloc wrapper around strconv.ParseUint.
  13. func parseUintBytes(b []byte, base int, bitSize int) (i uint64, err error) {
  14. s := unsafeBytesToString(b)
  15. return strconv.ParseUint(s, base, bitSize)
  16. }
  17. // parseFloatBytes is a zero-alloc wrapper around strconv.ParseFloat.
  18. func parseFloatBytes(b []byte, bitSize int) (float64, error) {
  19. s := unsafeBytesToString(b)
  20. return strconv.ParseFloat(s, bitSize)
  21. }
  22. // parseBoolBytes is a zero-alloc wrapper around strconv.ParseBool.
  23. func parseBoolBytes(b []byte) (bool, error) {
  24. return strconv.ParseBool(unsafeBytesToString(b))
  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. }