authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-10 14:56:39+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-10 19:43:11-05:00
log3237528a592f2dc22659f77f5fbfb061dc14566a
tree177e435a983f1be27a7dd86c94b300f08062b9b9
parent3170ead9eb3ca91218fd83ecc72170271f8b494c

fmt: Pass the fmt string to the inner formatters


1 files changed, 18 insertions(+), 4 deletions(-)

lib/std/fmt.zig+18-4
......@@ -373,11 +373,11 @@ pub fn formatType(
373373 try output(context, @typeName(T));
374374 if (enumInfo.is_exhaustive) {
375375 try output(context, ".");
376 return formatType(@tagName(value), "", options, context, Errors, output, max_depth);
376 try output(context, @tagName(value));
377377 } else {
378378 // TODO: when @tagName works on exhaustive enums print known enum strings
379379 try output(context, "(");
380 try formatType(@enumToInt(value), "", options, context, Errors, output, max_depth);
380 try formatType(@enumToInt(value), fmt, options, context, Errors, output, max_depth);
381381 try output(context, ")");
382382 }
383383 },
......@@ -397,7 +397,7 @@ pub fn formatType(
397397 try output(context, " = ");
398398 inline for (info.fields) |u_field| {
399399 if (@enumToInt(@as(UnionTagType, value)) == u_field.enum_field.?.value) {
400 try formatType(@field(value, u_field.name), "", options, context, Errors, output, max_depth - 1);
400 try formatType(@field(value, u_field.name), fmt, options, context, Errors, output, max_depth - 1);
401401 }
402402 }
403403 try output(context, " }");
......@@ -424,7 +424,7 @@ pub fn formatType(
424424 }
425425 try output(context, @memberName(T, field_i));
426426 try output(context, " = ");
427 try formatType(@field(value, @memberName(T, field_i)), "", options, context, Errors, output, max_depth - 1);
427 try formatType(@field(value, @memberName(T, field_i)), fmt, options, context, Errors, output, max_depth - 1);
428428 }
429429 try output(context, " }");
430430 },
......@@ -1343,6 +1343,20 @@ test "enum" {
13431343 try testFmt("enum: Enum.Two\n", "enum: {}\n", .{&value});
13441344}
13451345
1346test "non-exhaustive enum" {
1347 const Enum = enum(u16) {
1348 One = 0x000f,
1349 Two = 0xbeef,
1350 _,
1351 };
1352 try testFmt("enum: Enum(15)\n", "enum: {}\n", .{Enum.One});
1353 try testFmt("enum: Enum(48879)\n", "enum: {}\n", .{Enum.Two});
1354 try testFmt("enum: Enum(4660)\n", "enum: {}\n", .{@intToEnum(Enum, 0x1234)});
1355 try testFmt("enum: Enum(f)\n", "enum: {x}\n", .{Enum.One});
1356 try testFmt("enum: Enum(beef)\n", "enum: {x}\n", .{Enum.Two});
1357 try testFmt("enum: Enum(1234)\n", "enum: {x}\n", .{@intToEnum(Enum, 0x1234)});
1358}
1359
13461360test "float.scientific" {
13471361 try testFmt("f32: 1.34000003e+00", "f32: {e}", .{@as(f32, 1.34)});
13481362 try testFmt("f32: 1.23400001e+01", "f32: {e}", .{@as(f32, 12.34)});