refactor: do not create new function on each loop

pull/12043/head
Nikita Revenco 2025-01-11 17:49:01 +00:00
parent 5420ef5f63
commit 2c1af76fb0
1 changed files with 11 additions and 11 deletions

View File

@ -19,24 +19,24 @@ pub fn smart_case_conversion(
let mut should_capitalize_current = capitalize_first; let mut should_capitalize_current = capitalize_first;
let mut prev: Option<char> = None; let mut prev: Option<char> = None;
for current in chars.skip_while(|ch| ch.is_whitespace()) { let add_separator_if_needed = |prev: Option<char>, buf: &mut Tendril| {
let mut maybe_add_separator = || { if let Some(separator) = separator {
if let Some(separator) = separator { // We do not want to add a separator when the previous char is not a separator
// We do not want to add a separator when the previous char is not a separator // For example, snake__case is invalid
// For example, snake__case is invalid if prev.is_some_and(|ch| ch != separator) {
if prev.is_some_and(|ch| ch != separator) { buf.push(separator);
buf.push(separator);
}
} }
}; }
};
for current in chars.skip_while(|ch| ch.is_whitespace()) {
if current.is_alphanumeric() { if current.is_alphanumeric() {
// "camelCase" => transition at 'l' -> 'C' // "camelCase" => transition at 'l' -> 'C'
let has_camel_transition = let has_camel_transition =
current.is_uppercase() && prev.is_some_and(|ch| ch.is_lowercase()); current.is_uppercase() && prev.is_some_and(|ch| ch.is_lowercase());
if has_camel_transition { if has_camel_transition {
maybe_add_separator(); add_separator_if_needed(prev, buf);
should_capitalize_current = true; should_capitalize_current = true;
} }
if should_capitalize_current { if should_capitalize_current {
@ -47,7 +47,7 @@ pub fn smart_case_conversion(
} }
} else { } else {
should_capitalize_current = true; should_capitalize_current = true;
maybe_add_separator(); add_separator_if_needed(prev, buf);
} }
prev = Some(current); prev = Some(current);
} }