Skip to content

Commit

Permalink
Fix wrong assumption in DecodeUtf16::size_hint
Browse files Browse the repository at this point in the history
`self.buf` can contain a surrogate, but only a leading one.
  • Loading branch information
WaffleLapkin committed Jan 28, 2022
1 parent 9c8cd1f commit 2c97d10
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
13 changes: 9 additions & 4 deletions library/core/src/char/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,15 @@ impl<I: Iterator<Item = u16>> Iterator for DecodeUtf16<I> {
fn size_hint(&self) -> (usize, Option<usize>) {
let (low, high) = self.iter.size_hint();

// `self.buf` will never contain the first part of a surrogate,
// so the presence of `buf == Some(...)` always means +1
// on lower and upper bound.
let addition_from_buf = self.buf.is_some() as usize;
// If
// - `self.buf` contains a non surrogate (`u < 0xD800 || 0xDFFF < u`), or
// - `high == Some(0)` (and `self.buf` contains a leading surrogate since
// it can never contain a trailing surrogate)
//
// then buf contains an additional character or error that doesn't
// need a pair from `self.iter`, so it's +1 additional element.
let addition_from_buf =
self.buf.map_or(false, |u| u < 0xD800 || 0xDFFF < u || high == Some(0)) as usize;

// `self.iter` could contain entirely valid surrogates (2 elements per
// char), or entirely non-surrogates (1 element per char).
Expand Down
3 changes: 2 additions & 1 deletion library/core/tests/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ fn test_decode_utf16_size_hint() {

assert!(
lower <= count && count <= upper.unwrap(),
"lower = {lower}, upper = {upper:?}"
"lower = {lower}, count = {count}, upper = {upper:?}"
);

if let None = iter.next() {
Expand All @@ -328,6 +328,7 @@ fn test_decode_utf16_size_hint() {
}
}

check(&[0xD800, 0xD800, 0xDC00]);
check(&[0xD800, 0x41, 0x42]);
check(&[0xD800, 0]);
check(&[0xD834, 0x006d]);
Expand Down

0 comments on commit 2c97d10

Please sign in to comment.