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
+7
View File
@@ -4,6 +4,13 @@ The standard input stream.
## Static Methods
### **readByte**()
Reads one byte of input from stdin. Blocks the current fiber until a byte has
been received.
Returns the byte value as a number or `null` if stdin is closed.
### **readLine**()
Reads one line of input from stdin. Blocks the current fiber until a full line