unsafe.go 637 B

12345678910111213141516171819202122232425
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package utils
  3. import (
  4. "fmt"
  5. "unsafe"
  6. )
  7. var _ = fmt.Print
  8. // Unsafely converts s into a byte slice.
  9. // If you modify b, then s will also be modified. This violates the
  10. // property that strings are immutable.
  11. func UnsafeStringToBytes(s string) (b []byte) {
  12. return unsafe.Slice(unsafe.StringData(s), len(s))
  13. }
  14. // Unsafely converts b into a string.
  15. // If you modify b, then s will also be modified. This violates the
  16. // property that strings are immutable.
  17. func UnsafeBytesToString(b []byte) (s string) {
  18. return unsafe.String(unsafe.SliceData(b), len(b))
  19. }