Commit 6844dafeca

Marc Tiehuis <marc@tiehu.is>
2019-08-05 09:07:31
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 parent 57b90d2
Changed files (1)
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 => {