Skip to content
Open
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
25 changes: 20 additions & 5 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,9 @@ impl NBReader {
pub fn try_read(&mut self) -> Option<char> {
// discard eventual errors, EOF will be handled in read_until correctly
let _ = self.read_into_buffer();
if !self.buffer.is_empty() {
self.buffer.drain(..1).last()
} else {
None
}
let first = self.buffer.chars().next()?;
self.buffer.drain(..first.len_utf8());
Some(first)
}
}

Expand Down Expand Up @@ -457,4 +455,21 @@ mod tests {
assert_eq!(None, r.try_read());
assert_eq!(None, r.try_read());
}

#[test]
fn test_try_read_multibyte() {
let f = io::Cursor::new("\u{c3}");
let mut r = NBReader::new(f, Options::default());
// pump bytes from the reader thread into the buffer
let mut chars = String::new();
for _ in 0..10 {
if let Some(c) = r.try_read() {
chars.push(c);
}
thread::sleep(time::Duration::from_millis(5));
}
// Each raw byte is cast to a char before reaching the buffer, so the two
// UTF-8 bytes of "\u{c3}" surface as separate Latin-1 chars.
assert_eq!(chars, "\u{c3}\u{83}");
}
}