aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2018-04-02 11:34:31 -0400
committerAndrew Kelley <superjoe30@gmail.com>2018-04-02 11:34:31 -0400
commit4eb68987d81cc4a1a92ebb4b5e83be73669801ad (patch)
tree39466f90958c64d12a5573e5f3346689e26d1aa0 /std
parent2e5115b0687ee9b7078dbf70da7d7070a7c80399 (diff)
downloadzig-4eb68987d81cc4a1a92ebb4b5e83be73669801ad.tar.gz
zig-4eb68987d81cc4a1a92ebb4b5e83be73669801ad.zip
std.io.readLine function
this provides a better input for guess number example. see #882
Diffstat (limited to 'std')
-rw-r--r--std/io.zig17
1 files changed, 17 insertions, 0 deletions
diff --git a/std/io.zig b/std/io.zig
index 952d9081f2..93d50e6709 100644
--- a/std/io.zig
+++ b/std/io.zig
@@ -478,3 +478,20 @@ test "import io tests" {
}
}
+pub fn readLine(buf: []u8) !usize {
+ var stdin = getStdIn() catch return error.StdInUnavailable;
+ var adapter = FileInStream.init(&stdin);
+ var stream = &adapter.stream;
+ var index: usize = 0;
+ while (true) {
+ const byte = stream.readByte() catch return error.EndOfFile;
+ switch (byte) {
+ '\n' => return index,
+ else => {
+ if (index == buf.len) return error.InputTooLong;
+ buf[index] = byte;
+ index += 1;
+ },
+ }
+ }
+}