From 81de5ef1e243a54bbc713a44a294a8a2204e4ada Mon Sep 17 00:00:00 2001 From: Jorge Villatoro Date: Thu, 4 Aug 2022 18:41:13 -0700 Subject: [PATCH] fix: stop adding unnecessary null terminators to strings --- marshal_test.go | 30 +++++++++++++++--------------- value.go | 4 ++-- value_test.go | 2 +- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/marshal_test.go b/marshal_test.go index 296dc41..44ecd7f 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -79,8 +79,8 @@ func TestMarshal(t *testing.T) { &struct{ EBML TestNoOmitempty }{}, [][]byte{ { - 0x1A, 0x45, 0xDF, 0xA3, 0x8B, - 0x42, 0x82, 0x81, 0x00, + 0x1A, 0x45, 0xDF, 0xA3, 0x8A, + 0x42, 0x82, 0x80, 0x42, 0x87, 0x81, 0x00, 0x53, 0xAB, 0x80, }, @@ -127,8 +127,8 @@ func TestMarshal(t *testing.T) { &struct{ EBML TestSized }{TestSized{"abc", 0x012345, 0.0, 0.0, []byte{0x01, 0x02, 0x03}}}, [][]byte{ { - 0x1A, 0x45, 0xDF, 0xA3, 0xA5, - 0x42, 0x82, 0x84, 0x61, 0x62, 0x63, 0x00, + 0x1A, 0x45, 0xDF, 0xA3, 0xA4, + 0x42, 0x82, 0x83, 0x61, 0x62, 0x63, 0x42, 0x87, 0x83, 0x01, 0x23, 0x45, 0x44, 0x89, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x89, 0x84, 0x00, 0x00, 0x00, 0x00, @@ -140,8 +140,8 @@ func TestMarshal(t *testing.T) { &struct{ EBML TestPtr }{TestPtr{&str, &uinteger}}, [][]byte{ { - 0x1A, 0x45, 0xDF, 0xA3, 0x88, - 0x42, 0x82, 0x81, 0x00, + 0x1A, 0x45, 0xDF, 0xA3, 0x87, + 0x42, 0x82, 0x80, 0x42, 0x87, 0x81, 0x00, }, }, @@ -154,8 +154,8 @@ func TestMarshal(t *testing.T) { &struct{ EBML TestInterface }{TestInterface{str, uinteger}}, [][]byte{ { - 0x1A, 0x45, 0xDF, 0xA3, 0x88, - 0x42, 0x82, 0x81, 0x00, + 0x1A, 0x45, 0xDF, 0xA3, 0x87, + 0x42, 0x82, 0x80, 0x42, 0x87, 0x81, 0x00, }, }, @@ -164,8 +164,8 @@ func TestMarshal(t *testing.T) { &struct{ EBML TestInterface }{TestInterface{&str, &uinteger}}, [][]byte{ { - 0x1A, 0x45, 0xDF, 0xA3, 0x88, - 0x42, 0x82, 0x81, 0x00, + 0x1A, 0x45, 0xDF, 0xA3, 0x87, + 0x42, 0x82, 0x80, 0x42, 0x87, 0x81, 0x00, }, }, @@ -179,9 +179,9 @@ func TestMarshal(t *testing.T) { }, [][]byte{ { - 0x15, 0x49, 0xA9, 0x66, 0x90, - 0x4D, 0x80, 0x85, 0x74, 0x65, 0x73, 0x74, 0x00, - 0x57, 0x41, 0x85, 0x61, 0x62, 0x63, 0x64, 0x00, + 0x15, 0x49, 0xA9, 0x66, 0x8E, + 0x4D, 0x80, 0x84, 0x74, 0x65, 0x73, 0x74, + 0x57, 0x41, 0x84, 0x61, 0x62, 0x63, 0x64, }, { // Go map element order is unstable 0x15, 0x49, 0xA9, 0x66, 0x90, @@ -392,7 +392,7 @@ func ExampleMarshal() { fmt.Printf("0x%02x, ", int(b)) } // Output: - // 0x1a, 0x45, 0xdf, 0xa3, 0x90, 0x42, 0x82, 0x85, 0x77, 0x65, 0x62, 0x6d, 0x00, 0x42, 0x87, 0x81, 0x02, 0x42, 0x85, 0x81, 0x02, + // 0x1a, 0x45, 0xdf, 0xa3, 0x8f, 0x42, 0x82, 0x84, 0x77, 0x65, 0x62, 0x6d, 0x42, 0x87, 0x81, 0x02, 0x42, 0x85, 0x81, 0x02, } func ExampleWithDataSizeLen() { @@ -420,7 +420,7 @@ func ExampleWithDataSizeLen() { fmt.Printf("0x%02x, ", int(b)) } // Output: - // 0x1a, 0x45, 0xdf, 0xa3, 0x40, 0x13, 0x42, 0x82, 0x40, 0x05, 0x77, 0x65, 0x62, 0x6d, 0x00, 0x42, 0x87, 0x40, 0x01, 0x02, 0x42, 0x85, 0x40, 0x01, 0x02, + // 0x1a, 0x45, 0xdf, 0xa3, 0x40, 0x12, 0x42, 0x82, 0x40, 0x04, 0x77, 0x65, 0x62, 0x6d, 0x42, 0x87, 0x40, 0x01, 0x02, 0x42, 0x85, 0x40, 0x01, 0x02, } func TestMarshal_Tag(t *testing.T) { diff --git a/value.go b/value.go index fe5771a..81e63c2 100644 --- a/value.go +++ b/value.go @@ -358,8 +358,8 @@ func encodeString(i interface{}, n uint64) ([]byte, error) { if !ok { return []byte{}, wrapErrorf(ErrInvalidType, "writing %T as string", i) } - if uint64(len(v)+1) >= n { - return append([]byte(v), 0x00), nil + if uint64(len(v)) >= n { + return append([]byte(v)), nil } return append([]byte(v), bytes.Repeat([]byte{0x00}, int(n)-len(v))...), nil } diff --git a/value_test.go b/value_test.go index 13bd870..89b8a9b 100644 --- a/value_test.go +++ b/value_test.go @@ -204,7 +204,7 @@ func TestValue(t *testing.T) { }{ "Binary": {[]byte{0x01, 0x02, 0x03}, DataTypeBinary, []byte{0x01, 0x02, 0x03}, 0, nil}, "Binary(4B)": {[]byte{0x01, 0x02, 0x03, 0x00}, DataTypeBinary, []byte{0x01, 0x02, 0x03, 0x00}, 4, []byte{0x01, 0x02, 0x03}}, - "String": {[]byte{0x31, 0x32, 0x00}, DataTypeString, "12", 0, nil}, + "String": {[]byte{0x31, 0x32}, DataTypeString, "12", 0, nil}, "String(3B)": {[]byte{0x31, 0x32, 0x00}, DataTypeString, "12", 3, nil}, "String(4B)": {[]byte{0x31, 0x32, 0x00, 0x00}, DataTypeString, "12", 4, nil}, "Int8": {[]byte{0x01}, DataTypeInt, int64(0x01), 0, nil},