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

fix: check empty when validating utf8 #642

Merged
merged 2 commits into from
May 28, 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
12 changes: 11 additions & 1 deletion utf8/utf8.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package utf8

import (
`runtime`

`github.com/bytedance/sonic/internal/rt`
`github.com/bytedance/sonic/internal/native/types`
`github.com/bytedance/sonic/internal/native`
Expand Down Expand Up @@ -62,10 +64,18 @@ func CorrectWith(dst []byte, src []byte, repl string) []byte {

// Validate is a simd-accelereated drop-in replacement for the standard library's utf8.Valid.
func Validate(src []byte) bool {
if src == nil {
return true
}
return ValidateString(rt.Mem2Str(src))
}

// ValidateString as Validate, but for string.
func ValidateString(src string) bool {
return native.ValidateUTF8Fast(&src) == 0
if src == "" {
return true
}
ret := native.ValidateUTF8Fast(&src) == 0
runtime.KeepAlive(src)
return ret
}
3 changes: 3 additions & 0 deletions utf8/utf8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func TestValidate_Random(t *testing.T) {
assert.Equal(t, utf8.Valid(data), Validate(data), string(data))
}

compare(t, []byte{})
compare(t, nil)

// random testing
nums := 1000
maxLen := 1000
Expand Down
Loading