use std::borrow::Cow;
pub use comfy_table::*;
pub fn sanitize(text: &str) -> Cow<'_, str> {
if !text.chars().any(is_unsafe) {
return Cow::Borrowed(text);
}
text.chars()
.map(|c| {
if is_unsafe(c) {
char::REPLACEMENT_CHARACTER
} else {
c
}
})
.collect()
}
fn is_unsafe(c: char) -> bool {
c.is_control() || matches!(c, '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}')
}
#[cfg(test)]
mod tests {
use std::borrow::Cow;
use super::sanitize;
#[test]
fn sanitize_borrows_clean_text() {
assert!(matches!(
sanitize("Re: café ☕"),
Cow::Borrowed("Re: café ☕")
));
}
#[test]
fn sanitize_replaces_control_characters() {
assert_eq!(sanitize("a\x1b[2Jb\x07c\x7fd\u{9b}e\tf"), "a�[2Jb�c�d�e�f");
}
#[test]
fn sanitize_replaces_bidi_characters() {
assert_eq!(sanitize("invoice\u{202e}fdp.exe"), "invoice�fdp.exe");
assert_eq!(sanitize("a\u{2066}b\u{2069}c"), "a�b�c");
}
}