Skip to content

Commit

Permalink
fix: stop adding unnecessary null terminators to strings (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
voxeljorge authored Feb 19, 2024
1 parent f332838 commit 14de3a6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
36 changes: 18 additions & 18 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
Expand All @@ -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,
},
},
Expand All @@ -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,
},
},
Expand All @@ -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,
},
},
Expand All @@ -179,14 +179,14 @@ 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,
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,
},
},
},
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 []byte(v), nil
}
return append([]byte(v), bytes.Repeat([]byte{0x00}, int(n)-len(v))...), nil
}
Expand Down
2 changes: 1 addition & 1 deletion value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
6 changes: 3 additions & 3 deletions webm/blockwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 14de3a6

Please sign in to comment.