-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
fix: make std::bitset formattable again #3660
Conversation
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.
Thanks for the PR. I think it's reasonable to provide a formatter for bitset
but it shouldn't rely on ostream.
Sure, I can do that. The formatting itself is easy enough if sticking to the same as But do you have any preference for a formatter to derive from or what to base a parse() on? Edit: this last version works and without ostream. But supports no format specs whatsoever. |
d87baa8
to
4c3bd85
Compare
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.
But do you have any preference for a formatter to derive from or what to base a parse() on?
I think we should at least support width/precision/alignment. This can be done with nested_formatter
, something like:
template <size_t N>
struct fmt::formatter<std::bitset<N>> : nested_formatter<string_view> {
template <typename FormatContext>
auto format(const std::bitset<N>& bs, FormatContext& ctx) const {
return write_padded(ctx, [=](auto out) {
for (auto pos = N; pos; --pos) {
out = fmt::detail::write<char>(out, bs[pos - 1] ? '1' : '0');
}
return out;
});
}
};
but adapted to work with arbitrary code unit (character types).
4c3bd85
to
8c3c187
Compare
Done. I also went ahead and adapted the test case to that by padding on the left with extra zeroes. |
30619ec
to
cc4409c
Compare
Apparently |
cc4409c
to
2b2444c
Compare
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.
Mostly looks good, just a few more comments inline.
It used to be formattable via operator<<(ostream&) implicitly. Make it formattable again, but this time via formatter specialization.
Feel free to merge this if it looks good to you |
std::bitset::to_string allows you to specify the zero and one characters. It would be great if the formatter supported this |
Thank you! |
Thank you for your time and feedback!
@Roman-Koshelev what syntax would you suggest for this in the format spec? Not having a clue about what syntax is a good idea here is why I didn't even attempt that. |
I believe the correct syntax should look like this |
Why make zero mandatory but one optional? I understand this is practically the case for |
An alternative can be |
@muggenhor what do you think? |
@Roman-Koshelev sorry for the delayed reply. I'm super strapped for time lately. Anyway, that syntax looks good to me. Having the mandatory z/o characters there should even make parsing somewhat easier. A colleague mentioned that maybe s(et)/c(lear) would be preferred by some people? I don't care much either way |
* fix: make std::bitset formattable again It used to be formattable via operator<<(ostream&) implicitly. Make it formattable again, but this time via formatter specialization. * fix: make nested_formatter constexpr default constructible
It used to be formattable via
operator<<(ostream&)
implicitly. Make itformattable again, but this time via
formatter
specialization.