Make truncate_and_trailoff a bit more clear
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
parent
9bff82f161
commit
e15ffc8560
1 changed files with 8 additions and 11 deletions
|
@ -46,10 +46,10 @@ pub fn truncate(s: &str, max_chars: usize) -> &str {
|
||||||
pub fn truncate_and_trailoff(s: &str, max_chars: usize) -> String {
|
pub fn truncate_and_trailoff(s: &str, max_chars: usize) -> String {
|
||||||
debug_assert!(max_chars >= 5);
|
debug_assert!(max_chars >= 5);
|
||||||
|
|
||||||
if s.len() > max_chars {
|
let truncation_ix = s.char_indices().map(|(i, _)| i).nth(max_chars);
|
||||||
format!("{}…", truncate(s, max_chars.saturating_sub(3)))
|
match truncation_ix {
|
||||||
} else {
|
Some(length) => s[..length].to_string() + "…",
|
||||||
s.to_string()
|
None => s.to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -279,12 +279,9 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_trancate_and_trailoff() {
|
fn test_trancate_and_trailoff() {
|
||||||
const MAX_CHARS: usize = 24;
|
assert_eq!(truncate_and_trailoff("", 5), "");
|
||||||
assert_eq!(
|
assert_eq!(truncate_and_trailoff("èèèèèè", 7), "èèèèèè");
|
||||||
truncate_and_trailoff("ajouter un compte d'èèèès", MAX_CHARS),
|
assert_eq!(truncate_and_trailoff("èèèèèè", 6), "èèèèèè");
|
||||||
"ajouter un compte d'è…"
|
assert_eq!(truncate_and_trailoff("èèèèèè", 5), "èèèèè…");
|
||||||
);
|
|
||||||
assert_eq!(truncate_and_trailoff("ajouter", MAX_CHARS), "ajouter");
|
|
||||||
assert_eq!(truncate_and_trailoff("", MAX_CHARS), "");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue