diff options
Diffstat (limited to 'crates/stdx')
-rw-r--r-- | crates/stdx/src/lib.rs | 12 |
1 files changed, 10 insertions, 2 deletions
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; |