-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Implement size_hint for several unicode-based iterators. #50208
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -404,11 +404,16 @@ impl Iterator for ToLowercase { | |
fn next(&mut self) -> Option<char> { | ||
self.0.next() | ||
} | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.0.size_hint() | ||
} | ||
} | ||
|
||
#[stable(feature = "fused", since = "1.26.0")] | ||
impl FusedIterator for ToLowercase {} | ||
|
||
impl ExactSizeIterator 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 | ||
|
@@ -426,11 +431,16 @@ impl Iterator for ToUppercase { | |
fn next(&mut self) -> Option<char> { | ||
self.0.next() | ||
} | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.0.size_hint() | ||
} | ||
} | ||
|
||
#[stable(feature = "fused", since = "1.26.0")] | ||
impl FusedIterator for ToUppercase {} | ||
|
||
impl ExactSizeIterator for ToUppercase {} | ||
|
||
#[derive(Debug, Clone)] | ||
enum CaseMappingIter { | ||
Three(char, char, char), | ||
|
@@ -472,8 +482,26 @@ impl Iterator for CaseMappingIter { | |
CaseMappingIter::Zero => None, | ||
} | ||
} | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
match *self { | ||
CaseMappingIter::Three(_, _, _) => { | ||
(3, Some(3)) | ||
} | ||
CaseMappingIter::Two(_, _) => { | ||
(2, Some(2)) | ||
} | ||
CaseMappingIter::One(_) => { | ||
(1, Some(1)) | ||
} | ||
CaseMappingIter::Zero => (0, Some(0)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While I'm confident this is correct, I think it'd still be good to have a test to pin the behaviour; a bunch of things like |
||
} | ||
} | ||
} | ||
|
||
impl FusedIterator for CaseMappingIter {} | ||
|
||
impl ExactSizeIterator for CaseMappingIter {} | ||
|
||
impl fmt::Display for CaseMappingIter { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match *self { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ use str as core_str; | |
use fmt; | ||
use fmt::Write; | ||
use mem; | ||
use iter::FusedIterator; | ||
|
||
/// Lossy UTF-8 string. | ||
#[unstable(feature = "str_internals", issue = "0")] | ||
|
@@ -146,7 +147,13 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> { | |
broken: &[], | ||
}; | ||
self.source = &[]; | ||
return Some(r); | ||
Some(r) | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
// We might return no characters (we encounter an error). | ||
// We will return the most characters when the input is all ASCII. | ||
(0, Some(self.source.len())) | ||
} | ||
} | ||
|
||
|
@@ -177,6 +184,8 @@ impl fmt::Display for Utf8Lossy { | |
} | ||
} | ||
|
||
impl<'a> FusedIterator for Utf8LossyChunksIter<'a> {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there tests that verify this? (There might be something already; I dunno.) |
||
|
||
impl fmt::Debug for Utf8Lossy { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
f.write_char('"')?; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like the line above it, this should have an attribute, something like
#[stable(feature = "case_mapping_len", since = "1.27.0")]