Skip to content

Commit

Permalink
Simplify str CharOffsets iterator
Browse files Browse the repository at this point in the history
Only one uint is needed to keep track of the offset from the original
full string.
  • Loading branch information
root committed Jul 19, 2014
1 parent 4592164 commit c5e0736
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,20 +209,20 @@ impl<'a> DoubleEndedIterator<char> for Chars<'a> {
/// Use with the `std::iter` module.
#[deriving(Clone)]
pub struct CharOffsets<'a> {
front: uint,
back: uint,
front_offset: uint,
iter: Chars<'a>,
}

impl<'a> Iterator<(uint, char)> for CharOffsets<'a> {
#[inline]
fn next(&mut self) -> Option<(uint, char)> {
let (pre_len, _) = self.iter.iter.size_hint();
match self.iter.next() {
None => None,
Some(ch) => {
let index = self.front;
let index = self.front_offset;
let (len, _) = self.iter.iter.size_hint();
self.front += self.back - self.front - len;
self.front_offset += pre_len - len;
Some((index, ch))
}
}
Expand All @@ -241,8 +241,8 @@ impl<'a> DoubleEndedIterator<(uint, char)> for CharOffsets<'a> {
None => None,
Some(ch) => {
let (len, _) = self.iter.iter.size_hint();
self.back -= self.back - self.front - len;
Some((self.back, ch))
let index = self.front_offset + len;
Some((index, ch))
}
}
}
Expand Down Expand Up @@ -1680,7 +1680,7 @@ impl<'a> StrSlice<'a> for &'a str {

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

#[inline]
Expand Down

5 comments on commit c5e0736

@bors
Copy link
Contributor

@bors bors commented on c5e0736 Jul 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on c5e0736 Jul 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging blake2-ppc/rust/ptr-arithmetic-chars = c5e0736 into auto

@bors
Copy link
Contributor

@bors bors commented on c5e0736 Jul 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blake2-ppc/rust/ptr-arithmetic-chars = c5e0736 merged ok, testing candidate = ca38434

@bors
Copy link
Contributor

@bors bors commented on c5e0736 Jul 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = ca38434

Please sign in to comment.