Skip to content

Commit 643b7bc

Browse files
committed
[libc++] Fix simple cases of locale name construction
When using 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); ``` The new locale name is always "*". Locale names formed from parts of two named locales (that is, C++ locales having names) are supposed to have names in turn (see C++20 subclause 28.3.1.1 [locale.general] paragraph 8). This patch fixes the name construction for cases when either of locales are unnamed, when the category is locale::none, and when the two locale names are the same. Reviewed By: #libc, ldionne Differential Revision: https://reviews.llvm.org/D119441
1 parent cfbe86c commit 643b7bc

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

libcxx/src/locale.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ _LIBCPP_NORETURN static void __throw_runtime_error(const string &msg)
122122

123123
}
124124

125+
string
126+
build_name(const string& other, const string& one, locale::category c) {
127+
if (other == "*" || one == "*")
128+
return "*";
129+
if (c == locale::none || other == one)
130+
return other;
131+
132+
// FIXME: Handle the more complicated cases, such as when the locale has
133+
// different names for different categories.
134+
return "*";
135+
}
136+
125137
const locale::category locale::none;
126138
const locale::category locale::collate;
127139
const locale::category locale::ctype;
@@ -303,8 +315,7 @@ locale::__imp::__imp(const __imp& other)
303315
}
304316

305317
locale::__imp::__imp(const __imp& other, const string& name, locale::category c)
306-
: facets_(N),
307-
name_("*")
318+
: facets_(N), name_(build_name(other.name_, name, c))
308319
{
309320
facets_ = other.facets_;
310321
for (unsigned i = 0; i < facets_.size(); ++i)
@@ -396,8 +407,7 @@ locale::__imp::install_from(const locale::__imp& one)
396407
}
397408

398409
locale::__imp::__imp(const __imp& other, const __imp& one, locale::category c)
399-
: facets_(N),
400-
name_("*")
410+
: facets_(N), name_(build_name(other.name_, one.name_, c))
401411
{
402412
facets_ = other.facets_;
403413
for (unsigned i = 0; i < facets_.size(); ++i)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// REQUIRES: locale.en_US.UTF-8
10+
// REQUIRES: locale.zh_CN.UTF-8
11+
12+
// <locale>
13+
14+
// Test locale name construction for the following constructors:
15+
// locale(const locale& other, const char* std_name, category cat);
16+
// locale(const locale& other, const string& std_name, category cat);
17+
// locale(const locale& other, const locale& one, category cats);
18+
19+
// This test exercises the fix for locale name construction (D119441), which
20+
// isn't in the dylib for some systems.
21+
// 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}}
22+
23+
#include <locale>
24+
#include <cassert>
25+
#include "platform_support.h" // locale name macros
26+
27+
int main(int, char**) {
28+
std::locale en(LOCALE_en_US_UTF_8);
29+
std::locale zh(LOCALE_zh_CN_UTF_8);
30+
std::locale unnamed(std::locale(), new std::ctype<char>);
31+
{
32+
std::locale loc(unnamed, en, std::locale::time);
33+
assert(loc.name() == "*");
34+
}
35+
{
36+
std::locale loc(en, unnamed, std::locale::none);
37+
assert(loc.name() == "*");
38+
}
39+
{
40+
std::locale loc(en, "", std::locale::none);
41+
assert(loc.name() == en.name());
42+
}
43+
{
44+
std::locale loc(en, zh, std::locale::none);
45+
assert(loc.name() == en.name());
46+
}
47+
{
48+
std::locale loc(en, LOCALE_en_US_UTF_8, std::locale::time);
49+
assert(loc.name() == en.name());
50+
}
51+
{
52+
std::locale loc(en, std::string(LOCALE_en_US_UTF_8), std::locale::collate);
53+
assert(loc.name() == en.name());
54+
}
55+
return 0;
56+
}

0 commit comments

Comments
 (0)