-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
65 lines (59 loc) · 2.26 KB
/
lib.rs
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
use unicode_width::UnicodeWidthChar;
const ZWJ: u32 = 0x200d;
/// Get the column width of a string
pub fn width(string: &str) -> usize {
let mut cw = 0;
let mut iter = string.chars();
while let Some(c) = iter.next() {
if u32::from(c) == ZWJ {
iter.next();
continue;
}
cw += c.width().unwrap_or(0);
}
cw
}
/// Truncate a string to a specific column width
pub fn truncate(string: &str, width: usize) -> &str {
let mut cw = 0;
let mut iter = string.char_indices();
while let Some((i, c)) = iter.next() {
if u32::from(c) == ZWJ {
iter.next();
continue;
}
cw += c.width().unwrap_or(0);
if cw > width {
return &string[..i];
}
}
string
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_width() {
// basic tests
assert_eq!(width("teststring"), 10);
// full-width (2 column) characters test
assert_eq!(width("잘라야"), 6);
// combining characters (zalgo text) test
assert_eq!(width("ę̵̡̛̮̹̼̝̲͓̳̣͉̞͔̳̥̝͍̩̣̹͙̘̼̥̗̼͈̯͎̮̥̤̪̻̮͕̩̮͓͔̟͈͇͎̣͉͇̦͔̝̣͎͎͔͇̭͈̌̂̈̄̈́̾͑̀̈̓̂͗̾̉͊͒̆̽͊̽͘̕͜͜͝͠ :width"), 8);
// zero-width-joiner (emoji) test
assert_eq!(width("👨👩👦:width"), 8);
}
#[test]
fn test_truncation() {
// basic tests
assert_eq!(truncate("teststring", 50), "teststring");
assert_eq!(truncate("teststring", 5), "tests");
assert_eq!(truncate("teststring", 0), "");
// full-width (2 column) characters test
assert_eq!(truncate("잘라야", 4), "잘라");
// combining characters (zalgo text) test
assert_eq!(truncate("ę̵̡̛̮̹̼̝̲͓̳̣͉̞͔̳̥̝͍̩̣̹͙̘̼̥̗̼͈̯͎̮̥̤̪̻̮͕̩̮͓͔̟͈͇͎̣͉͇̦͔̝̣͎͎͔͇̭͈̌̂̈̄̈́̾͑̀̈̓̂͗̾̉͊͒̆̽͊̽͘̕͜͜͝͠ :trunc", 3), "ę̵̡̛̮̹̼̝̲͓̳̣͉̞͔̳̥̝͍̩̣̹͙̘̼̥̗̼͈̯͎̮̥̤̪̻̮͕̩̮͓͔̟͈͇͎̣͉͇̦͔̝̣͎͎͔͇̭͈̌̂̈̄̈́̾͑̀̈̓̂͗̾̉͊͒̆̽͊̽͘̕͜͜͝͠ :");
// zero-width-joiner (emoji) test
assert_eq!(truncate("👨👩👦:trunc", 3), "👨👩👦:");
}
}