zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 1d97747665b993eb0c625c8d0a28e2d0c8e167a3 (tree)
parent db1e97d4b19d8399252e0fbc85fc3563b005a892
Author: ryuukk <44361234+ryuukk@users.noreply.github.com>
Date:   Wed, 14 Oct 2020 18:38:59 +0200

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?

Diffstat:
Mlib/std/fmt.zig | 12++++++++++++
1 file changed, 12 insertions(+), 0 deletions(-)

diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig @@ -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) }); },