about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRyo Yoshida <low.ryoshida@gmail.com>2023-02-03 18:11:11 +0900
committerRyo Yoshida <low.ryoshida@gmail.com>2023-02-03 18:11:11 +0900
commit98c8077495583ddb4102e8db6c62331b1ae13ec2 (patch)
treeaab2656e75145b667fa62b3b7ddc8bd0f59d2822
parent04850a192cd82f94ddbd3f4b202d0e4ea3e62daa (diff)
fix: support non-ascii characters in case conversion
-rw-r--r--crates/hir-ty/src/diagnostics/decl_check/case_conv.rs2
-rw-r--r--crates/stdx/src/lib.rs14
2 files changed, 11 insertions, 5 deletions
diff --git a/crates/hir-ty/src/diagnostics/decl_check/case_conv.rs b/crates/hir-ty/src/diagnostics/decl_check/case_conv.rs
index 88d607194f7..2c136896209 100644
--- a/crates/hir-ty/src/diagnostics/decl_check/case_conv.rs
+++ b/crates/hir-ty/src/diagnostics/decl_check/case_conv.rs
@@ -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"]);
     }
 }
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs
index 51e109798d1..bd24d7d28ba 100644
--- a/crates/stdx/src/lib.rs
+++ b/crates/stdx/src/lib.rs
@@ -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);