1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#![no_std]
extern crate alloc;
use alloc::{string::String, vec::Vec};
use unicode_segmentation::UnicodeSegmentation;
pub fn name(s: &str) -> String {
let mut name = String::with_capacity(s.len());
let mut it = s.unicode_words();
if let Some(w) = it.next() {
name.push_str(w);
for w in it {
name.push(' ');
name.push_str(w)
}
}
name
}
pub fn name_with_limit(s: &str, limit: usize) -> String {
let mut name: Vec<&str> = s.unicode_words().collect();
if name.is_empty() {
return String::new();
}
let len = name.len();
let mut n = Vec::with_capacity(len);
let mut sum = 0;
for &w in &name {
let c = w.graphemes(true).count();
sum += c;
n.push(c);
}
let spaces = len - 1;
let mut count = sum + spaces;
for (w, c) in name.iter_mut().zip(n).rev() {
if count <= limit {
break;
}
count -= c - 1;
*w = w.graphemes(true).next().unwrap();
}
if count <= limit {
name.join(" ")
} else if (count - spaces) <= limit {
name.concat()
} else {
name[..limit].concat()
}
}
#[cfg(test)]
mod tests {
#[test]
fn name() {
let s = "(Even), Olsson&Rogstadkjærnet?";
assert_eq!(crate::name(s), "Even Olsson Rogstadkjærnet");
assert_eq!(crate::name_with_limit(s, 25), "Even Olsson R");
assert_eq!(crate::name_with_limit(s, 12), "Even O R");
assert_eq!(crate::name_with_limit(s, 7), "E O R");
assert_eq!(crate::name_with_limit(s, 4), "EOR");
assert_eq!(crate::name_with_limit(s, 2), "EO");
assert_eq!(crate::name_with_limit(s, 1), "E");
assert_eq!(crate::name_with_limit(s, 0), "");
}
}