diff options
| author | blake2-ppc <blake2-ppc> | 2013-08-30 20:00:17 +0200 |
|---|---|---|
| committer | blake2-ppc <blake2-ppc> | 2013-08-30 20:06:26 +0200 |
| commit | 46a6dbc5418ce7858eeafd2b0e252d7b66049519 (patch) | |
| tree | f4e9954d8393382c1b1829072a997d40d91ed8f0 /src/libstd | |
| parent | db22f2627d625ba9481a16e44f1cc3195e8d6ef7 (diff) | |
| download | rust-46a6dbc5418ce7858eeafd2b0e252d7b66049519.tar.gz rust-46a6dbc5418ce7858eeafd2b0e252d7b66049519.zip | |
std::str: Use reverse enumerate and .rposition
Simplify code by using the reversibility of enumerate and use .rposition().
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/str.rs | 21 |
1 files changed, 6 insertions, 15 deletions
diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 3265c470e90..0360d4ac72f 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, ExactSizeDoubleEndedIterator}; 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; } @@ -2008,16 +2007,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` @@ -2028,18 +2024,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 |
