unsafeconv.go 366 B

123456789101112131415
  1. package unsafeconv
  2. import "unsafe"
  3. // UnsafeString returns a string view of b without allocation.
  4. func UnsafeString(b []byte) string {
  5. // #nosec G103
  6. return *(*string)(unsafe.Pointer(&b))
  7. }
  8. // UnsafeBytes returns a byte slice view of s without allocation.
  9. func UnsafeBytes(s string) []byte {
  10. // #nosec G103
  11. return unsafe.Slice(unsafe.StringData(s), len(s))
  12. }