xref: /freebsd/contrib/llvm-project/libcxx/include/__filesystem/u8path.h (revision 4d3fc8b0570b29fb0d6ee9525f104d52176ff0d4)
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 #include <type_traits>
19 
20 // Only required on Windows for __widen_from_utf8, and included conservatively
21 // because it requires support for localization.
22 #if defined(_LIBCPP_WIN32API)
23 # include <locale>
24 #endif
25 
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 #  pragma GCC system_header
28 #endif
29 
30 #ifndef _LIBCPP_CXX03_LANG
31 
32 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
33 
34 _LIBCPP_AVAILABILITY_FILESYSTEM_PUSH
35 
36 template <class _InputIt>
37 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_WITH_CHAR8_T
38     typename enable_if<__is_pathable<_InputIt>::value, path>::type
39     u8path(_InputIt __f, _InputIt __l) {
40   static_assert(
41 #ifndef _LIBCPP_HAS_NO_CHAR8_T
42       is_same<typename __is_pathable<_InputIt>::__char_type, char8_t>::value ||
43 #endif
44       is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
45       "u8path(Iter, Iter) requires Iter have a value_type of type 'char'"
46       " or 'char8_t'");
47 #if defined(_LIBCPP_WIN32API)
48   string __tmp(__f, __l);
49   using _CVT = __widen_from_utf8<sizeof(wchar_t) * __CHAR_BIT__>;
50   _VSTD::wstring __w;
51   __w.reserve(__tmp.size());
52   _CVT()(back_inserter(__w), __tmp.data(), __tmp.data() + __tmp.size());
53   return path(__w);
54 #else
55   return path(__f, __l);
56 #endif /* !_LIBCPP_WIN32API */
57 }
58 
59 #if defined(_LIBCPP_WIN32API)
60 template <class _InputIt>
61 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_WITH_CHAR8_T
62     typename enable_if<__is_pathable<_InputIt>::value, path>::type
63     u8path(_InputIt __f, _NullSentinel) {
64   static_assert(
65 #ifndef _LIBCPP_HAS_NO_CHAR8_T
66       is_same<typename __is_pathable<_InputIt>::__char_type, char8_t>::value ||
67 #endif
68       is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
69       "u8path(Iter, Iter) requires Iter have a value_type of type 'char'"
70       " or 'char8_t'");
71   string __tmp;
72   const char __sentinel = char{};
73   for (; *__f != __sentinel; ++__f)
74     __tmp.push_back(*__f);
75   using _CVT = __widen_from_utf8<sizeof(wchar_t) * __CHAR_BIT__>;
76   _VSTD::wstring __w;
77   __w.reserve(__tmp.size());
78   _CVT()(back_inserter(__w), __tmp.data(), __tmp.data() + __tmp.size());
79   return path(__w);
80 }
81 #endif /* _LIBCPP_WIN32API */
82 
83 template <class _Source>
84 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_WITH_CHAR8_T
85     typename enable_if<__is_pathable<_Source>::value, path>::type
86     u8path(const _Source& __s) {
87   static_assert(
88 #ifndef _LIBCPP_HAS_NO_CHAR8_T
89       is_same<typename __is_pathable<_Source>::__char_type, char8_t>::value ||
90 #endif
91       is_same<typename __is_pathable<_Source>::__char_type, char>::value,
92       "u8path(Source const&) requires Source have a character type of type "
93       "'char' or 'char8_t'");
94 #if defined(_LIBCPP_WIN32API)
95   using _Traits = __is_pathable<_Source>;
96   return u8path(_VSTD::__unwrap_iter(_Traits::__range_begin(__s)), _VSTD::__unwrap_iter(_Traits::__range_end(__s)));
97 #else
98   return path(__s);
99 #endif
100 }
101 
102 _LIBCPP_AVAILABILITY_FILESYSTEM_POP
103 
104 _LIBCPP_END_NAMESPACE_FILESYSTEM
105 
106 #endif // _LIBCPP_CXX03_LANG
107 
108 #endif // _LIBCPP___FILESYSTEM_U8PATH_H
109