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 {...@@ -101,6 +101,7 @@ pub const OutStream = struct {
101 CloseBrace,101 CloseBrace,
102 Integer,102 Integer,
103 IntegerWidth,103 IntegerWidth,
104 Character,
104 };105 };
105106
106 /// Calls print and then flushes the buffer.107 /// Calls print and then flushes the buffer.
...@@ -155,6 +156,9 @@ pub const OutStream = struct {...@@ -155,6 +156,9 @@ pub const OutStream = struct {
155 width = 0;156 width = 0;
156 state = State.Integer;157 state = State.Integer;
157 },158 },
159 'c' => {
160 state = State.Character;
161 },
158 else => @compileError("Unknown format character: " ++ []u8{c}),162 else => @compileError("Unknown format character: " ++ []u8{c}),
159 },163 },
160 State.CloseBrace => switch (c) {164 State.CloseBrace => switch (c) {
...@@ -188,6 +192,15 @@ pub const OutStream = struct {...@@ -188,6 +192,15 @@ pub const OutStream = struct {
188 '0' ... '9' => {},192 '0' ... '9' => {},
189 else => @compileError("Unexpected character in format string: " ++ []u8{c}),193 else => @compileError("Unexpected character in format string: " ++ []u8{c}),
190 },194 },
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 },
191 }204 }
192 }205 }
193 comptime {206 comptime {
...@@ -228,6 +241,14 @@ pub const OutStream = struct {...@@ -228,6 +241,14 @@ pub const OutStream = struct {
228 self.index += amt_printed;241 self.index += amt_printed;
229 }242 }
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
231 pub fn flush(self: &OutStream) -> %void {252 pub fn flush(self: &OutStream) -> %void {
232 while (true) {253 while (true) {
233 const write_ret = system.write(self.fd, &self.buffer[0], self.index);254 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;...@@ -335,9 +335,9 @@ pub const b_text = a_text;
335const io = @import("std").io;335const io = @import("std").io;
336336
337pub fn main(args: [][]u8) -> %void {337pub 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'));
339}339}
340 )SOURCE", "Hello, world!\n0012 012\n");340 )SOURCE", "Hello, world!\n0012 012 a\n");
341341
342342
343 add_simple_case_libc("number literals", R"SOURCE(343 add_simple_case_libc("number literals", R"SOURCE(