diff options
author | Igor Aleksanov <[email protected]> | 2020-10-08 06:33:35 +0100 |
---|---|---|
committer | Igor Aleksanov <[email protected]> | 2020-10-12 09:05:00 +0100 |
commit | 559cc970732d80e3ec624c20da4f8aac219d6b2e (patch) | |
tree | cc0efeae2653890f8c178c4b970851d7fc9552af /crates | |
parent | ebd30033b3743fafe0a0182b5ae34ffb27fe43ff (diff) |
Add to_upper_snake_case function to stdx
Diffstat (limited to 'crates')
-rw-r--r-- | crates/hir_ty/src/diagnostics/decl_check.rs | 4 | ||||
-rw-r--r-- | crates/hir_ty/src/diagnostics/decl_check/case_conv.rs (renamed from crates/hir_ty/src/diagnostics/decl_check/str_helpers.rs) | 3 | ||||
-rw-r--r-- | crates/stdx/src/lib.rs | 12 |
3 files changed, 13 insertions, 6 deletions
diff --git a/crates/hir_ty/src/diagnostics/decl_check.rs b/crates/hir_ty/src/diagnostics/decl_check.rs index 4c20921e5..901ccc94f 100644 --- a/crates/hir_ty/src/diagnostics/decl_check.rs +++ b/crates/hir_ty/src/diagnostics/decl_check.rs | |||
@@ -10,7 +10,7 @@ | |||
10 | //! - static items (e.g. `static FOO: u8 = 10;`) | 10 | //! - static items (e.g. `static FOO: u8 = 10;`) |
11 | //! - match arm bindings (e.g. `foo @ Some(_)`) | 11 | //! - match arm bindings (e.g. `foo @ Some(_)`) |
12 | 12 | ||
13 | mod str_helpers; | 13 | mod case_conv; |
14 | 14 | ||
15 | use hir_def::{ | 15 | use hir_def::{ |
16 | adt::VariantData, | 16 | adt::VariantData, |
@@ -29,7 +29,7 @@ use syntax::{ | |||
29 | 29 | ||
30 | use crate::{ | 30 | use crate::{ |
31 | db::HirDatabase, | 31 | db::HirDatabase, |
32 | diagnostics::{decl_check::str_helpers::*, CaseType, IncorrectCase}, | 32 | diagnostics::{decl_check::case_conv::*, CaseType, IncorrectCase}, |
33 | }; | 33 | }; |
34 | 34 | ||
35 | pub(super) struct DeclValidator<'a, 'b: 'a> { | 35 | pub(super) struct DeclValidator<'a, 'b: 'a> { |
diff --git a/crates/hir_ty/src/diagnostics/decl_check/str_helpers.rs b/crates/hir_ty/src/diagnostics/decl_check/case_conv.rs index 2e1468c4c..3800f2a6b 100644 --- a/crates/hir_ty/src/diagnostics/decl_check/str_helpers.rs +++ b/crates/hir_ty/src/diagnostics/decl_check/case_conv.rs | |||
@@ -136,8 +136,7 @@ pub fn to_upper_snake_case(ident: &str) -> Option<String> { | |||
136 | } | 136 | } |
137 | 137 | ||
138 | // Normalize the string from whatever form it's in currently, and then just make it uppercase. | 138 | // Normalize the string from whatever form it's in currently, and then just make it uppercase. |
139 | let upper_snake_case = | 139 | let upper_snake_case = stdx::to_upper_snake_case(ident); |
140 | stdx::to_lower_snake_case(ident).chars().map(|c| c.to_ascii_uppercase()).collect(); | ||
141 | 140 | ||
142 | if upper_snake_case == ident { | 141 | if upper_snake_case == ident { |
143 | // While we didn't detect the correct case at the beginning, there | 142 | // While we didn't detect the correct case at the beginning, there |
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index b55de813e..59d89f47d 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs | |||
@@ -28,7 +28,7 @@ pub fn timeit(label: &'static str) -> impl Drop { | |||
28 | Guard { label, start: Instant::now() } | 28 | Guard { label, start: Instant::now() } |
29 | } | 29 | } |
30 | 30 | ||
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 { |
32 | let mut buf = String::with_capacity(s.len()); | 32 | let mut buf = String::with_capacity(s.len()); |
33 | let mut prev = false; | 33 | let mut prev = false; |
34 | for c in s.chars() { | 34 | for c in s.chars() { |
@@ -41,11 +41,19 @@ pub fn to_lower_snake_case(s: &str) -> String { | |||
41 | } | 41 | } |
42 | prev = true; | 42 | prev = true; |
43 | 43 | ||
44 | buf.push(c.to_ascii_lowercase()); | 44 | buf.push(change_case(&c)); |
45 | } | 45 | } |
46 | buf | 46 | buf |
47 | } | 47 | } |
48 | 48 | ||
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 | |||
49 | pub fn replace(buf: &mut String, from: char, to: &str) { | 57 | pub fn replace(buf: &mut String, from: char, to: &str) { |
50 | if !buf.contains(from) { | 58 | if !buf.contains(from) { |
51 | return; | 59 | return; |