mirror of
https://github.com/ziglang/zig.git
synced 2024-11-26 23:22:44 +00:00
80 lines
2.5 KiB
Zig
80 lines
2.5 KiB
Zig
const Writer = struct {
|
|
/// Calls print and then flushes the buffer.
|
|
pub fn print(self: *Writer, comptime format: []const u8, args: anytype) anyerror!void {
|
|
const State = enum {
|
|
start,
|
|
open_brace,
|
|
close_brace,
|
|
};
|
|
|
|
comptime var start_index: usize = 0;
|
|
comptime var state = State.start;
|
|
comptime var next_arg: usize = 0;
|
|
|
|
inline for (format, 0..) |c, i| {
|
|
switch (state) {
|
|
State.start => switch (c) {
|
|
'{' => {
|
|
if (start_index < i) try self.write(format[start_index..i]);
|
|
state = State.open_brace;
|
|
},
|
|
'}' => {
|
|
if (start_index < i) try self.write(format[start_index..i]);
|
|
state = State.close_brace;
|
|
},
|
|
else => {},
|
|
},
|
|
State.open_brace => switch (c) {
|
|
'{' => {
|
|
state = State.start;
|
|
start_index = i;
|
|
},
|
|
'}' => {
|
|
try self.printValue(args[next_arg]);
|
|
next_arg += 1;
|
|
state = State.start;
|
|
start_index = i + 1;
|
|
},
|
|
's' => {
|
|
continue;
|
|
},
|
|
else => @compileError("Unknown format character: " ++ [1]u8{c}),
|
|
},
|
|
State.close_brace => switch (c) {
|
|
'}' => {
|
|
state = State.start;
|
|
start_index = i;
|
|
},
|
|
else => @compileError("Single '}' encountered in format string"),
|
|
},
|
|
}
|
|
}
|
|
comptime {
|
|
if (args.len != next_arg) {
|
|
@compileError("Unused arguments");
|
|
}
|
|
if (state != State.start) {
|
|
@compileError("Incomplete format string: " ++ format);
|
|
}
|
|
}
|
|
if (start_index < format.len) {
|
|
try self.write(format[start_index..format.len]);
|
|
}
|
|
try self.flush();
|
|
}
|
|
|
|
fn write(self: *Writer, value: []const u8) !void {
|
|
_ = self;
|
|
_ = value;
|
|
}
|
|
pub fn printValue(self: *Writer, value: anytype) !void {
|
|
_ = self;
|
|
_ = value;
|
|
}
|
|
fn flush(self: *Writer) !void {
|
|
_ = self;
|
|
}
|
|
};
|
|
|
|
// syntax
|