Skip to content

Commit 50d7b9d

Browse files
committed
fix #124714 str.to_lowercase sigma handling
1 parent 6d721dd commit 50d7b9d

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

library/alloc/src/str.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -368,21 +368,17 @@ impl str {
368368
pub fn to_lowercase(&self) -> String {
369369
let out = convert_while_ascii(self.as_bytes(), u8::to_ascii_lowercase);
370370

371-
// Safety: we know this is a valid char boundary since
372-
// out.len() is only progressed if ascii bytes are found
373-
let rest = unsafe { self.get_unchecked(out.len()..) };
374-
375371
// Safety: We have written only valid ASCII to our vec
376372
let mut s = unsafe { String::from_utf8_unchecked(out) };
377373

378-
for (i, c) in rest[..].char_indices() {
374+
for (i, c) in self.char_indices().skip(s.len()) {
379375
if c == 'Σ' {
380376
// Σ maps to σ, except at the end of a word where it maps to ς.
381377
// This is the only conditional (contextual) but language-independent mapping
382378
// in `SpecialCasing.txt`,
383379
// so hard-code it rather than have a generic "condition" mechanism.
384380
// See https://github.com/rust-lang/rust/issues/26035
385-
map_uppercase_sigma(rest, i, &mut s)
381+
s.push(map_uppercase_sigma(&self, i));
386382
} else {
387383
match conversions::to_lower(c) {
388384
[a, '\0', _] => s.push(a),
@@ -400,13 +396,13 @@ impl str {
400396
}
401397
return s;
402398

403-
fn map_uppercase_sigma(from: &str, i: usize, to: &mut String) {
399+
fn map_uppercase_sigma(from: &str, i: usize) -> char {
404400
// See https://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G33992
405401
// for the definition of `Final_Sigma`.
406402
debug_assert!('Σ'.len_utf8() == 2);
407403
let is_word_final = case_ignorable_then_cased(from[..i].chars().rev())
408404
&& !case_ignorable_then_cased(from[i + 2..].chars());
409-
to.push_str(if is_word_final { "ς" } else { "σ" });
405+
if is_word_final { 'ς' } else { 'σ' }
410406
}
411407

412408
fn case_ignorable_then_cased<I: Iterator<Item = char>>(iter: I) -> bool {

library/alloc/tests/str.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,9 @@ fn to_lowercase() {
18481848
assert_eq!("ΑΣ'Α".to_lowercase(), "ασ'α");
18491849
assert_eq!("ΑΣ''Α".to_lowercase(), "ασ''α");
18501850

1851+
// https://github.com/rust-lang/rust/issues/124714
1852+
assert_eq!("abcdefghijklmnopΣ".to_lowercase(), "abcdefghijklmnopς");
1853+
18511854
// a really long string that has it's lowercase form
18521855
// even longer. this tests that implementations don't assume
18531856
// an incorrect upper bound on allocations

0 commit comments

Comments
 (0)