From 1a402a5fafb212fcbbfc943a86a8afdcd86fa4da Mon Sep 17 00:00:00 2001 From: Clar Charr Date: Tue, 10 Jan 2017 17:26:02 -0500 Subject: [PATCH] DoubleEndedIterator and ExactSizeIterator for To*case. --- src/libstd_unicode/char.rs | 107 ++++++++++++++++++++++++++++++++++++- src/libstd_unicode/lib.rs | 2 + 2 files changed, 108 insertions(+), 1 deletion(-) diff --git a/src/libstd_unicode/char.rs b/src/libstd_unicode/char.rs index f2c53efda1737..e4b3e84617cd1 100644 --- a/src/libstd_unicode/char.rs +++ b/src/libstd_unicode/char.rs @@ -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}; @@ -63,11 +63,40 @@ impl Iterator for ToLowercase { fn next(&mut self) -> Option { self.0.next() } + fn size_hint(&self) -> (usize, Option) { + self.0.size_hint() + } + fn count(self) -> usize { + self.0.count() + } + fn last(self) -> Option { + self.0.last() + } +} + +#[stable(feature = "to_case_extra", since = "1.17.0")] +impl DoubleEndedIterator for ToLowercase { + fn next_back(&mut self) -> Option { + 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 @@ -84,11 +113,40 @@ impl Iterator for ToUppercase { fn next(&mut self) -> Option { self.0.next() } + fn size_hint(&self) -> (usize, Option) { + self.0.size_hint() + } + fn count(self) -> usize { + self.0.count() + } + fn last(self) -> Option { + self.0.last() + } +} + +#[stable(feature = "to_case_extra", since = "1.17.0")] +impl DoubleEndedIterator for ToUppercase { + fn next_back(&mut self) -> Option { + 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), @@ -129,6 +187,53 @@ impl Iterator for CaseMappingIter { CaseMappingIter::Zero => None, } } + fn size_hint(&self) -> (usize, Option) { + (self.len(), Some(self.len())) + } + fn count(self) -> usize { + self.len() + } + fn last(mut self) -> Option { + self.next_back() + } +} + +impl DoubleEndedIterator for CaseMappingIter { + fn next_back(&mut self) -> Option { + 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")] diff --git a/src/libstd_unicode/lib.rs b/src/libstd_unicode/lib.rs index d52d1549b5173..c40776f19c90a 100644 --- a/src/libstd_unicode/lib.rs +++ b/src/libstd_unicode/lib.rs @@ -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;