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/src/lib.rs') 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 From 8eb3272ad6f774bccb967ee640b72a9a17273e7b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 19 May 2020 22:25:07 +0200 Subject: Use snippets in add function --- crates/stdx/src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'crates/stdx/src/lib.rs') diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 0f34ce70e..71a57fba2 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs @@ -116,3 +116,11 @@ pub fn to_lower_snake_case(s: &str) -> String { } buf } + +pub fn replace(buf: &mut String, from: char, to: &str) { + if !buf.contains(from) { + return; + } + // FIXME: do this in place. + *buf = buf.replace(from, to) +} -- cgit v1.2.3