feat: better implementation of title_case

pull/12043/head
Nikita Revenco 2024-12-20 08:01:51 +00:00
parent 355b2ba290
commit f1cf46f022
1 changed files with 5 additions and 3 deletions

View File

@ -125,8 +125,8 @@ pub fn to_kebab_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
} }
pub fn to_title_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) { pub fn to_title_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
let mut at_word_start = false; let mut at_word_start = true;
for c in text { for (i, c) in text.enumerate() {
// we don't count _ as a word char here so case conversions work well // we don't count _ as a word char here so case conversions work well
if !c.is_alphanumeric() { if !c.is_alphanumeric() {
at_word_start = true; at_word_start = true;
@ -134,7 +134,9 @@ pub fn to_title_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
} }
if at_word_start { if at_word_start {
at_word_start = false; at_word_start = false;
buf.push(' '); if i != 0 {
buf.push(' ');
}
buf.extend(c.to_uppercase()); buf.extend(c.to_uppercase());
} else { } else {
buf.push(c) buf.push(c)