Skip to content

Measure display width in tuple formatter - #4631

Merged
Stephan T. Lavavej (StephanTLavavej) merged 1 commit into
microsoft:mainfrom
cpplearner:measure-display-width
May 20, 2024
Merged

Measure display width in tuple formatter#4631
Stephan T. Lavavej (StephanTLavavej) merged 1 commit into
microsoft:mainfrom
cpplearner:measure-display-width

Conversation

@cpplearner

@cpplearner S. B. Tam (cpplearner) commented Apr 26, 2024

Copy link
Copy Markdown
Contributor

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

  1. The output contains multibyte Unicode characters, or
  2. _Tmp_buf.size() is larger than INT_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 to INT_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.

fsb4000

This comment was marked as resolved.

Comment thread stl/inc/format
@StephanTLavavej

Copy link
Copy Markdown
Member

I'm speculatively mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej

Copy link
Copy Markdown
Member

Thanks for fixing this runtime correctness bug in a hopefully-popular new feature! 😻 🐞 🛠️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working format C++20/23 format

Projects

None yet

Development

Successfully merging this pull request may close these issues.

P2286R8_text_formatting_tuple fails on non-English locale <format>: Misbehavior for huge strings

4 participants