Index: libcxx/src/locale.cpp =================================================================== --- libcxx/src/locale.cpp +++ libcxx/src/locale.cpp @@ -122,6 +122,18 @@ } +string +build_name(const string& other, const 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 "*"; +} + const locale::category locale::none; const locale::category locale::collate; const locale::category locale::ctype; @@ -303,8 +315,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) @@ -396,8 +407,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) Index: libcxx/test/std/localization/locales/locale/locale.cons/name_construction.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/localization/locales/locale/locale.cons/name_construction.pass.cpp @@ -0,0 +1,56 @@ +//===----------------------------------------------------------------------===// +// +// 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: stdlib=apple-libc++ && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}} + +#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; +}