From 559cc970732d80e3ec624c20da4f8aac219d6b2e Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Thu, 8 Oct 2020 08:33:35 +0300 Subject: Add to_upper_snake_case function to stdx --- crates/stdx/src/lib.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'crates/stdx') 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 { Guard { label, start: Instant::now() } } -pub fn to_lower_snake_case(s: &str) -> String { +fn to_snake_case char>(s: &str, change_case: F) -> String { let mut buf = String::with_capacity(s.len()); let mut prev = false; for c in s.chars() { @@ -41,11 +41,19 @@ pub fn to_lower_snake_case(s: &str) -> String { } prev = true; - buf.push(c.to_ascii_lowercase()); + buf.push(change_case(&c)); } 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) { if !buf.contains(from) { return; -- cgit v1.2.3