Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 40f80e2

Browse files
committed
move to_camel_case and char_has_case from case_conv to stdx
1 parent d3cc3bc commit 40f80e2

File tree

2 files changed

+55
-50
lines changed

2 files changed

+55
-50
lines changed

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

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,7 @@ pub(crate) fn to_camel_case(ident: &str) -> Option<String> {
1111
return None;
1212
}
1313

14-
// Taken from rustc.
15-
let ret = ident
16-
.trim_matches('_')
17-
.split('_')
18-
.filter(|component| !component.is_empty())
19-
.map(|component| {
20-
let mut camel_cased_component = String::with_capacity(component.len());
21-
22-
let mut new_word = true;
23-
let mut prev_is_lower_case = true;
24-
25-
for c in component.chars() {
26-
// Preserve the case if an uppercase letter follows a lowercase letter, so that
27-
// `camelCase` is converted to `CamelCase`.
28-
if prev_is_lower_case && c.is_uppercase() {
29-
new_word = true;
30-
}
31-
32-
if new_word {
33-
camel_cased_component.extend(c.to_uppercase());
34-
} else {
35-
camel_cased_component.extend(c.to_lowercase());
36-
}
37-
38-
prev_is_lower_case = c.is_lowercase();
39-
new_word = false;
40-
}
41-
42-
camel_cased_component
43-
})
44-
.fold((String::new(), None), |(acc, prev): (_, Option<String>), next| {
45-
// separate two components with an underscore if their boundary cannot
46-
// be distinguished using an uppercase/lowercase case distinction
47-
let join = prev
48-
.and_then(|prev| {
49-
let f = next.chars().next()?;
50-
let l = prev.chars().last()?;
51-
Some(!char_has_case(l) && !char_has_case(f))
52-
})
53-
.unwrap_or(false);
54-
(acc + if join { "_" } else { "" } + &next, Some(next))
55-
})
56-
.0;
57-
Some(ret)
14+
Some(stdx::to_camel_case(ident))
5815
}
5916

6017
/// Converts an identifier to a lower_snake_case form.
@@ -97,7 +54,9 @@ fn is_camel_case(name: &str) -> bool {
9754
&& !name.chars().any(|snd| {
9855
let ret = match fst {
9956
None => false,
100-
Some(fst) => char_has_case(fst) && snd == '_' || char_has_case(snd) && fst == '_',
57+
Some(fst) => {
58+
stdx::char_has_case(fst) && snd == '_' || stdx::char_has_case(snd) && fst == '_'
59+
}
10160
};
10261
fst = Some(snd);
10362

@@ -135,11 +94,6 @@ fn is_snake_case<F: Fn(char) -> bool>(ident: &str, wrong_case: F) -> bool {
13594
})
13695
}
13796

138-
// Taken from rustc.
139-
fn char_has_case(c: char) -> bool {
140-
c.is_lowercase() || c.is_uppercase()
141-
}
142-
14397
#[cfg(test)]
14498
mod tests {
14599
use super::*;

crates/stdx/src/lib.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,57 @@ where
8989
words.join("_")
9090
}
9191

92+
// Taken from rustc.
93+
pub fn to_camel_case(ident: &str) -> String {
94+
ident
95+
.trim_matches('_')
96+
.split('_')
97+
.filter(|component| !component.is_empty())
98+
.map(|component| {
99+
let mut camel_cased_component = String::with_capacity(component.len());
100+
101+
let mut new_word = true;
102+
let mut prev_is_lower_case = true;
103+
104+
for c in component.chars() {
105+
// Preserve the case if an uppercase letter follows a lowercase letter, so that
106+
// `camelCase` is converted to `CamelCase`.
107+
if prev_is_lower_case && c.is_uppercase() {
108+
new_word = true;
109+
}
110+
111+
if new_word {
112+
camel_cased_component.extend(c.to_uppercase());
113+
} else {
114+
camel_cased_component.extend(c.to_lowercase());
115+
}
116+
117+
prev_is_lower_case = c.is_lowercase();
118+
new_word = false;
119+
}
120+
121+
camel_cased_component
122+
})
123+
.fold((String::new(), None), |(acc, prev): (_, Option<String>), next| {
124+
// separate two components with an underscore if their boundary cannot
125+
// be distinguished using an uppercase/lowercase case distinction
126+
let join = prev
127+
.and_then(|prev| {
128+
let f = next.chars().next()?;
129+
let l = prev.chars().last()?;
130+
Some(!char_has_case(l) && !char_has_case(f))
131+
})
132+
.unwrap_or(false);
133+
(acc + if join { "_" } else { "" } + &next, Some(next))
134+
})
135+
.0
136+
}
137+
138+
// Taken from rustc.
139+
pub fn char_has_case(c: char) -> bool {
140+
c.is_lowercase() || c.is_uppercase()
141+
}
142+
92143
pub fn replace(buf: &mut String, from: char, to: &str) {
93144
if !buf.contains(from) {
94145
return;

0 commit comments

Comments
 (0)