| author | |
| committer | |
| log | 4c8f26e9f68367f18a19852d468e514e41f1e0d2 |
| tree | 09887e8334d88f0ae267cabdf9728b345fac183e |
| parent | c7dc56f737ad9eab83b604e132cc3b33bf35e7f0 |
2 files changed, 7 insertions(+), 27 deletions(-)
example/guess_number/main.zig+7-7| ... | ... | @@ -5,7 +5,7 @@ import "rand.zig"; |
| 5 | 5 | import "os.zig"; |
| 6 | 6 | |
| 7 | 7 | pub 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"); | |
| 9 | 9 | |
| 10 | 10 | var seed : u32 = undefined; |
| 11 | 11 | const seed_bytes = (&u8)(&seed)[0...4]; |
| ... | ... | @@ -16,24 +16,24 @@ pub fn main(args: [][]u8) -> %void { |
| 16 | 16 | const answer = rand.range_u64(0, 100) + 1; |
| 17 | 17 | |
| 18 | 18 | while (true) { |
| 19 | %%stderr.print_str("\nGuess a number between 1 and 100: "); | |
| 19 | %%stdout.printf("\nGuess a number between 1 and 100: "); | |
| 20 | 20 | var line_buf : [20]u8 = undefined; |
| 21 | 21 | |
| 22 | 22 | 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"); | |
| 24 | 24 | return err; |
| 25 | 25 | }; |
| 26 | 26 | |
| 27 | 27 | const guess = parse_u64(line_buf[0...line_len - 1], 10) %% { |
| 28 | %%stderr.print_str("Invalid number.\n"); | |
| 28 | %%stdout.printf("Invalid number.\n"); | |
| 29 | 29 | continue; |
| 30 | 30 | }; |
| 31 | 31 | if (guess > answer) { |
| 32 | %%stderr.print_str("Guess lower.\n"); | |
| 32 | %%stdout.printf("Guess lower.\n"); | |
| 33 | 33 | } else if (guess < answer) { |
| 34 | %%stderr.print_str("Guess higher.\n"); | |
| 34 | %%stdout.printf("Guess higher.\n"); | |
| 35 | 35 | } else { |
| 36 | %%stderr.print_str("You win!\n"); | |
| 36 | %%stdout.printf("You win!\n"); | |
| 37 | 37 | return; |
| 38 | 38 | } |
| 39 | 39 | } |
std/std.zig-20| ... | ... | @@ -14,14 +14,12 @@ pub var stdout = OutStream { |
| 14 | 14 | .fd = stdout_fileno, |
| 15 | 15 | .buffer = undefined, |
| 16 | 16 | .index = 0, |
| 17 | .buffered = true, | |
| 18 | 17 | }; |
| 19 | 18 | |
| 20 | 19 | pub var stderr = OutStream { |
| 21 | 20 | .fd = stderr_fileno, |
| 22 | 21 | .buffer = undefined, |
| 23 | 22 | .index = 0, |
| 24 | .buffered = false, | |
| 25 | 23 | }; |
| 26 | 24 | |
| 27 | 25 | /// The function received invalid input at runtime. An Invalid error means a |
| ... | ... | @@ -50,9 +48,6 @@ pub struct OutStream { |
| 50 | 48 | fd: isize, |
| 51 | 49 | buffer: [buffer_size]u8, |
| 52 | 50 | index: isize, |
| 53 | // TODO remove this. let the user flush at will. | |
| 54 | // for stderr the user can use printf | |
| 55 | buffered: bool, | |
| 56 | 51 | |
| 57 | 52 | pub fn print_str(os: &OutStream, str: []const u8) -> %isize { |
| 58 | 53 | var src_bytes_left = str.len; |
| ... | ... | @@ -68,9 +63,6 @@ pub struct OutStream { |
| 68 | 63 | } |
| 69 | 64 | src_bytes_left -= copy_amt; |
| 70 | 65 | } |
| 71 | if (!os.buffered) { | |
| 72 | %return os.flush(); | |
| 73 | } | |
| 74 | 66 | return str.len; |
| 75 | 67 | } |
| 76 | 68 | |
| ... | ... | @@ -89,10 +81,6 @@ pub struct OutStream { |
| 89 | 81 | const amt_printed = buf_print_u64(os.buffer[os.index...], x); |
| 90 | 82 | os.index += amt_printed; |
| 91 | 83 | |
| 92 | if (!os.buffered) { | |
| 93 | %return os.flush(); | |
| 94 | } | |
| 95 | ||
| 96 | 84 | return amt_printed; |
| 97 | 85 | } |
| 98 | 86 | |
| ... | ... | @@ -103,10 +91,6 @@ pub struct OutStream { |
| 103 | 91 | const amt_printed = buf_print_i64(os.buffer[os.index...], x); |
| 104 | 92 | os.index += amt_printed; |
| 105 | 93 | |
| 106 | if (!os.buffered) { | |
| 107 | %return os.flush(); | |
| 108 | } | |
| 109 | ||
| 110 | 94 | return amt_printed; |
| 111 | 95 | } |
| 112 | 96 | |
| ... | ... | @@ -117,10 +101,6 @@ pub struct OutStream { |
| 117 | 101 | const amt_printed = buf_print_f64(os.buffer[os.index...], x, 4); |
| 118 | 102 | os.index += amt_printed; |
| 119 | 103 | |
| 120 | if (!os.buffered) { | |
| 121 | %return os.flush(); | |
| 122 | } | |
| 123 | ||
| 124 | 104 | return amt_printed; |
| 125 | 105 | } |
| 126 | 106 |