Skip to content

Account for self.extra in size_hint for EncodeWide #86463

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 1 commit into from
Jun 20, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion library/std/src/sys_common/wtf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,10 +853,11 @@ impl<'a> Iterator for EncodeWide<'a> {
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (low, high) = self.code_points.size_hint();
let ext = (self.extra != 0) as usize;
// every code point gets either one u16 or two u16,
// so this iterator is between 1 or 2 times as
// long as the underlying iterator.
(low, high.and_then(|n| n.checked_mul(2)))
(low + ext, high.and_then(|n| n.checked_mul(2)).and_then(|n| n.checked_add(ext)))
}
}

Expand Down
12 changes: 12 additions & 0 deletions library/std/src/sys_common/wtf8/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,15 @@ fn wtf8_encode_wide() {
vec![0x61, 0xE9, 0x20, 0xD83D, 0xD83D, 0xDCA9]
);
}

#[test]
fn wtf8_encode_wide_size_hint() {
let string = Wtf8Buf::from_str("\u{12345}");
let mut iter = string.encode_wide();
assert_eq!((1, Some(8)), iter.size_hint());
iter.next().unwrap();
assert_eq!((1, Some(1)), iter.size_hint());
iter.next().unwrap();
assert_eq!((0, Some(0)), iter.size_hint());
assert!(iter.next().is_none());
}