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 <__config>
15 #include <__filesystem/path.h>
16 #include <__locale>
17 #include <string>
18
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 # pragma GCC system_header
21 #endif
22
23 #if _LIBCPP_STD_VER >= 17
24
25 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
26
27 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH
28
29 template <class _InputIt, __enable_if_t<__is_pathable<_InputIt>::value, int> = 0>
u8path(_InputIt __f,_InputIt __l)30 _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_WITH_CHAR8_T path u8path(_InputIt __f, _InputIt __l) {
31 static_assert(
32 # if _LIBCPP_HAS_CHAR8_T
33 is_same<typename __is_pathable<_InputIt>::__char_type, char8_t>::value ||
34 # endif
35 is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
36 "u8path(Iter, Iter) requires Iter have a value_type of type 'char'"
37 " or 'char8_t'");
38 # if defined(_LIBCPP_WIN32API)
39 string __tmp(__f, __l);
40 using _CVT = __widen_from_utf8<sizeof(wchar_t) * __CHAR_BIT__>;
41 std::wstring __w;
42 __w.reserve(__tmp.size());
43 _CVT()(back_inserter(__w), __tmp.data(), __tmp.data() + __tmp.size());
44 return path(__w);
45 # else
46 return path(__f, __l);
47 # endif /* !_LIBCPP_WIN32API */
48 }
49
50 # if defined(_LIBCPP_WIN32API)
51 template <class _InputIt, __enable_if_t<__is_pathable<_InputIt>::value, int> = 0>
u8path(_InputIt __f,_NullSentinel)52 _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_WITH_CHAR8_T path u8path(_InputIt __f, _NullSentinel) {
53 static_assert(
54 # if _LIBCPP_HAS_CHAR8_T
55 is_same<typename __is_pathable<_InputIt>::__char_type, char8_t>::value ||
56 # endif
57 is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
58 "u8path(Iter, Iter) requires Iter have a value_type of type 'char'"
59 " or 'char8_t'");
60 string __tmp;
61 const char __sentinel = char{};
62 for (; *__f != __sentinel; ++__f)
63 __tmp.push_back(*__f);
64 using _CVT = __widen_from_utf8<sizeof(wchar_t) * __CHAR_BIT__>;
65 std::wstring __w;
66 __w.reserve(__tmp.size());
67 _CVT()(back_inserter(__w), __tmp.data(), __tmp.data() + __tmp.size());
68 return path(__w);
69 }
70 # endif /* _LIBCPP_WIN32API */
71
72 template <class _Source, __enable_if_t<__is_pathable<_Source>::value, int> = 0>
u8path(const _Source & __s)73 _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_WITH_CHAR8_T path u8path(const _Source& __s) {
74 static_assert(
75 # if _LIBCPP_HAS_CHAR8_T
76 is_same<typename __is_pathable<_Source>::__char_type, char8_t>::value ||
77 # endif
78 is_same<typename __is_pathable<_Source>::__char_type, char>::value,
79 "u8path(Source const&) requires Source have a character type of type "
80 "'char' or 'char8_t'");
81 # if defined(_LIBCPP_WIN32API)
82 using _Traits = __is_pathable<_Source>;
83 return u8path(std::__unwrap_iter(_Traits::__range_begin(__s)), std::__unwrap_iter(_Traits::__range_end(__s)));
84 # else
85 return path(__s);
86 # endif
87 }
88
89 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP
90
91 _LIBCPP_END_NAMESPACE_FILESYSTEM
92
93 #endif // _LIBCPP_STD_VER >= 17
94
95 #endif // _LIBCPP___FILESYSTEM_U8PATH_H
96