helper_not_unsafe.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // +build !unsafe
  2. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  3. // Use of this source code is governed by a MIT license found in the LICENSE file.
  4. package codec
  5. // stringView returns a view of the []byte as a string.
  6. // In unsafe mode, it doesn't incur allocation and copying caused by conversion.
  7. // In regular safe mode, it is an allocation and copy.
  8. //
  9. // Usage: Always maintain a reference to v while result of this call is in use,
  10. // and call keepAlive4BytesView(v) at point where done with view.
  11. func stringView(v []byte) string {
  12. return string(v)
  13. }
  14. // bytesView returns a view of the string as a []byte.
  15. // In unsafe mode, it doesn't incur allocation and copying caused by conversion.
  16. // In regular safe mode, it is an allocation and copy.
  17. //
  18. // Usage: Always maintain a reference to v while result of this call is in use,
  19. // and call keepAlive4BytesView(v) at point where done with view.
  20. func bytesView(v string) []byte {
  21. return []byte(v)
  22. }
  23. // keepAlive4BytesView maintains a reference to the input parameter for bytesView.
  24. //
  25. // Usage: call this at point where done with the bytes view.
  26. func keepAlive4BytesView(v string) {}
  27. // keepAlive4BytesView maintains a reference to the input parameter for stringView.
  28. //
  29. // Usage: call this at point where done with the string view.
  30. func keepAlive4StringView(v []byte) {}