Skip to content

Commit a2c62a6

Browse files
committed
fix #124714 str.to_lowercase sigma handling
1 parent d287f3e commit a2c62a6

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

library/alloc/src/str.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#![allow(unused_imports)]
99

1010
use core::borrow::{Borrow, BorrowMut};
11+
use core::intrinsics::unlikely;
1112
use core::iter::FusedIterator;
1213
use core::mem;
1314
use core::ptr;
@@ -375,14 +376,16 @@ impl str {
375376
// Safety: We have written only valid ASCII to our vec
376377
let mut s = unsafe { String::from_utf8_unchecked(out) };
377378

378-
for (i, c) in rest[..].char_indices() {
379-
if c == 'Σ' {
379+
for (i, c) in rest.char_indices() {
380+
if unlikely(c == 'Σ') {
380381
// Σ maps to σ, except at the end of a word where it maps to ς.
381382
// This is the only conditional (contextual) but language-independent mapping
382383
// in `SpecialCasing.txt`,
383384
// so hard-code it rather than have a generic "condition" mechanism.
384385
// See https://github.com/rust-lang/rust/issues/26035
385-
map_uppercase_sigma(rest, i, &mut s)
386+
let out_len = self.len() - rest.len();
387+
let sigma_lowercase = map_uppercase_sigma(&self, i + out_len);
388+
s.push(sigma_lowercase);
386389
} else {
387390
match conversions::to_lower(c) {
388391
[a, '\0', _] => s.push(a),
@@ -400,13 +403,13 @@ impl str {
400403
}
401404
return s;
402405

403-
fn map_uppercase_sigma(from: &str, i: usize, to: &mut String) {
406+
fn map_uppercase_sigma(from: &str, i: usize) -> char {
404407
// See https://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G33992
405408
// for the definition of `Final_Sigma`.
406409
debug_assert!('Σ'.len_utf8() == 2);
407410
let is_word_final = case_ignorable_then_cased(from[..i].chars().rev())
408411
&& !case_ignorable_then_cased(from[i + 2..].chars());
409-
to.push_str(if is_word_final { "ς" } else { "σ" });
412+
if is_word_final { 'ς' } else { 'σ' }
410413
}
411414

412415
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)