mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-26 20:09:19 +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
|
@ -10,7 +10,7 @@
|
||||||
//! - static items (e.g. `static FOO: u8 = 10;`)
|
//! - static items (e.g. `static FOO: u8 = 10;`)
|
||||||
//! - match arm bindings (e.g. `foo @ Some(_)`)
|
//! - match arm bindings (e.g. `foo @ Some(_)`)
|
||||||
|
|
||||||
mod str_helpers;
|
mod case_conv;
|
||||||
|
|
||||||
use hir_def::{
|
use hir_def::{
|
||||||
adt::VariantData,
|
adt::VariantData,
|
||||||
|
@ -29,7 +29,7 @@ use syntax::{
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::HirDatabase,
|
db::HirDatabase,
|
||||||
diagnostics::{decl_check::str_helpers::*, CaseType, IncorrectCase},
|
diagnostics::{decl_check::case_conv::*, CaseType, IncorrectCase},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(super) struct DeclValidator<'a, 'b: 'a> {
|
pub(super) struct DeclValidator<'a, 'b: 'a> {
|
||||||
|
|
|
@ -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.
|
// Normalize the string from whatever form it's in currently, and then just make it uppercase.
|
||||||
let upper_snake_case =
|
let upper_snake_case = stdx::to_upper_snake_case(ident);
|
||||||
stdx::to_lower_snake_case(ident).chars().map(|c| c.to_ascii_uppercase()).collect();
|
|
||||||
|
|
||||||
if upper_snake_case == ident {
|
if upper_snake_case == ident {
|
||||||
// While we didn't detect the correct case at the beginning, there
|
// While we didn't detect the correct case at the beginning, there
|
|
@ -28,7 +28,7 @@ pub fn timeit(label: &'static str) -> impl Drop {
|
||||||
Guard { label, start: Instant::now() }
|
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 buf = String::with_capacity(s.len());
|
||||||
let mut prev = false;
|
let mut prev = false;
|
||||||
for c in s.chars() {
|
for c in s.chars() {
|
||||||
|
@ -41,11 +41,19 @@ pub fn to_lower_snake_case(s: &str) -> String {
|
||||||
}
|
}
|
||||||
prev = true;
|
prev = true;
|
||||||
|
|
||||||
buf.push(c.to_ascii_lowercase());
|
buf.push(change_case(&c));
|
||||||
}
|
}
|
||||||
buf
|
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) {
|
pub fn replace(buf: &mut String, from: char, to: &str) {
|
||||||
if !buf.contains(from) {
|
if !buf.contains(from) {
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue