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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#[macro_export]
macro_rules! big_if_true {
($a:expr) => {
if $a {
1
} else {
0
}
};
}
type WChar = u32;
type Interval = (WChar, WChar);
pub struct CodePointsIterator<'a> {
rest: std::str::Chars<'a>,
}
impl<'a> Iterator for CodePointsIterator<'a> {
type Item = WChar;
fn next(&mut self) -> Option<WChar> {
self.rest.next().map(|c| c as WChar)
}
}
pub trait CodePointsIter {
fn code_points(&self) -> CodePointsIterator;
}
impl CodePointsIter for str {
fn code_points(&self) -> CodePointsIterator {
CodePointsIterator { rest: self.chars() }
}
}
impl CodePointsIter for &str {
fn code_points(&self) -> CodePointsIterator {
CodePointsIterator { rest: self.chars() }
}
}
fn bisearch(ucs: WChar, table: &'static [Interval]) -> bool {
let mut min = 0;
let mut mid;
let mut max = table.len() - 1;
if ucs < table[0].0 || ucs > table[max].1 {
return false;
}
while max >= min {
mid = (min + max) / 2;
if ucs > table[mid].1 {
min = mid + 1;
} else if ucs < table[mid].0 {
max = mid - 1;
} else {
return true;
}
}
false
}
pub fn wcwidth(ucs: WChar) -> Option<usize> {
if bisearch(ucs, super::tables::ASCII) {
Some(1)
} else if bisearch(ucs, super::tables::PRIVATE) {
None
} else if bisearch(ucs, super::tables::NONPRINT) {
None
} else if bisearch(ucs, super::tables::COMBINING) {
None
} else if bisearch(ucs, super::tables::DOUBLEWIDE) {
Some(2)
} else if bisearch(ucs, super::tables::AMBIGUOUS) {
Some(1)
} else if bisearch(ucs, super::tables::UNASSIGNED) {
Some(2)
} else if bisearch(ucs, super::tables::WIDENEDIN9) {
Some(2)
} else {
Some(1)
}
}
#[test]
fn test_wcwidth() {
assert_eq!(
&"abc\0".code_points().collect::<Vec<_>>(),
&[0x61, 0x62, 0x63, 0x0]
);
assert_eq!(&"●".code_points().collect::<Vec<_>>(), &[0x25cf]);
assert_eq!(&"📎".code_points().collect::<Vec<_>>(), &[0x1f4ce]);
assert_eq!(
&"𐼹𐼺𐼻𐼼𐼽".code_points().collect::<Vec<_>>(),
&[0x10F39, 0x10F3A, 0x10F3B, 0x10F3C, 0x10F3D]
);
assert_eq!(
&"𐼹a𐼽b".code_points().collect::<Vec<_>>(),
&[0x10F39, 0x61, 0x10F3D, 0x62]
);
assert_eq!(
&"📎\u{FE0E}".code_points().collect::<Vec<_>>(),
&[0x1f4ce, 0xfe0e]
);
use crate::text_processing::grapheme_clusters::TextProcessing;
assert_eq!("●".grapheme_width(), 1);
assert_eq!("●📎".grapheme_width(), 3);
assert_eq!("●\u{FE0E}📎\u{FE0E}".grapheme_width(), 3);
assert_eq!("🎃".grapheme_width(), 2);
assert_eq!("👻".grapheme_width(), 2);
}
pub fn wcswidth(mut pwcs: WChar, mut n: usize) -> Option<usize> {
let mut width = 0;
while pwcs > 0 && n > 0 {
if let Some(w) = wcwidth(pwcs) {
width += w;
} else {
return None;
}
pwcs += 1;
n -= 1;
}
Some(width)
}