Skip to content

Commit

Permalink
Pretty print Slices
Browse files Browse the repository at this point in the history
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?
  • Loading branch information
ryuukk authored and andrewrk committed Jan 3, 2021
1 parent db1e97d commit 1d97747
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/std/fmt.zig
Expand Up @@ -504,6 +504,18 @@ pub fn formatType(
}
if (ptr_info.child == u8) {
return formatText(value, fmt, options, writer);
} else if (fmt.len > 0 and ((fmt[0] == 'v') or (fmt[0] == 'V'))) {
try format(writer, "[", .{});
var i: usize = 0;
for (value) |one| {
if (i == value.len - 1) {
try format(writer, "{}", .{one});
} else{
try format(writer, "{}, ", .{one});
}
i += 1;
}
return format(writer, "]", .{});
}
return format(writer, "{}@{x}", .{ @typeName(ptr_info.child), @ptrToInt(value.ptr) });
},
Expand Down

0 comments on commit 1d97747

Please sign in to comment.