feat: add Stdin.readByte() method for reading single byte from stdin

Implement a new `readByte()` static method on the `Stdin` class that reads one byte from standard input, blocking the current fiber until a byte is received. The method returns the byte value as a number or `null` if stdin is closed.

Refactor the internal buffering mechanism by introducing a shared `read_()` helper method that both `readByte()` and `readLine()` use, replacing the previous per-method buffering logic. The `readByte()` implementation peels off the first byte from the internal `__buffered` string using `bytes[0]` and slicing.

Add two test files: `read_byte.wren` verifying byte-by-byte reading of "first" and "second" input lines, and `read_byte_eof.wren` confirming that reading after stdin is closed returns the expected error message.
This commit is contained in:
Bob Nystrom
2016-05-20 18:50:14 +00:00
parent bdc8e1f409
commit 6394e1c486
7 changed files with 140 additions and 55 deletions
+21
View File
@@ -0,0 +1,21 @@
import "io" for Stdin
for (i in 1...13) {
System.print(Stdin.readByte())
}
// stdin: first
// expect: 102
// expect: 105
// expect: 114
// expect: 115
// expect: 116
// expect: 10
// stdin: second
// expect: 115
// expect: 101
// expect: 99
// expect: 111
// expect: 110
// expect: 100