1
0
Fork 0
mirror of https://forgejo.ellis.link/continuwuation/continuwuity.git synced 2025-07-28 10:48:30 +00:00
continuwuity/src/macros/utils.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

26 lines
545 B
Rust
Raw Normal View History

#[must_use]
pub(crate) fn camel_to_snake_string(s: &str) -> String {
let mut output = String::with_capacity(
s.chars()
.fold(s.len(), |a, ch| a.saturating_add(usize::from(ch.is_ascii_uppercase()))),
);
let mut state = false;
s.chars().for_each(|ch| {
let m = ch.is_ascii_uppercase();
let s = exchange(&mut state, !m);
if m && s {
output.push('_');
}
output.push(ch.to_ascii_lowercase());
});
output
}
pub(crate) fn exchange<T: Clone>(state: &mut T, source: T) -> T {
let ret = state.clone();
*state = source;
ret
}