pub fn truncate(s: &str, max_bytes: usize) -> &str {
if s.len() <= max_bytes {
return s;
}
if max_bytes <= 3 {
return &s[..0]; }
let limit = max_bytes - 3;
let mut index = limit;
while index > 0 && !s.is_char_boundary(index) {
index -= 1;
}
&s[..index]
}
pub fn truncate_owned(s: &str, max_bytes: usize) -> String {
if s.len() <= max_bytes {
return s.to_string();
}
let truncated = truncate(s, max_bytes);
if truncated.is_empty() {
".".repeat(max_bytes)
} else {
format!("{truncated}...")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_truncate() {
assert_eq!(truncate("hello world", 8), "hello");
assert_eq!(truncate_owned("hello world", 8), "hello...");
assert_eq!(truncate_owned("hello", 10), "hello");
assert_eq!(truncate_owned("🦀🦀🦀🦀", 8), "🦀...");
}
}