Auto merge of #14082 - lowr:fix/change-non-ascii-cases, r=Veykril

fix: support non-ascii characters in case conversion

Fixes #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.
This commit is contained in:
bors 2023-02-03 09:28:49 +00:00
commit 46846eae97
2 changed files with 11 additions and 5 deletions

View file

@ -162,6 +162,7 @@ mod tests {
check(to_lower_snake_case, "a", expect![[""]]);
check(to_lower_snake_case, "abc", expect![[""]]);
check(to_lower_snake_case, "foo__bar", expect![["foo_bar"]]);
check(to_lower_snake_case, "Δ", expect!["δ"]);
}
#[test]
@ -195,5 +196,6 @@ mod tests {
check(to_upper_snake_case, "X86_64", expect![[""]]);
check(to_upper_snake_case, "FOO_BAr", expect![["FOO_BAR"]]);
check(to_upper_snake_case, "FOO__BAR", expect![["FOO_BAR"]]);
check(to_upper_snake_case, "ß", expect!["SS"]);
}
}

View file

@ -2,9 +2,9 @@
#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
use std::io as sio;
use std::process::Command;
use std::{cmp::Ordering, ops, time::Instant};
use std::{io as sio, iter};
mod macros;
pub mod hash;
@ -39,15 +39,19 @@ Uncomment `default = [ "backtrace" ]` in `crates/stdx/Cargo.toml`.
}
pub fn to_lower_snake_case(s: &str) -> String {
to_snake_case(s, char::to_ascii_lowercase)
to_snake_case(s, char::to_lowercase)
}
pub fn to_upper_snake_case(s: &str) -> String {
to_snake_case(s, char::to_ascii_uppercase)
to_snake_case(s, char::to_uppercase)
}
// Code partially taken from rust/compiler/rustc_lint/src/nonstandard_style.rs
// commit: 9626f2b
fn to_snake_case<F: Fn(&char) -> char>(mut s: &str, change_case: F) -> String {
fn to_snake_case<F, I>(mut s: &str, change_case: F) -> String
where
F: Fn(char) -> I,
I: Iterator<Item = char>,
{
let mut words = vec![];
// Preserve leading underscores
@ -75,7 +79,7 @@ fn to_snake_case<F: Fn(&char) -> char>(mut s: &str, change_case: F) -> String {
}
last_upper = ch.is_uppercase();
buf.extend(iter::once(change_case(&ch)));
buf.extend(change_case(ch));
}
words.push(buf);