diff options
| author | bors <bors@rust-lang.org> | 2015-08-31 09:15:55 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-08-31 09:15:55 +0000 |
| commit | b0f77ba26ad2b52be0586e92e18a0256d2396b3f (patch) | |
| tree | 19814d560940e7680ea331205029278b8c7df1f4 | |
| parent | 05cc464d90f0189b776859c56c87db2e99cdbd87 (diff) | |
| parent | dacf2725ec6230e4b99486fd8f87cc9ab5ad995c (diff) | |
| download | rust-b0f77ba26ad2b52be0586e92e18a0256d2396b3f.tar.gz rust-b0f77ba26ad2b52be0586e92e18a0256d2396b3f.zip | |
Auto merge of #28101 - ijks:24214-str-bytes, r=alexcrichton
Specifically, `count`, `last`, and `nth` are implemented to use the methods of the underlying slice iterator. Partially closes #24214.
| -rw-r--r-- | src/libcollectionstest/str.rs | 31 | ||||
| -rw-r--r-- | src/libcore/str/mod.rs | 15 |
2 files changed, 46 insertions, 0 deletions
diff --git a/src/libcollectionstest/str.rs b/src/libcollectionstest/str.rs index a16809e100f..8c468e91567 100644 --- a/src/libcollectionstest/str.rs +++ b/src/libcollectionstest/str.rs @@ -834,6 +834,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"; let p = [0, 3, 6, 9, 12, 15, 18, 19, 20, 23, 24, 25, 26, 27]; diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 7aeda24a290..7d32f61b3dd 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -411,6 +411,21 @@ impl<'a> Iterator for Bytes<'a> { fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() } + + #[inline] + fn count(self) -> usize { + self.0.count() + } + + #[inline] + fn last(self) -> Option<Self::Item> { + self.0.last() + } + + #[inline] + fn nth(&mut self, n: usize) -> Option<Self::Item> { + self.0.nth(n) + } } #[stable(feature = "rust1", since = "1.0.0")] |
