aboutsummaryrefslogtreecommitdiffstats
path: root/askama_shared
diff options
context:
space:
mode:
authorLibravatar Axel Kappel <69117984+Kl4rry@users.noreply.github.com>2022-03-26 16:28:51 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2022-03-26 18:50:42 +0100
commitb9d09fb02cb3dfe8cd81b6d286f439bcdd70b072 (patch)
treefa0757df5ae6f91f52099480d4d4dec4a8a2cd0f /askama_shared
parentb14982f97ffd20039286171d56e6fcfab21f56bc (diff)
downloadaskama-b9d09fb02cb3dfe8cd81b6d286f439bcdd70b072.tar.gz
askama-b9d09fb02cb3dfe8cd81b6d286f439bcdd70b072.tar.bz2
askama-b9d09fb02cb3dfe8cd81b6d286f439bcdd70b072.zip
made capitalize filter unicode aware
Diffstat (limited to 'askama_shared')
-rw-r--r--askama_shared/src/filters/mod.rs23
1 files changed, 10 insertions, 13 deletions
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<T: fmt::Display>(s: T) -> Result<String> {
- 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]