Skip to content

Commit

Permalink
fix: stop adding unnecessary null terminators to strings
Browse files Browse the repository at this point in the history
  • Loading branch information
voxeljorge committed Aug 5, 2022
1 parent f7149a0 commit 81de5ef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
30 changes: 15 additions & 15 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,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,
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 append([]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

0 comments on commit 81de5ef

Please sign in to comment.