-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNTS.go
43 lines (34 loc) · 879 Bytes
/
NTS.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package nts
import (
"bytes"
"errors"
)
// Package for processing null-terminated strings.
const NUL = 0
const ErrNTSIsNotValid = "null-terminated string is not valid"
// ByteArrayToStrings converts a byte array of null-terminated strings into a
// slice of Golang's strings.
func ByteArrayToStrings(ba []byte) (ss []string, err error) {
if len(ba) == 0 {
return ss, nil
}
// If the last byte is not a NUL byte,
// then this null-terminated string is not valid.
if ba[len(ba)-1] != NUL {
return nil, errors.New(ErrNTSIsNotValid)
}
// Split the byte array into strings.
ss = make([]string, 0)
window := ba[:]
sepIdx := bytes.IndexByte(window, NUL)
for sepIdx >= 0 {
ss = append(ss, string(window[:sepIdx]))
// Next.
if sepIdx == len(window)-1 {
break
}
window = window[sepIdx+1:]
sepIdx = bytes.IndexByte(window, NUL)
}
return ss, nil
}