mirror of
https://github.com/ziglang/zig.git
synced 2026-01-20 22:35:24 +00:00
std: remove auto flushing of stderr. use printf
This commit is contained in:
parent
c7dc56f737
commit
4c8f26e9f6
@ -5,7 +5,7 @@ import "rand.zig";
|
||||
import "os.zig";
|
||||
|
||||
pub fn main(args: [][]u8) -> %void {
|
||||
%%stderr.print_str("Welcome to the Guess Number Game in Zig.\n");
|
||||
%%stdout.printf("Welcome to the Guess Number Game in Zig.\n");
|
||||
|
||||
var seed : u32 = undefined;
|
||||
const seed_bytes = (&u8)(&seed)[0...4];
|
||||
@ -16,24 +16,24 @@ pub fn main(args: [][]u8) -> %void {
|
||||
const answer = rand.range_u64(0, 100) + 1;
|
||||
|
||||
while (true) {
|
||||
%%stderr.print_str("\nGuess a number between 1 and 100: ");
|
||||
%%stdout.printf("\nGuess a number between 1 and 100: ");
|
||||
var line_buf : [20]u8 = undefined;
|
||||
|
||||
const line_len = stdin.read(line_buf) %% |err| {
|
||||
%%stderr.print_str("Unable to read from stdin.\n");
|
||||
%%stdout.printf("Unable to read from stdin.\n");
|
||||
return err;
|
||||
};
|
||||
|
||||
const guess = parse_u64(line_buf[0...line_len - 1], 10) %% {
|
||||
%%stderr.print_str("Invalid number.\n");
|
||||
%%stdout.printf("Invalid number.\n");
|
||||
continue;
|
||||
};
|
||||
if (guess > answer) {
|
||||
%%stderr.print_str("Guess lower.\n");
|
||||
%%stdout.printf("Guess lower.\n");
|
||||
} else if (guess < answer) {
|
||||
%%stderr.print_str("Guess higher.\n");
|
||||
%%stdout.printf("Guess higher.\n");
|
||||
} else {
|
||||
%%stderr.print_str("You win!\n");
|
||||
%%stdout.printf("You win!\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
20
std/std.zig
20
std/std.zig
@ -14,14 +14,12 @@ pub var stdout = OutStream {
|
||||
.fd = stdout_fileno,
|
||||
.buffer = undefined,
|
||||
.index = 0,
|
||||
.buffered = true,
|
||||
};
|
||||
|
||||
pub var stderr = OutStream {
|
||||
.fd = stderr_fileno,
|
||||
.buffer = undefined,
|
||||
.index = 0,
|
||||
.buffered = false,
|
||||
};
|
||||
|
||||
/// The function received invalid input at runtime. An Invalid error means a
|
||||
@ -50,9 +48,6 @@ pub struct OutStream {
|
||||
fd: isize,
|
||||
buffer: [buffer_size]u8,
|
||||
index: isize,
|
||||
// TODO remove this. let the user flush at will.
|
||||
// for stderr the user can use printf
|
||||
buffered: bool,
|
||||
|
||||
pub fn print_str(os: &OutStream, str: []const u8) -> %isize {
|
||||
var src_bytes_left = str.len;
|
||||
@ -68,9 +63,6 @@ pub struct OutStream {
|
||||
}
|
||||
src_bytes_left -= copy_amt;
|
||||
}
|
||||
if (!os.buffered) {
|
||||
%return os.flush();
|
||||
}
|
||||
return str.len;
|
||||
}
|
||||
|
||||
@ -89,10 +81,6 @@ pub struct OutStream {
|
||||
const amt_printed = buf_print_u64(os.buffer[os.index...], x);
|
||||
os.index += amt_printed;
|
||||
|
||||
if (!os.buffered) {
|
||||
%return os.flush();
|
||||
}
|
||||
|
||||
return amt_printed;
|
||||
}
|
||||
|
||||
@ -103,10 +91,6 @@ pub struct OutStream {
|
||||
const amt_printed = buf_print_i64(os.buffer[os.index...], x);
|
||||
os.index += amt_printed;
|
||||
|
||||
if (!os.buffered) {
|
||||
%return os.flush();
|
||||
}
|
||||
|
||||
return amt_printed;
|
||||
}
|
||||
|
||||
@ -117,10 +101,6 @@ pub struct OutStream {
|
||||
const amt_printed = buf_print_f64(os.buffer[os.index...], x, 4);
|
||||
os.index += amt_printed;
|
||||
|
||||
if (!os.buffered) {
|
||||
%return os.flush();
|
||||
}
|
||||
|
||||
return amt_printed;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user