[libcxx] Implement c++2a char8_t input/output of std::filesystem::path
This implements the std::filesystem parts of P0482 (which is already
marked as in progress), and applies the actions that are suggested
in P1423.
Differential Revision: https://reviews.llvm.org/D90222
GitOrigin-RevId: 6be11e35d5397ae1c117eb840a969585fdd7d08d
diff --git a/include/filesystem b/include/filesystem
index b4a6476..d24751b 100644
--- a/include/filesystem
+++ b/include/filesystem
@@ -547,6 +547,13 @@
static const bool value = true;
using __char_type = wchar_t;
};
+#ifndef _LIBCPP_NO_HAS_CHAR8_T
+template <>
+struct __can_convert_char<char8_t> {
+ static const bool value = true;
+ using __char_type = char8_t;
+};
+#endif
template <>
struct __can_convert_char<char16_t> {
static const bool value = true;
@@ -995,7 +1002,11 @@
_LIBCPP_INLINE_VISIBILITY operator string_type() const { return __pn_; }
_LIBCPP_INLINE_VISIBILITY _VSTD::string string() const { return __pn_; }
+#ifndef _LIBCPP_NO_HAS_CHAR8_T
+ _LIBCPP_INLINE_VISIBILITY _VSTD::u8string u8string() const { return _VSTD::u8string(__pn_.begin(), __pn_.end()); }
+#else
_LIBCPP_INLINE_VISIBILITY _VSTD::string u8string() const { return __pn_; }
+#endif
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
template <class _ECharT, class _Traits = char_traits<_ECharT>,
@@ -1023,7 +1034,11 @@
// generic format observers
_VSTD::string generic_string() const { return __pn_; }
+#ifndef _LIBCPP_NO_HAS_CHAR8_T
+ _VSTD::u8string generic_u8string() const { return _VSTD::u8string(__pn_.begin(), __pn_.end()); }
+#else
_VSTD::string generic_u8string() const { return __pn_; }
+#endif
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
template <class _ECharT, class _Traits = char_traits<_ECharT>,
@@ -1213,23 +1228,37 @@
size_t hash_value(const path& __p) noexcept;
template <class _Source>
-_LIBCPP_INLINE_VISIBILITY
+_LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_WITH_CHAR8_T
typename enable_if<__is_pathable<_Source>::value, path>::type
u8path(const _Source& __s) {
static_assert(
+#ifndef _LIBCPP_NO_HAS_CHAR8_T
+ is_same<typename __is_pathable<_Source>::__char_type, char8_t>::value ||
+#endif
is_same<typename __is_pathable<_Source>::__char_type, char>::value,
"u8path(Source const&) requires Source have a character type of type "
- "'char'");
+ "'char'"
+#ifndef _LIBCPP_NO_HAS_CHAR8_T
+ " or 'char8_t'"
+#endif
+ );
return path(__s);
}
template <class _InputIt>
-_LIBCPP_INLINE_VISIBILITY
+_LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_WITH_CHAR8_T
typename enable_if<__is_pathable<_InputIt>::value, path>::type
u8path(_InputIt __f, _InputIt __l) {
static_assert(
+#ifndef _LIBCPP_NO_HAS_CHAR8_T
+ is_same<typename __is_pathable<_InputIt>::__char_type, char8_t>::value ||
+#endif
is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
- "u8path(Iter, Iter) requires Iter have a value_type of type 'char'");
+ "u8path(Iter, Iter) requires Iter have a value_type of type 'char'"
+#ifndef _LIBCPP_NO_HAS_CHAR8_T
+ " or 'char8_t'"
+#endif
+ );
return path(__f, __l);
}