Add to_upper_snake_case function to stdx

This commit is contained in:
Igor Aleksanov 2020-10-08 08:33:35 +03:00
parent ebd30033b3
commit 559cc97073
3 changed files with 13 additions and 6 deletions

View file

@ -10,7 +10,7 @@
//! - static items (e.g. `static FOO: u8 = 10;`)
//! - match arm bindings (e.g. `foo @ Some(_)`)
mod str_helpers;
mod case_conv;
use hir_def::{
adt::VariantData,
@ -29,7 +29,7 @@ use syntax::{
use crate::{
db::HirDatabase,
diagnostics::{decl_check::str_helpers::*, CaseType, IncorrectCase},
diagnostics::{decl_check::case_conv::*, CaseType, IncorrectCase},
};
pub(super) struct DeclValidator<'a, 'b: 'a> {

View file

@ -136,8 +136,7 @@ pub fn to_upper_snake_case(ident: &str) -> Option<String> {
}
// Normalize the string from whatever form it's in currently, and then just make it uppercase.
let upper_snake_case =
stdx::to_lower_snake_case(ident).chars().map(|c| c.to_ascii_uppercase()).collect();
let upper_snake_case = stdx::to_upper_snake_case(ident);
if upper_snake_case == ident {
// While we didn't detect the correct case at the beginning, there

View file

@ -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;