Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty print Slices #6680

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/std/fmt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,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'))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v for vendetta? Why bother with two format specifiers when you're treating them the same way?
This way you can't specify what format to use for the content, I'd accept two-char formats too (eg. xv or vx to print the slice contents as hex).

Maybe this is better suited for the * specifier, that's supposed to deref the input.

try format(writer, "[", .{});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're not formatting anything then writer.writeByte('[').

var i: usize = 0;
for (value) |one| {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (value) |one| {
for (value) |elem, i| {

No need to evaluate the i outside.

if (i == value.len - 1) {
try format(writer, "{}", .{one});
} else {
try format(writer, "{}, ", .{one});
}
i += 1;
}
return format(writer, "]", .{});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

}
return format(writer, "{}@{x}", .{ @typeName(ptr_info.child), @ptrToInt(value.ptr) });
},
Expand Down