aboutsummaryrefslogtreecommitdiff
path: root/crates/stdx
diff options
context:
space:
mode:
authorIgor Aleksanov <[email protected]>2020-10-08 06:33:35 +0100
committerIgor Aleksanov <[email protected]>2020-10-12 09:05:00 +0100
commit559cc970732d80e3ec624c20da4f8aac219d6b2e (patch)
treecc0efeae2653890f8c178c4b970851d7fc9552af /crates/stdx
parentebd30033b3743fafe0a0182b5ae34ffb27fe43ff (diff)
Add to_upper_snake_case function to stdx
Diffstat (limited to 'crates/stdx')
-rw-r--r--crates/stdx/src/lib.rs12
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
31pub fn to_lower_snake_case(s: &str) -> String { 31fn 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
49pub fn to_lower_snake_case(s: &str) -> String {
50 to_snake_case(s, char::to_ascii_lowercase)
51}
52
53pub fn to_upper_snake_case(s: &str) -> String {
54 to_snake_case(s, char::to_ascii_uppercase)
55}
56
49pub fn replace(buf: &mut String, from: char, to: &str) { 57pub fn replace(buf: &mut String, from: char, to: &str) {
50 if !buf.contains(from) { 58 if !buf.contains(from) {
51 return; 59 return;