fix: to care surrogate-pair on stringDecoder

fix #364
This commit is contained in:
Nao Yonashiro 2022-04-26 01:40:44 +09:00
parent 22be3b9a93
commit 6911114fb4
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)) 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)] v3 := hexToInt[char(src, 4)]
v4 := hexToInt[char(src, 5)] v4 := hexToInt[char(src, 5)]
code := rune((v1 << 12) | (v2 << 8) | (v3 << 4) | v4) 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 var b [utf8.UTFMax]byte
n := utf8.EncodeRune(b[:], code) n := utf8.EncodeRune(b[:], code)
switch n { switch n {