From e98dce3e00a7b6bfd264418ef993bbf9cdb1f0b6 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 1 Apr 2015 11:28:34 -0700 Subject: std: Changing the meaning of the count to splitn This commit is an implementation of [RFC 979][rfc] which changes the meaning of the count parameter to the `splitn` function on strings and slices. The parameter now means the number of items that are returned from the iterator, not the number of splits that are made. [rfc]: https://github.com/rust-lang/rfcs/pull/979 Closes #23911 [breaking-change] --- src/libcore/slice.rs | 14 ++++++++------ src/libcore/str/mod.rs | 20 +++++++++----------- 2 files changed, 17 insertions(+), 17 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index d8856130fab..70e60adf64c 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -1126,18 +1126,20 @@ impl> Iterator for GenericSplitN { #[inline] fn next(&mut self) -> Option { - if self.count == 0 { - self.iter.finish() - } else { - self.count -= 1; - if self.invert { self.iter.next_back() } else { self.iter.next() } + match self.count { + 0 => None, + 1 => { self.count -= 1; self.iter.finish() } + _ => { + self.count -= 1; + if self.invert {self.iter.next_back()} else {self.iter.next()} + } } } #[inline] fn size_hint(&self) -> (usize, Option) { let (lower, upper_opt) = self.iter.size_hint(); - (lower, upper_opt.map(|upper| cmp::min(self.count + 1, upper))) + (lower, upper_opt.map(|upper| cmp::min(self.count, upper))) } } diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 934c4515614..23d6a9c1193 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -505,7 +505,7 @@ struct CharSplits<'a, P: Pattern<'a>> { /// splitting at most `count` times. struct CharSplitsN<'a, P: Pattern<'a>> { iter: CharSplits<'a, P>, - /// The number of splits remaining + /// The number of items remaining count: usize, } @@ -612,11 +612,10 @@ impl<'a, P: Pattern<'a>> Iterator for CharSplitsN<'a, P> { #[inline] fn next(&mut self) -> Option<&'a str> { - if self.count != 0 { - self.count -= 1; - self.iter.next() - } else { - self.iter.get_end() + match self.count { + 0 => None, + 1 => { self.count = 0; self.iter.get_end() } + _ => { self.count -= 1; self.iter.next() } } } } @@ -666,11 +665,10 @@ impl<'a, P: Pattern<'a>> Iterator for RCharSplitsN<'a, P> #[inline] fn next(&mut self) -> Option<&'a str> { - if self.count != 0 { - self.count -= 1; - self.iter.next() - } else { - self.iter.get_remainder() + match self.count { + 0 => None, + 1 => { self.count -= 1; self.iter.get_remainder() } + _ => { self.count -= 1; self.iter.next() } } } } -- cgit 1.4.1-3-g733a5