string.go 585 B

123456789101112131415161718
  1. package nlenc
  2. import "bytes"
  3. // Bytes returns a null-terminated byte slice with the contents of s.
  4. func Bytes(s string) []byte {
  5. return append([]byte(s), 0x00)
  6. }
  7. // String returns a string with the contents of b from a null-terminated
  8. // byte slice.
  9. func String(b []byte) string {
  10. // If the string has more than one NULL terminator byte, we want to remove
  11. // all of them before returning the string to the caller; hence the use of
  12. // strings.TrimRight instead of strings.TrimSuffix (which previously only
  13. // removed a single NULL).
  14. return string(bytes.TrimRight(b, "\x00"))
  15. }