1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef _LIBCPP___FILESYSTEM_U8PATH_H
11 #define _LIBCPP___FILESYSTEM_U8PATH_H
12
13 #include <__algorithm/unwrap_iter.h>
14 #include <__availability>
15 #include <__config>
16 #include <__filesystem/path.h>
17 #include <string>
18
19 // Only required on Windows for __widen_from_utf8, and included conservatively
20 // because it requires support for localization.
21 #if defined(_LIBCPP_WIN32API)
22 # include <locale>
23 #endif
24
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 # pragma GCC system_header
27 #endif
28
29 #if _LIBCPP_STD_VER >= 17
30
31 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
32
33 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH
34
35 template <class _InputIt, __enable_if_t<__is_pathable<_InputIt>::value, int> = 0>
u8path(_InputIt __f,_InputIt __l)36 _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_WITH_CHAR8_T path u8path(_InputIt __f, _InputIt __l) {
37 static_assert(
38 # ifndef _LIBCPP_HAS_NO_CHAR8_T
39 is_same<typename __is_pathable<_InputIt>::__char_type, char8_t>::value ||
40 # endif
41 is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
42 "u8path(Iter, Iter) requires Iter have a value_type of type 'char'"
43 " or 'char8_t'");
44 # if defined(_LIBCPP_WIN32API)
45 string __tmp(__f, __l);
46 using _CVT = __widen_from_utf8<sizeof(wchar_t) * __CHAR_BIT__>;
47 std::wstring __w;
48 __w.reserve(__tmp.size());
49 _CVT()(back_inserter(__w), __tmp.data(), __tmp.data() + __tmp.size());
50 return path(__w);
51 # else
52 return path(__f, __l);
53 # endif /* !_LIBCPP_WIN32API */
54 }
55
56 # if defined(_LIBCPP_WIN32API)
57 template <class _InputIt, __enable_if_t<__is_pathable<_InputIt>::value, int> = 0>
u8path(_InputIt __f,_NullSentinel)58 _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_WITH_CHAR8_T path u8path(_InputIt __f, _NullSentinel) {
59 static_assert(
60 # ifndef _LIBCPP_HAS_NO_CHAR8_T
61 is_same<typename __is_pathable<_InputIt>::__char_type, char8_t>::value ||
62 # endif
63 is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
64 "u8path(Iter, Iter) requires Iter have a value_type of type 'char'"
65 " or 'char8_t'");
66 string __tmp;
67 const char __sentinel = char{};
68 for (; *__f != __sentinel; ++__f)
69 __tmp.push_back(*__f);
70 using _CVT = __widen_from_utf8<sizeof(wchar_t) * __CHAR_BIT__>;
71 std::wstring __w;
72 __w.reserve(__tmp.size());
73 _CVT()(back_inserter(__w), __tmp.data(), __tmp.data() + __tmp.size());
74 return path(__w);
75 }
76 # endif /* _LIBCPP_WIN32API */
77
78 template <class _Source, __enable_if_t<__is_pathable<_Source>::value, int> = 0>
u8path(const _Source & __s)79 _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_WITH_CHAR8_T path u8path(const _Source& __s) {
80 static_assert(
81 # ifndef _LIBCPP_HAS_NO_CHAR8_T
82 is_same<typename __is_pathable<_Source>::__char_type, char8_t>::value ||
83 # endif
84 is_same<typename __is_pathable<_Source>::__char_type, char>::value,
85 "u8path(Source const&) requires Source have a character type of type "
86 "'char' or 'char8_t'");
87 # if defined(_LIBCPP_WIN32API)
88 using _Traits = __is_pathable<_Source>;
89 return u8path(std::__unwrap_iter(_Traits::__range_begin(__s)), std::__unwrap_iter(_Traits::__range_end(__s)));
90 # else
91 return path(__s);
92 # endif
93 }
94
95 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP
96
97 _LIBCPP_END_NAMESPACE_FILESYSTEM
98
99 #endif // _LIBCPP_STD_VER >= 17
100
101 #endif // _LIBCPP___FILESYSTEM_U8PATH_H
102