diff options
| author | LemonBoy <thatlemon@gmail.com> | 2020-02-10 14:56:39 +0100 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-02-10 19:43:11 -0500 |
| commit | 3237528a592f2dc22659f77f5fbfb061dc14566a (patch) | |
| tree | 177e435a983f1be27a7dd86c94b300f08062b9b9 /lib/std | |
| parent | 3170ead9eb3ca91218fd83ecc72170271f8b494c (diff) | |
| download | zig-3237528a592f2dc22659f77f5fbfb061dc14566a.tar.gz zig-3237528a592f2dc22659f77f5fbfb061dc14566a.zip | |
fmt: Pass the fmt string to the inner formatters
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/fmt.zig | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 370717a4f7..4a653823e5 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -373,11 +373,11 @@ pub fn formatType( try output(context, @typeName(T)); if (enumInfo.is_exhaustive) { try output(context, "."); - return formatType(@tagName(value), "", options, context, Errors, output, max_depth); + try output(context, @tagName(value)); } else { // TODO: when @tagName works on exhaustive enums print known enum strings try output(context, "("); - try formatType(@enumToInt(value), "", options, context, Errors, output, max_depth); + try formatType(@enumToInt(value), fmt, options, context, Errors, output, max_depth); try output(context, ")"); } }, @@ -397,7 +397,7 @@ pub fn formatType( try output(context, " = "); inline for (info.fields) |u_field| { if (@enumToInt(@as(UnionTagType, value)) == u_field.enum_field.?.value) { - try formatType(@field(value, u_field.name), "", options, context, Errors, output, max_depth - 1); + try formatType(@field(value, u_field.name), fmt, options, context, Errors, output, max_depth - 1); } } try output(context, " }"); @@ -424,7 +424,7 @@ pub fn formatType( } try output(context, @memberName(T, field_i)); try output(context, " = "); - try formatType(@field(value, @memberName(T, field_i)), "", options, context, Errors, output, max_depth - 1); + try formatType(@field(value, @memberName(T, field_i)), fmt, options, context, Errors, output, max_depth - 1); } try output(context, " }"); }, @@ -1343,6 +1343,20 @@ test "enum" { try testFmt("enum: Enum.Two\n", "enum: {}\n", .{&value}); } +test "non-exhaustive enum" { + const Enum = enum(u16) { + One = 0x000f, + Two = 0xbeef, + _, + }; + try testFmt("enum: Enum(15)\n", "enum: {}\n", .{Enum.One}); + try testFmt("enum: Enum(48879)\n", "enum: {}\n", .{Enum.Two}); + try testFmt("enum: Enum(4660)\n", "enum: {}\n", .{@intToEnum(Enum, 0x1234)}); + try testFmt("enum: Enum(f)\n", "enum: {x}\n", .{Enum.One}); + try testFmt("enum: Enum(beef)\n", "enum: {x}\n", .{Enum.Two}); + try testFmt("enum: Enum(1234)\n", "enum: {x}\n", .{@intToEnum(Enum, 0x1234)}); +} + test "float.scientific" { try testFmt("f32: 1.34000003e+00", "f32: {e}", .{@as(f32, 1.34)}); try testFmt("f32: 1.23400001e+01", "f32: {e}", .{@as(f32, 12.34)}); |
