std.io.readLine function

this provides a better input for guess number example.

see #882
This commit is contained in:
Andrew Kelley 2018-04-02 11:34:31 -04:00
parent 2e5115b068
commit 4eb68987d8
2 changed files with 24 additions and 6 deletions

View File

@ -9,8 +9,6 @@ pub fn main() !void {
var stdout_file_stream = io.FileOutStream.init(&stdout_file);
const stdout = &stdout_file_stream.stream;
var stdin_file = try io.getStdIn();
try stdout.print("Welcome to the Guess Number Game in Zig.\n");
var seed_bytes: [@sizeOf(u64)]u8 = undefined;
@ -27,12 +25,15 @@ pub fn main() !void {
try stdout.print("\nGuess a number between 1 and 100: ");
var line_buf : [20]u8 = undefined;
const line_len = stdin_file.read(line_buf[0..]) catch |err| {
try stdout.print("Unable to read from stdin: {}\n", @errorName(err));
return err;
const line_len = io.readLine(line_buf[0..]) catch |err| switch (err) {
error.InputTooLong => {
try stdout.print("Input too long.\n");
continue;
},
error.EndOfFile, error.StdInUnavailable => return err,
};
const guess = fmt.parseUnsigned(u8, line_buf[0..line_len - 1], 10) catch {
const guess = fmt.parseUnsigned(u8, line_buf[0..line_len], 10) catch {
try stdout.print("Invalid number.\n");
continue;
};

View File

@ -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;
},
}
}
}