From 66882f1a249983a1b262a219dbcd47a0e2835418 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sun, 3 May 2020 21:35:21 +0300 Subject: Move snake case method to heck --- crates/stdx/src/lib.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'crates/stdx') diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 01cdf452c..0f34ce70e 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs @@ -102,3 +102,17 @@ pub fn timeit(label: &'static str) -> impl Drop { Guard { label, start: Instant::now() } } + +pub fn to_lower_snake_case(s: &str) -> String { + let mut buf = String::with_capacity(s.len()); + let mut prev = false; + for c in s.chars() { + if c.is_ascii_uppercase() && prev { + buf.push('_') + } + prev = true; + + buf.push(c.to_ascii_lowercase()); + } + buf +} -- cgit v1.2.3