Skip to content

Commit b0f77ba

Browse files
committed
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.
2 parents 05cc464 + dacf272 commit b0f77ba

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/libcollectionstest/str.rs

+31
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,37 @@ fn test_bytes_revator() {
833833
}
834834
}
835835

836+
#[test]
837+
fn test_bytesator_nth() {
838+
let s = "ศไทย中华Việt Nam";
839+
let v = [
840+
224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228,
841+
184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97,
842+
109
843+
];
844+
845+
let mut b = s.bytes();
846+
assert_eq!(b.nth(2).unwrap(), v[2]);
847+
assert_eq!(b.nth(10).unwrap(), v[10]);
848+
assert_eq!(b.nth(200), None);
849+
}
850+
851+
#[test]
852+
fn test_bytesator_count() {
853+
let s = "ศไทย中华Việt Nam";
854+
855+
let b = s.bytes();
856+
assert_eq!(b.count(), 28)
857+
}
858+
859+
#[test]
860+
fn test_bytesator_last() {
861+
let s = "ศไทย中华Việt Nam";
862+
863+
let b = s.bytes();
864+
assert_eq!(b.last().unwrap(), 109)
865+
}
866+
836867
#[test]
837868
fn test_char_indicesator() {
838869
let s = "ศไทย中华Việt Nam";

src/libcore/str/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,21 @@ impl<'a> Iterator for Bytes<'a> {
411411
fn size_hint(&self) -> (usize, Option<usize>) {
412412
self.0.size_hint()
413413
}
414+
415+
#[inline]
416+
fn count(self) -> usize {
417+
self.0.count()
418+
}
419+
420+
#[inline]
421+
fn last(self) -> Option<Self::Item> {
422+
self.0.last()
423+
}
424+
425+
#[inline]
426+
fn nth(&mut self, n: usize) -> Option<Self::Item> {
427+
self.0.nth(n)
428+
}
414429
}
415430

416431
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)