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

Optimize core::char::CaseMappingIter #122616

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Changes from 1 commit
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
54 changes: 27 additions & 27 deletions library/core/src/char/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,10 @@ impl ExactSizeIterator for ToUppercase {}

#[derive(Debug, Clone)]
enum CaseMappingIter {
Three(char, char, char),
Two(char, char),
One(char),
Zero,
One(char),
Two([char; 2]),
Three([char; 3]),
}

impl CaseMappingIter {
Expand All @@ -455,10 +455,10 @@ impl CaseMappingIter {
if chars[1] == '\0' {
CaseMappingIter::One(chars[0]) // Including if chars[0] == '\0'
} else {
CaseMappingIter::Two(chars[0], chars[1])
CaseMappingIter::Two([chars[0], chars[1]])
}
} else {
CaseMappingIter::Three(chars[0], chars[1], chars[2])
CaseMappingIter::Three([chars[0], chars[1], chars[2]])
}
}
}
Expand All @@ -467,28 +467,28 @@ impl Iterator for CaseMappingIter {
type Item = char;
fn next(&mut self) -> Option<char> {
match *self {
CaseMappingIter::Three(a, b, c) => {
*self = CaseMappingIter::Two(b, c);
Some(a)
CaseMappingIter::Zero => None,
CaseMappingIter::One(c) => {
*self = CaseMappingIter::Zero;
Some(c)
}
CaseMappingIter::Two(b, c) => {
CaseMappingIter::Two([b, c]) => {
*self = CaseMappingIter::One(c);
Some(b)
}
CaseMappingIter::One(c) => {
*self = CaseMappingIter::Zero;
Some(c)
CaseMappingIter::Three([a, b, c]) => {
*self = CaseMappingIter::Two([b, c]);
Some(a)
}
CaseMappingIter::Zero => None,
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
let size = match self {
CaseMappingIter::Three(..) => 3,
CaseMappingIter::Two(..) => 2,
CaseMappingIter::One(_) => 1,
CaseMappingIter::Zero => 0,
CaseMappingIter::One(_) => 1,
CaseMappingIter::Two(..) => 2,
CaseMappingIter::Three(..) => 3,
};
(size, Some(size))
}
Expand All @@ -497,37 +497,37 @@ 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);
CaseMappingIter::Zero => None,
CaseMappingIter::One(c) => {
*self = CaseMappingIter::Zero;
Some(c)
}
CaseMappingIter::Two(b, c) => {
CaseMappingIter::Two([b, c]) => {
*self = CaseMappingIter::One(b);
Some(c)
}
CaseMappingIter::One(c) => {
*self = CaseMappingIter::Zero;
CaseMappingIter::Three([a, b, c]) => {
*self = CaseMappingIter::Two([a, b]);
Some(c)
}
CaseMappingIter::Zero => None,
}
}
}

impl fmt::Display for CaseMappingIter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
CaseMappingIter::Three(a, b, c) => {
f.write_char(a)?;
CaseMappingIter::Zero => Ok(()),
CaseMappingIter::One(c) => f.write_char(c),
CaseMappingIter::Two([b, c]) => {
f.write_char(b)?;
f.write_char(c)
}
CaseMappingIter::Two(b, c) => {
CaseMappingIter::Three([a, b, c]) => {
f.write_char(a)?;
f.write_char(b)?;
f.write_char(c)
}
CaseMappingIter::One(c) => f.write_char(c),
CaseMappingIter::Zero => Ok(()),
}
}
}
Expand Down
Loading