Move snake case method to heck

This commit is contained in:
Kirill Bulatov 2020-05-03 21:35:21 +03:00
parent ba8ffab644
commit 66882f1a24
2 changed files with 16 additions and 15 deletions

View file

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