From 17f1026c46e6e3797caf3c69737f66bd612c58e1 Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Sat, 3 Oct 2020 16:45:16 +0300 Subject: Improve string helpers functions --- crates/stdx/src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'crates/stdx/src/lib.rs') diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 011935cad..522a9c1ab 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs @@ -32,8 +32,12 @@ 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() { + // `&& prev` is required to not insert `_` before the first symbol. if c.is_ascii_uppercase() && prev { - buf.push('_') + // This check is required to not translate `Weird_Case` into `weird__case`. + if buf.chars().last() != Some('_') { + buf.push('_') + } } prev = true; -- cgit v1.2.3