authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2019-08-05 19:07:31+12:00
committergravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2019-08-18 11:06:54+12:00
log6844dafeca42daaf904bc4df0347c6fd625dbcaf
tree99120928adfa4387b03b2a492e47ed1a8656d01a
parent57b90d2d98154e382c58f1b385de2bcef132f7d9

std/fmt: Move pointer parsing out of main state machine

This allows us to format a pointer with alignment/padding as we would with any other format specifier. e.g. {*:5}

1 files changed, 7 insertions(+), 17 deletions(-)

std/fmt.zig+7-17
......@@ -69,7 +69,6 @@ pub fn format(
6969 FormatFillAndAlign,
7070 FormatWidth,
7171 FormatPrecision,
72 Pointer,
7372 };
7473
7574 comptime var start_index = 0;
......@@ -109,9 +108,6 @@ pub fn format(
109108 state = .Start;
110109 start_index = i;
111110 },
112 '*' => {
113 state = .Pointer;
114 },
115111 ':' => {
116112 state = if (comptime peekIsAlign(fmt[i..])) State.FormatFillAndAlign else State.FormatWidth;
117113 specifier_end = i;
......@@ -256,19 +252,6 @@ pub fn format(
256252 @compileError("Unexpected character in precision value: " ++ [_]u8{c});
257253 },
258254 },
259 .Pointer => switch (c) {
260 '}' => {
261 const arg_to_print = comptime nextArg(&used_pos_args, maybe_pos_arg, &next_arg);
262
263 try output(context, @typeName(@typeOf(args[arg_to_print]).Child));
264 try output(context, "@");
265 try formatInt(@ptrToInt(args[arg_to_print]), 16, false, 0, context, Errors, output);
266
267 state = .Start;
268 start_index = i + 1;
269 },
270 else => @compileError("Unexpected format character after '*'"),
271 },
272255 }
273256 }
274257 comptime {
......@@ -299,6 +282,13 @@ pub fn formatType(
299282 output: fn (@typeOf(context), []const u8) Errors!void,
300283 max_depth: usize,
301284) Errors!void {
285 if (comptime std.mem.eql(u8, fmt, "*")) {
286 try output(context, @typeName(@typeOf(value).Child));
287 try output(context, "@");
288 try formatInt(@ptrToInt(value), 16, false, 0, context, Errors, output);
289 return;
290 }
291
302292 const T = @typeOf(value);
303293 switch (@typeInfo(T)) {
304294 .ComptimeInt, .Int, .Float => {