Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement DoubleEndedIterator for CaseMappingIter #60112

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/libcore/char/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,13 @@ impl Iterator for ToLowercase {
}
}

#[stable(feature = "double_ended_case_mapping_iter", since = "1.36.0")]
impl DoubleEndedIterator for ToLowercase {
fn next_back(&mut self) -> Option<char> {
self.0.next_back()
}
}

#[stable(feature = "fused", since = "1.26.0")]
impl FusedIterator for ToLowercase {}

Expand Down Expand Up @@ -422,6 +429,13 @@ impl Iterator for ToUppercase {
}
}

#[stable(feature = "double_ended_case_mapping_iter", since = "1.36.0")]
impl DoubleEndedIterator for ToUppercase {
fn next_back(&mut self) -> Option<char> {
self.0.next_back()
}
}

#[stable(feature = "fused", since = "1.26.0")]
impl FusedIterator for ToUppercase {}

Expand Down Expand Up @@ -481,6 +495,26 @@ impl Iterator for CaseMappingIter {
}
}

impl DoubleEndedIterator for CaseMappingIter {
fn next_back(&mut self) -> Option<char> {
match *self {
CaseMappingIter::Three(a, b, c) => {
*self = CaseMappingIter::Two(a, b);
Some(c)
}
CaseMappingIter::Two(a, b) => {
*self = CaseMappingIter::One(a);
Some(b)
}
CaseMappingIter::One(a) => {
*self = CaseMappingIter::Zero;
Some(a)
}
CaseMappingIter::Zero => None,
}
}
}

impl fmt::Display for CaseMappingIter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/tests/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ fn test_to_lowercase() {
let iter: String = c.to_lowercase().collect();
let disp: String = c.to_lowercase().to_string();
assert_eq!(iter, disp);
assert!(iter.chars().rev().eq(c.to_lowercase().rev()));
iter
}
assert_eq!(lower('A'), "a");
Expand Down Expand Up @@ -108,6 +109,7 @@ fn test_to_uppercase() {
let iter: String = c.to_uppercase().collect();
let disp: String = c.to_uppercase().to_string();
assert_eq!(iter, disp);
assert!(iter.chars().rev().eq(c.to_uppercase().rev()));
iter
}
assert_eq!(upper('a'), "A");
Expand Down