From b9d09fb02cb3dfe8cd81b6d286f439bcdd70b072 Mon Sep 17 00:00:00 2001 From: Axel Kappel <69117984+Kl4rry@users.noreply.github.com> Date: Sat, 26 Mar 2022 16:28:51 +0100 Subject: made capitalize filter unicode aware --- askama_shared/src/filters/mod.rs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'askama_shared/src') diff --git a/askama_shared/src/filters/mod.rs b/askama_shared/src/filters/mod.rs index 9fb10d2..9b3a51f 100644 --- a/askama_shared/src/filters/mod.rs +++ b/askama_shared/src/filters/mod.rs @@ -330,20 +330,14 @@ where /// Capitalize a value. The first character will be uppercase, all others lowercase. pub fn capitalize(s: T) -> Result { - let mut s = s.to_string(); - - match s.get_mut(0..1).map(|s| { - s.make_ascii_uppercase(); - &*s - }) { - None => Ok(s), - _ => { - s.get_mut(1..).map(|s| { - s.make_ascii_lowercase(); - &*s - }); - Ok(s) + let s = s.to_string(); + match s.chars().next() { + Some(c) => { + let mut replacement: String = c.to_uppercase().collect(); + replacement.push_str(&s[c.len_utf8()..].to_lowercase()); + Ok(replacement) } + _ => Ok(s) } } @@ -655,6 +649,9 @@ mod tests { assert_eq!(capitalize(&"").unwrap(), "".to_string()); assert_eq!(capitalize(&"FoO").unwrap(), "Foo".to_string()); assert_eq!(capitalize(&"foO BAR").unwrap(), "Foo bar".to_string()); + assert_eq!(capitalize(&"äØÄÅÖ").unwrap(), "Äøäåö".to_string()); + assert_eq!(capitalize(&"ß").unwrap(), "SS".to_string()); + assert_eq!(capitalize(&"ßß").unwrap(), "SSß".to_string()); } #[test] -- cgit