From 85eb6036b14f76785b0e056c9ba281cb20c6a272 Mon Sep 17 00:00:00 2001 From: Jorge Villatoro Date: Thu, 4 Aug 2022 18:41:13 -0700 Subject: [PATCH 1/4] 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}, From e30ede8f7bebf669a0ef8508202194f764d22404 Mon Sep 17 00:00:00 2001 From: Jorge Villatoro Date: Wed, 4 Oct 2023 13:11:07 +0000 Subject: [PATCH 2/4] fix a broken test --- webm/blockwriter_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/webm/blockwriter_test.go b/webm/blockwriter_test.go index 36fe704..5093ed1 100644 --- a/webm/blockwriter_test.go +++ b/webm/blockwriter_test.go @@ -179,11 +179,11 @@ func TestBlockWriter_NewSimpleWriter(t *testing.T) { expectedBytes := []byte{ 0x18, 0x53, 0x80, 0x67, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0x16, 0x54, 0xAE, 0x6B, 0x8F, - 0xAE, 0x8D, + 0x16, 0x54, 0xAE, 0x6B, 0x8E, + 0xAE, 0x8C, 0xD7, 0x81, 0x01, 0x73, 0xC5, 0x81, 0x02, - 0x86, 0x81, 0x00, + 0x86, 0x80, 0x83, 0x81, 0x01, 0x1F, 0x43, 0xB6, 0x75, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0x81, 0x00, From 3fa66ab53954ce6c3476003d75b4ec64c47f7eef Mon Sep 17 00:00:00 2001 From: Jorge Villatoro Date: Sun, 11 Feb 2024 09:12:03 -0800 Subject: [PATCH 3/4] wip: fix one more data block for the tests --- marshal_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/marshal_test.go b/marshal_test.go index 44ecd7f..d3ce5a6 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -184,9 +184,9 @@ func TestMarshal(t *testing.T) { 0x57, 0x41, 0x84, 0x61, 0x62, 0x63, 0x64, }, { // Go map element order is unstable - 0x15, 0x49, 0xA9, 0x66, 0x90, - 0x57, 0x41, 0x85, 0x61, 0x62, 0x63, 0x64, 0x00, - 0x4D, 0x80, 0x85, 0x74, 0x65, 0x73, 0x74, 0x00, + 0x15, 0x49, 0xA9, 0x66, 0x8e, + 0x57, 0x41, 0x84, 0x61, 0x62, 0x63, 0x64, + 0x4D, 0x80, 0x84, 0x74, 0x65, 0x73, 0x74, }, }, }, From 98735d039157a5452c4d45b01da403f471051a78 Mon Sep 17 00:00:00 2001 From: Atsushi Watanabe Date: Mon, 19 Feb 2024 13:41:29 +0900 Subject: [PATCH 4/4] Remove redundant append --- value.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/value.go b/value.go index 81e63c2..7b99d55 100644 --- a/value.go +++ b/value.go @@ -359,7 +359,7 @@ func encodeString(i interface{}, n uint64) ([]byte, error) { return []byte{}, wrapErrorf(ErrInvalidType, "writing %T as string", i) } if uint64(len(v)) >= n { - return append([]byte(v)), nil + return []byte(v), nil } return append([]byte(v), bytes.Repeat([]byte{0x00}, int(n)-len(v))...), nil }