authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-23 18:56:10-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-23 18:56:10-05:00
log4b99f5978f35e54d4b4f1bfae022f48f193e40fd
tree0a42d88386db3b719cf5611eff96c50faeda90f0
parent3075d8aee728d10b1b8428ead070ba47856e78c3

add character format specifier to std.io.OutStream.printf


2 files changed, 23 insertions(+), 2 deletions(-)

std/io.zig+21
......@@ -101,6 +101,7 @@ pub const OutStream = struct {
101101 CloseBrace,
102102 Integer,
103103 IntegerWidth,
104 Character,
104105 };
105106
106107 /// Calls print and then flushes the buffer.
......@@ -155,6 +156,9 @@ pub const OutStream = struct {
155156 width = 0;
156157 state = State.Integer;
157158 },
159 'c' => {
160 state = State.Character;
161 },
158162 else => @compileError("Unknown format character: " ++ []u8{c}),
159163 },
160164 State.CloseBrace => switch (c) {
......@@ -188,6 +192,15 @@ pub const OutStream = struct {
188192 '0' ... '9' => {},
189193 else => @compileError("Unexpected character in format string: " ++ []u8{c}),
190194 },
195 State.Character => switch (c) {
196 '}' => {
197 self.printAsciiChar(args[next_arg]);
198 next_arg += 1;
199 state = State.Start;
200 start_index = i + 1;
201 },
202 else => @compileError("Unexpected character in format string: " ++ []u8{c}),
203 },
191204 }
192205 }
193206 comptime {
......@@ -228,6 +241,14 @@ pub const OutStream = struct {
228241 self.index += amt_printed;
229242 }
230243
244 pub fn printAsciiChar(self: &OutStream, c: u8) -> %void {
245 if (self.index + 1 >= self.buffer.len) {
246 %return self.flush();
247 }
248 self.buffer[self.index] = c;
249 self.index += 1;
250 }
251
231252 pub fn flush(self: &OutStream) -> %void {
232253 while (true) {
233254 const write_ret = system.write(self.fd, &self.buffer[0], self.index);
test/run_tests.cpp+2-2
......@@ -335,9 +335,9 @@ pub const b_text = a_text;
335335const io = @import("std").io;
336336
337337pub fn main(args: [][]u8) -> %void {
338 %%io.stdout.printf("Hello, world!\n{d4} {x3}\n", u32(12), u16(0x12));
338 %%io.stdout.printf("Hello, world!\n{d4} {x3} {c}\n", u32(12), u16(0x12), u8('a'));
339339}
340 )SOURCE", "Hello, world!\n0012 012\n");
340 )SOURCE", "Hello, world!\n0012 012 a\n");
341341
342342
343343 add_simple_case_libc("number literals", R"SOURCE(