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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use alloc::{borrow::Cow, str::from_utf8_unchecked};
use crate::to_substring_in_place;
/// To extend `str` and `Cow<str>` to have `remove_all_invisible_characters` method.
pub trait RemoveInvisibleCharacters<'a> {
/// Removes all invisible or non-printable characters from a given string.
///
/// This function filters out a comprehensive set of Unicode characters that are typically
/// invisible or used for control or formatting purposes. This includes:
///
/// - ASCII control characters (U+0000 to U+001F and U+007F), exclude `\t` and `\n`
/// - Zero-width characters and format controls:
/// - U+200B (Zero Width Space)
/// - U+200C (Zero Width Non-Joiner)
/// - U+200D (Zero Width Joiner)
/// - U+200E to U+200F (Directional marks)
/// - U+202A to U+202E (Directional formatting)
/// - U+2060 to U+2064 (Word Joiner and Invisible Math Symbols)
/// - U+2066 to U+2069 (Bidi Isolates)
/// - U+FEFF (Byte Order Mark / Zero Width No-Break Space)
///
/// These characters can interfere with text rendering, parsing, and display,
/// and are often used in text-based attacks (e.g., for spoofing).
fn remove_all_invisible_characters(self) -> Cow<'a, str>;
}
impl<'a> RemoveInvisibleCharacters<'a> for &'a str {
fn remove_all_invisible_characters(self) -> Cow<'a, str> {
let s = self;
let bytes = s.as_bytes();
let length = bytes.len();
let mut p = 0;
let check_character_whether_to_remove = |p: usize, e: u8, width: usize| -> bool {
match width {
1 => {
match e {
// ascii controls, only remain \t, \n
0..=8 | 11..=13 | 14..=31 | 127 => return true,
_ => (),
}
},
3 => match e {
0xE2 => match bytes[p + 1] {
// zero width characters and bidirectional controls
0x80 => match bytes[p + 2] {
0x8B..=0x8F | 0xAA..=0xAE => return true,
_ => (),
},
// word joiner, invisible times/separator/plus and isolate characters
0x81 => match bytes[p + 2] {
0xA0 | 0xA2..=0xA4 | 0xA6..=0xA9 => return true,
_ => (),
},
_ => (),
},
// zero width character
0xEF => {
if bytes[p + 1] == 0xBB && bytes[p + 2] == 0xBF {
return true;
}
},
_ => (),
},
_ => (),
}
false
};
let width = loop {
if p == length {
return Cow::Borrowed(s);
}
let e = bytes[p];
let width = unsafe { utf8_width::get_width_assume_valid(e) };
if check_character_whether_to_remove(p, e, width) {
break width;
} else {
p += width;
}
};
let heading_normal_characters_end_index = p;
p += width;
// there are four situations which can use a string slice:
// 1. <invisible_characters>
// 2. <normal_characters><invisible_characters>
// 3. <invisible_characters><normal_characters>
// 4. <invisible_characters><normal_characters><invisible_characters>
// continue to find more invisible characters
let width = loop {
if p == length {
// situation 1 or situation 2
return Cow::Borrowed(unsafe {
from_utf8_unchecked(&bytes[..heading_normal_characters_end_index])
});
}
let e = bytes[p];
let width = unsafe { utf8_width::get_width_assume_valid(e) };
if check_character_whether_to_remove(p, e, width) {
p += width;
} else {
break width;
}
};
let following_invisible_characters_end_index = p;
p += width;
// continue to find more normal characters
let width = loop {
if p == length {
// situation 3
return Cow::Borrowed(unsafe {
from_utf8_unchecked(&bytes[following_invisible_characters_end_index..])
});
}
let e = bytes[p];
let width = unsafe { utf8_width::get_width_assume_valid(e) };
if check_character_whether_to_remove(p, e, width) {
break width;
} else {
p += width;
}
};
let following_normal_characters_end_index = p;
p += width;
// continue to find more invisible characters
let width = loop {
if p == length {
// situation 4
return Cow::Borrowed(unsafe {
from_utf8_unchecked(
&bytes[following_invisible_characters_end_index
..following_normal_characters_end_index],
)
});
}
let e = bytes[p];
let width = unsafe { utf8_width::get_width_assume_valid(e) };
if check_character_whether_to_remove(p, e, width) {
p += width;
} else {
break width;
}
};
let mut new_v = bytes
[following_invisible_characters_end_index..following_normal_characters_end_index]
.to_vec();
let mut start = p;
p += width;
loop {
if p == length {
break;
}
let e = bytes[p];
let width = unsafe { utf8_width::get_width_assume_valid(e) };
if check_character_whether_to_remove(p, e, width) {
new_v.extend_from_slice(&bytes[start..p]);
start = p + width;
}
p += width;
}
new_v.extend_from_slice(&bytes[start..p]);
Cow::Owned(unsafe { String::from_utf8_unchecked(new_v) })
}
}
impl<'a> RemoveInvisibleCharacters<'a> for Cow<'a, str> {
#[inline]
fn remove_all_invisible_characters(self) -> Cow<'a, str> {
match self {
Cow::Borrowed(s) => s.remove_all_invisible_characters(),
Cow::Owned(s) => match s.remove_all_invisible_characters() {
Cow::Borrowed(ss) => Cow::Owned(unsafe { to_substring_in_place!(s, ss) }),
Cow::Owned(s) => Cow::Owned(s),
},
}
}
}