commit 6844dafeca42daaf904bc4df0347c6fd625dbcaf (tree)
parent 57b90d2d98154e382c58f1b385de2bcef132f7d9
Author: Marc Tiehuis <marc@tiehu.is>
Date: Mon, 5 Aug 2019 19:07:31 +1200
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}
Diffstat:
1 file changed, 7 insertions(+), 17 deletions(-)
diff --git a/std/fmt.zig b/std/fmt.zig
@@ -69,7 +69,6 @@ pub fn format(
FormatFillAndAlign,
FormatWidth,
FormatPrecision,
- Pointer,
};
comptime var start_index = 0;
@@ -109,9 +108,6 @@ pub fn format(
state = .Start;
start_index = i;
},
- '*' => {
- state = .Pointer;
- },
':' => {
state = if (comptime peekIsAlign(fmt[i..])) State.FormatFillAndAlign else State.FormatWidth;
specifier_end = i;
@@ -256,19 +252,6 @@ pub fn format(
@compileError("Unexpected character in precision value: " ++ [_]u8{c});
},
},
- .Pointer => switch (c) {
- '}' => {
- const arg_to_print = comptime nextArg(&used_pos_args, maybe_pos_arg, &next_arg);
-
- try output(context, @typeName(@typeOf(args[arg_to_print]).Child));
- try output(context, "@");
- try formatInt(@ptrToInt(args[arg_to_print]), 16, false, 0, context, Errors, output);
-
- state = .Start;
- start_index = i + 1;
- },
- else => @compileError("Unexpected format character after '*'"),
- },
}
}
comptime {
@@ -299,6 +282,13 @@ pub fn formatType(
output: fn (@typeOf(context), []const u8) Errors!void,
max_depth: usize,
) Errors!void {
+ if (comptime std.mem.eql(u8, fmt, "*")) {
+ try output(context, @typeName(@typeOf(value).Child));
+ try output(context, "@");
+ try formatInt(@ptrToInt(value), 16, false, 0, context, Errors, output);
+ return;
+ }
+
const T = @typeOf(value);
switch (@typeInfo(T)) {
.ComptimeInt, .Int, .Float => {