Skip to content

<format>: Add grapheme clusterization support for width computation - #2119

Merged
Stephan T. Lavavej (StephanTLavavej) merged 71 commits into
microsoft:mainfrom
barcharcraz:format_uax29
Apr 27, 2022
Merged

Stephan T. Lavavej (StephanTLavavej) merged 71 commits into
microsoft:mainfrom
barcharcraz:format_uax29

Conversation

@barcharcraz

@barcharcraz Charlie Barto (barcharcraz) commented Aug 13, 2021 •

Copy link
Copy Markdown
Contributor
  • Adds three(ish) new iterators, one for codepoint-by-codepoint unicode decoding (utf-8 or 16), one for grapheme clusterization, and one for counting the width of a string. (The reason the third is needed is that for legacy encodings we'd like to only count the width, we have no need to actually decode them to UTF-32.)
  • Uses these in _Measure_string_prefix to count only the first character of each unicode extended grapheme cluster (untailored) as defined in UAX29 and using the Unicode 13 data files (included in tools/unicode_properties_parse). This new processing is only turned on for the "statically UTF-8" case, and not when the system codepage is set to UTF-8, this could be improved in the future, although arguably leads to more consistent behavior.
  • _Fmt_codec is adapted to use the new unicode parsing functions, this makes it more robust against malformed (in particular truncated) UTF-8. _Parse_align still gets only the number of code units in the alignment character, so it will copy an entire malformed subsequence to the format specs, not a U+FFFD replacement (this is not tested).
  • tests for unicode decoding and clusterization. Clusterization test data is again generated from the unicode data files. Note the no-op _Decode_utf function for char32_t inputs, this is to facilitate testing, but could be removed without too much trouble (after modifying the test generator to encode its output).

Future improvements:

  • we could support clusterization for gb18030, especially if set that as the execution charset, doing this would require actually doing unicode decoding for 18030, not decoding it as GBK, which we do now. Also it's not really clear what we should do on invalid data, since sometimes it's impossible to recover from invalid 18030 like it is with UTF-8.
  • The decoding function could surely be faster, but has to be able to handle malformed data correctly, and should be optimized for decoding fairly small quantities of text at once (we absolutely could decode in chunks though)
  • it's possible we should be using the codecvt facets here to do the conversion, but I'm not sure if those do the maximal invalid subsequence replacement thing, which I'd like to do here.
  • it's possible to implement the first set of break rules as a transition table style state-machine, (maybe the others as well, but the table gets much bigger). This is probably a decent idea.
  • If we don't do the above then marking branches as hot or cold in the decode function and the break function may be worthwhile, but this would require vtune investigation.

closes: #1945

@barcharcraz
Charlie Barto (barcharcraz) requested a review from a team as a code owner August 13, 2021 02:05
@StephanTLavavej Stephan T. Lavavej (StephanTLavavej) added enhancement Something can be improved format C++20/23 format labels Aug 13, 2021
Comment thread stl/inc/format Resolved
Comment thread stl/inc/format Resolved
Comment thread stl/inc/format Resolved
Comment thread stl/inc/format Resolved
Comment thread stl/inc/format Resolved
Comment thread stl/inc/format Resolved
Comment thread stl/inc/format Resolved
Comment thread stl/inc/format Resolved
Comment thread tools/validate/validate.cpp Resolved
Comment thread tools/validate/validate.cpp Resolved
Comment thread stl/inc/format Resolved
Comment thread stl/inc/format Resolved
Comment thread stl/inc/format Resolved
Comment thread stl/inc/format Resolved
Comment thread stl/inc/format Resolved
@StephanTLavavej

This comment was marked as resolved.

@barcharcraz

This comment was marked as resolved.

@StephanTLavavej

This comment was marked as resolved.

Comment thread stl/CMakeLists.txt Resolved
Comment thread stl/CMakeLists.txt Resolved
Comment thread stl/inc/format Resolved
Comment thread stl/inc/format Resolved
@StephanTLavavej

Copy link
Copy Markdown
Member

⚠️ Note to self:

  1. We'll need MSVC-internal build/setup changes when mirroring this PR.
  2. VS TPN.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable, just some small nits (and a terrified comment)

Comment thread stl/inc/format Resolved
Comment thread stl/inc/format Resolved
Comment thread stl/inc/format Resolved
Comment thread stl/inc/format Resolved
@StephanTLavavej

Copy link
Copy Markdown
Member

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

@StephanTLavavej

Copy link
Copy Markdown
Member

😻 🎉 🚀

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

Labels

enhancement Something can be improved format C++20/23 format

Projects

None yet

Development

Successfully merging this pull request may close these issues.

<format>: Grapheme clusterization

8 participants