Skip to content

Commit dd33850

Browse files
committed
Add unicode capability (demonstration)
Signed-off-by: Leni Aniva <[email protected]>
1 parent 52edda7 commit dd33850

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/reader.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,23 @@ impl NBReader {
162162
if self.eof {
163163
return Ok(());
164164
}
165+
// FIXME: Temporary flag to demonstrate utf-8 capabilities
166+
let unicode = true;
167+
let mut char_buf: Vec<u8> = Vec::new();
168+
165169
while let Ok(from_channel) = self.reader.try_recv() {
166170
match from_channel {
167-
Ok(PipedChar::Char(c)) => self.buffer.push(c as char),
171+
Ok(PipedChar::Char(c)) => {
172+
if unicode {
173+
char_buf.push(c);
174+
if let Ok(s) = std::str::from_utf8(&char_buf) {
175+
self.buffer.push(s.chars().next().unwrap());
176+
char_buf.clear();
177+
}
178+
} else {
179+
self.buffer.push(c as char)
180+
}
181+
},
168182
Ok(PipedChar::EOF) => self.eof = true,
169183
// this is just from experience, e.g. "sleep 5" returns the other error which
170184
// most probably means that there is no stdout stream at all -> send EOF
@@ -300,6 +314,22 @@ mod tests {
300314
Err(_) => panic!(),
301315
}
302316
}
317+
#[test]
318+
fn test_expect_unicode() {
319+
let f = io::Cursor::new("∀ melon\r\n");
320+
let mut r = NBReader::new(f, None);
321+
assert_eq!(
322+
("∀ melon".to_string(), "\r\n".to_string()),
323+
r.read_until(&ReadUntil::String("\r\n".to_string()))
324+
.expect("cannot read line")
325+
);
326+
// check for EOF
327+
match r.read_until(&ReadUntil::NBytes(10)) {
328+
Ok(_) => panic!(),
329+
Err(Error::EOF { .. }) => {}
330+
Err(_) => panic!(),
331+
}
332+
}
303333

304334
#[test]
305335
fn test_regex() {

0 commit comments

Comments
 (0)