Skip to content

Commit 192920c

Browse files
committed
Auto merge of rust-lang#86463 - fee1-dead:fixed-encode_wide, r=m-ou-se
Account for self.extra in size_hint for EncodeWide Fixes rust-lang#86414.
2 parents f639657 + 15cdb28 commit 192920c

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

library/std/src/sys_common/wtf8.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -853,10 +853,11 @@ impl<'a> Iterator for EncodeWide<'a> {
853853
#[inline]
854854
fn size_hint(&self) -> (usize, Option<usize>) {
855855
let (low, high) = self.code_points.size_hint();
856+
let ext = (self.extra != 0) as usize;
856857
// every code point gets either one u16 or two u16,
857858
// so this iterator is between 1 or 2 times as
858859
// long as the underlying iterator.
859-
(low, high.and_then(|n| n.checked_mul(2)))
860+
(low + ext, high.and_then(|n| n.checked_mul(2)).and_then(|n| n.checked_add(ext)))
860861
}
861862
}
862863

library/std/src/sys_common/wtf8/tests.rs

+12
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,15 @@ fn wtf8_encode_wide() {
395395
vec![0x61, 0xE9, 0x20, 0xD83D, 0xD83D, 0xDCA9]
396396
);
397397
}
398+
399+
#[test]
400+
fn wtf8_encode_wide_size_hint() {
401+
let string = Wtf8Buf::from_str("\u{12345}");
402+
let mut iter = string.encode_wide();
403+
assert_eq!((1, Some(8)), iter.size_hint());
404+
iter.next().unwrap();
405+
assert_eq!((1, Some(1)), iter.size_hint());
406+
iter.next().unwrap();
407+
assert_eq!((0, Some(0)), iter.size_hint());
408+
assert!(iter.next().is_none());
409+
}

0 commit comments

Comments
 (0)