Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -34,4 +48,27 @@ 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é");
}
}