adedd string unmarshal

This commit is contained in:
Dave Cheney 2013-11-05 12:31:20 +11:00
parent 30b997f450
commit 9187a3b3b2
2 changed files with 23 additions and 0 deletions

View File

@ -48,3 +48,8 @@ func unmarshalUint64(b []byte) (uint64, []byte) {
l, b := unmarshalUint32(b)
return uint64(h)<<32 | uint64(l), b
}
func unmarshalString(b []byte) (string, []byte) {
n, b := unmarshalUint32(b)
return string(b[:n]), b[n:]
}

View File

@ -128,3 +128,21 @@ func TestUnmarshalUint64(t *testing.T) {
}
}
}
var unmarshalStringTests = []struct {
b []byte
want string
rest []byte
}{
{marshalString(nil, ""), "", nil},
{marshalString(nil, "blah"), "blah", nil},
}
func TestUnmarshalString(t *testing.T) {
for _, tt := range unmarshalStringTests {
got, rest := unmarshalString(tt.b)
if got != tt.want || !bytes.Equal(rest, tt.rest) {
t.Errorf("unmarshalUint64(%v): want %q, %#v, got %q, %#v", tt.b, tt.want, tt.rest, got, rest)
}
}
}