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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use {
unicode_width::UnicodeWidthChar,
std::borrow::Cow,
};
pub static TAB_REPLACEMENT: &str = " ";
#[derive(Debug, Clone, Copy)]
pub struct StrFit {
bytes_count: usize,
cols_count: usize,
has_tab: bool,
}
impl StrFit {
pub fn from(s: &str, cols_max: usize) -> Self {
let mut bytes_count = 0;
let mut cols_count: i32 = 0;
let mut has_tab = false;
for (idx, c) in s.char_indices() {
let char_width: i32 = match c {
'\t' => {
has_tab = true;
TAB_REPLACEMENT.len() as i32
}
'\x08' => {
-1
}
_ => UnicodeWidthChar::width(c).map(|w| w as i32).unwrap_or(0),
};
let next_str_width = cols_count + char_width;
if next_str_width > 0 && next_str_width as usize > cols_max {
break;
}
cols_count = next_str_width;
bytes_count = idx + c.len_utf8();
}
Self {
bytes_count,
cols_count: cols_count.max(0) as usize,
has_tab,
}
}
pub fn count_fitting(s: &str, cols_max: usize) -> (usize, usize) {
let fit = StrFit::from(s, cols_max);
(fit.bytes_count, fit.cols_count)
}
pub fn make_string(s: &str, cols_max: usize) -> (String, usize) {
let fit = StrFit::from(s, cols_max);
if fit.has_tab {
let string = (&s[0..fit.bytes_count]).replace('\t', TAB_REPLACEMENT);
(string, fit.cols_count)
} else {
(s[0..fit.bytes_count].to_string(), fit.cols_count)
}
}
pub fn make_cow<'a>(s: &'a str, cols_max: usize) -> (Cow<'a, str>, usize) {
let fit = StrFit::from(s, cols_max);
if fit.has_tab {
let string = (&s[0..fit.bytes_count]).replace('\t', TAB_REPLACEMENT);
(Cow::Owned(string), fit.cols_count)
} else {
(Cow::Borrowed(&s[0..fit.bytes_count]), fit.cols_count)
}
}
}
#[cfg(test)]
mod fitting_count_tests {
use super::*;
#[test]
fn test_count_fitting() {
assert_eq!(StrFit::count_fitting("test", 3), (3, 3));
assert_eq!(StrFit::count_fitting("test", 5), (4, 4));
let c12 = "Comunicações";
assert_eq!(c12.len(), 14);
assert_eq!(c12.chars().count(), 12);
assert_eq!(StrFit::count_fitting(c12, 12), (14, 12));
assert_eq!(StrFit::count_fitting(c12, 10), (12, 10));
assert_eq!(StrFit::count_fitting(c12, 11), (13, 11));
let c14 = "Comunicações";
assert_eq!(c14.len(), 16);
assert_eq!(c14.chars().count(), 14);
assert_eq!(StrFit::count_fitting(c14, 12), (16, 12));
let ja = "概要";
assert_eq!(ja.len(), 6);
assert_eq!(ja.chars().count(), 2);
assert_eq!(StrFit::count_fitting(ja, 1), (0, 0));
assert_eq!(StrFit::count_fitting(ja, 2), (3, 2));
assert_eq!(StrFit::count_fitting(ja, 3), (3, 2));
assert_eq!(StrFit::count_fitting(ja, 4), (6, 4));
assert_eq!(StrFit::count_fitting(ja, 5), (6, 4));
}
}