Skip to content

Make char::DecodeUtf16::size_hist more precise #93347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions library/core/src/char/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,21 @@ impl<I: Iterator<Item = u16>> Iterator for DecodeUtf16<I> {
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (low, high) = self.iter.size_hint();
// we could be entirely valid surrogates (2 elements per
// char), or entirely non-surrogates (1 element per char)
(low / 2, high)

// `self.buf` will never contain the first part of a surrogate,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? It doesn't seem to me like that's the case.

For example the following would fail the test below.

check(&[0xD800, 0xD800, 0xDC00]);
thread 'char::test_decode_utf16_size_hint' panicked at 'lower = 2, upper = Some(2)', library/core/tests/char.rs:320:13

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a wrong assumption from the original PR that I haven't checked 😅

I pushed a fix that checks the contents of the buf.

// so the presence of `buf == Some(...)` always means +1
// on lower and upper bound.
let addition_from_buf = self.buf.is_some() as usize;

// `self.iter` could contain entirely valid surrogates (2 elements per
// char), or entirely non-surrogates (1 element per char).
//
// On odd lower bound, at least one element must stay unpaired
// (with other elements from `self.iter`), so we round up.
let low = low.div_ceil(2) + addition_from_buf;
let high = high.and_then(|h| h.checked_add(addition_from_buf));

(low, high)
}
}

Expand Down
25 changes: 25 additions & 0 deletions library/core/tests/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,31 @@ fn test_decode_utf16() {
check(&[0xD800, 0], &[Err(0xD800), Ok('\0')]);
}

#[test]
fn test_decode_utf16_size_hint() {
fn check(s: &[u16]) {
let mut iter = char::decode_utf16(s.iter().cloned());

loop {
let count = iter.clone().count();
let (lower, upper) = iter.size_hint();

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

if let None = iter.next() {
break;
}
}
}

check(&[0xD800, 0x41, 0x42]);
check(&[0xD800, 0]);
check(&[0xD834, 0x006d]);
}

#[test]
fn ed_iterator_specializations() {
// Check counting
Expand Down