Skip to content

DoubleEndedIterator and ExactSizeIterator for To*case. #38968

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

Closed
wants to merge 1 commit into from
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
107 changes: 106 additions & 1 deletion src/libstd_unicode/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#![stable(feature = "rust1", since = "1.0.0")]

use core::char::CharExt as C;
use core::iter::FusedIterator;
use core::iter::{FusedIterator, TrustedLen};
use core::fmt::{self, Write};
use tables::{conversions, derived_property, general_category, property};

Expand Down Expand Up @@ -63,11 +63,40 @@ impl Iterator for ToLowercase {
fn next(&mut self) -> Option<char> {
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
fn count(self) -> usize {
self.0.count()
}
fn last(self) -> Option<char> {
self.0.last()
}
}

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

#[stable(feature = "to_case_extra", since = "1.17.0")]
impl ExactSizeIterator for ToLowercase {
fn len(&self) -> usize {
self.0.len()
}
fn is_empty(&self) -> bool {
self.0.is_empty()
}
}

#[unstable(feature = "fused", issue = "35602")]
impl FusedIterator for ToLowercase {}

#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl TrustedLen for ToLowercase {}

/// Returns an iterator that yields the uppercase equivalent of a `char`.
///
/// This `struct` is created by the [`to_uppercase()`] method on [`char`]. See
Expand All @@ -84,11 +113,40 @@ impl Iterator for ToUppercase {
fn next(&mut self) -> Option<char> {
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
fn count(self) -> usize {
self.0.count()
}
fn last(self) -> Option<char> {
self.0.last()
}
}

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

#[stable(feature = "to_case_extra", since = "1.17.0")]
impl ExactSizeIterator for ToUppercase {
fn len(&self) -> usize {
self.0.len()
}
fn is_empty(&self) -> bool {
self.0.is_empty()
}
}

#[unstable(feature = "fused", issue = "35602")]
impl FusedIterator for ToUppercase {}

#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl TrustedLen for ToUppercase {}

enum CaseMappingIter {
Three(char, char, char),
Two(char, char),
Expand Down Expand Up @@ -129,6 +187,53 @@ impl Iterator for CaseMappingIter {
CaseMappingIter::Zero => None,
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len(), Some(self.len()))
}
fn count(self) -> usize {
self.len()
}
fn last(mut self) -> Option<char> {
self.next_back()
}
}

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 ExactSizeIterator for CaseMappingIter {
fn len(&self) -> usize {
match *self {
CaseMappingIter::Three(..) => 3,
CaseMappingIter::Two(..) => 2,
CaseMappingIter::One(..) => 1,
CaseMappingIter::Zero => 0,
}
}
fn is_empty(&self) -> bool {
if let CaseMappingIter::Zero = *self {
true
} else {
false
}
}
}

#[stable(feature = "char_struct_display", since = "1.17.0")]
Expand Down
2 changes: 2 additions & 0 deletions src/libstd_unicode/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
#![feature(char_escape_debug)]
#![feature(core_char_ext)]
#![feature(decode_utf8)]
#![feature(exact_size_is_empty)]
#![feature(fused)]
#![feature(lang_items)]
#![feature(staged_api)]
#![feature(trusted_len)]
#![feature(try_from)]

mod tables;
Expand Down