Skip to content

Commit

Permalink
Fix handling of CR, LR, tabs in IRC format parser
Browse files Browse the repository at this point in the history
Fixes #366
  • Loading branch information
osa1 committed Nov 25, 2021
1 parent b7094c9 commit 2c723f6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# Unreleased

- Fixed handling of CR, LF, and tab characters in IRC format parser. IRC RFCs
don't allow standalone CR and LF characters, but some servers still them.
tiny now shows those characters as single space. Tab characters are shown as
8 spaces, as in tiny 0.9.0.

This bug was introduced in 0.10.0 with 33df77e. (#366)

# 2021/11/07: 0.10.0

Thanks to @trevarj for contributing to this release.
Expand Down
31 changes: 30 additions & 1 deletion crates/libtiny_wire/src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const CHAR_HEX_COLOR: char = '\x04';
const CHAR_REVERSE_COLOR: char = '\x16';
const CHAR_RESET: char = '\x0F';

static TAB_STR: &str = " ";

#[derive(Debug, PartialEq, Eq)]
pub enum IrcFormatEvent<'a> {
Text(&'a str),
Expand Down Expand Up @@ -193,7 +195,7 @@ impl<'a> FormatEventParser<'a> {
fn parse_text(&mut self) -> &'a str {
let cursor = self.cursor;
while let Some(next) = self.next() {
if is_irc_format_char(next) {
if is_irc_format_char(next) || next.is_ascii_control() {
self.cursor -= 1;
return &self.str[cursor..self.cursor];
}
Expand Down Expand Up @@ -344,7 +346,20 @@ impl<'a> Iterator for FormatEventParser<'a> {
return Some(IrcFormatEvent::Reset);
}

'\t' => {
self.bump(1);
return Some(IrcFormatEvent::Text(TAB_STR));
}

'\n' | '\r' => {
// RFC 2812 does not allow standalone CR or LF in messages so we're free to
// interpret this however we want.
self.bump(1);
return Some(IrcFormatEvent::Text(" "));
}

other if other.is_ascii_control() => {
// ASCII controls other than tab, CR, and LF are ignored.
self.bump(1);
continue;
}
Expand Down Expand Up @@ -533,3 +548,17 @@ fn test_parse_color() {
Some((Color::Black, Some(Color::Blue)))
);
}

#[test]
fn test_newline() {
assert_eq!(remove_irc_control_chars("\na\nb\nc\n"), " a b c ");
assert_eq!(remove_irc_control_chars("\ra\rb\rc\r"), " a b c ");
}

#[test]
fn test_tab() {
assert_eq!(
remove_irc_control_chars("\ta\tb\tc\t"),
" a b c "
);
}

0 comments on commit 2c723f6

Please sign in to comment.