From 3909b89a9bc68627ef6d6082abb248669ca43a7b Mon Sep 17 00:00:00 2001 From: scrola Date: Sun, 15 Nov 2026 19:44:51 -0300 Subject: [PATCH 1/2] fix: fixed text --- src/text.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/src/text.rs b/src/text.rs index 26732bc..70db285 100644 --- a/src/text.rs +++ b/src/text.rs @@ -2,11 +2,25 @@ //! output and repository content, which is untrusted, so control characters //! that could move the cursor or rewrite earlier lines never reach the terminal. +/// Returns whether a character is a Unicode bidirectional formatting control. +fn is_bidi_control(character: char) -> bool { + matches!( + character as u32, + 0x061C + | 0x200E..=0x200F + | 0x202A..=0x202E + | 0x2066..=0x2069 + ) +} + /// Replaces every control character except a newline and a tab. pub fn terminal_safe(text: &str) -> String { text.chars() .map(|character| { - if character.is_control() && character != '\n' && character != '\t' { + if (character.is_control() || is_bidi_control(character)) + && character != '\n' + && character != '\t' + { '\u{FFFD}' } else { character @@ -34,4 +48,36 @@ mod tests { assert_eq!(terminal_safe("line\n\tindent"), "line\n\tindent"); assert_eq!(one_line("a\nb\tc"), "a b c"); } -} + + #[test] + fn bidirectional_override_is_replaced() { + assert_eq!( + terminal_safe("file\u{202E}gpj.exe"), + "file\u{FFFD}gpj.exe" + ); + } + + #[test] + fn bidirectional_isolates_are_replaced() { + assert_eq!( + terminal_safe("text\u{2066}hidden\u{2069}"), + "text\u{FFFD}hidden\u{FFFD}" + ); + } + + #[test] + fn bidirectional_controls_are_removed_from_single_line_output() { + assert_eq!( + one_line("report\u{202E}txt"), + "report\u{FFFD}txt" + ); + } + + #[test] + fn ordinary_unicode_is_preserved() { + assert_eq!( + terminal_safe("Café — résumé"), + "Café — résumé" + ); + } +} \ No newline at end of file From a1540cfd6e0678fb7138bb7c92ba35808156f740 Mon Sep 17 00:00:00 2001 From: Mohsen Azimi Date: Tue, 17 Nov 2026 14:13:26 +0100 Subject: [PATCH 2/2] Format bidi-control regression tests --- src/text.rs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/text.rs b/src/text.rs index 70db285..6fbdb4c 100644 --- a/src/text.rs +++ b/src/text.rs @@ -51,10 +51,7 @@ mod tests { #[test] fn bidirectional_override_is_replaced() { - assert_eq!( - terminal_safe("file\u{202E}gpj.exe"), - "file\u{FFFD}gpj.exe" - ); + assert_eq!(terminal_safe("file\u{202E}gpj.exe"), "file\u{FFFD}gpj.exe"); } #[test] @@ -67,17 +64,11 @@ mod tests { #[test] fn bidirectional_controls_are_removed_from_single_line_output() { - assert_eq!( - one_line("report\u{202E}txt"), - "report\u{FFFD}txt" - ); + assert_eq!(one_line("report\u{202E}txt"), "report\u{FFFD}txt"); } #[test] fn ordinary_unicode_is_preserved() { - assert_eq!( - terminal_safe("Café — résumé"), - "Café — résumé" - ); + assert_eq!(terminal_safe("Café — résumé"), "Café — résumé"); } -} \ No newline at end of file +}