Merge pull request #365 from orisano/fix/#364

fix: care surrogate-pair for non stream string decoder
This commit is contained in:
Masaaki Goshima 2022-04-28 18:33:39 +09:00 committed by GitHub
commit 7719c4e239
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -3931,3 +3931,16 @@ func TestIssue359(t *testing.T) {
t.Errorf("unexpected result: %v", string(v))
}
}
func TestIssue364(t *testing.T) {
var v struct {
Description string `json:"description"`
}
err := json.Unmarshal([]byte(`{"description":"\uD83D\uDE87 Toledo is a metro station"}`), &v)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if v.Description != "🚇 Toledo is a metro station" {
t.Errorf("unexpected result: %v", v.Description)
}
}

View File

@ -386,6 +386,19 @@ func unescapeString(buf []byte) int {
v3 := hexToInt[char(src, 4)]
v4 := hexToInt[char(src, 5)]
code := rune((v1 << 12) | (v2 << 8) | (v3 << 4) | v4)
if code >= 0xd800 && code < 0xdc00 && uintptr(unsafeAdd(src, 11)) < uintptr(end) {
if char(src, 6) == '\\' && char(src, 7) == 'u' {
v1 := hexToInt[char(src, 8)]
v2 := hexToInt[char(src, 9)]
v3 := hexToInt[char(src, 10)]
v4 := hexToInt[char(src, 11)]
lo := rune((v1 << 12) | (v2 << 8) | (v3 << 4) | v4)
if lo >= 0xdc00 && lo < 0xe000 {
code = (code-0xd800)<<10 | (lo - 0xdc00) + 0x10000
src = unsafeAdd(src, 6)
}
}
}
var b [utf8.UTFMax]byte
n := utf8.EncodeRune(b[:], code)
switch n {