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
+9
View File
@@ -0,0 +1,9 @@
import "io" for Stdin
Stdin.readLine() // stdin: one line
var error = Fiber.new {
Stdin.readLine()
}.try()
System.print(error) // expect: Stdin was closed.