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
Pretty print Slices #6680
Conversation
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?
} else if (fmt.len > 0 and ((fmt[0] == 'v') or (fmt[0] == 'V'))) { | ||
try format(writer, "[", .{}); | ||
var i: usize = 0; | ||
for (value) |one| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (value) |one| { | |
for (value) |elem, i| { |
No need to evaluate the i
outside.
@@ -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'))) { | |||
try format(writer, "[", .{}); |
There was a problem hiding this comment.
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('[')
.
} | ||
i += 1; | ||
} | ||
return format(writer, "]", .{}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
@@ -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'))) { |
There was a problem hiding this comment.
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.
What should the following code print? var slicea = [0]u8{};
var sliceb = [3]u8{ 1, 2, 3 };
std.log.info("Content: {v}", .{slicea});
std.log.info("Content: {v}", .{sliceb}); |
What does Proposal: pretty format slices by default, without additional specifier.
👍 |
I tried to apply changes from review, but as soon as i replace When i build my sample project, Semantic Analysis runs forever until i run out of memory I'm not good with zig yet, so i'm giving up on the PR, if someone want to do it instead and figure out why it takes forever, go ahead |
That sounds like a compiler bug. |
Oct 28, 2020 1:34:57 AM Tetralux <notifications@github.com>:
That sounds like a compiler bug.
Pretty sure its an infinite loop in comptime code.
|
I was under the impression that a loop of a while loop counted as a backwards branch. @pixelherodev |
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
will print:
Question:
Should we drop
{v}
and make it the default behavior?