about summary refs log tree commit diff
diff options
context:
space:
mode:
authorroot <root@localhost>2014-07-19 15:39:02 +0200
committerroot <root@localhost>2014-07-19 15:39:02 +0200
commitc5e0736c2434fbed6b7f249b42b336f0e27804a6 (patch)
treecc60959a3e895cbe7be56c4b01f5277a1dbbe217
parent45921648699a42fa1d257f6a54d2dbe9e46b0e20 (diff)
downloadrust-c5e0736c2434fbed6b7f249b42b336f0e27804a6.tar.gz
rust-c5e0736c2434fbed6b7f249b42b336f0e27804a6.zip
Simplify str CharOffsets iterator
Only one uint is needed to keep track of the offset from the original
full string.
-rw-r--r--src/libcore/str.rs14
1 files changed, 7 insertions, 7 deletions
diff --git a/src/libcore/str.rs b/src/libcore/str.rs
index 293c0118af1..c6aff9c8bda 100644
--- a/src/libcore/str.rs
+++ b/src/libcore/str.rs
@@ -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))
             }
         }
@@ -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))
             }
         }
     }
@@ -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]