Cheers
Dave
func stringzToString(buff []byte) (l []string) {
l = make([]string, 1, 0)
for _, s := range bytes.Split(buff, []byte { 0 }, -1) {
l = append(l, string(s))
}
return
}
Comments and suggestions welcome (except the horrid function name,
i'll think of something better)
func stringzToString(buff []byte) (l []string) {
l = make([]string, 1, 0)
for _, s := range bytes.Split(buff, []byte { 0 }, -1) {
l = append(l, string(s))
}
return
}Comments and suggestions welcome (except the horrid function name,
i'll think of something better)
Here are two versions that are pretty much equivalent
(actually the second one will ignore a non-null-terminated
string at the end of b, which might or might not
be what you want).
The second is probably a little faster (bytes.Split could
perhaps get a special case for a 1-byte separator)
var zb = []byte{0}
func NullTermToStrings(b []byte) (s []string) {
for _, x := range bytes.Split(b, zb, -1) {
s = append(s, string(x))
}
if len(s) > 0 && s[len(s)-1] == "" {
s = s[:len(s)-1]
}
return
}
func NullTermToStrings(b []byte) (s []string) {
for {
i := bytes.IndexByte(b, '\0')
if i == -1 {
break
}
s = append(s, string(b[0:i]))
b = b[i+1:]
}
return