scovich commented on code in PR #7721:
URL: https://github.com/apache/arrow-rs/pull/7721#discussion_r2159678798
##########
arrow-json/src/reader/tape.rs:
##########
@@ -705,9 +705,16 @@ fn err(b: u8, ctx: &str) -> ArrowError {
/// Creates a character from an UTF-16 surrogate pair
fn char_from_surrogate_pair(low: u16, high: u16) -> Result<char, ArrowError> {
- let n = (((high - 0xD800) as u32) << 10) | ((low - 0xDC00) as u32 +
0x1_0000);
- char::from_u32(n)
- .ok_or_else(|| ArrowError::JsonError(format!("Invalid UTF-16 surrogate
pair {n}")))
+ match (low, high) {
+ (0xDC00..=0xDFFF, 0xD800..=0xDBFF) => {
+ let n = (((high - 0xD800) as u32) << 10) | ((low - 0xDC00) as u32
+ 0x1_0000);
+ char::from_u32(n)
+ .ok_or_else(|| ArrowError::JsonError(format!("Invalid UTF-16
surrogate pair {n}")))
+ }
+ _ => Err(ArrowError::JsonError(format!(
+ "Invalid UTF-16 surrogate pair. High: {high:#02X}, Low: {low:#02X}"
+ ))),
+ }
Review Comment:
Would a pair of
[checked_sub](https://doc.rust-lang.org/std/primitive.u16.html#method.checked_sub)
work?
```suggestion
char_from_surrogate_pair_opt(low, high).ok_or_else(|| {
ArrowError::JsonError(format!("Invalid UTF-16 surrogate pair:
{lo:#02X}, {high:#02X}"))
})
}
fn char_from_surrogate_pair_opt(low: u16, high: u16) -> Option<char> {
let high = high.checked_sub(0xD800)? as u32;
let low = low.checked_sub(0xDC00)? as u32;
char::from_u32((high << 10) | (low + 0x1_0000))
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]