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

Pretty print Slices #6680

wants to merge 2 commits into from

Conversation

ryuukk
Copy link
Contributor

@ryuukk ryuukk commented Oct 14, 2020

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

    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?

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| {
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.

@@ -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, "[", .{});
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('[').

}
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.

@@ -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.

@zigazeljko
Copy link
Contributor

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});

@data-man
Copy link
Contributor

data-man commented Oct 18, 2020

What does v mean? Value? Verbose?

Proposal: pretty format slices by default, without additional specifier.
IMO, messages like *const [1]u8@202940 are useless.
If such strings are useful to someone, then there is a * specifier.

Should we drop {v} and make it the default behavior?

👍

@ryuukk ryuukk closed this Oct 26, 2020
@ryuukk ryuukk deleted the fmt_slice branch October 26, 2020 02:52
@ryuukk
Copy link
Contributor Author

ryuukk commented Oct 28, 2020

I tried to apply changes from review, but as soon as i replace else if (fmt.len > 0 and ((fmt[0] == 'v') or (fmt[0] == 'V'))) { with else if (fmt.len == 0) {

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

@Tetralux
Copy link
Contributor

That sounds like a compiler bug.

@pixelherodev
Copy link
Contributor

pixelherodev commented Oct 28, 2020 via email

@Tetralux
Copy link
Contributor

I was under the impression that a loop of a while loop counted as a backwards branch. @pixelherodev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants