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(...@@ -69,7 +69,6 @@ pub fn format(
69 FormatFillAndAlign,69 FormatFillAndAlign,
70 FormatWidth,70 FormatWidth,
71 FormatPrecision,71 FormatPrecision,
72 Pointer,
73 };72 };
7473
75 comptime var start_index = 0;74 comptime var start_index = 0;
...@@ -109,9 +108,6 @@ pub fn format(...@@ -109,9 +108,6 @@ pub fn format(
109 state = .Start;108 state = .Start;
110 start_index = i;109 start_index = i;
111 },110 },
112 '*' => {
113 state = .Pointer;
114 },
115 ':' => {111 ':' => {
116 state = if (comptime peekIsAlign(fmt[i..])) State.FormatFillAndAlign else State.FormatWidth;112 state = if (comptime peekIsAlign(fmt[i..])) State.FormatFillAndAlign else State.FormatWidth;
117 specifier_end = i;113 specifier_end = i;
...@@ -256,19 +252,6 @@ pub fn format(...@@ -256,19 +252,6 @@ pub fn format(
256 @compileError("Unexpected character in precision value: " ++ [_]u8{c});252 @compileError("Unexpected character in precision value: " ++ [_]u8{c});
257 },253 },
258 },254 },
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 },
272 }255 }
273 }256 }
274 comptime {257 comptime {
...@@ -299,6 +282,13 @@ pub fn formatType(...@@ -299,6 +282,13 @@ pub fn formatType(
299 output: fn (@typeOf(context), []const u8) Errors!void,282 output: fn (@typeOf(context), []const u8) Errors!void,
300 max_depth: usize,283 max_depth: usize,
301) Errors!void {284) 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
302 const T = @typeOf(value);292 const T = @typeOf(value);
303 switch (@typeInfo(T)) {293 switch (@typeInfo(T)) {
304 .ComptimeInt, .Int, .Float => {294 .ComptimeInt, .Int, .Float => {