Skip to content

Commit

Permalink
feat(enc.str): better handle utf8 in fast path
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Nov 2, 2021
1 parent bcb8f76 commit ecc1bb3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions enc_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,11 @@ func (e *Encoder) strEscape(i int, v string, valLen int) {
func (e *Encoder) Str(v string) {
length := len(v)
e.buf = append(e.buf, '"')
// write string, the fast path, without utf8 and escape support
// Fast path, without utf8 and escape support.
i := 0
for ; i < length; i++ {
c := v[i]
if c > 31 && c != '"' && c != '\\' {
if c > 31 && c != '"' && c != '\\' && c < utf8.RuneSelf {
e.buf = append(e.buf, c)
} else {
break
Expand Down
5 changes: 5 additions & 0 deletions enc_str_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ func TestEncoder_String(t *testing.T) {
e.StrEscape("\uFFFD")
t.Logf("%v", e.Bytes())
})
t.Run("BadUnicode", func(t *testing.T) {
e := GetEncoder()
e.StrEscape("a\xc5z")
t.Logf("%q", e)
})
}

0 comments on commit ecc1bb3

Please sign in to comment.