Skip to content

Commit 46a6dbc

Browse files
author
blake2-ppc
committed
std::str: Use reverse enumerate and .rposition
Simplify code by using the reversibility of enumerate and use .rposition().
1 parent db22f26 commit 46a6dbc

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

src/libstd/str.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use container::{Container, Mutable};
2424
use num::Times;
2525
use iterator::{Iterator, FromIterator, Extendable};
2626
use iterator::{Filter, AdditiveIterator, Map};
27-
use iterator::{Invert, DoubleEndedIterator};
27+
use iterator::{Invert, DoubleEndedIterator, ExactSizeDoubleEndedIterator};
2828
use libc;
2929
use num::{Saturating};
3030
use option::{None, Option, Some};
@@ -484,9 +484,8 @@ for CharSplitIterator<'self, Sep> {
484484
let mut next_split = None;
485485

486486
if self.only_ascii {
487-
for (j, byte) in self.string.byte_rev_iter().enumerate() {
487+
for (idx, byte) in self.string.byte_iter().enumerate().invert() {
488488
if self.sep.matches(byte as char) && byte < 128u8 {
489-
let idx = len - j - 1;
490489
next_split = Some((idx, idx + 1));
491490
break;
492491
}
@@ -2008,16 +2007,13 @@ impl<'self> StrSlice<'self> for &'self str {
20082007
/// or `None` if there is no match
20092008
fn find<C: CharEq>(&self, search: C) -> Option<uint> {
20102009
if search.only_ascii() {
2011-
for (i, b) in self.byte_iter().enumerate() {
2012-
if search.matches(b as char) { return Some(i) }
2013-
}
2010+
self.byte_iter().position(|b| search.matches(b as char))
20142011
} else {
20152012
for (index, c) in self.char_offset_iter() {
20162013
if search.matches(c) { return Some(index); }
20172014
}
2015+
None
20182016
}
2019-
2020-
None
20212017
}
20222018
20232019
/// Returns the byte index of the last character of `self` that matches `search`
@@ -2028,18 +2024,13 @@ impl<'self> StrSlice<'self> for &'self str {
20282024
/// or `None` if there is no match
20292025
fn rfind<C: CharEq>(&self, search: C) -> Option<uint> {
20302026
if search.only_ascii() {
2031-
let mut index = self.len();
2032-
for b in self.byte_rev_iter() {
2033-
index -= 1;
2034-
if search.matches(b as char) { return Some(index); }
2035-
}
2027+
self.byte_iter().rposition(|b| search.matches(b as char))
20362028
} else {
20372029
for (index, c) in self.char_offset_rev_iter() {
20382030
if search.matches(c) { return Some(index); }
20392031
}
2032+
None
20402033
}
2041-
2042-
None
20432034
}
20442035
20452036
/// Returns the byte index of the first matching substring

0 commit comments

Comments
 (0)