Skip to content

Commit 559cc97

Browse files
committed
Add to_upper_snake_case function to stdx
1 parent ebd3003 commit 559cc97

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

crates/hir_ty/src/diagnostics/decl_check.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! - static items (e.g. `static FOO: u8 = 10;`)
1111
//! - match arm bindings (e.g. `foo @ Some(_)`)
1212
13-
mod str_helpers;
13+
mod case_conv;
1414

1515
use hir_def::{
1616
adt::VariantData,
@@ -29,7 +29,7 @@ use syntax::{
2929

3030
use crate::{
3131
db::HirDatabase,
32-
diagnostics::{decl_check::str_helpers::*, CaseType, IncorrectCase},
32+
diagnostics::{decl_check::case_conv::*, CaseType, IncorrectCase},
3333
};
3434

3535
pub(super) struct DeclValidator<'a, 'b: 'a> {

crates/hir_ty/src/diagnostics/decl_check/str_helpers.rs renamed to crates/hir_ty/src/diagnostics/decl_check/case_conv.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ pub fn to_upper_snake_case(ident: &str) -> Option<String> {
136136
}
137137

138138
// Normalize the string from whatever form it's in currently, and then just make it uppercase.
139-
let upper_snake_case =
140-
stdx::to_lower_snake_case(ident).chars().map(|c| c.to_ascii_uppercase()).collect();
139+
let upper_snake_case = stdx::to_upper_snake_case(ident);
141140

142141
if upper_snake_case == ident {
143142
// While we didn't detect the correct case at the beginning, there

crates/stdx/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn timeit(label: &'static str) -> impl Drop {
2828
Guard { label, start: Instant::now() }
2929
}
3030

31-
pub fn to_lower_snake_case(s: &str) -> String {
31+
fn to_snake_case<F: Fn(&char) -> char>(s: &str, change_case: F) -> String {
3232
let mut buf = String::with_capacity(s.len());
3333
let mut prev = false;
3434
for c in s.chars() {
@@ -41,11 +41,19 @@ pub fn to_lower_snake_case(s: &str) -> String {
4141
}
4242
prev = true;
4343

44-
buf.push(c.to_ascii_lowercase());
44+
buf.push(change_case(&c));
4545
}
4646
buf
4747
}
4848

49+
pub fn to_lower_snake_case(s: &str) -> String {
50+
to_snake_case(s, char::to_ascii_lowercase)
51+
}
52+
53+
pub fn to_upper_snake_case(s: &str) -> String {
54+
to_snake_case(s, char::to_ascii_uppercase)
55+
}
56+
4957
pub fn replace(buf: &mut String, from: char, to: &str) {
5058
if !buf.contains(from) {
5159
return;

0 commit comments

Comments
 (0)