Measure display width in tuple formatter #4631
Merged
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.
Currently, the tuple formatter uses
static_cast<int>(_Tmp_buf.size())
to compute the width of output (in order to determine the number of fill characters to insert). This gives the wrong width if_Tmp_buf.size()
is larger thanINT_MAX
(see<format>
: Misbehavior for huge strings #4479).Both problems can be avoided by using
_Measure_string_prefix
, which computes the display width and clamps the result toINT_MAX
(which means no fill character will be inserted).Closes #4479
While working on this, I noticed that the tuple formatter test uses
\u00d6
which cannot be represented in some legacy text encodings. This PR adjusted the test to make it encoding-independent.Closes #4635
Future work: if no width is specified in the format string, there should be no need to use a temporary buffer. The use of
_Tmp_buf
should be skipped in this case.