-
Notifications
You must be signed in to change notification settings - Fork 1.6k
<regex>: regex_traits::transform_primary should yield primary sort keys appropriate for the imbued locale
#5444
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
Merged
Stephan T. Lavavej (StephanTLavavej)
merged 15 commits into
microsoft:main
from
muellerj2:regex-transform_primary
May 10, 2025
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
898777a
`<regex>`: `regex_traits::transform_primary` should yield primary sor…
muellerj3 c74e118
fix unused variable warnings when RTTI is disabled
muellerj3 413b306
add scary comments
muellerj3 4697464
update expected_results.txt
muellerj3 dd16d0f
Merge branch 'main' into regex-transform_primary
StephanTLavavej 4549c6b
Add regex.cpp to the MSBuild machinery.
StephanTLavavej 5c750f1
Extended friendship is magic.
StephanTLavavej e5d9137
Comment cleanups.
StephanTLavavej cf5cbec
Avoid uninitialized `_Count`, transform `for` to `while`.
StephanTLavavej a303ef2
Guard entrypoints to separately compiled code with `#ifdef _CPPRTTI`.
StephanTLavavej 510562e
Apply GH 5431's fix: `INT_MAX` => `static_cast<size_t>(-1)`
StephanTLavavej 2d4d244
Fix digit transposition.
StephanTLavavej a66c773
Fix Unicode comments.
StephanTLavavej 7b0ceed
Fix /clr:pure.
StephanTLavavej 15d0965
Merge branch 'main' into regex-transform_primary
StephanTLavavej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
|
StephanTLavavej marked this conversation as resolved.
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| // This file is compiled into the import library. | ||
| // MAJOR LIMITATIONS apply to what can be included here! | ||
| // Before editing this file, read: /docs/import_library.md | ||
|
|
||
| #include <__msvc_xlocinfo_types.hpp> | ||
| #include <clocale> | ||
| #include <crtdefs.h> | ||
| #include <cstdlib> | ||
| #include <cstring> | ||
| #include <internal_shared.h> | ||
|
|
||
| #include <Windows.h> | ||
|
|
||
| #undef _ENFORCE_ONLY_CORE_HEADERS | ||
| #include "awint.hpp" | ||
|
StephanTLavavej marked this conversation as resolved.
|
||
|
|
||
| extern "C" { | ||
|
|
||
| // derived from xstrxfrm.cpp | ||
| size_t __stdcall __std_regex_transform_primary_char( | ||
| _Out_writes_(end1 - string1) _Post_readable_size_(return) char* string1, char* end1, | ||
| _In_reads_(end2 - string2) const char* string2, const char* end2, _In_opt_ const _Collvec* ploc) noexcept { | ||
| size_t n1 = end1 - string1; | ||
| size_t n2 = end2 - string2; | ||
| size_t retval = static_cast<size_t>(-1); | ||
| UINT codepage; | ||
| const wchar_t* locale_name; | ||
|
|
||
| if (ploc == nullptr) { | ||
| locale_name = ___lc_locale_name_func()[LC_COLLATE]; | ||
| codepage = ___lc_collate_cp_func(); | ||
| } else { | ||
| locale_name = ploc->_LocaleName; | ||
| codepage = ploc->_Page; | ||
| } | ||
|
|
||
| if (locale_name == nullptr && codepage == CP_ACP) { | ||
| if (n2 <= n1) { | ||
| memcpy(string1, string2, n2); | ||
| } | ||
| retval = n2; | ||
| } else { | ||
| // Inquire size of dst string in BYTES | ||
| const int dstlen = __crtLCMapStringA(locale_name, | ||
| LCMAP_SORTKEY | LINGUISTIC_IGNORECASE | LINGUISTIC_IGNOREDIACRITIC | NORM_IGNOREKANATYPE | NORM_IGNOREWIDTH, | ||
| string2, static_cast<int>(n2), nullptr, 0, codepage, TRUE); | ||
|
|
||
| if (dstlen != 0) { | ||
| retval = dstlen; | ||
|
|
||
| // if not enough room, return amount needed | ||
| if (dstlen <= static_cast<int>(n1)) { | ||
| // Map src string to dst string | ||
| __crtLCMapStringA(locale_name, | ||
| LCMAP_SORTKEY | LINGUISTIC_IGNORECASE | LINGUISTIC_IGNOREDIACRITIC | NORM_IGNOREKANATYPE | ||
| | NORM_IGNOREWIDTH, | ||
| string2, static_cast<int>(n2), string1, static_cast<int>(n1), codepage, TRUE); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return retval; | ||
| } | ||
|
|
||
| // derived from xwcsxfrm.cpp | ||
| size_t __stdcall __std_regex_transform_primary_wchar_t( | ||
| _Out_writes_(end1 - string1) _Post_readable_size_(return) wchar_t* string1, wchar_t* end1, | ||
| _In_reads_(end2 - string2) const wchar_t* string2, const wchar_t* end2, _In_opt_ const _Collvec* ploc) noexcept { | ||
| size_t n1 = end1 - string1; | ||
| size_t n2 = end2 - string2; | ||
| size_t size = static_cast<size_t>(-1); | ||
| const wchar_t* locale_name; | ||
|
|
||
| if (ploc == nullptr) { | ||
| locale_name = ___lc_locale_name_func()[LC_COLLATE]; | ||
| } else { | ||
| locale_name = ploc->_LocaleName; | ||
| } | ||
|
|
||
| if (locale_name == nullptr) { | ||
| if (n2 <= n1) { | ||
| memcpy(string1, string2, n2 * sizeof(wchar_t)); | ||
| } | ||
| size = n2; | ||
| } else { | ||
| // When using LCMAP_SORTKEY, LCMapStringW handles BYTES not wide | ||
| // chars. We use a byte buffer to hold bytes and then convert the | ||
| // byte string to a wide char string and return this so it can be | ||
| // compared using wcscmp(). User's buffer is n1 wide chars, so | ||
| // use an internal buffer of n1 bytes. | ||
|
|
||
| auto bbuffer = _malloc_crt_t(unsigned char, n1); | ||
|
|
||
| if (bbuffer) { | ||
| #pragma warning(push) | ||
| #pragma warning(disable : 6386) // PREfast doesn't understand LCMAP_SORTKEY | ||
| size = __crtLCMapStringW(locale_name, | ||
| LCMAP_SORTKEY | LINGUISTIC_IGNORECASE | LINGUISTIC_IGNOREDIACRITIC | NORM_IGNOREKANATYPE | ||
| | NORM_IGNOREWIDTH, | ||
| string2, static_cast<int>(n2), reinterpret_cast<wchar_t*>(bbuffer.get()), static_cast<int>(n1)); | ||
| #pragma warning(pop) | ||
|
|
||
| if (size == 0) { | ||
| // buffer not big enough, get size required. | ||
| size = __crtLCMapStringW(locale_name, | ||
| LCMAP_SORTKEY | LINGUISTIC_IGNORECASE | LINGUISTIC_IGNOREDIACRITIC | NORM_IGNOREKANATYPE | ||
| | NORM_IGNOREWIDTH, | ||
| string2, static_cast<int>(n2), nullptr, 0); | ||
|
|
||
| if (size == 0) { | ||
| size = static_cast<size_t>(-1); // default error | ||
| } | ||
| } else { | ||
| // string successfully mapped, convert to wide char | ||
|
|
||
| for (size_t i = 0; i < size; ++i) { | ||
| string1[i] = static_cast<wchar_t>(bbuffer.get()[i]); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return size; | ||
| } | ||
| } // extern "C" | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.