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(...@@ -373,11 +373,11 @@ pub fn formatType(
373 try output(context, @typeName(T));373 try output(context, @typeName(T));
374 if (enumInfo.is_exhaustive) {374 if (enumInfo.is_exhaustive) {
375 try output(context, ".");375 try output(context, ".");
376 return formatType(@tagName(value), "", options, context, Errors, output, max_depth);376 try output(context, @tagName(value));
377 } else {377 } else {
378 // TODO: when @tagName works on exhaustive enums print known enum strings378 // TODO: when @tagName works on exhaustive enums print known enum strings
379 try output(context, "(");379 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);
381 try output(context, ")");381 try output(context, ")");
382 }382 }
383 },383 },
...@@ -397,7 +397,7 @@ pub fn formatType(...@@ -397,7 +397,7 @@ pub fn formatType(
397 try output(context, " = ");397 try output(context, " = ");
398 inline for (info.fields) |u_field| {398 inline for (info.fields) |u_field| {
399 if (@enumToInt(@as(UnionTagType, value)) == u_field.enum_field.?.value) {399 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);
401 }401 }
402 }402 }
403 try output(context, " }");403 try output(context, " }");
...@@ -424,7 +424,7 @@ pub fn formatType(...@@ -424,7 +424,7 @@ pub fn formatType(
424 }424 }
425 try output(context, @memberName(T, field_i));425 try output(context, @memberName(T, field_i));
426 try output(context, " = ");426 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);
428 }428 }
429 try output(context, " }");429 try output(context, " }");
430 },430 },
...@@ -1343,6 +1343,20 @@ test "enum" {...@@ -1343,6 +1343,20 @@ test "enum" {
1343 try testFmt("enum: Enum.Two\n", "enum: {}\n", .{&value});1343 try testFmt("enum: Enum.Two\n", "enum: {}\n", .{&value});
1344}1344}
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
1346test "float.scientific" {1360test "float.scientific" {
1347 try testFmt("f32: 1.34000003e+00", "f32: {e}", .{@as(f32, 1.34)});1361 try testFmt("f32: 1.34000003e+00", "f32: {e}", .{@as(f32, 1.34)});
1348 try testFmt("f32: 1.23400001e+01", "f32: {e}", .{@as(f32, 12.34)});1362 try testFmt("f32: 1.23400001e+01", "f32: {e}", .{@as(f32, 12.34)});