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# if !defined(_LIBCPP_WIN32API) || _LIBCPP_HAS_LOCALIZATION
28template <class _InputIt, __enable_if_t<__is_pathable<_InputIt>::value, int> = 0>
29[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_WITH_CHAR8_T path u8path(_InputIt __f, _InputIt __l) {
30 static_assert(
31# if _LIBCPP_HAS_CHAR8_T
32 is_same<typename __is_pathable<_InputIt>::__char_type, char8_t>::value ||
33# endif
34 is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
35 "u8path(Iter, Iter) requires Iter have a value_type of type 'char'"
36 " or 'char8_t'");
37# if defined(_LIBCPP_WIN32API)
38 string __tmp(__f, __l);
39 using _CVT = __widen_from_utf8<sizeof(wchar_t) * __CHAR_BIT__>;
40 std::wstring __w;
41 __w.reserve(__tmp.size());
42 _CVT()(back_inserter(__w), __tmp.data(), __tmp.data() + __tmp.size());
43 return path(__w);
44# else
45 return path(__f, __l);
46# endif // defined(_LIBCPP_WIN32API)
47}
48# endif // !defined(_LIBCPP_WIN32API) || _LIBCPP_HAS_LOCALIZATION
49
50# if defined(_LIBCPP_WIN32API) && _LIBCPP_HAS_LOCALIZATION
51template <class _InputIt, __enable_if_t<__is_pathable<_InputIt>::value, int> = 0>
52[[nodiscard]] _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 // defined(_LIBCPP_WIN32API) && _LIBCPP_HAS_LOCALIZATION
71
72template <class _Source, __enable_if_t<__is_pathable<_Source>::value, int> = 0>
73[[nodiscard]] _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_END_NAMESPACE_FILESYSTEM
90
91#endif // _LIBCPP_STD_VER >= 17
92
93#endif // _LIBCPP___FILESYSTEM_U8PATH_H
94