Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: proto.UnmarshalValue simplify utf8 string decoding #446

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions proto/value_unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,31 +224,22 @@ func UnmarshalValue(b []byte, arch byte, baseType basetype.BaseType, profileType
}
return SliceString(vals), nil
}
b = trimRightZero(b)
return String(utf8String(b)), nil
}

return Value{}, fmt.Errorf("type %s(%d) is not supported: %w", baseType, baseType, ErrTypeNotSupported)
}

// trimRightZero returns a subslice of b up to the null-terminated
// string ('\x00') and discard the remaining bytes, as these are likely
// padding bytes used to meet the desired length.
func trimRightZero(b []byte) []byte {
for i := range b {
if b[i] == 0 {
return b[:i]
}
}
return b
}

// utf8String converts b into a valid UTF-8 string. If it encounters
// utf8.RuneError character, it will discard that character.
// utf8.RuneError character it will discard it. It stops when all bytes
// have been successfully decoded or reach a null-terminated string '\x00'.
func utf8String(b []byte) string {
buf := make([]byte, 0, 255)
for len(b) > 0 {
r, size := utf8.DecodeRune(b)
if r == 0 {
break
}
if r != utf8.RuneError {
buf = utf8.AppendRune(buf, r)
}
Expand Down
41 changes: 6 additions & 35 deletions proto/value_unmarshal_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,18 @@ import (
"testing"
)

func TestTrimRightZero(t *testing.T) {
tt := []struct {
str string
expected string
}{
{str: "", expected: ""},
{str: "\x00", expected: ""},
{str: "Open Water", expected: "Open Water"},
{str: "Open Water\x00", expected: "Open Water"},
{str: "Open Water\x00\x00", expected: "Open Water"},
{str: "Walk or jog lightly.\x00��", expected: "Walk or jog lightly."},
{str: "Walk or jog lightly.��", expected: "Walk or jog lightly.��"},
}

for _, tc := range tt {
t.Run(tc.str, func(t *testing.T) {
res := trimRightZero([]byte(tc.str))
if string(res) != tc.expected {
t.Fatalf("expected: %s, got: %s", tc.expected, res)
}
})
}
}

func BenchmarkTrimRightZero(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = trimRightZero([]byte(""))
_ = trimRightZero([]byte("\x00"))
_ = trimRightZero([]byte("Open Water"))
_ = trimRightZero([]byte("Open Water\x00"))
_ = trimRightZero([]byte("Open Water\x00\x00"))
_ = trimRightZero([]byte("Walk or jog lightly.\x00��"))
}
}

func TestUTF8String(t *testing.T) {
tt := []struct {
in []byte
out string
}{
{in: []byte(""), out: ""},
{in: []byte("\x00"), out: ""},
{in: []byte("Open Water"), out: "Open Water"},
{in: []byte("Open Water\x00"), out: "Open Water"},
{in: []byte("Open Water\x00\x00"), out: "Open Water"},
{in: []byte("Walk or jog lightly.��"), out: "Walk or jog lightly."},
{in: []byte("Walk or jog lightly.\x00��"), out: "Walk or jog lightly."},
{in: []byte("0000000000000�0000000"), out: "00000000000000000000"},
{in: []byte("0000000000000\xe80000000"), out: "00000000000000000000"},
}
Expand Down