Skip to content

Commit 11d3464

Browse files
committed
Extract common logic of surrogate encode
1 parent cb4a251 commit 11d3464

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/read.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -858,17 +858,21 @@ fn parse_escape<'de, R: Read<'de>>(
858858
b'r' => scratch.push(b'\r'),
859859
b't' => scratch.push(b'\t'),
860860
b'u' => {
861+
fn encode_surrogate(scratch: &mut Vec<u8>, n: u16) {
862+
scratch.extend_from_slice(&[
863+
(n >> 12 & 0x0F) as u8 | 0b1110_0000,
864+
(n >> 6 & 0x3F) as u8 | 0b1000_0000,
865+
(n & 0x3F) as u8 | 0b1000_0000,
866+
]);
867+
}
868+
861869
let c = match tri!(read.decode_hex_escape()) {
862870
n @ 0xDC00..=0xDFFF => {
863871
if validate {
864872
return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
865873
}
866874

867-
scratch.extend_from_slice(&[
868-
(n >> 12 & 0x0F) as u8 | 0b1110_0000,
869-
(n >> 6 & 0x3F) as u8 | 0b1000_0000,
870-
(n & 0x3F) as u8 | 0b1000_0000,
871-
]);
875+
encode_surrogate(scratch, n);
872876

873877
return Ok(());
874878
}
@@ -884,11 +888,7 @@ fn parse_escape<'de, R: Read<'de>>(
884888
return error(read, ErrorCode::UnexpectedEndOfHexEscape);
885889
}
886890

887-
scratch.extend_from_slice(&[
888-
(n1 >> 12 & 0x0F) as u8 | 0b1110_0000,
889-
(n1 >> 6 & 0x3F) as u8 | 0b1000_0000,
890-
(n1 & 0x3F) as u8 | 0b1000_0000,
891-
]);
891+
encode_surrogate(scratch, n1);
892892

893893
return Ok(());
894894
}
@@ -900,11 +900,7 @@ fn parse_escape<'de, R: Read<'de>>(
900900
return error(read, ErrorCode::UnexpectedEndOfHexEscape);
901901
}
902902

903-
scratch.extend_from_slice(&[
904-
(n1 >> 12 & 0x0F) as u8 | 0b1110_0000,
905-
(n1 >> 6 & 0x3F) as u8 | 0b1000_0000,
906-
(n1 & 0x3F) as u8 | 0b1000_0000,
907-
]);
903+
encode_surrogate(scratch, n1);
908904

909905
// The \ prior to this byte started an escape sequence,
910906
// so we need to parse that now. This recursive call

0 commit comments

Comments
 (0)