types/json: replace binary.Read with binary.BigEndian.Uint16 (#23845)

This commit is contained in:
Calvin Xiao
2021-04-06 10:35:24 +08:00
committed by GitHub
parent 8107e2732e
commit 6a0cd8e093
2 changed files with 11 additions and 5 deletions

View File

@ -137,11 +137,8 @@ func decodeEscapedUnicode(s []byte) (char [4]byte, size int, err error) {
// The unicode must can be represented in 2 bytes.
return char, 0, errors.Trace(err)
}
var unicode uint16
err = binary.Read(bytes.NewReader(char[0:2]), binary.BigEndian, &unicode)
if err != nil {
return char, 0, errors.Trace(err)
}
unicode := binary.BigEndian.Uint16(char[0:2])
size = utf8.RuneLen(rune(unicode))
utf8.EncodeRune(char[0:size], rune(unicode))
return

View File

@ -13,6 +13,8 @@
package json
import (
"testing"
. "github.com/pingcap/check"
)
@ -29,3 +31,10 @@ func (s *testJSONFuncSuite) TestdecodeEscapedUnicode(c *C) {
c.Assert(err, IsNil)
}
func BenchmarkDecodeEscapedUnicode(b *testing.B) {
for i := 0; i < b.N; i++ {
in := "597d"
decodeEscapedUnicode([]byte(in))
}
}