Skip to content

Commit

Permalink
Add unicode capability (demonstration)
Browse files Browse the repository at this point in the history
Signed-off-by: Leni Aniva <[email protected]>
  • Loading branch information
lenianiva committed Aug 17, 2023
1 parent 52edda7 commit dd33850
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,23 @@ impl NBReader {
if self.eof {
return Ok(());
}
// FIXME: Temporary flag to demonstrate utf-8 capabilities
let unicode = true;
let mut char_buf: Vec<u8> = Vec::new();

while let Ok(from_channel) = self.reader.try_recv() {
match from_channel {
Ok(PipedChar::Char(c)) => self.buffer.push(c as char),
Ok(PipedChar::Char(c)) => {
if unicode {
char_buf.push(c);
if let Ok(s) = std::str::from_utf8(&char_buf) {
self.buffer.push(s.chars().next().unwrap());
char_buf.clear();
}
} else {
self.buffer.push(c as char)
}
},
Ok(PipedChar::EOF) => self.eof = true,
// this is just from experience, e.g. "sleep 5" returns the other error which
// most probably means that there is no stdout stream at all -> send EOF
Expand Down Expand Up @@ -300,6 +314,22 @@ mod tests {
Err(_) => panic!(),
}
}
#[test]
fn test_expect_unicode() {
let f = io::Cursor::new("∀ melon\r\n");
let mut r = NBReader::new(f, None);
assert_eq!(
("∀ melon".to_string(), "\r\n".to_string()),
r.read_until(&ReadUntil::String("\r\n".to_string()))
.expect("cannot read line")
);
// check for EOF
match r.read_until(&ReadUntil::NBytes(10)) {
Ok(_) => panic!(),
Err(Error::EOF { .. }) => {}
Err(_) => panic!(),
}
}

#[test]
fn test_regex() {
Expand Down

0 comments on commit dd33850

Please sign in to comment.