use alloc::string::String;
#[inline]
fn is_bidi_control(c: char) -> bool {
matches!(
c,
'\u{202A}'..='\u{202E}' | '\u{2066}'..='\u{2069}' | '\u{200E}' | '\u{200F}' )
}
#[inline]
fn is_format(c: char) -> bool {
matches!(
c,
'\u{200B}'..='\u{200F}'
| '\u{2060}'..='\u{2064}'
| '\u{061C}'
| '\u{180B}'..='\u{180E}'
| '\u{180F}'
| '\u{FEFF}'
| '\u{FE00}'..='\u{FE0F}'
| '\u{E0100}'..='\u{E01EF}'
| '\u{E0001}'
| '\u{E0020}'..='\u{E007F}'
| '\u{202A}'..='\u{202E}'
| '\u{2066}'..='\u{2069}'
)
}
pub(super) fn strip(input: &str, drop_bidi: bool, drop_format: bool) -> String {
let mut out = String::with_capacity(input.len());
for c in input.chars() {
let is_bidi = is_bidi_control(c);
let is_fmt = is_format(c);
if drop_bidi && is_bidi {
continue;
}
if drop_format && is_fmt {
continue;
}
out.push(c);
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn drops_zwsp() {
assert_eq!(strip("a\u{200B}b", false, true), "ab");
}
#[test]
fn drops_bom() {
assert_eq!(strip("\u{FEFF}hello", false, true), "hello");
}
#[test]
fn drops_rlo() {
assert_eq!(strip("admin\u{202E}", true, false), "admin");
}
#[test]
fn drops_variation_selector_16() {
assert_eq!(strip("a\u{FE0F}", false, true), "a");
}
#[test]
fn drops_tag_char() {
assert_eq!(strip("\u{E0061}", false, true), "");
}
#[test]
fn keeps_normal_chars() {
let s = "héllo, мир!";
assert_eq!(strip(s, true, true), s);
}
#[test]
fn flags_are_independent() {
assert_eq!(strip("a\u{200B}\u{202E}b", true, false), "a\u{200B}b");
}
#[test]
fn empty_passes_through() {
assert_eq!(strip("", true, true), "");
}
}