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/str/mod.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'src/libcore/str') 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