Improve string helpers functions

This commit is contained in:
Igor Aleksanov 2020-10-03 16:45:16 +03:00
parent 21dd704b6b
commit 17f1026c46
2 changed files with 13 additions and 2 deletions

View file

@ -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;