authorgravatar for 44361234+ryuukk@users.noreply.github.comryuukk <44361234+ryuukk@users.noreply.github.com> 2020-10-14 18:38:59+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-02 17:12:57-07:00
log1d97747665b993eb0c625c8d0a28e2d0c8e167a3
tree39704246d55eb124bf02b80a7df696f962bce22c
parentdb1e97d4b19d8399252e0fbc85fc3563b005a892

Pretty print Slices

This code is adapted from pixelherodev paste from IRC I have added a new fmt option to handle printing slice values ``{v}`` or ``{V}`` While i think it can be made the default, i want your opinion about it ```zig var slicea = [0]u32{}; var sliceb = [3]u32{ 1, 2, 3 }; std.log.info("Content: {v}", .{slicea}); std.log.info("Content: {v}", .{sliceb}); ``` will print: ``` info: Content: [] info: Content: [1, 2, 3] ``` Question: Should we drop ``{v}`` and make it the default behavior?

1 files changed, 12 insertions(+), 0 deletions(-)

lib/std/fmt.zig+12
......@@ -504,6 +504,18 @@ pub fn formatType(
504504 }
505505 if (ptr_info.child == u8) {
506506 return formatText(value, fmt, options, writer);
507 } else if (fmt.len > 0 and ((fmt[0] == 'v') or (fmt[0] == 'V'))) {
508 try format(writer, "[", .{});
509 var i: usize = 0;
510 for (value) |one| {
511 if (i == value.len - 1) {
512 try format(writer, "{}", .{one});
513 } else{
514 try format(writer, "{}, ", .{one});
515 }
516 i += 1;
517 }
518 return format(writer, "]", .{});
507519 }
508520 return format(writer, "{}@{x}", .{ @typeName(ptr_info.child), @ptrToInt(value.ptr) });
509521 },