diff options
author | Louis Dionne <ldionne.2@gmail.com> | 2025-01-27 12:41:00 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-27 12:41:00 -0500 |
commit | 88cca8ea209bb034eaec6af09a0227fb8cc7303e (patch) | |
tree | 20e68eab4f19a3d76af84e9b9e5e45ce388cfe1a /libcxx/src | |
parent | 658f8500c84fcdcfbf5470ae7b4f732ef4a3c32f (diff) | |
download | llvm-88cca8ea209bb034eaec6af09a0227fb8cc7303e.zip llvm-88cca8ea209bb034eaec6af09a0227fb8cc7303e.tar.gz llvm-88cca8ea209bb034eaec6af09a0227fb8cc7303e.tar.bz2 |
[libc++] Add more missing bits to the locale base API (#122531)
This patch adds the following pieces to the locale base API:
- __setlocale (for std::setlocale)
- __lconv_t (for std::lconv)
- _LIBCPP_FOO_MASK and _LIBCPP_LC_ALL
This should be sufficient to implement all of the platform-agnostic
localization support in libc++ without relying directly on any public
API names from the C library. This makes it possible to port libc++ to
platforms that don't provide the usual locale APIs.
Diffstat (limited to 'libcxx/src')
-rw-r--r-- | libcxx/src/iostream.cpp | 2 | ||||
-rw-r--r-- | libcxx/src/locale.cpp | 44 | ||||
-rw-r--r-- | libcxx/src/support/win32/locale_win32.cpp | 2 |
3 files changed, 24 insertions, 24 deletions
diff --git a/libcxx/src/iostream.cpp b/libcxx/src/iostream.cpp index 6db02d5..9bc9ec0 100644 --- a/libcxx/src/iostream.cpp +++ b/libcxx/src/iostream.cpp @@ -103,7 +103,7 @@ alignas(wostream) _LIBCPP_EXPORTED_FROM_ABI char wclog[sizeof(wostream)] static void force_locale_initialization() { #if defined(_LIBCPP_MSVCRT_LIKE) static bool once = []() { - auto loc = __locale::__newlocale(LC_ALL_MASK, "C", 0); + auto loc = __locale::__newlocale(_LIBCPP_ALL_MASK, "C", 0); { __locale::__locale_guard g(loc); // forces initialization of locale TLS ((void)g); diff --git a/libcxx/src/locale.cpp b/libcxx/src/locale.cpp index fb67a72..81f3ad4 100644 --- a/libcxx/src/locale.cpp +++ b/libcxx/src/locale.cpp @@ -51,7 +51,7 @@ _LIBCPP_PUSH_MACROS _LIBCPP_BEGIN_NAMESPACE_STD struct __libcpp_unique_locale { - __libcpp_unique_locale(const char* nm) : __loc_(__locale::__newlocale(LC_ALL_MASK, nm, 0)) {} + __libcpp_unique_locale(const char* nm) : __loc_(__locale::__newlocale(_LIBCPP_ALL_MASK, nm, 0)) {} ~__libcpp_unique_locale() { if (__loc_) @@ -74,7 +74,7 @@ __locale::__locale_t __cloc() { // In theory this could create a race condition. In practice // the race condition is non-fatal since it will just create // a little resource leak. Better approach would be appreciated. - static __locale::__locale_t result = __locale::__newlocale(LC_ALL_MASK, "C", 0); + static __locale::__locale_t result = __locale::__newlocale(_LIBCPP_ALL_MASK, "C", 0); return result; } #endif // __cloc_defined @@ -570,7 +570,7 @@ locale locale::global(const locale& loc) { locale r = g; g = loc; if (g.name() != "*") - setlocale(LC_ALL, g.name().c_str()); + __locale::__setlocale(_LIBCPP_LC_ALL, g.name().c_str()); return r; } @@ -600,7 +600,7 @@ long locale::id::__get() { // template <> class collate_byname<char> collate_byname<char>::collate_byname(const char* n, size_t refs) - : collate<char>(refs), __l_(__locale::__newlocale(LC_ALL_MASK, n, 0)) { + : collate<char>(refs), __l_(__locale::__newlocale(_LIBCPP_ALL_MASK, n, 0)) { if (__l_ == 0) __throw_runtime_error( ("collate_byname<char>::collate_byname" @@ -610,7 +610,7 @@ collate_byname<char>::collate_byname(const char* n, size_t refs) } collate_byname<char>::collate_byname(const string& name, size_t refs) - : collate<char>(refs), __l_(__locale::__newlocale(LC_ALL_MASK, name.c_str(), 0)) { + : collate<char>(refs), __l_(__locale::__newlocale(_LIBCPP_ALL_MASK, name.c_str(), 0)) { if (__l_ == 0) __throw_runtime_error( ("collate_byname<char>::collate_byname" @@ -644,7 +644,7 @@ collate_byname<char>::string_type collate_byname<char>::do_transform(const char_ #if _LIBCPP_HAS_WIDE_CHARACTERS collate_byname<wchar_t>::collate_byname(const char* n, size_t refs) - : collate<wchar_t>(refs), __l_(__locale::__newlocale(LC_ALL_MASK, n, 0)) { + : collate<wchar_t>(refs), __l_(__locale::__newlocale(_LIBCPP_ALL_MASK, n, 0)) { if (__l_ == 0) __throw_runtime_error( ("collate_byname<wchar_t>::collate_byname(size_t refs)" @@ -654,7 +654,7 @@ collate_byname<wchar_t>::collate_byname(const char* n, size_t refs) } collate_byname<wchar_t>::collate_byname(const string& name, size_t refs) - : collate<wchar_t>(refs), __l_(__locale::__newlocale(LC_ALL_MASK, name.c_str(), 0)) { + : collate<wchar_t>(refs), __l_(__locale::__newlocale(_LIBCPP_ALL_MASK, name.c_str(), 0)) { if (__l_ == 0) __throw_runtime_error( ("collate_byname<wchar_t>::collate_byname(size_t refs)" @@ -1047,7 +1047,7 @@ const unsigned short* ctype<char>::__classic_upper_table() _NOEXCEPT { // template <> class ctype_byname<char> ctype_byname<char>::ctype_byname(const char* name, size_t refs) - : ctype<char>(0, false, refs), __l_(__locale::__newlocale(LC_ALL_MASK, name, 0)) { + : ctype<char>(0, false, refs), __l_(__locale::__newlocale(_LIBCPP_ALL_MASK, name, 0)) { if (__l_ == 0) __throw_runtime_error( ("ctype_byname<char>::ctype_byname" @@ -1057,7 +1057,7 @@ ctype_byname<char>::ctype_byname(const char* name, size_t refs) } ctype_byname<char>::ctype_byname(const string& name, size_t refs) - : ctype<char>(0, false, refs), __l_(__locale::__newlocale(LC_ALL_MASK, name.c_str(), 0)) { + : ctype<char>(0, false, refs), __l_(__locale::__newlocale(_LIBCPP_ALL_MASK, name.c_str(), 0)) { if (__l_ == 0) __throw_runtime_error( ("ctype_byname<char>::ctype_byname" @@ -1092,7 +1092,7 @@ const char* ctype_byname<char>::do_tolower(char_type* low, const char_type* high #if _LIBCPP_HAS_WIDE_CHARACTERS ctype_byname<wchar_t>::ctype_byname(const char* name, size_t refs) - : ctype<wchar_t>(refs), __l_(__locale::__newlocale(LC_ALL_MASK, name, 0)) { + : ctype<wchar_t>(refs), __l_(__locale::__newlocale(_LIBCPP_ALL_MASK, name, 0)) { if (__l_ == 0) __throw_runtime_error( ("ctype_byname<wchar_t>::ctype_byname" @@ -1102,7 +1102,7 @@ ctype_byname<wchar_t>::ctype_byname(const char* name, size_t refs) } ctype_byname<wchar_t>::ctype_byname(const string& name, size_t refs) - : ctype<wchar_t>(refs), __l_(__locale::__newlocale(LC_ALL_MASK, name.c_str(), 0)) { + : ctype<wchar_t>(refs), __l_(__locale::__newlocale(_LIBCPP_ALL_MASK, name.c_str(), 0)) { if (__l_ == 0) __throw_runtime_error( ("ctype_byname<wchar_t>::ctype_byname" @@ -1342,7 +1342,7 @@ constinit locale::id codecvt<wchar_t, char, mbstate_t>::id; codecvt<wchar_t, char, mbstate_t>::codecvt(size_t refs) : locale::facet(refs), __l_(_LIBCPP_GET_C_LOCALE) {} codecvt<wchar_t, char, mbstate_t>::codecvt(const char* nm, size_t refs) - : locale::facet(refs), __l_(__locale::__newlocale(LC_ALL_MASK, nm, 0)) { + : locale::facet(refs), __l_(__locale::__newlocale(_LIBCPP_ALL_MASK, nm, 0)) { if (__l_ == 0) __throw_runtime_error( ("codecvt_byname<wchar_t, char, mbstate_t>::codecvt_byname" @@ -4067,7 +4067,7 @@ void numpunct_byname<char>::__init(const char* nm) { string(nm)) .c_str()); - lconv* lc = __locale::__localeconv(loc.get()); + __locale::__lconv_t* lc = __locale::__localeconv(loc.get()); if (!checked_string_to_char_convert(__decimal_point_, lc->decimal_point, loc.get())) __decimal_point_ = base::do_decimal_point(); if (!checked_string_to_char_convert(__thousands_sep_, lc->thousands_sep, loc.get())) @@ -4098,7 +4098,7 @@ void numpunct_byname<wchar_t>::__init(const char* nm) { string(nm)) .c_str()); - lconv* lc = __locale::__localeconv(loc.get()); + __locale::__lconv_t* lc = __locale::__localeconv(loc.get()); checked_string_to_wchar_convert(__decimal_point_, lc->decimal_point, loc.get()); checked_string_to_wchar_convert(__thousands_sep_, lc->thousands_sep, loc.get()); __grouping_ = lc->grouping; @@ -4442,12 +4442,12 @@ const wstring& __time_get_c_storage<wchar_t>::__r() const { // time_get_byname -__time_get::__time_get(const char* nm) : __loc_(__locale::__newlocale(LC_ALL_MASK, nm, 0)) { +__time_get::__time_get(const char* nm) : __loc_(__locale::__newlocale(_LIBCPP_ALL_MASK, nm, 0)) { if (__loc_ == 0) __throw_runtime_error(("time_get_byname failed to construct for " + string(nm)).c_str()); } -__time_get::__time_get(const string& nm) : __loc_(__locale::__newlocale(LC_ALL_MASK, nm.c_str(), 0)) { +__time_get::__time_get(const string& nm) : __loc_(__locale::__newlocale(_LIBCPP_ALL_MASK, nm.c_str(), 0)) { if (__loc_ == 0) __throw_runtime_error(("time_get_byname failed to construct for " + nm).c_str()); } @@ -5027,12 +5027,12 @@ time_base::dateorder __time_get_storage<wchar_t>::__do_date_order() const { // time_put -__time_put::__time_put(const char* nm) : __loc_(__locale::__newlocale(LC_ALL_MASK, nm, 0)) { +__time_put::__time_put(const char* nm) : __loc_(__locale::__newlocale(_LIBCPP_ALL_MASK, nm, 0)) { if (__loc_ == 0) __throw_runtime_error(("time_put_byname failed to construct for " + string(nm)).c_str()); } -__time_put::__time_put(const string& nm) : __loc_(__locale::__newlocale(LC_ALL_MASK, nm.c_str(), 0)) { +__time_put::__time_put(const string& nm) : __loc_(__locale::__newlocale(_LIBCPP_ALL_MASK, nm.c_str(), 0)) { if (__loc_ == 0) __throw_runtime_error(("time_put_byname failed to construct for " + nm).c_str()); } @@ -5433,7 +5433,7 @@ void moneypunct_byname<char, false>::init(const char* nm) { if (!loc) __throw_runtime_error(("moneypunct_byname failed to construct for " + string(nm)).c_str()); - lconv* lc = __locale::__localeconv(loc.get()); + __locale::__lconv_t* lc = __locale::__localeconv(loc.get()); if (!checked_string_to_char_convert(__decimal_point_, lc->mon_decimal_point, loc.get())) __decimal_point_ = base::do_decimal_point(); if (!checked_string_to_char_convert(__thousands_sep_, lc->mon_thousands_sep, loc.get())) @@ -5468,7 +5468,7 @@ void moneypunct_byname<char, true>::init(const char* nm) { if (!loc) __throw_runtime_error(("moneypunct_byname failed to construct for " + string(nm)).c_str()); - lconv* lc = __locale::__localeconv(loc.get()); + __locale::__lconv_t* lc = __locale::__localeconv(loc.get()); if (!checked_string_to_char_convert(__decimal_point_, lc->mon_decimal_point, loc.get())) __decimal_point_ = base::do_decimal_point(); if (!checked_string_to_char_convert(__thousands_sep_, lc->mon_thousands_sep, loc.get())) @@ -5523,7 +5523,7 @@ void moneypunct_byname<wchar_t, false>::init(const char* nm) { __libcpp_unique_locale loc(nm); if (!loc) __throw_runtime_error(("moneypunct_byname failed to construct for " + string(nm)).c_str()); - lconv* lc = __locale::__localeconv(loc.get()); + __locale::__lconv_t* lc = __locale::__localeconv(loc.get()); if (!checked_string_to_wchar_convert(__decimal_point_, lc->mon_decimal_point, loc.get())) __decimal_point_ = base::do_decimal_point(); if (!checked_string_to_wchar_convert(__thousands_sep_, lc->mon_thousands_sep, loc.get())) @@ -5578,7 +5578,7 @@ void moneypunct_byname<wchar_t, true>::init(const char* nm) { if (!loc) __throw_runtime_error(("moneypunct_byname failed to construct for " + string(nm)).c_str()); - lconv* lc = __locale::__localeconv(loc.get()); + __locale::__lconv_t* lc = __locale::__localeconv(loc.get()); if (!checked_string_to_wchar_convert(__decimal_point_, lc->mon_decimal_point, loc.get())) __decimal_point_ = base::do_decimal_point(); if (!checked_string_to_wchar_convert(__thousands_sep_, lc->mon_thousands_sep, loc.get())) diff --git a/libcxx/src/support/win32/locale_win32.cpp b/libcxx/src/support/win32/locale_win32.cpp index ec2dd7f..24402e8 100644 --- a/libcxx/src/support/win32/locale_win32.cpp +++ b/libcxx/src/support/win32/locale_win32.cpp @@ -26,7 +26,7 @@ __locale_t __newlocale(int /*mask*/, const char* locale, __locale_t /*base*/) { return {::_create_locale(LC_ALL, locale), locale}; } -lconv* __localeconv(__locale_t& loc) { +__lconv_t* __localeconv(__locale_t& loc) { __locale_guard __current(loc); lconv* lc = std::localeconv(); if (!lc) |