@@ -121,23 +121,31 @@ impl<I: Iterator<Item = u16>> Iterator for DecodeUtf16<I> {
121
121
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
122
122
let ( low, high) = self . iter . size_hint ( ) ;
123
123
124
- // If
125
- // - `self.buf` contains a non surrogate (`u < 0xD800 || 0xDFFF < u`), or
126
- // - `high == Some(0)` (and `self.buf` contains a leading surrogate since
127
- // it can never contain a trailing surrogate)
128
- //
129
- // then buf contains an additional character or error that doesn't
130
- // need a pair from `self.iter`, so it's +1 additional element.
131
- let addition_from_buf =
132
- self . buf . map_or ( false , |u| u < 0xD800 || 0xDFFF < u || high == Some ( 0 ) ) as usize ;
124
+ let ( low_buf, high_buf) = match self . buf {
125
+ // buf is empty, no additional elements from it.
126
+ None => ( 0 , 0 ) ,
127
+ // `u` is a non surrogate, so it's always an additional character.
128
+ Some ( u) if u < 0xD800 || 0xDFFF < u => ( 1 , 1 ) ,
129
+ // `u` is a leading surrogate (it can never be a trailing surrogate and
130
+ // it's a surrogate due to the previous branch) and `self.iter` is empty.
131
+ //
132
+ // `u` can't be paired, since the `self.iter` is empty,
133
+ // so it will always become an additional element (error).
134
+ Some ( _u) if high == Some ( 0 ) => ( 1 , 1 ) ,
135
+ // `u` is a leading surrogate and `iter` may be non-empty.
136
+ //
137
+ // `u` can either pair with a trailing surrogate, in which case no additional elements
138
+ // are produced, or it can become an error, in which case it's an additional character (error).
139
+ Some ( _u) => ( 0 , 1 ) ,
140
+ } ;
133
141
134
142
// `self.iter` could contain entirely valid surrogates (2 elements per
135
143
// char), or entirely non-surrogates (1 element per char).
136
144
//
137
145
// On odd lower bound, at least one element must stay unpaired
138
146
// (with other elements from `self.iter`), so we round up.
139
- let low = low. div_ceil ( 2 ) + addition_from_buf ;
140
- let high = high. and_then ( |h| h. checked_add ( addition_from_buf ) ) ;
147
+ let low = low. div_ceil ( 2 ) + low_buf ;
148
+ let high = high. and_then ( |h| h. checked_add ( high_buf ) ) ;
141
149
142
150
( low, high)
143
151
}
0 commit comments