about summary refs log tree commit diff
path: root/src/libstd/str.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-09-03 06:56:05 -0700
committerbors <bors@rust-lang.org>2013-09-03 06:56:05 -0700
commit1ac8e8885bb1917f71ce432dcf181253b47f0bca (patch)
tree35276fcd4e7a3c376c0a71123c1e77dcb160d235 /src/libstd/str.rs
parent7048e05d5fb6aae8647494148a89bd902e5a913f (diff)
parent7c369ee7337cee50f8ef05b9d2833e2aa30d802e (diff)
downloadrust-1ac8e8885bb1917f71ce432dcf181253b47f0bca.tar.gz
rust-1ac8e8885bb1917f71ce432dcf181253b47f0bca.zip
auto merge of #8884 : blake2-ppc/rust/exact-size-hint, r=huonw
The message of the first commit explains (edited for changed trait name):

The trait `ExactSize` is introduced to solve a few small niggles:

* We can't reverse (`.invert()`) an enumeration iterator
* for a vector, we have `v.iter().position(f)` but `v.rposition(f)`.
* We can't reverse `Zip` even if both iterators are from vectors

`ExactSize` is an empty trait that is intended to indicate that an
iterator, for example `VecIterator`, knows its exact finite size and
reports it correctly using `.size_hint()`. Only adaptors that preserve
this at all times, can expose this trait further. (Where here we say
finite for fitting in uint).

---

It may seem complicated just to solve these small "niggles",
(It's really the reversible enumerate case that's the most interesting)
but only a few core iterators need to implement this trait.

While we gain more capabilities generically for some iterators,
it becomes a tad more complicated to figure out if a type has
the right trait impls for it.
Diffstat (limited to 'src/libstd/str.rs')
-rw-r--r--src/libstd/str.rs21
1 files changed, 6 insertions, 15 deletions
diff --git a/src/libstd/str.rs b/src/libstd/str.rs
index 4a7ab6d6732..e4d1b324e73 100644
--- a/src/libstd/str.rs
+++ b/src/libstd/str.rs
@@ -24,7 +24,7 @@ use container::{Container, Mutable};
 use num::Times;
 use iterator::{Iterator, FromIterator, Extendable};
 use iterator::{Filter, AdditiveIterator, Map};
-use iterator::{Invert, DoubleEndedIterator};
+use iterator::{Invert, DoubleEndedIterator, ExactSize};
 use libc;
 use num::{Saturating};
 use option::{None, Option, Some};
@@ -484,9 +484,8 @@ for CharSplitIterator<'self, Sep> {
         let mut next_split = None;
 
         if self.only_ascii {
-            for (j, byte) in self.string.byte_rev_iter().enumerate() {
+            for (idx, byte) in self.string.byte_iter().enumerate().invert() {
                 if self.sep.matches(byte as char) && byte < 128u8 {
-                    let idx = len - j - 1;
                     next_split = Some((idx, idx + 1));
                     break;
                 }
@@ -2006,16 +2005,13 @@ impl<'self> StrSlice<'self> for &'self str {
     /// or `None` if there is no match
     fn find<C: CharEq>(&self, search: C) -> Option<uint> {
         if search.only_ascii() {
-            for (i, b) in self.byte_iter().enumerate() {
-                if search.matches(b as char) { return Some(i) }
-            }
+            self.byte_iter().position(|b| search.matches(b as char))
         } else {
             for (index, c) in self.char_offset_iter() {
                 if search.matches(c) { return Some(index); }
             }
+            None
         }
-
-        None
     }
 
     /// Returns the byte index of the last character of `self` that matches `search`
@@ -2026,18 +2022,13 @@ impl<'self> StrSlice<'self> for &'self str {
     /// or `None` if there is no match
     fn rfind<C: CharEq>(&self, search: C) -> Option<uint> {
         if search.only_ascii() {
-            let mut index = self.len();
-            for b in self.byte_rev_iter() {
-                index -= 1;
-                if search.matches(b as char) { return Some(index); }
-            }
+            self.byte_iter().rposition(|b| search.matches(b as char))
         } else {
             for (index, c) in self.char_offset_rev_iter() {
                 if search.matches(c) { return Some(index); }
             }
+            None
         }
-
-        None
     }
 
     /// Returns the byte index of the first matching substring