mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-03 18:29:23 +00:00
Add to_upper_snake_case function to stdx
This commit is contained in:
parent
ebd30033b3
commit
559cc97073
3 changed files with 13 additions and 6 deletions
|
@ -28,7 +28,7 @@ pub fn timeit(label: &'static str) -> impl Drop {
|
|||
Guard { label, start: Instant::now() }
|
||||
}
|
||||
|
||||
pub fn to_lower_snake_case(s: &str) -> String {
|
||||
fn to_snake_case<F: Fn(&char) -> char>(s: &str, change_case: F) -> String {
|
||||
let mut buf = String::with_capacity(s.len());
|
||||
let mut prev = false;
|
||||
for c in s.chars() {
|
||||
|
@ -41,11 +41,19 @@ pub fn to_lower_snake_case(s: &str) -> String {
|
|||
}
|
||||
prev = true;
|
||||
|
||||
buf.push(c.to_ascii_lowercase());
|
||||
buf.push(change_case(&c));
|
||||
}
|
||||
buf
|
||||
}
|
||||
|
||||
pub fn to_lower_snake_case(s: &str) -> String {
|
||||
to_snake_case(s, char::to_ascii_lowercase)
|
||||
}
|
||||
|
||||
pub fn to_upper_snake_case(s: &str) -> String {
|
||||
to_snake_case(s, char::to_ascii_uppercase)
|
||||
}
|
||||
|
||||
pub fn replace(buf: &mut String, from: char, to: &str) {
|
||||
if !buf.contains(from) {
|
||||
return;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue