Skip to content

Commit c5e0736

Browse files
author
root
committed
Simplify str CharOffsets iterator
Only one uint is needed to keep track of the offset from the original full string.
1 parent 4592164 commit c5e0736

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/libcore/str.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,20 @@ impl<'a> DoubleEndedIterator<char> for Chars<'a> {
209209
/// Use with the `std::iter` module.
210210
#[deriving(Clone)]
211211
pub struct CharOffsets<'a> {
212-
front: uint,
213-
back: uint,
212+
front_offset: uint,
214213
iter: Chars<'a>,
215214
}
216215

217216
impl<'a> Iterator<(uint, char)> for CharOffsets<'a> {
218217
#[inline]
219218
fn next(&mut self) -> Option<(uint, char)> {
219+
let (pre_len, _) = self.iter.iter.size_hint();
220220
match self.iter.next() {
221221
None => None,
222222
Some(ch) => {
223-
let index = self.front;
223+
let index = self.front_offset;
224224
let (len, _) = self.iter.iter.size_hint();
225-
self.front += self.back - self.front - len;
225+
self.front_offset += pre_len - len;
226226
Some((index, ch))
227227
}
228228
}
@@ -241,8 +241,8 @@ impl<'a> DoubleEndedIterator<(uint, char)> for CharOffsets<'a> {
241241
None => None,
242242
Some(ch) => {
243243
let (len, _) = self.iter.iter.size_hint();
244-
self.back -= self.back - self.front - len;
245-
Some((self.back, ch))
244+
let index = self.front_offset + len;
245+
Some((index, ch))
246246
}
247247
}
248248
}
@@ -1680,7 +1680,7 @@ impl<'a> StrSlice<'a> for &'a str {
16801680

16811681
#[inline]
16821682
fn char_indices(&self) -> CharOffsets<'a> {
1683-
CharOffsets{front: 0, back: self.len(), iter: self.chars()}
1683+
CharOffsets{front_offset: 0, iter: self.chars()}
16841684
}
16851685

16861686
#[inline]

0 commit comments

Comments
 (0)