Skip to content

Only convert chars when needed in str case conversion methods. #52061

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 3 commits 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
1 change: 1 addition & 0 deletions src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
#![feature(ptr_internals)]
#![feature(ptr_offset_from)]
#![feature(rustc_attrs)]
#![feature(slice_index_methods)]
#![feature(specialization)]
#![feature(split_ascii_whitespace)]
#![feature(staged_api)]
Expand Down
56 changes: 46 additions & 10 deletions src/liballoc/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,18 +360,35 @@ impl str {
#[stable(feature = "unicode_case_mapping", since = "1.2.0")]
pub fn to_lowercase(&self) -> String {
let mut s = String::with_capacity(self.len());
let mut lower = 0;
for (i, c) in self[..].char_indices() {
if c == 'Σ' {
// Σ maps to σ, except at the end of a word where it maps to ς.
// This is the only conditional (contextual) but language-independent mapping
// in `SpecialCasing.txt`,
// so hard-code it rather than have a generic "condition" mechanism.
// See https://github.com/rust-lang/rust/issues/26035
map_uppercase_sigma(self, i, &mut s)
} else {
s.extend(c.to_lowercase());
// Lowercase `char`s are inserted into `s` in slices,
// uppercase `char`s are converted to lowercase and inserted individually.
if c.is_uppercase() {
if lower != i {
unsafe {
// lower..i is an interval of lowercase `char`s before `c`.
s.push_str((lower..i).get_unchecked(self));
}
}
if c == 'Σ' {
// Σ maps to σ, except at the end of a word where it maps to ς.
// This is the only conditional (contextual) but language-independent mapping
// in `SpecialCasing.txt`,
// so hard-code it rather than have a generic "condition" mechanism.
// See https://github.com/rust-lang/rust/issues/26035
map_uppercase_sigma(self, i, &mut s);
} else {
s.extend(c.to_lowercase());
}

// The next possible interval of lowercase `char`s start after `c`.
lower = i + c.len_utf8();
}
}
unsafe {
s.push_str((lower..self.len()).get_unchecked(self));
}
return s;

fn map_uppercase_sigma(from: &str, i: usize, to: &mut String) {
Expand Down Expand Up @@ -423,7 +440,26 @@ impl str {
#[stable(feature = "unicode_case_mapping", since = "1.2.0")]
pub fn to_uppercase(&self) -> String {
let mut s = String::with_capacity(self.len());
s.extend(self.chars().flat_map(|c| c.to_uppercase()));
let mut lower = 0;
for (i, c) in self[..].char_indices() {
// Uppercase `char`s are inserted into `s` in slices,
// lowercase `char`s are converted to uppercase and inserted individually.
if c.is_lowercase() {
if lower != i {
unsafe {
// lower..i is an interval of uppercase `char`s before `c`.
s.push_str((lower..i).get_unchecked(self));
}
}
s.extend(c.to_uppercase());

// The next possible interval of uppercase `char`s start after `c`.
lower = i + c.len_utf8();
}
}
unsafe {
s.push_str((lower..self.len()).get_unchecked(self));
}
return s;
}

Expand Down