[libcxx-commits] [libcxx] [libc++] Update locale.cpp to use some newer C++ features (PR #173286)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Dec 22 09:10:10 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Nikolas Klauser (philnik777)
<details>
<summary>Changes</summary>
`locale.cpp` has been written a long time ago and has implemented a few things again that have only been added in newer C++ versions. This replaces the custom implementations with standard ones.
---
Full diff: https://github.com/llvm/llvm-project/pull/173286.diff
1 Files Affected:
- (modified) libcxx/src/locale.cpp (+31-42)
``````````diff
diff --git a/libcxx/src/locale.cpp b/libcxx/src/locale.cpp
index aca21173dfe02..1bae0a2153f8b 100644
--- a/libcxx/src/locale.cpp
+++ b/libcxx/src/locale.cpp
@@ -60,9 +60,8 @@ struct __libcpp_unique_locale {
__locale::__locale_t __loc_;
-private:
- __libcpp_unique_locale(__libcpp_unique_locale const&);
- __libcpp_unique_locale& operator=(__libcpp_unique_locale const&);
+ __libcpp_unique_locale(__libcpp_unique_locale const&) = delete;
+ __libcpp_unique_locale& operator=(__libcpp_unique_locale const&) = delete;
};
#ifdef __cloc_defined
@@ -88,16 +87,6 @@ T& make(Args... args) {
return *obj;
}
-template <typename T, size_t N>
-inline constexpr size_t countof(const T (&)[N]) {
- return N;
-}
-
-template <typename T>
-inline constexpr size_t countof(const T* const begin, const T* const end) {
- return static_cast<size_t>(end - begin);
-}
-
string build_name(const string& other, const string& one, locale::category c) {
if (other == "*" || one == "*")
return "*";
@@ -4369,7 +4358,7 @@ string __time_get_storage<char>::__analyze(char fmt, const ctype<char>& ct) {
char f[3] = {0};
f[0] = '%';
f[1] = fmt;
- size_t n = __locale::__strftime(buf, countof(buf), f, &t, __loc_);
+ size_t n = __locale::__strftime(buf, std::size(buf), f, &t, __loc_);
char* bb = buf;
char* be = buf + n;
string result;
@@ -4500,12 +4489,12 @@ wstring __time_get_storage<wchar_t>::__analyze(char fmt, const ctype<wchar_t>& c
char f[3] = {0};
f[0] = '%';
f[1] = fmt;
- __locale::__strftime(buf, countof(buf), f, &t, __loc_);
+ __locale::__strftime(buf, std::size(buf), f, &t, __loc_);
wchar_t wbuf[100];
wchar_t* wbb = wbuf;
mbstate_t mb = {0};
const char* bb = buf;
- size_t j = __locale::__mbsrtowcs(wbb, &bb, countof(wbuf), &mb, __loc_);
+ size_t j = __locale::__mbsrtowcs(wbb, &bb, std::size(wbuf), &mb, __loc_);
if (j == size_t(-1))
std::__throw_runtime_error("locale not supported");
wchar_t* wbe = wbb + j;
@@ -4626,25 +4615,25 @@ void __time_get_storage<char>::init(const ctype<char>& ct) {
// __weeks_
for (int i = 0; i < 7; ++i) {
t.tm_wday = i;
- __locale::__strftime(buf, countof(buf), "%A", &t, __loc_);
+ __locale::__strftime(buf, std::size(buf), "%A", &t, __loc_);
__weeks_[i] = buf;
- __locale::__strftime(buf, countof(buf), "%a", &t, __loc_);
+ __locale::__strftime(buf, std::size(buf), "%a", &t, __loc_);
__weeks_[i + 7] = buf;
}
// __months_
for (int i = 0; i < 12; ++i) {
t.tm_mon = i;
- __locale::__strftime(buf, countof(buf), "%B", &t, __loc_);
+ __locale::__strftime(buf, std::size(buf), "%B", &t, __loc_);
__months_[i] = buf;
- __locale::__strftime(buf, countof(buf), "%b", &t, __loc_);
+ __locale::__strftime(buf, std::size(buf), "%b", &t, __loc_);
__months_[i + 12] = buf;
}
// __am_pm_
t.tm_hour = 1;
- __locale::__strftime(buf, countof(buf), "%p", &t, __loc_);
+ __locale::__strftime(buf, std::size(buf), "%p", &t, __loc_);
__am_pm_[0] = buf;
t.tm_hour = 13;
- __locale::__strftime(buf, countof(buf), "%p", &t, __loc_);
+ __locale::__strftime(buf, std::size(buf), "%p", &t, __loc_);
__am_pm_[1] = buf;
__c_ = __analyze('c', ct);
__r_ = __analyze('r', ct);
@@ -4663,18 +4652,18 @@ void __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct) {
// __weeks_
for (int i = 0; i < 7; ++i) {
t.tm_wday = i;
- __locale::__strftime(buf, countof(buf), "%A", &t, __loc_);
+ __locale::__strftime(buf, std::size(buf), "%A", &t, __loc_);
mb = mbstate_t();
const char* bb = buf;
- size_t j = __locale::__mbsrtowcs(wbuf, &bb, countof(wbuf), &mb, __loc_);
+ size_t j = __locale::__mbsrtowcs(wbuf, &bb, std::size(wbuf), &mb, __loc_);
if (j == size_t(-1) || j == 0)
std::__throw_runtime_error("locale not supported");
wbe = wbuf + j;
__weeks_[i].assign(wbuf, wbe);
- __locale::__strftime(buf, countof(buf), "%a", &t, __loc_);
+ __locale::__strftime(buf, std::size(buf), "%a", &t, __loc_);
mb = mbstate_t();
bb = buf;
- j = __locale::__mbsrtowcs(wbuf, &bb, countof(wbuf), &mb, __loc_);
+ j = __locale::__mbsrtowcs(wbuf, &bb, std::size(wbuf), &mb, __loc_);
if (j == size_t(-1) || j == 0)
std::__throw_runtime_error("locale not supported");
wbe = wbuf + j;
@@ -4683,18 +4672,18 @@ void __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct) {
// __months_
for (int i = 0; i < 12; ++i) {
t.tm_mon = i;
- __locale::__strftime(buf, countof(buf), "%B", &t, __loc_);
+ __locale::__strftime(buf, std::size(buf), "%B", &t, __loc_);
mb = mbstate_t();
const char* bb = buf;
- size_t j = __locale::__mbsrtowcs(wbuf, &bb, countof(wbuf), &mb, __loc_);
+ size_t j = __locale::__mbsrtowcs(wbuf, &bb, std::size(wbuf), &mb, __loc_);
if (j == size_t(-1) || j == 0)
std::__throw_runtime_error("locale not supported");
wbe = wbuf + j;
__months_[i].assign(wbuf, wbe);
- __locale::__strftime(buf, countof(buf), "%b", &t, __loc_);
+ __locale::__strftime(buf, std::size(buf), "%b", &t, __loc_);
mb = mbstate_t();
bb = buf;
- j = __locale::__mbsrtowcs(wbuf, &bb, countof(wbuf), &mb, __loc_);
+ j = __locale::__mbsrtowcs(wbuf, &bb, std::size(wbuf), &mb, __loc_);
if (j == size_t(-1) || j == 0)
std::__throw_runtime_error("locale not supported");
wbe = wbuf + j;
@@ -4702,19 +4691,19 @@ void __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct) {
}
// __am_pm_
t.tm_hour = 1;
- __locale::__strftime(buf, countof(buf), "%p", &t, __loc_);
+ __locale::__strftime(buf, std::size(buf), "%p", &t, __loc_);
mb = mbstate_t();
const char* bb = buf;
- size_t j = __locale::__mbsrtowcs(wbuf, &bb, countof(wbuf), &mb, __loc_);
+ size_t j = __locale::__mbsrtowcs(wbuf, &bb, std::size(wbuf), &mb, __loc_);
if (j == size_t(-1))
std::__throw_runtime_error("locale not supported");
wbe = wbuf + j;
__am_pm_[0].assign(wbuf, wbe);
t.tm_hour = 13;
- __locale::__strftime(buf, countof(buf), "%p", &t, __loc_);
+ __locale::__strftime(buf, std::size(buf), "%p", &t, __loc_);
mb = mbstate_t();
bb = buf;
- j = __locale::__mbsrtowcs(wbuf, &bb, countof(wbuf), &mb, __loc_);
+ j = __locale::__mbsrtowcs(wbuf, &bb, std::size(wbuf), &mb, __loc_);
if (j == size_t(-1))
std::__throw_runtime_error("locale not supported");
wbe = wbuf + j;
@@ -4943,7 +4932,7 @@ void __time_put::__do_put(char* __nb, char*& __ne, const tm* __tm, char __fmt, c
char fmt[] = {'%', __fmt, __mod, 0};
if (__mod != 0)
swap(fmt[1], fmt[2]);
- size_t n = __locale::__strftime(__nb, countof(__nb, __ne), fmt, __tm, __loc_);
+ size_t n = __locale::__strftime(__nb, std::distance(__nb, __ne), fmt, __tm, __loc_);
__ne = __nb + n;
}
@@ -4954,7 +4943,7 @@ void __time_put::__do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm, char __
__do_put(__nar, __ne, __tm, __fmt, __mod);
mbstate_t mb = {0};
const char* __nb = __nar;
- size_t j = __locale::__mbsrtowcs(__wb, &__nb, countof(__wb, __we), &mb, __loc_);
+ size_t j = __locale::__mbsrtowcs(__wb, &__nb, std::distance(__wb, __we), &mb, __loc_);
if (j == size_t(-1))
std::__throw_runtime_error("locale not supported");
__we = __wb + j;
@@ -5429,7 +5418,7 @@ void moneypunct_byname<wchar_t, false>::init(const char* nm) {
wchar_t wbuf[100];
mbstate_t mb = {0};
const char* bb = lc->currency_symbol;
- size_t j = __locale::__mbsrtowcs(wbuf, &bb, countof(wbuf), &mb, loc.get());
+ size_t j = __locale::__mbsrtowcs(wbuf, &bb, std::size(wbuf), &mb, loc.get());
if (j == size_t(-1))
std::__throw_runtime_error("locale not supported");
wchar_t* wbe = wbuf + j;
@@ -5443,7 +5432,7 @@ void moneypunct_byname<wchar_t, false>::init(const char* nm) {
else {
mb = mbstate_t();
bb = lc->positive_sign;
- j = __locale::__mbsrtowcs(wbuf, &bb, countof(wbuf), &mb, loc.get());
+ j = __locale::__mbsrtowcs(wbuf, &bb, std::size(wbuf), &mb, loc.get());
if (j == size_t(-1))
std::__throw_runtime_error("locale not supported");
wbe = wbuf + j;
@@ -5454,7 +5443,7 @@ void moneypunct_byname<wchar_t, false>::init(const char* nm) {
else {
mb = mbstate_t();
bb = lc->negative_sign;
- j = __locale::__mbsrtowcs(wbuf, &bb, countof(wbuf), &mb, loc.get());
+ j = __locale::__mbsrtowcs(wbuf, &bb, std::size(wbuf), &mb, loc.get());
if (j == size_t(-1))
std::__throw_runtime_error("locale not supported");
wbe = wbuf + j;
@@ -5484,7 +5473,7 @@ void moneypunct_byname<wchar_t, true>::init(const char* nm) {
wchar_t wbuf[100];
mbstate_t mb = {0};
const char* bb = lc->int_curr_symbol;
- size_t j = __locale::__mbsrtowcs(wbuf, &bb, countof(wbuf), &mb, loc.get());
+ size_t j = __locale::__mbsrtowcs(wbuf, &bb, std::size(wbuf), &mb, loc.get());
if (j == size_t(-1))
std::__throw_runtime_error("locale not supported");
wchar_t* wbe = wbuf + j;
@@ -5502,7 +5491,7 @@ void moneypunct_byname<wchar_t, true>::init(const char* nm) {
else {
mb = mbstate_t();
bb = lc->positive_sign;
- j = __locale::__mbsrtowcs(wbuf, &bb, countof(wbuf), &mb, loc.get());
+ j = __locale::__mbsrtowcs(wbuf, &bb, std::size(wbuf), &mb, loc.get());
if (j == size_t(-1))
std::__throw_runtime_error("locale not supported");
wbe = wbuf + j;
@@ -5517,7 +5506,7 @@ void moneypunct_byname<wchar_t, true>::init(const char* nm) {
else {
mb = mbstate_t();
bb = lc->negative_sign;
- j = __locale::__mbsrtowcs(wbuf, &bb, countof(wbuf), &mb, loc.get());
+ j = __locale::__mbsrtowcs(wbuf, &bb, std::size(wbuf), &mb, loc.get());
if (j == size_t(-1))
std::__throw_runtime_error("locale not supported");
wbe = wbuf + j;
``````````
</details>
https://github.com/llvm/llvm-project/pull/173286
More information about the libcxx-commits
mailing list