Skip to content

Commit 1a402a5

Browse files
author
Clar Charr
committed
DoubleEndedIterator and ExactSizeIterator for To*case.
1 parent fe597dc commit 1a402a5

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

src/libstd_unicode/char.rs

+106-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#![stable(feature = "rust1", since = "1.0.0")]
3030

3131
use core::char::CharExt as C;
32-
use core::iter::FusedIterator;
32+
use core::iter::{FusedIterator, TrustedLen};
3333
use core::fmt::{self, Write};
3434
use tables::{conversions, derived_property, general_category, property};
3535

@@ -63,11 +63,40 @@ impl Iterator for ToLowercase {
6363
fn next(&mut self) -> Option<char> {
6464
self.0.next()
6565
}
66+
fn size_hint(&self) -> (usize, Option<usize>) {
67+
self.0.size_hint()
68+
}
69+
fn count(self) -> usize {
70+
self.0.count()
71+
}
72+
fn last(self) -> Option<char> {
73+
self.0.last()
74+
}
75+
}
76+
77+
#[stable(feature = "to_case_extra", since = "1.17.0")]
78+
impl DoubleEndedIterator for ToLowercase {
79+
fn next_back(&mut self) -> Option<char> {
80+
self.0.next_back()
81+
}
82+
}
83+
84+
#[stable(feature = "to_case_extra", since = "1.17.0")]
85+
impl ExactSizeIterator for ToLowercase {
86+
fn len(&self) -> usize {
87+
self.0.len()
88+
}
89+
fn is_empty(&self) -> bool {
90+
self.0.is_empty()
91+
}
6692
}
6793

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

97+
#[unstable(feature = "trusted_len", issue = "37572")]
98+
unsafe impl TrustedLen for ToLowercase {}
99+
71100
/// Returns an iterator that yields the uppercase equivalent of a `char`.
72101
///
73102
/// This `struct` is created by the [`to_uppercase()`] method on [`char`]. See
@@ -84,11 +113,40 @@ impl Iterator for ToUppercase {
84113
fn next(&mut self) -> Option<char> {
85114
self.0.next()
86115
}
116+
fn size_hint(&self) -> (usize, Option<usize>) {
117+
self.0.size_hint()
118+
}
119+
fn count(self) -> usize {
120+
self.0.count()
121+
}
122+
fn last(self) -> Option<char> {
123+
self.0.last()
124+
}
125+
}
126+
127+
#[stable(feature = "to_case_extra", since = "1.17.0")]
128+
impl DoubleEndedIterator for ToUppercase {
129+
fn next_back(&mut self) -> Option<char> {
130+
self.0.next_back()
131+
}
132+
}
133+
134+
#[stable(feature = "to_case_extra", since = "1.17.0")]
135+
impl ExactSizeIterator for ToUppercase {
136+
fn len(&self) -> usize {
137+
self.0.len()
138+
}
139+
fn is_empty(&self) -> bool {
140+
self.0.is_empty()
141+
}
87142
}
88143

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

147+
#[unstable(feature = "trusted_len", issue = "37572")]
148+
unsafe impl TrustedLen for ToUppercase {}
149+
92150
enum CaseMappingIter {
93151
Three(char, char, char),
94152
Two(char, char),
@@ -129,6 +187,53 @@ impl Iterator for CaseMappingIter {
129187
CaseMappingIter::Zero => None,
130188
}
131189
}
190+
fn size_hint(&self) -> (usize, Option<usize>) {
191+
(self.len(), Some(self.len()))
192+
}
193+
fn count(self) -> usize {
194+
self.len()
195+
}
196+
fn last(mut self) -> Option<char> {
197+
self.next_back()
198+
}
199+
}
200+
201+
impl DoubleEndedIterator for CaseMappingIter {
202+
fn next_back(&mut self) -> Option<char> {
203+
match *self {
204+
CaseMappingIter::Three(a, b, c) => {
205+
*self = CaseMappingIter::Two(a, b);
206+
Some(c)
207+
}
208+
CaseMappingIter::Two(a, b) => {
209+
*self = CaseMappingIter::One(a);
210+
Some(b)
211+
}
212+
CaseMappingIter::One(a) => {
213+
*self = CaseMappingIter::Zero;
214+
Some(a)
215+
}
216+
CaseMappingIter::Zero => None,
217+
}
218+
}
219+
}
220+
221+
impl ExactSizeIterator for CaseMappingIter {
222+
fn len(&self) -> usize {
223+
match *self {
224+
CaseMappingIter::Three(..) => 3,
225+
CaseMappingIter::Two(..) => 2,
226+
CaseMappingIter::One(..) => 1,
227+
CaseMappingIter::Zero => 0,
228+
}
229+
}
230+
fn is_empty(&self) -> bool {
231+
if let CaseMappingIter::Zero = *self {
232+
true
233+
} else {
234+
false
235+
}
236+
}
132237
}
133238

134239
#[stable(feature = "char_struct_display", since = "1.17.0")]

src/libstd_unicode/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535
#![feature(char_escape_debug)]
3636
#![feature(core_char_ext)]
3737
#![feature(decode_utf8)]
38+
#![feature(exact_size_is_empty)]
3839
#![feature(fused)]
3940
#![feature(lang_items)]
4041
#![feature(staged_api)]
42+
#![feature(trusted_len)]
4143
#![feature(try_from)]
4244

4345
mod tables;

0 commit comments

Comments
 (0)