From dacf2725ec6230e4b99486fd8f87cc9ab5ad995c Mon Sep 17 00:00:00 2001 From: Daan Rijks Date: Sun, 30 Aug 2015 17:32:43 +0200 Subject: [PATCH] Add overrides to iterator methods for `str::Bytes` Specifically, `count`, `last`, and `nth` are implemented to use the methods of the underlying slice iterator. Partially closes #24214. --- src/libcollectionstest/str.rs | 31 +++++++++++++++++++++++++++++++ src/libcore/str/mod.rs | 15 +++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/libcollectionstest/str.rs b/src/libcollectionstest/str.rs index 69f6eb6c397d7..dfdf2db025032 100644 --- a/src/libcollectionstest/str.rs +++ b/src/libcollectionstest/str.rs @@ -813,6 +813,37 @@ fn test_bytes_revator() { } } +#[test] +fn test_bytesator_nth() { + let s = "ศไทย中华Việt Nam"; + let v = [ + 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228, + 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97, + 109 + ]; + + let mut b = s.bytes(); + assert_eq!(b.nth(2).unwrap(), v[2]); + assert_eq!(b.nth(10).unwrap(), v[10]); + assert_eq!(b.nth(200), None); +} + +#[test] +fn test_bytesator_count() { + let s = "ศไทย中华Việt Nam"; + + let b = s.bytes(); + assert_eq!(b.count(), 28) +} + +#[test] +fn test_bytesator_last() { + let s = "ศไทย中华Việt Nam"; + + let b = s.bytes(); + assert_eq!(b.last().unwrap(), 109) +} + #[test] fn test_char_indicesator() { let s = "ศไทย中华Việt Nam"; diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 48118c18029fe..d9dffb76c64d4 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -387,6 +387,21 @@ impl<'a> Iterator for Bytes<'a> { fn size_hint(&self) -> (usize, Option) { self.0.size_hint() } + + #[inline] + fn count(self) -> usize { + self.0.count() + } + + #[inline] + fn last(self) -> Option { + self.0.last() + } + + #[inline] + fn nth(&mut self, n: usize) -> Option { + self.0.nth(n) + } } #[stable(feature = "rust1", since = "1.0.0")]