[libcxx-commits] [libcxx] 643b7bc - [libc++] Fix simple cases of locale name construction

Jake Egan via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jul 8 19:03:35 PDT 2023


Author: Jake Egan
Date: 2023-07-08T22:02:56-04:00
New Revision: 643b7bcdb40433958e7c7022278748b77fe038c4

URL: https://github.com/llvm/llvm-project/commit/643b7bcdb40433958e7c7022278748b77fe038c4
DIFF: https://github.com/llvm/llvm-project/commit/643b7bcdb40433958e7c7022278748b77fe038c4.diff

LOG: [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

Added: 
    libcxx/test/std/localization/locales/locale/locale.cons/name_construction.pass.cpp

Modified: 
    libcxx/src/locale.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/src/locale.cpp b/libcxx/src/locale.cpp
index 930bf4f8f3a2b4..1eea9cf8c1fc75 100644
--- a/libcxx/src/locale.cpp
+++ b/libcxx/src/locale.cpp
@@ -122,6 +122,18 @@ _LIBCPP_NORETURN static void __throw_runtime_error(const string &msg)
 
 }
 
+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
+    // 
diff erent names for 
diff erent 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)
 }
 
 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::install_from(const locale::__imp& one)
 }
 
 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)

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
index 00000000000000..471c66989f22fb
--- /dev/null
+++ b/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
+
+// <locale>
+
+// 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 <locale>
+#include <cassert>
+#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<char>);
+    {
+        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;
+}


        


More information about the libcxx-commits mailing list