Skip to content

Commit 46846ea

Browse files
committed
Auto merge of rust-lang#14082 - lowr:fix/change-non-ascii-cases, r=Veykril
fix: support non-ascii characters in case conversion Fixes rust-lang#13521 (the attribute problem is tracked in another issue, as commented) Note that other functions like `to_camel_case()` and `is_lower_snake_case()` already handle non-ascii characters.
2 parents 04850a1 + 98c8077 commit 46846ea

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

crates/hir-ty/src/diagnostics/decl_check/case_conv.rs

+2
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ mod tests {
162162
check(to_lower_snake_case, "a", expect![[""]]);
163163
check(to_lower_snake_case, "abc", expect![[""]]);
164164
check(to_lower_snake_case, "foo__bar", expect![["foo_bar"]]);
165+
check(to_lower_snake_case, "Δ", expect!["δ"]);
165166
}
166167

167168
#[test]
@@ -195,5 +196,6 @@ mod tests {
195196
check(to_upper_snake_case, "X86_64", expect![[""]]);
196197
check(to_upper_snake_case, "FOO_BAr", expect![["FOO_BAR"]]);
197198
check(to_upper_snake_case, "FOO__BAR", expect![["FOO_BAR"]]);
199+
check(to_upper_snake_case, "ß", expect!["SS"]);
198200
}
199201
}

crates/stdx/src/lib.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
33
#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
44

5+
use std::io as sio;
56
use std::process::Command;
67
use std::{cmp::Ordering, ops, time::Instant};
7-
use std::{io as sio, iter};
88

99
mod macros;
1010
pub mod hash;
@@ -39,15 +39,19 @@ Uncomment `default = [ "backtrace" ]` in `crates/stdx/Cargo.toml`.
3939
}
4040

4141
pub fn to_lower_snake_case(s: &str) -> String {
42-
to_snake_case(s, char::to_ascii_lowercase)
42+
to_snake_case(s, char::to_lowercase)
4343
}
4444
pub fn to_upper_snake_case(s: &str) -> String {
45-
to_snake_case(s, char::to_ascii_uppercase)
45+
to_snake_case(s, char::to_uppercase)
4646
}
4747

4848
// Code partially taken from rust/compiler/rustc_lint/src/nonstandard_style.rs
4949
// commit: 9626f2b
50-
fn to_snake_case<F: Fn(&char) -> char>(mut s: &str, change_case: F) -> String {
50+
fn to_snake_case<F, I>(mut s: &str, change_case: F) -> String
51+
where
52+
F: Fn(char) -> I,
53+
I: Iterator<Item = char>,
54+
{
5155
let mut words = vec![];
5256

5357
// Preserve leading underscores
@@ -75,7 +79,7 @@ fn to_snake_case<F: Fn(&char) -> char>(mut s: &str, change_case: F) -> String {
7579
}
7680

7781
last_upper = ch.is_uppercase();
78-
buf.extend(iter::once(change_case(&ch)));
82+
buf.extend(change_case(ch));
7983
}
8084

8185
words.push(buf);

0 commit comments

Comments
 (0)