Skip to content

String formatting requiring alignment when specifying fill character is surprising #13932

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

Open
tobbez opened this issue Dec 14, 2022 · 1 comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@tobbez
Copy link

tobbez commented Dec 14, 2022

Many users coming from other languages (such as Python, Rust, and even C to some extent) would (not unreasonably, given prior experience with other languages) expect the following code:

std.log.info("'{:02}'", .{1});

to print '01', but because Zig requires alignment to be specified when specifying a fill character, it will instead print ' 1'. The code that would do what the user expected in this case is:

std.log.info("'{:0>2}'", .{1});

This mismatch is likely to produce bugs and confuse users (until they find the appropriate documentation).

The current behavior is not necessarily wrong for Zig, but it would make sense to at least consider whether some change in behavior is warranted.

Note that this isn't a purely theoretical concern, as it has already happened at least once in practice.

I searched for prior discussions on this topic, but only found a single comment that also found the current behavior unexpected and offered two ideas for improvements (along with code implementing them): produce a compile error on leading zeros in the width parameter, or honor unambiguous fill characters even without alignment.

@dee0xeed
Copy link

as it has already happened at least once in practice

twice
https://ziggit.dev/t/what-does-alignment-component-in-format-string-mean/452
I spent an hour to understand what was going on.

@Vexu Vexu added enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library. labels Dec 14, 2022
@Vexu Vexu added this to the 0.12.0 milestone Dec 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

3 participants