ruff/crates/ruff_linter/src/text_helpers.rs
aditya pillai ed947792cf
Handle non-printable characters in diff view (#11687)
Co-authored-by: Micha Reiser <micha@reiser.io>
2024-06-08 06:22:03 +00:00

23 lines
658 B
Rust

use std::borrow::Cow;
pub(crate) trait ShowNonprinting {
fn show_nonprinting(&self) -> Cow<'_, str>;
}
macro_rules! impl_show_nonprinting {
($(($from:expr, $to:expr)),+) => {
impl ShowNonprinting for str {
fn show_nonprinting(&self) -> Cow<'_, str> {
if self.find(&[$($from),*][..]).is_some() {
Cow::Owned(
self.$(replace($from, $to)).*
)
} else {
Cow::Borrowed(self)
}
}
}
};
}
impl_show_nonprinting!(('\x07', ""), ('\x08', ""), ('\x1b', ""), ('\x7f', ""));