_Locinfo: Use _wsetlocale to query and restore locales#5781
Open
lb90 wants to merge 13 commits intomicrosoft:mainfrom
Open
_Locinfo: Use _wsetlocale to query and restore locales#5781lb90 wants to merge 13 commits intomicrosoft:mainfrom
_Locinfo: Use _wsetlocale to query and restore locales#5781lb90 wants to merge 13 commits intomicrosoft:mainfrom
Conversation
_LocInfo changes the locale temporarily and then reverts to the previous locale on destruction. The sequence of setlocale calls look as follows: 1. oldlocname = setlocale(LC_ALL, nullptr) to query the locale string 2. setlocale(LC_ALL, newlocname) to set the temporary locale 3. setlocale(LC_ALL, oldlocname) to restore the previous locale However there's a catch here: the fully-qualified locale names returned by setlocale are not always ASCII strings (more on that below). This creates challenges because the oldlocname is encoded depending on the "outer" locale, while the setlocale call at point 3) expects an encoding which depend on the "inner" locale, and the two may not match. To solve this issue, use the wide variant of setlocale: _wsetlocale. This way all strings are UTF-16 and there's no issue with varying narrow string encodings. Addendum: Actually, the C RunTime library does its best to use ASCII strings! It queries the english name of the locale using GetLocaleInfoEx. MSDN says that the returned string is always ASCII [1], but that's not always the case [2]. Fixes microsoft#5780 References: 1. https://learn.microsoft.com/en-us/windows/win32/intl/locale-senglish-constants 2. https://developercommunity.visualstudio.com/t/GetLocaleInfoEx-w-LOCALE_SENGLISHLANGUA/10981789
muellerj2
reviewed
Oct 14, 2025
StephanTLavavej
requested changes
Oct 14, 2025
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Keep ABI for `_Locinfo`
This comment was marked as resolved.
This comment was marked as resolved.
_Locinfo: Use _wsetlocale to query and restore locales
12632f1 to
8f157f4
Compare
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
cpplearner
reviewed
Feb 1, 2026
StephanTLavavej
approved these changes
Feb 18, 2026
Member
|
Thank you! 😻 (And apologies for taking so long to review this, |
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.
_Locinfochanges the locale temporarily and then reverts to the previous locale on destruction. The sequence ofsetlocalecalls are as follows:oldlocname = setlocale(LC_ALL, nullptr)to query the locale stringsetlocale(LC_ALL, newlocname)to set the temporary localesetlocale(LC_ALL, oldlocname)to restore the previous localeHowever there's a catch here: the fully-qualified locale names returned by setlocale are not always ASCII strings (more on that below). This creates challenges because the oldlocname is encoded depending on the "outer" locale, while the
setlocalecall at point 3) expects an encoding which follows the "inner" locale, and the two may not match.To solve this issue, use the wide variant of setlocale: _wsetlocale. This way all strings are UTF-16 and there's no issue with varying narrow string encodings.
Addendum:
Actually, the C RunTime library does its best to use ASCII strings! It queries the english name of the locale using
GetLocaleInfoEx. MSDN says that the returned string is always ASCII [1], but that's not always the case [2].Fixes #5780
References:
I made a similar change in
libc++: llvm/llvm-project#160479