diff --git a/libcxx/src/locale.cpp b/libcxx/src/locale.cpp --- a/libcxx/src/locale.cpp +++ b/libcxx/src/locale.cpp @@ -163,6 +163,8 @@ void install(facet* f, long id); template void install(F* f) {install(f, f->id.__get());} template void install_from(const __imp& other); + + string build_name(string other, string one, locale::category c); }; locale::__imp::__imp(size_t refs) @@ -309,9 +311,7 @@ } locale::__imp::__imp(const __imp& other, const string& name, locale::category c) - : facets_(N), - name_("*") -{ + : facets_(N), name_(build_name(other.name_, name, c)) { facets_ = other.facets_; for (unsigned i = 0; i < facets_.size(); ++i) if (facets_[i]) @@ -402,9 +402,7 @@ } locale::__imp::__imp(const __imp& other, const __imp& one, locale::category c) - : facets_(N), - name_("*") -{ + : facets_(N), name_(build_name(other.name_, one.name_, c)) { facets_ = other.facets_; for (unsigned i = 0; i < facets_.size(); ++i) if (facets_[i]) @@ -541,6 +539,17 @@ return facets_[static_cast(id)]; } +string locale::__imp::build_name(string other, string one, locale::category c) { + if (other == "*" || one == "*") + return "*"; + if (c == locale::none || other == one) + return other; + + // FIXME: Handle the more complicated cases, such as when the locale has + // different names for different categories. + return "*"; +} + // locale const locale& diff --git a/libcxx/test/std/localization/locales/locale/locale.cons/name_construction.pass.cpp b/libcxx/test/std/localization/locales/locale/locale.cons/name_construction.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/localization/locales/locale/locale.cons/name_construction.pass.cpp @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// REQUIRES: locale.en_US.UTF-8 +// REQUIRES: locale.zh_CN.UTF-8 + +// + +// Test locale name construction for the following constructors: +// locale(const locale& other, const char* std_name, category cat); +// locale(const locale& other, const string& std_name, category cat); +// locale(const locale& other, const locale& one, category cats); + +// This test exercises the fix for locale name construction (D119441), which +// isn't in the dylib for some systems. +// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12|13|14|15}} + +#include +#include +#include "platform_support.h" // locale name macros + +int main(int, char**) { + std::locale en(LOCALE_en_US_UTF_8); + std::locale zh(LOCALE_zh_CN_UTF_8); + std::locale unnamed(std::locale(), new std::ctype); + + { + std::locale loc(unnamed, en, std::locale::time); + assert(loc.name() == "*"); + } + { + std::locale loc(en, unnamed, std::locale::none); + assert(loc.name() == "*"); + } + { + std::locale loc(en, "", std::locale::none); + assert(loc.name() == en.name()); + } + { + std::locale loc(en, zh, std::locale::none); + assert(loc.name() == en.name()); + } + { + std::locale loc(en, LOCALE_en_US_UTF_8, std::locale::time); + assert(loc.name() == en.name()); + } + { + std::locale loc(en, std::string(LOCALE_en_US_UTF_8), std::locale::collate); + assert(loc.name() == en.name()); + } + return 0; +}