1pub fn truncate_chars(text: &str, max_chars: usize) -> String {
4 text.chars().take(max_chars).collect()
5}
6
7pub fn truncate_chars_counted(text: &str, max_chars: usize) -> Option<(String, usize)> {
13 let total = text.chars().count();
14 if total <= max_chars {
15 return None;
16 }
17 Some((truncate_chars(text, max_chars), total - max_chars))
18}
19
20#[cfg(test)]
21mod tests {
22 use super::*;
23
24 #[test]
25 fn test_truncate_chars() {
26 assert_eq!(truncate_chars("hello world", 5), "hello");
27 assert_eq!(truncate_chars("hello", 5), "hello");
28 assert_eq!(truncate_chars("hi", 5), "hi");
29 assert_eq!(truncate_chars("hello", 0), "");
30 assert_eq!(truncate_chars("", 5), "");
31 assert_eq!(truncate_chars("ππ", 1), "π");
32 assert_eq!(truncate_chars("ππ", 2), "ππ");
33 assert_eq!(truncate_chars("γγγ«γ‘γ―", 2), "γγ");
34 assert_eq!(truncate_chars("μλ
νμΈμ", 3), "μλ
ν");
35 assert_eq!(truncate_chars("δ½ ε₯½δΈη", 1), "δ½ ");
36 }
37
38 #[test]
39 fn test_truncate_chars_counted() {
40 assert_eq!(truncate_chars_counted("hello", 5), None);
41 assert_eq!(truncate_chars_counted("δ½ ε₯½δΈη", 4), None);
42 assert_eq!(
43 truncate_chars_counted("δ½ ε₯½δΈη", 2),
44 Some(("δ½ ε₯½".to_string(), 2))
45 );
46 let cjk = "ζ₯".repeat(30_000);
47 let (text, dropped) = truncate_chars_counted(&cjk, 20_000).unwrap();
48 assert_eq!(text.chars().count(), 20_000);
49 assert_eq!(dropped, 10_000);
50 }
51}