Skip to content

Commit

Permalink
Merge branch 'main' into test/lspace_recover
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed May 29, 2024
2 parents 0c81c72 + d79801f commit f3904d1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
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

0 comments on commit f3904d1

Please sign in to comment.