sq/libsq/core/bytez/bytez.go
Neil O'Toole a3cd01f36a
#353 Diff performance (#399)
* Diff refactor
2024-02-20 16:26:45 -07:00

17 lines
363 B
Go

package bytez
const newline = '\n'
// TerminateNewline returns a slice whose last byte is newline, if b is
// non-empty. If b is empty or already newline-terminated, b is returned as-is.
func TerminateNewline(b []byte) []byte {
if len(b) == 0 || b[len(b)-1] == newline {
return b
}
s := make([]byte, len(b)+1)
copy(s, b)
s[len(b)] = newline
return s
}