Simplify unicode escape handling

This does not affect performance.
This commit is contained in:
Alisa Sireneva 2024-08-12 19:53:51 +03:00
parent 2f28d106e6
commit 236cc8247d

View File

@ -898,21 +898,27 @@ fn parse_unicode_escape<'de, R: Read<'de>>(
validate: bool,
scratch: &mut Vec<u8>,
) -> Result<()> {
let c = match tri!(read.decode_hex_escape()) {
n @ 0xDC00..=0xDFFF => {
return if validate {
error(read, ErrorCode::LoneLeadingSurrogateInHexEscape)
} else {
push_wtf8_codepoint(n as u32, scratch);
Ok(())
};
}
let n = tri!(read.decode_hex_escape());
// Non-BMP characters are encoded as a sequence of two hex
// escapes, representing UTF-16 surrogates. If deserializing a
// utf-8 string the surrogates are required to be paired,
// whereas deserializing a byte string accepts lone surrogates.
n1 @ 0xD800..=0xDBFF => {
if validate && n >= 0xDC00 && n <= 0xDFFF {
// XXX: This is actually a trailing surrogate.
return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
}
if n < 0xD800 || n > 0xDBFF {
// Every u16 outside of the surrogate ranges is guaranteed to be a
// legal char.
push_wtf8_codepoint(n as u32, scratch);
return Ok(());
}
// n is a leading surrogate, we now expect a trailing surrogate.
let n1 = n;
if tri!(peek_or_eof(read)) == b'\\' {
read.discard();
} else {
@ -950,15 +956,8 @@ fn parse_unicode_escape<'de, R: Read<'de>>(
// This value is in range U+10000..=U+10FFFF, which is always a
// valid codepoint.
(((n1 - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) + 0x1_0000
}
// Every u16 outside of the surrogate ranges above is guaranteed
// to be a legal char.
n => n as u32,
};
push_wtf8_codepoint(c, scratch);
let n = (((n1 - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) + 0x1_0000;
push_wtf8_codepoint(n, scratch);
Ok(())
}