authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-06 19:48:42-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-06 19:48:42-07:00
log4c8f26e9f68367f18a19852d468e514e41f1e0d2
tree09887e8334d88f0ae267cabdf9728b345fac183e
parentc7dc56f737ad9eab83b604e132cc3b33bf35e7f0

std: remove auto flushing of stderr. use printf


2 files changed, 7 insertions(+), 27 deletions(-)

example/guess_number/main.zig+7-7
......@@ -5,7 +5,7 @@ import "rand.zig";
55import "os.zig";
66
77pub fn main(args: [][]u8) -> %void {
8 %%stderr.print_str("Welcome to the Guess Number Game in Zig.\n");
8 %%stdout.printf("Welcome to the Guess Number Game in Zig.\n");
99
1010 var seed : u32 = undefined;
1111 const seed_bytes = (&u8)(&seed)[0...4];
......@@ -16,24 +16,24 @@ pub fn main(args: [][]u8) -> %void {
1616 const answer = rand.range_u64(0, 100) + 1;
1717
1818 while (true) {
19 %%stderr.print_str("\nGuess a number between 1 and 100: ");
19 %%stdout.printf("\nGuess a number between 1 and 100: ");
2020 var line_buf : [20]u8 = undefined;
2121
2222 const line_len = stdin.read(line_buf) %% |err| {
23 %%stderr.print_str("Unable to read from stdin.\n");
23 %%stdout.printf("Unable to read from stdin.\n");
2424 return err;
2525 };
2626
2727 const guess = parse_u64(line_buf[0...line_len - 1], 10) %% {
28 %%stderr.print_str("Invalid number.\n");
28 %%stdout.printf("Invalid number.\n");
2929 continue;
3030 };
3131 if (guess > answer) {
32 %%stderr.print_str("Guess lower.\n");
32 %%stdout.printf("Guess lower.\n");
3333 } else if (guess < answer) {
34 %%stderr.print_str("Guess higher.\n");
34 %%stdout.printf("Guess higher.\n");
3535 } else {
36 %%stderr.print_str("You win!\n");
36 %%stdout.printf("You win!\n");
3737 return;
3838 }
3939 }
std/std.zig-20
......@@ -14,14 +14,12 @@ pub var stdout = OutStream {
1414 .fd = stdout_fileno,
1515 .buffer = undefined,
1616 .index = 0,
17 .buffered = true,
1817};
1918
2019pub var stderr = OutStream {
2120 .fd = stderr_fileno,
2221 .buffer = undefined,
2322 .index = 0,
24 .buffered = false,
2523};
2624
2725/// The function received invalid input at runtime. An Invalid error means a
......@@ -50,9 +48,6 @@ pub struct OutStream {
5048 fd: isize,
5149 buffer: [buffer_size]u8,
5250 index: isize,
53 // TODO remove this. let the user flush at will.
54 // for stderr the user can use printf
55 buffered: bool,
5651
5752 pub fn print_str(os: &OutStream, str: []const u8) -> %isize {
5853 var src_bytes_left = str.len;
......@@ -68,9 +63,6 @@ pub struct OutStream {
6863 }
6964 src_bytes_left -= copy_amt;
7065 }
71 if (!os.buffered) {
72 %return os.flush();
73 }
7466 return str.len;
7567 }
7668
......@@ -89,10 +81,6 @@ pub struct OutStream {
8981 const amt_printed = buf_print_u64(os.buffer[os.index...], x);
9082 os.index += amt_printed;
9183
92 if (!os.buffered) {
93 %return os.flush();
94 }
95
9684 return amt_printed;
9785 }
9886
......@@ -103,10 +91,6 @@ pub struct OutStream {
10391 const amt_printed = buf_print_i64(os.buffer[os.index...], x);
10492 os.index += amt_printed;
10593
106 if (!os.buffered) {
107 %return os.flush();
108 }
109
11094 return amt_printed;
11195 }
11296
......@@ -117,10 +101,6 @@ pub struct OutStream {
117101 const amt_printed = buf_print_f64(os.buffer[os.index...], x, 4);
118102 os.index += amt_printed;
119103
120 if (!os.buffered) {
121 %return os.flush();
122 }
123
124104 return amt_printed;
125105 }
126106