You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
_LocInfo: Use _wsetlocale to query and restore locales
_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#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
0 commit comments