about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRyan Mehri <ryan.mehri1@gmail.com>2023-09-30 08:04:04 -0700
committerRyan Mehri <ryan.mehri1@gmail.com>2023-09-30 08:04:04 -0700
commit40f80e29a5f33a5e5209f3075aa771789a3b6002 (patch)
tree536ce8f61d50999d144fa55aed4067ef12d681c1
parentd3cc3bc00e310ff49268ce0c593eaa6bf4724bbd (diff)
downloadrust-40f80e29a5f33a5e5209f3075aa771789a3b6002.tar.gz
rust-40f80e29a5f33a5e5209f3075aa771789a3b6002.zip
move `to_camel_case` and `char_has_case` from case_conv to stdx
-rw-r--r--crates/hir-ty/src/diagnostics/decl_check/case_conv.rs54
-rw-r--r--crates/stdx/src/lib.rs51
2 files changed, 55 insertions, 50 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 2c136896209..cbe1af15703 100644
--- a/crates/hir-ty/src/diagnostics/decl_check/case_conv.rs
+++ b/crates/hir-ty/src/diagnostics/decl_check/case_conv.rs
@@ -11,50 +11,7 @@ pub(crate) fn to_camel_case(ident: &str) -> Option<String> {
         return None;
     }
 
-    // Taken from rustc.
-    let ret = ident
-        .trim_matches('_')
-        .split('_')
-        .filter(|component| !component.is_empty())
-        .map(|component| {
-            let mut camel_cased_component = String::with_capacity(component.len());
-
-            let mut new_word = true;
-            let mut prev_is_lower_case = true;
-
-            for c in component.chars() {
-                // Preserve the case if an uppercase letter follows a lowercase letter, so that
-                // `camelCase` is converted to `CamelCase`.
-                if prev_is_lower_case && c.is_uppercase() {
-                    new_word = true;
-                }
-
-                if new_word {
-                    camel_cased_component.extend(c.to_uppercase());
-                } else {
-                    camel_cased_component.extend(c.to_lowercase());
-                }
-
-                prev_is_lower_case = c.is_lowercase();
-                new_word = false;
-            }
-
-            camel_cased_component
-        })
-        .fold((String::new(), None), |(acc, prev): (_, Option<String>), next| {
-            // separate two components with an underscore if their boundary cannot
-            // be distinguished using an uppercase/lowercase case distinction
-            let join = prev
-                .and_then(|prev| {
-                    let f = next.chars().next()?;
-                    let l = prev.chars().last()?;
-                    Some(!char_has_case(l) && !char_has_case(f))
-                })
-                .unwrap_or(false);
-            (acc + if join { "_" } else { "" } + &next, Some(next))
-        })
-        .0;
-    Some(ret)
+    Some(stdx::to_camel_case(ident))
 }
 
 /// Converts an identifier to a lower_snake_case form.
@@ -97,7 +54,9 @@ fn is_camel_case(name: &str) -> bool {
         && !name.chars().any(|snd| {
             let ret = match fst {
                 None => false,
-                Some(fst) => char_has_case(fst) && snd == '_' || char_has_case(snd) && fst == '_',
+                Some(fst) => {
+                    stdx::char_has_case(fst) && snd == '_' || stdx::char_has_case(snd) && fst == '_'
+                }
             };
             fst = Some(snd);
 
@@ -135,11 +94,6 @@ fn is_snake_case<F: Fn(char) -> bool>(ident: &str, wrong_case: F) -> bool {
     })
 }
 
-// Taken from rustc.
-fn char_has_case(c: char) -> bool {
-    c.is_lowercase() || c.is_uppercase()
-}
-
 #[cfg(test)]
 mod tests {
     use super::*;
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs
index 24990d6a0e7..89c54eee55f 100644
--- a/crates/stdx/src/lib.rs
+++ b/crates/stdx/src/lib.rs
@@ -89,6 +89,57 @@ where
     words.join("_")
 }
 
+// Taken from rustc.
+pub fn to_camel_case(ident: &str) -> String {
+    ident
+        .trim_matches('_')
+        .split('_')
+        .filter(|component| !component.is_empty())
+        .map(|component| {
+            let mut camel_cased_component = String::with_capacity(component.len());
+
+            let mut new_word = true;
+            let mut prev_is_lower_case = true;
+
+            for c in component.chars() {
+                // Preserve the case if an uppercase letter follows a lowercase letter, so that
+                // `camelCase` is converted to `CamelCase`.
+                if prev_is_lower_case && c.is_uppercase() {
+                    new_word = true;
+                }
+
+                if new_word {
+                    camel_cased_component.extend(c.to_uppercase());
+                } else {
+                    camel_cased_component.extend(c.to_lowercase());
+                }
+
+                prev_is_lower_case = c.is_lowercase();
+                new_word = false;
+            }
+
+            camel_cased_component
+        })
+        .fold((String::new(), None), |(acc, prev): (_, Option<String>), next| {
+            // separate two components with an underscore if their boundary cannot
+            // be distinguished using an uppercase/lowercase case distinction
+            let join = prev
+                .and_then(|prev| {
+                    let f = next.chars().next()?;
+                    let l = prev.chars().last()?;
+                    Some(!char_has_case(l) && !char_has_case(f))
+                })
+                .unwrap_or(false);
+            (acc + if join { "_" } else { "" } + &next, Some(next))
+        })
+        .0
+}
+
+// Taken from rustc.
+pub fn char_has_case(c: char) -> bool {
+    c.is_lowercase() || c.is_uppercase()
+}
+
 pub fn replace(buf: &mut String, from: char, to: &str) {
     if !buf.contains(from) {
         return;