[libcxx-commits] [libcxx] [libc++] Mostly Implement P1885R12: `<text_encoding>` (PR #141312)
William Tran-Viet via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Mar 13 18:24:40 PDT 2026
================
@@ -0,0 +1,61 @@
+//===----------------------------------------------------------------------===//
+//
+// 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: std-at-least-c++26
+// REQUIRES: locale.en_US.UTF-8
+
+// UNSUPPORTED: no-localization
+// UNSUPPORTED: availability-te-environment-missing
+
+// <text_encoding>
+
+// text_encoding text_encoding::environment();
+
+#include <cassert>
+#include <clocale>
+#include <text_encoding>
+
+#include "../test_text_encoding.h"
+#include "platform_support.h"
+
+int main(int, char**) {
+#if !defined(__ANDROID__) || (defined(__ANDROID__) && __ANDROID_API__ >= 26)
+ std::text_encoding te = std::text_encoding::environment();
+ // 1. Depending on the platform's default, verify that environment() returns the corresponding text encoding.
+ {
+# if defined(__ANDROID__)
+ assert(te.mib() == std::text_encoding::UTF8);
----------------
smallp-o-p wrote:
If I'm understanding you correctly, on Android we can implement `text_encoding::environment()` as such:
```
#if defined(__ANDROID__)
// return UTF-8
#else
// nl_langinfo stuff
#endif
```
Which is fine, and I think that makes it the _most_ compliant implementation (not affected by the `LANG` environment variable!) of `std::text_encoding::environment()`.
Another concern I have is what happens when we need the encoding for a locale name that isn't `""`, such as in `locale::encoding()`. On non-Android systems this is fine because we can use `nl_langinfo_l(...)`, but on Android the implementation will return either [ASCII or UTF-8 as far as I can tell](https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/bionic/langinfo.cpp), which may be a little problematic if the `std::locale` object doesn't represent one of those.
`__get_locale_encoding(string_view)` was written this way so it could be used for such a purpose.
https://github.com/llvm/llvm-project/pull/141312
More information about the libcxx-commits
mailing list