support fill, alignment and width in range and tuple formatters - #4910
Open
avionicharshit-byte wants to merge 1 commit into
Open
support fill, alignment and width in range and tuple formatters#4910avionicharshit-byte wants to merge 1 commit into
avionicharshit-byte wants to merge 1 commit into
Conversation
avionicharshit-byte
marked this pull request as ready for review
September 2, 2026 18:06
avionicharshit-byte
force-pushed
the
range-tuple-width
branch
from
September 4, 2026 17:24
a772570 to
3a644c7
Compare
Author
|
@vitaut ci is waiting on workflow approval. the earlier failure was a missing |
vitaut
requested changes
Sep 5, 2026
vitaut
left a comment
Contributor
There was a problem hiding this comment.
Thanks for the PR! Overall looks good but please update the doc per inline comment.
Comment on lines
+632
to
+634
| The `fill`, `align` and `width` options have the same meaning as in the | ||
| [standard format specification](#format-specification-mini-language) and apply | ||
| to the whole formatted range rather than to its elements. |
Contributor
There was a problem hiding this comment.
Please clarify that : is not allowed, something like:
The fill, align and width options have the same meaning as in the standard format specification, except that : cannot be used as a fill character because it introduces the underlying element specification.
Author
There was a problem hiding this comment.
done , used your wording
avionicharshit-byte
force-pushed
the
range-tuple-width
branch
from
September 5, 2026 15:34
3a644c7 to
1ac7c89
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fmt::format("{:*>20}", std::vector<int>{1,2,3})doesnt compile , it fails with "invalid format specifier" . same for tuple , pair and map .std::formataccepts it .in C++23 both
range-format-specandtuple-format-specstart withfill-and-align_opt width_opt, so fill , alignment and width belong to the composed output .range_formatter::parsenever calledparse_alignorparse_width, it went straight to looking forn,sand?.doc/syntax.mddocumented the shorter grammar as well , so thats updated too .the fix adds
detail::composed_specsininclude/fmt/ranges.h, used from the range , tuple and map formatters . it parses alignment and width before the range's own options , and skips a leading:so{::>5}still means the underlying element spec , which is whatstd::formatdoes . informat(), when a width is asked for the composed output goes into amemory_bufferfirst and is then written padded . thats the same buffer and pad idiomnested_formatteruses , and the same one #4860 used for thestd::exceptionformatter .each formatter's old body moved into
write_bodyunchanged andformat()became the padding wrapper . nothing existing changed behaviour , no existing test expectation was edited .i checked 22 cases against real
std::formatoutput from libc++ : default align ,<>^, custom fill , dynamic width{:*>{}}, width withn, width with a nested element spec , sets , maps ,vector<std::string>debug quoting , tuple , pair , and output longer than the width . 21 of the 22 match byte for byte . addedranges_test.format_widthcovering them , all 22 test suites pass ,clang-formatclean .the 22nd is not part of this PR .
fmt::format("{:n}", std::pair<int, int>{1, 2})gives12wherestd::formatgives1, 2, fmt drops the separator along with the brackets for tuples but keeps it for ranges . thats already on master and i havent touched it , fixing it would change the expectations attest/ranges-test.cc:176and:183. happy to file it separately .