Skip to content

Commit 9868947

Browse files
committed
WIP
1 parent fa5e283 commit 9868947

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

library/core/src/char/decode.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,25 @@ impl<I: Iterator<Item = u16>> Iterator for DecodeUtf16<I> {
119119

120120
#[inline]
121121
fn size_hint(&self) -> (usize, Option<usize>) {
122-
let (low, high) = self.iter.size_hint();
122+
let (mut low, mut high) = self.iter.size_hint();
123123
// we could be entirely valid surrogates (2 elements per
124124
// char), or entirely non-surrogates (1 element per char)
125125
//
126126
// In addition to `self.iter`, there's potentially one
127127
// additional number in `self.buf`.
128128
// On odd lower bound, at least one element must stay unpaired
129129
// (with other elements from `self.iter`).
130-
(low.div_ceil(2), try { high?.checked_add(1)? })
130+
//
131+
// `self.buf` will never contain the first part of a surrogate,
132+
// so the presence of `buf == Some(...)` always means +1
133+
// on lower and upper bound.
134+
135+
low = low.div_ceil(2);
136+
if self.buf.is_some() {
137+
low += 1;
138+
high = try { high?.checked_add(1)? }
139+
}
140+
(low, high)
131141
}
132142
}
133143

0 commit comments

Comments
 (0)