114f5a3b0Smrg // Class filesystem::path -*- C++ -*-
214f5a3b0Smrg
3b1e83836Smrg // Copyright (C) 2014-2022 Free Software Foundation, Inc.
414f5a3b0Smrg //
514f5a3b0Smrg // This file is part of the GNU ISO C++ Library. This library is free
614f5a3b0Smrg // software; you can redistribute it and/or modify it under the
714f5a3b0Smrg // terms of the GNU General Public License as published by the
814f5a3b0Smrg // Free Software Foundation; either version 3, or (at your option)
914f5a3b0Smrg // any later version.
1014f5a3b0Smrg
1114f5a3b0Smrg // This library is distributed in the hope that it will be useful,
1214f5a3b0Smrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
1314f5a3b0Smrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414f5a3b0Smrg // GNU General Public License for more details.
1514f5a3b0Smrg
1614f5a3b0Smrg // Under Section 7 of GPL version 3, you are granted additional
1714f5a3b0Smrg // permissions described in the GCC Runtime Library Exception, version
1814f5a3b0Smrg // 3.1, as published by the Free Software Foundation.
1914f5a3b0Smrg
2014f5a3b0Smrg // You should have received a copy of the GNU General Public License and
2114f5a3b0Smrg // a copy of the GCC Runtime Library Exception along with this program;
2214f5a3b0Smrg // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
2314f5a3b0Smrg // <http://www.gnu.org/licenses/>.
2414f5a3b0Smrg
2514f5a3b0Smrg /** @file include/bits/fs_path.h
2614f5a3b0Smrg * This is an internal header file, included by other library headers.
2714f5a3b0Smrg * Do not attempt to use it directly. @headername{filesystem}
2814f5a3b0Smrg */
2914f5a3b0Smrg
3014f5a3b0Smrg #ifndef _GLIBCXX_FS_PATH_H
3114f5a3b0Smrg #define _GLIBCXX_FS_PATH_H 1
3214f5a3b0Smrg
3314f5a3b0Smrg #if __cplusplus >= 201703L
3414f5a3b0Smrg
3514f5a3b0Smrg #include <type_traits>
3614f5a3b0Smrg #include <locale>
3714f5a3b0Smrg #include <iosfwd>
38181254a7Smrg #include <iomanip>
3914f5a3b0Smrg #include <codecvt>
4014f5a3b0Smrg #include <string_view>
4114f5a3b0Smrg #include <system_error>
4214f5a3b0Smrg #include <bits/stl_algobase.h>
43b1e83836Smrg #include <bits/stl_pair.h>
4414f5a3b0Smrg #include <bits/locale_conv.h>
45181254a7Smrg #include <ext/concurrence.h>
46181254a7Smrg #include <bits/shared_ptr.h>
47181254a7Smrg #include <bits/unique_ptr.h>
4814f5a3b0Smrg
49fb8a8121Smrg #if __cplusplus > 201703L
50fb8a8121Smrg # include <compare>
51fb8a8121Smrg #endif
52fb8a8121Smrg
5314f5a3b0Smrg #if defined(_WIN32) && !defined(__CYGWIN__)
5414f5a3b0Smrg # define _GLIBCXX_FILESYSTEM_IS_WINDOWS 1
5514f5a3b0Smrg #endif
5614f5a3b0Smrg
_GLIBCXX_VISIBILITY(default)5714f5a3b0Smrg namespace std _GLIBCXX_VISIBILITY(default)
5814f5a3b0Smrg {
5914f5a3b0Smrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
6014f5a3b0Smrg
6114f5a3b0Smrg namespace filesystem
6214f5a3b0Smrg {
6314f5a3b0Smrg _GLIBCXX_BEGIN_NAMESPACE_CXX11
6414f5a3b0Smrg
65fb8a8121Smrg class path;
66fb8a8121Smrg
67fb8a8121Smrg /// @cond undocumented
68fb8a8121Smrg namespace __detail
6914f5a3b0Smrg {
70b1e83836Smrg /// @addtogroup filesystem
71b1e83836Smrg /// @{
72fb8a8121Smrg template<typename _CharT>
73b1e83836Smrg inline constexpr bool __is_encoded_char = false;
74b1e83836Smrg template<>
75b1e83836Smrg inline constexpr bool __is_encoded_char<char> = true;
76181254a7Smrg #ifdef _GLIBCXX_USE_CHAR8_T
77b1e83836Smrg template<>
78b1e83836Smrg inline constexpr bool __is_encoded_char<char8_t> = true;
79181254a7Smrg #endif
80fb8a8121Smrg #if _GLIBCXX_USE_WCHAR_T
81b1e83836Smrg template<>
82b1e83836Smrg inline constexpr bool __is_encoded_char<wchar_t> = true;
83fb8a8121Smrg #endif
84b1e83836Smrg template<>
85b1e83836Smrg inline constexpr bool __is_encoded_char<char16_t> = true;
86b1e83836Smrg template<>
87b1e83836Smrg inline constexpr bool __is_encoded_char<char32_t> = true;
8814f5a3b0Smrg
89b1e83836Smrg #if __cpp_concepts >= 201907L
9014f5a3b0Smrg template<typename _Iter>
91b1e83836Smrg using __safe_iterator_traits = std::iterator_traits<_Iter>;
92b1e83836Smrg #else
9314f5a3b0Smrg template<typename _Iter>
94b1e83836Smrg struct __safe_iterator_traits : std::iterator_traits<_Iter>
95b1e83836Smrg { };
96b1e83836Smrg
97b1e83836Smrg // Protect against ill-formed iterator_traits specializations in C++17
98b1e83836Smrg template<> struct __safe_iterator_traits<void*> { };
99b1e83836Smrg template<> struct __safe_iterator_traits<const void*> { };
100b1e83836Smrg template<> struct __safe_iterator_traits<volatile void*> { };
101b1e83836Smrg template<> struct __safe_iterator_traits<const volatile void*> { };
102b1e83836Smrg #endif
103b1e83836Smrg
104b1e83836Smrg template<typename _Iter_traits, typename = void>
105*0a307195Smrg inline constexpr bool __is_path_iter_src = false;
106b1e83836Smrg
107b1e83836Smrg template<typename _Iter_traits>
108*0a307195Smrg inline constexpr bool
109*0a307195Smrg __is_path_iter_src<_Iter_traits, void_t<typename _Iter_traits::value_type>>
110*0a307195Smrg = __is_encoded_char<typename _Iter_traits::value_type>;
11114f5a3b0Smrg
11214f5a3b0Smrg template<typename _Source>
113b1e83836Smrg inline constexpr bool __is_path_src
114*0a307195Smrg = __is_path_iter_src<iterator_traits<decay_t<_Source>>>;
11514f5a3b0Smrg
116b1e83836Smrg template<>
117b1e83836Smrg inline constexpr bool __is_path_src<path> = false;
11814f5a3b0Smrg
119b1e83836Smrg template<>
120b1e83836Smrg inline constexpr bool __is_path_src<volatile path> = false;
12114f5a3b0Smrg
122b1e83836Smrg template<>
123b1e83836Smrg inline constexpr bool __is_path_src<void*> = false;
12414f5a3b0Smrg
125b1e83836Smrg template<>
126b1e83836Smrg inline constexpr bool __is_path_src<const void*> = false;
127b1e83836Smrg
128b1e83836Smrg template<>
129b1e83836Smrg inline constexpr bool __is_path_src<volatile void*> = false;
130b1e83836Smrg
131b1e83836Smrg template<>
132b1e83836Smrg inline constexpr bool __is_path_src<const volatile void*> = false;
13314f5a3b0Smrg
13414f5a3b0Smrg template<typename _CharT, typename _Traits, typename _Alloc>
135b1e83836Smrg inline constexpr bool
136b1e83836Smrg __is_path_src<basic_string<_CharT, _Traits, _Alloc>>
137b1e83836Smrg = __is_encoded_char<_CharT>;
138b1e83836Smrg
139b1e83836Smrg template<typename _CharT, typename _Traits>
140b1e83836Smrg inline constexpr bool
141b1e83836Smrg __is_path_src<basic_string_view<_CharT, _Traits>>
142b1e83836Smrg = __is_encoded_char<_CharT>;
143b1e83836Smrg
144b1e83836Smrg // SFINAE constraint for Source parameters as required by [fs.path.req].
145b1e83836Smrg template<typename _Tp>
146b1e83836Smrg using _Path = enable_if_t<__is_path_src<_Tp>, path>;
147b1e83836Smrg
148b1e83836Smrg // SFINAE constraint for InputIterator parameters as required by [fs.req].
149b1e83836Smrg template<typename _Iter, typename _Tr = __safe_iterator_traits<_Iter>>
150*0a307195Smrg using _Path2 = enable_if_t<__is_path_iter_src<_Tr>, path>;
151b1e83836Smrg
152b1e83836Smrg #if __cpp_lib_concepts
153b1e83836Smrg template<typename _Iter>
154b1e83836Smrg constexpr bool __is_contiguous = std::contiguous_iterator<_Iter>;
155b1e83836Smrg #else
156b1e83836Smrg template<typename _Iter>
157b1e83836Smrg constexpr bool __is_contiguous = false;
158b1e83836Smrg #endif
159b1e83836Smrg
160b1e83836Smrg template<typename _Tp>
161b1e83836Smrg constexpr bool __is_contiguous<_Tp*> = true;
162b1e83836Smrg
163b1e83836Smrg template<typename _Tp, typename _Seq>
164b1e83836Smrg constexpr bool
165b1e83836Smrg __is_contiguous<__gnu_cxx::__normal_iterator<_Tp*, _Seq>> = true;
166b1e83836Smrg
167b1e83836Smrg #if !defined _GLIBCXX_FILESYSTEM_IS_WINDOWS && defined _GLIBCXX_USE_CHAR8_T
168b1e83836Smrg // For POSIX treat char8_t sequences as char without encoding conversions.
169b1e83836Smrg template<typename _EcharT>
170b1e83836Smrg using __unified_u8_t
171b1e83836Smrg = __conditional_t<is_same_v<_EcharT, char8_t>, char, _EcharT>;
172b1e83836Smrg #else
173b1e83836Smrg template<typename _EcharT>
174b1e83836Smrg using __unified_u8_t = _EcharT;
175b1e83836Smrg #endif
176b1e83836Smrg
177b1e83836Smrg // The __effective_range overloads convert a Source parameter into
178b1e83836Smrg // either a basic_string_view<C> or basic_string<C> containing the
179b1e83836Smrg // effective range of the Source, as defined in [fs.path.req].
18014f5a3b0Smrg
18114f5a3b0Smrg template<typename _CharT, typename _Traits, typename _Alloc>
182b1e83836Smrg inline basic_string_view<_CharT>
183b1e83836Smrg __effective_range(const basic_string<_CharT, _Traits, _Alloc>& __source)
184b1e83836Smrg noexcept
185b1e83836Smrg { return __source; }
18614f5a3b0Smrg
18714f5a3b0Smrg template<typename _CharT, typename _Traits>
188b1e83836Smrg inline basic_string_view<_CharT>
189b1e83836Smrg __effective_range(const basic_string_view<_CharT, _Traits>& __source)
190b1e83836Smrg noexcept
191b1e83836Smrg { return __source; }
192b1e83836Smrg
193b1e83836Smrg // Return the effective range of an NTCTS.
194b1e83836Smrg template<typename _Source>
195b1e83836Smrg auto
196b1e83836Smrg __effective_range(const _Source& __source)
197b1e83836Smrg {
198b1e83836Smrg // Remove a level of normal/safe iterator indirection, or decay an array.
199b1e83836Smrg using _Iter = decltype(std::__niter_base(__source));
200b1e83836Smrg using value_type = typename iterator_traits<_Iter>::value_type;
201b1e83836Smrg
202b1e83836Smrg if constexpr (__is_contiguous<_Iter>)
203b1e83836Smrg return basic_string_view<value_type>{&*__source};
204b1e83836Smrg else
205b1e83836Smrg {
206b1e83836Smrg // _Source is an input iterator that iterates over an NTCTS.
207b1e83836Smrg // Create a basic_string by reading until the null character.
208b1e83836Smrg basic_string<__unified_u8_t<value_type>> __str;
209b1e83836Smrg _Source __it = __source;
210b1e83836Smrg for (value_type __ch = *__it; __ch != value_type(); __ch = *++__it)
211b1e83836Smrg __str.push_back(__ch);
212b1e83836Smrg return __str;
213b1e83836Smrg }
214b1e83836Smrg }
215b1e83836Smrg
216b1e83836Smrg // The value type of a Source parameter's effective range.
217b1e83836Smrg template<typename _Source>
218b1e83836Smrg struct __source_value_type_impl
219b1e83836Smrg {
220b1e83836Smrg using type
221b1e83836Smrg = typename __safe_iterator_traits<decay_t<_Source>>::value_type;
222b1e83836Smrg };
223b1e83836Smrg
224b1e83836Smrg template<typename _CharT, typename _Traits, typename _Alloc>
225b1e83836Smrg struct __source_value_type_impl<basic_string<_CharT, _Traits, _Alloc>>
226b1e83836Smrg {
227b1e83836Smrg using type = _CharT;
228b1e83836Smrg };
22914f5a3b0Smrg
23014f5a3b0Smrg template<typename _CharT, typename _Traits>
231b1e83836Smrg struct __source_value_type_impl<basic_string_view<_CharT, _Traits>>
232b1e83836Smrg {
233b1e83836Smrg using type = _CharT;
234b1e83836Smrg };
23514f5a3b0Smrg
236b1e83836Smrg // The value type of a Source parameter's effective range.
237b1e83836Smrg template<typename _Source>
238b1e83836Smrg using __source_value_t = typename __source_value_type_impl<_Source>::type;
239b1e83836Smrg
240b1e83836Smrg // SFINAE helper to check that an effective range has value_type char,
241b1e83836Smrg // as required by path constructors taking a std::locale parameter.
242b1e83836Smrg // The type _Tp must have already been checked by _Path<Tp> or _Path2<_Tp>.
243b1e83836Smrg template<typename _Tp, typename _Val = __source_value_t<_Tp>>
24414f5a3b0Smrg using __value_type_is_char
245b1e83836Smrg = std::enable_if_t<std::is_same_v<_Val, char>, _Val>;
24614f5a3b0Smrg
247b1e83836Smrg // As above, but also allows char8_t, as required by u8path
248b1e83836Smrg // C++20 [depr.fs.path.factory]
249b1e83836Smrg template<typename _Tp, typename _Val = __source_value_t<_Tp>>
250fb8a8121Smrg using __value_type_is_char_or_char8_t
251b1e83836Smrg = std::enable_if_t<std::is_same_v<_Val, char>
252fb8a8121Smrg #ifdef _GLIBCXX_USE_CHAR8_T
253b1e83836Smrg || std::is_same_v<_Val, char8_t>
254fb8a8121Smrg #endif
255b1e83836Smrg , _Val>;
256fb8a8121Smrg
257b1e83836Smrg // Create a basic_string<C> or basic_string_view<C> from an iterator range.
258b1e83836Smrg template<typename _InputIterator>
259b1e83836Smrg inline auto
260b1e83836Smrg __string_from_range(_InputIterator __first, _InputIterator __last)
261b1e83836Smrg {
262b1e83836Smrg using _EcharT
263b1e83836Smrg = typename std::iterator_traits<_InputIterator>::value_type;
264b1e83836Smrg static_assert(__is_encoded_char<_EcharT>); // C++17 [fs.req]/3
265b1e83836Smrg
266b1e83836Smrg if constexpr (__is_contiguous<_InputIterator>)
267b1e83836Smrg {
268b1e83836Smrg // For contiguous iterators we can just return a string view.
269b1e83836Smrg if (auto __len = __last - __first) [[__likely__]]
270b1e83836Smrg return basic_string_view<_EcharT>(&*__first, __len);
271b1e83836Smrg return basic_string_view<_EcharT>();
272b1e83836Smrg }
273b1e83836Smrg else
274b1e83836Smrg {
275b1e83836Smrg // Conversion requires contiguous characters, so create a string.
276b1e83836Smrg return basic_string<__unified_u8_t<_EcharT>>(__first, __last);
277b1e83836Smrg }
278b1e83836Smrg }
279b1e83836Smrg
280b1e83836Smrg /// @} group filesystem
281fb8a8121Smrg } // namespace __detail
282fb8a8121Smrg /// @endcond
283fb8a8121Smrg
284b1e83836Smrg /// @addtogroup filesystem
285b1e83836Smrg /// @{
286b1e83836Smrg
287b1e83836Smrg /// A filesystem path
288*0a307195Smrg /**
289*0a307195Smrg * @ingroup filesystem
290*0a307195Smrg * @headerfile filesystem
291*0a307195Smrg * @since C++17
292*0a307195Smrg */
293fb8a8121Smrg class path
294fb8a8121Smrg {
29514f5a3b0Smrg public:
29614f5a3b0Smrg #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
297fb8a8121Smrg using value_type = wchar_t;
29814f5a3b0Smrg static constexpr value_type preferred_separator = L'\\';
29914f5a3b0Smrg #else
300fb8a8121Smrg # ifdef _GLIBCXX_DOXYGEN
301fb8a8121Smrg /// Windows uses wchar_t for path::value_type, POSIX uses char.
302fb8a8121Smrg using value_type = __os_dependent__;
303fb8a8121Smrg # else
304fb8a8121Smrg using value_type = char;
305fb8a8121Smrg # endif
30614f5a3b0Smrg static constexpr value_type preferred_separator = '/';
30714f5a3b0Smrg #endif
308fb8a8121Smrg using string_type = std::basic_string<value_type>;
30914f5a3b0Smrg
310fb8a8121Smrg /// path::format is ignored in this implementation
311181254a7Smrg enum format : unsigned char { native_format, generic_format, auto_format };
31214f5a3b0Smrg
31314f5a3b0Smrg // constructors and destructor
31414f5a3b0Smrg
31514f5a3b0Smrg path() noexcept { }
31614f5a3b0Smrg
31714f5a3b0Smrg path(const path& __p) = default;
31814f5a3b0Smrg
319b1e83836Smrg path(path&& __p) noexcept
320181254a7Smrg : _M_pathname(std::move(__p._M_pathname)),
321181254a7Smrg _M_cmpts(std::move(__p._M_cmpts))
322181254a7Smrg { __p.clear(); }
32314f5a3b0Smrg
32414f5a3b0Smrg path(string_type&& __source, format = auto_format)
32514f5a3b0Smrg : _M_pathname(std::move(__source))
32614f5a3b0Smrg { _M_split_cmpts(); }
32714f5a3b0Smrg
32814f5a3b0Smrg template<typename _Source,
329fb8a8121Smrg typename _Require = __detail::_Path<_Source>>
33014f5a3b0Smrg path(_Source const& __source, format = auto_format)
331b1e83836Smrg : _M_pathname(_S_convert(__detail::__effective_range(__source)))
33214f5a3b0Smrg { _M_split_cmpts(); }
33314f5a3b0Smrg
33414f5a3b0Smrg template<typename _InputIterator,
335b1e83836Smrg typename _Require = __detail::_Path2<_InputIterator>>
33614f5a3b0Smrg path(_InputIterator __first, _InputIterator __last, format = auto_format)
337b1e83836Smrg : _M_pathname(_S_convert(__detail::__string_from_range(__first, __last)))
33814f5a3b0Smrg { _M_split_cmpts(); }
33914f5a3b0Smrg
34014f5a3b0Smrg template<typename _Source,
341fb8a8121Smrg typename _Require = __detail::_Path<_Source>,
342fb8a8121Smrg typename _Require2 = __detail::__value_type_is_char<_Source>>
343b1e83836Smrg path(_Source const& __src, const locale& __loc, format = auto_format)
344b1e83836Smrg : _M_pathname(_S_convert_loc(__detail::__effective_range(__src), __loc))
34514f5a3b0Smrg { _M_split_cmpts(); }
34614f5a3b0Smrg
34714f5a3b0Smrg template<typename _InputIterator,
348b1e83836Smrg typename _Require = __detail::_Path2<_InputIterator>,
349b1e83836Smrg typename _Req2 = __detail::__value_type_is_char<_InputIterator>>
35014f5a3b0Smrg path(_InputIterator __first, _InputIterator __last, const locale& __loc,
35114f5a3b0Smrg format = auto_format)
35214f5a3b0Smrg : _M_pathname(_S_convert_loc(__first, __last, __loc))
35314f5a3b0Smrg { _M_split_cmpts(); }
35414f5a3b0Smrg
35514f5a3b0Smrg ~path() = default;
35614f5a3b0Smrg
35714f5a3b0Smrg // assignments
35814f5a3b0Smrg
359181254a7Smrg path& operator=(const path&);
360181254a7Smrg path& operator=(path&&) noexcept;
36114f5a3b0Smrg path& operator=(string_type&& __source);
36214f5a3b0Smrg path& assign(string_type&& __source);
36314f5a3b0Smrg
36414f5a3b0Smrg template<typename _Source>
365fb8a8121Smrg __detail::_Path<_Source>&
36614f5a3b0Smrg operator=(_Source const& __source)
36714f5a3b0Smrg { return *this = path(__source); }
36814f5a3b0Smrg
36914f5a3b0Smrg template<typename _Source>
370fb8a8121Smrg __detail::_Path<_Source>&
37114f5a3b0Smrg assign(_Source const& __source)
37214f5a3b0Smrg { return *this = path(__source); }
37314f5a3b0Smrg
37414f5a3b0Smrg template<typename _InputIterator>
375b1e83836Smrg __detail::_Path2<_InputIterator>&
37614f5a3b0Smrg assign(_InputIterator __first, _InputIterator __last)
37714f5a3b0Smrg { return *this = path(__first, __last); }
37814f5a3b0Smrg
37914f5a3b0Smrg // appends
38014f5a3b0Smrg
381181254a7Smrg path& operator/=(const path& __p);
38214f5a3b0Smrg
383fb8a8121Smrg template<typename _Source>
384fb8a8121Smrg __detail::_Path<_Source>&
38514f5a3b0Smrg operator/=(_Source const& __source)
386181254a7Smrg {
387b1e83836Smrg _M_append(_S_convert(__detail::__effective_range(__source)));
388181254a7Smrg return *this;
389181254a7Smrg }
39014f5a3b0Smrg
39114f5a3b0Smrg template<typename _Source>
392fb8a8121Smrg __detail::_Path<_Source>&
39314f5a3b0Smrg append(_Source const& __source)
394181254a7Smrg {
395b1e83836Smrg _M_append(_S_convert(__detail::__effective_range(__source)));
396181254a7Smrg return *this;
397181254a7Smrg }
39814f5a3b0Smrg
39914f5a3b0Smrg template<typename _InputIterator>
400b1e83836Smrg __detail::_Path2<_InputIterator>&
40114f5a3b0Smrg append(_InputIterator __first, _InputIterator __last)
402181254a7Smrg {
403b1e83836Smrg _M_append(_S_convert(__detail::__string_from_range(__first, __last)));
404181254a7Smrg return *this;
405181254a7Smrg }
40614f5a3b0Smrg
40714f5a3b0Smrg // concatenation
40814f5a3b0Smrg
40914f5a3b0Smrg path& operator+=(const path& __x);
41014f5a3b0Smrg path& operator+=(const string_type& __x);
41114f5a3b0Smrg path& operator+=(const value_type* __x);
41214f5a3b0Smrg path& operator+=(value_type __x);
41314f5a3b0Smrg path& operator+=(basic_string_view<value_type> __x);
41414f5a3b0Smrg
41514f5a3b0Smrg template<typename _Source>
416fb8a8121Smrg __detail::_Path<_Source>&
41714f5a3b0Smrg operator+=(_Source const& __x) { return concat(__x); }
41814f5a3b0Smrg
41914f5a3b0Smrg template<typename _CharT>
420b1e83836Smrg __detail::_Path2<_CharT*>&
42114f5a3b0Smrg operator+=(_CharT __x);
42214f5a3b0Smrg
42314f5a3b0Smrg template<typename _Source>
424fb8a8121Smrg __detail::_Path<_Source>&
42514f5a3b0Smrg concat(_Source const& __x)
426181254a7Smrg {
427b1e83836Smrg _M_concat(_S_convert(__detail::__effective_range(__x)));
428181254a7Smrg return *this;
429181254a7Smrg }
43014f5a3b0Smrg
43114f5a3b0Smrg template<typename _InputIterator>
432b1e83836Smrg __detail::_Path2<_InputIterator>&
43314f5a3b0Smrg concat(_InputIterator __first, _InputIterator __last)
434181254a7Smrg {
435b1e83836Smrg _M_concat(_S_convert(__detail::__string_from_range(__first, __last)));
436181254a7Smrg return *this;
437181254a7Smrg }
43814f5a3b0Smrg
43914f5a3b0Smrg // modifiers
44014f5a3b0Smrg
44114f5a3b0Smrg void clear() noexcept { _M_pathname.clear(); _M_split_cmpts(); }
44214f5a3b0Smrg
44314f5a3b0Smrg path& make_preferred();
44414f5a3b0Smrg path& remove_filename();
44514f5a3b0Smrg path& replace_filename(const path& __replacement);
44614f5a3b0Smrg path& replace_extension(const path& __replacement = path());
44714f5a3b0Smrg
44814f5a3b0Smrg void swap(path& __rhs) noexcept;
44914f5a3b0Smrg
45014f5a3b0Smrg // native format observers
45114f5a3b0Smrg
45214f5a3b0Smrg const string_type& native() const noexcept { return _M_pathname; }
45314f5a3b0Smrg const value_type* c_str() const noexcept { return _M_pathname.c_str(); }
45414f5a3b0Smrg operator string_type() const { return _M_pathname; }
45514f5a3b0Smrg
45614f5a3b0Smrg template<typename _CharT, typename _Traits = std::char_traits<_CharT>,
45714f5a3b0Smrg typename _Allocator = std::allocator<_CharT>>
45814f5a3b0Smrg std::basic_string<_CharT, _Traits, _Allocator>
45914f5a3b0Smrg string(const _Allocator& __a = _Allocator()) const;
46014f5a3b0Smrg
46114f5a3b0Smrg std::string string() const;
46214f5a3b0Smrg #if _GLIBCXX_USE_WCHAR_T
46314f5a3b0Smrg std::wstring wstring() const;
46414f5a3b0Smrg #endif
465181254a7Smrg #ifdef _GLIBCXX_USE_CHAR8_T
466181254a7Smrg __attribute__((__abi_tag__("__u8")))
467181254a7Smrg std::u8string u8string() const;
468181254a7Smrg #else
46914f5a3b0Smrg std::string u8string() const;
470181254a7Smrg #endif // _GLIBCXX_USE_CHAR8_T
47114f5a3b0Smrg std::u16string u16string() const;
47214f5a3b0Smrg std::u32string u32string() const;
47314f5a3b0Smrg
47414f5a3b0Smrg // generic format observers
47514f5a3b0Smrg template<typename _CharT, typename _Traits = std::char_traits<_CharT>,
47614f5a3b0Smrg typename _Allocator = std::allocator<_CharT>>
47714f5a3b0Smrg std::basic_string<_CharT, _Traits, _Allocator>
47814f5a3b0Smrg generic_string(const _Allocator& __a = _Allocator()) const;
47914f5a3b0Smrg
48014f5a3b0Smrg std::string generic_string() const;
48114f5a3b0Smrg #if _GLIBCXX_USE_WCHAR_T
48214f5a3b0Smrg std::wstring generic_wstring() const;
48314f5a3b0Smrg #endif
484181254a7Smrg #ifdef _GLIBCXX_USE_CHAR8_T
485181254a7Smrg __attribute__((__abi_tag__("__u8")))
486181254a7Smrg std::u8string generic_u8string() const;
487181254a7Smrg #else
48814f5a3b0Smrg std::string generic_u8string() const;
489181254a7Smrg #endif // _GLIBCXX_USE_CHAR8_T
49014f5a3b0Smrg std::u16string generic_u16string() const;
49114f5a3b0Smrg std::u32string generic_u32string() const;
49214f5a3b0Smrg
49314f5a3b0Smrg // compare
49414f5a3b0Smrg
49514f5a3b0Smrg int compare(const path& __p) const noexcept;
496181254a7Smrg int compare(const string_type& __s) const noexcept;
497181254a7Smrg int compare(const value_type* __s) const noexcept;
498181254a7Smrg int compare(basic_string_view<value_type> __s) const noexcept;
49914f5a3b0Smrg
50014f5a3b0Smrg // decomposition
50114f5a3b0Smrg
50214f5a3b0Smrg path root_name() const;
50314f5a3b0Smrg path root_directory() const;
50414f5a3b0Smrg path root_path() const;
50514f5a3b0Smrg path relative_path() const;
50614f5a3b0Smrg path parent_path() const;
50714f5a3b0Smrg path filename() const;
50814f5a3b0Smrg path stem() const;
50914f5a3b0Smrg path extension() const;
51014f5a3b0Smrg
51114f5a3b0Smrg // query
51214f5a3b0Smrg
51314f5a3b0Smrg [[nodiscard]] bool empty() const noexcept { return _M_pathname.empty(); }
514181254a7Smrg bool has_root_name() const noexcept;
515181254a7Smrg bool has_root_directory() const noexcept;
516181254a7Smrg bool has_root_path() const noexcept;
517181254a7Smrg bool has_relative_path() const noexcept;
518181254a7Smrg bool has_parent_path() const noexcept;
519181254a7Smrg bool has_filename() const noexcept;
520181254a7Smrg bool has_stem() const noexcept;
521181254a7Smrg bool has_extension() const noexcept;
522181254a7Smrg bool is_absolute() const noexcept;
523181254a7Smrg bool is_relative() const noexcept { return !is_absolute(); }
52414f5a3b0Smrg
52514f5a3b0Smrg // generation
52614f5a3b0Smrg path lexically_normal() const;
52714f5a3b0Smrg path lexically_relative(const path& base) const;
52814f5a3b0Smrg path lexically_proximate(const path& base) const;
52914f5a3b0Smrg
53014f5a3b0Smrg // iterators
53114f5a3b0Smrg class iterator;
532fb8a8121Smrg using const_iterator = iterator;
53314f5a3b0Smrg
534b1e83836Smrg iterator begin() const noexcept;
535b1e83836Smrg iterator end() const noexcept;
53614f5a3b0Smrg
537181254a7Smrg /// Write a path to a stream
538181254a7Smrg template<typename _CharT, typename _Traits>
539181254a7Smrg friend std::basic_ostream<_CharT, _Traits>&
540181254a7Smrg operator<<(std::basic_ostream<_CharT, _Traits>& __os, const path& __p)
541181254a7Smrg {
542181254a7Smrg __os << std::quoted(__p.string<_CharT, _Traits>());
543181254a7Smrg return __os;
544181254a7Smrg }
545181254a7Smrg
546181254a7Smrg /// Read a path from a stream
547181254a7Smrg template<typename _CharT, typename _Traits>
548181254a7Smrg friend std::basic_istream<_CharT, _Traits>&
549181254a7Smrg operator>>(std::basic_istream<_CharT, _Traits>& __is, path& __p)
550181254a7Smrg {
551181254a7Smrg std::basic_string<_CharT, _Traits> __tmp;
552181254a7Smrg if (__is >> std::quoted(__tmp))
553181254a7Smrg __p = std::move(__tmp);
554181254a7Smrg return __is;
555181254a7Smrg }
556181254a7Smrg
557181254a7Smrg // non-member operators
558181254a7Smrg
559181254a7Smrg /// Compare paths
560fb8a8121Smrg friend bool operator==(const path& __lhs, const path& __rhs) noexcept
561b1e83836Smrg { return path::_S_compare(__lhs, __rhs) == 0; }
562fb8a8121Smrg
563fb8a8121Smrg #if __cpp_lib_three_way_comparison
564fb8a8121Smrg /// Compare paths
565fb8a8121Smrg friend strong_ordering
566fb8a8121Smrg operator<=>(const path& __lhs, const path& __rhs) noexcept
567b1e83836Smrg { return path::_S_compare(__lhs, __rhs) <=> 0; }
568fb8a8121Smrg #else
569fb8a8121Smrg /// Compare paths
570fb8a8121Smrg friend bool operator!=(const path& __lhs, const path& __rhs) noexcept
571fb8a8121Smrg { return !(__lhs == __rhs); }
572fb8a8121Smrg
573fb8a8121Smrg /// Compare paths
574181254a7Smrg friend bool operator<(const path& __lhs, const path& __rhs) noexcept
575181254a7Smrg { return __lhs.compare(__rhs) < 0; }
576181254a7Smrg
577181254a7Smrg /// Compare paths
578181254a7Smrg friend bool operator<=(const path& __lhs, const path& __rhs) noexcept
579181254a7Smrg { return !(__rhs < __lhs); }
580181254a7Smrg
581181254a7Smrg /// Compare paths
582181254a7Smrg friend bool operator>(const path& __lhs, const path& __rhs) noexcept
583181254a7Smrg { return __rhs < __lhs; }
584181254a7Smrg
585181254a7Smrg /// Compare paths
586181254a7Smrg friend bool operator>=(const path& __lhs, const path& __rhs) noexcept
587181254a7Smrg { return !(__lhs < __rhs); }
588fb8a8121Smrg #endif
589181254a7Smrg
590181254a7Smrg /// Append one path to another
591181254a7Smrg friend path operator/(const path& __lhs, const path& __rhs)
592181254a7Smrg {
593181254a7Smrg path __result(__lhs);
594181254a7Smrg __result /= __rhs;
595181254a7Smrg return __result;
596181254a7Smrg }
597181254a7Smrg
59814f5a3b0Smrg private:
59914f5a3b0Smrg enum class _Type : unsigned char {
600181254a7Smrg _Multi = 0, _Root_name, _Root_dir, _Filename
60114f5a3b0Smrg };
60214f5a3b0Smrg
6037d4dc15bSmrg path(basic_string_view<value_type> __str, _Type __type);
60414f5a3b0Smrg
60514f5a3b0Smrg enum class _Split { _Stem, _Extension };
60614f5a3b0Smrg
607181254a7Smrg void _M_append(basic_string_view<value_type>);
608181254a7Smrg void _M_concat(basic_string_view<value_type>);
60914f5a3b0Smrg
610181254a7Smrg pair<const string_type*, size_t> _M_find_extension() const noexcept;
61114f5a3b0Smrg
612b1e83836Smrg // path::_S_convert creates a basic_string<value_type> or
613b1e83836Smrg // basic_string_view<value_type> from a basic_string<C> or
614b1e83836Smrg // basic_string_view<C>, for an encoded character type C,
615b1e83836Smrg // performing the conversions required by [fs.path.type.cvt].
616b1e83836Smrg template<typename _Tp>
617b1e83836Smrg static auto
618b1e83836Smrg _S_convert(_Tp __str)
619b1e83836Smrg noexcept(is_same_v<typename _Tp::value_type, value_type>)
62014f5a3b0Smrg {
621b1e83836Smrg if constexpr (is_same_v<typename _Tp::value_type, value_type>)
622b1e83836Smrg return __str; // No conversion needed.
623b1e83836Smrg #if !defined _GLIBCXX_FILESYSTEM_IS_WINDOWS && defined _GLIBCXX_USE_CHAR8_T
624b1e83836Smrg else if constexpr (is_same_v<_Tp, std::u8string>)
625b1e83836Smrg // Calling _S_convert<char8_t> will return a u8string_view that
626b1e83836Smrg // refers to __str and would dangle after this function returns.
627b1e83836Smrg // Return a string_type instead, to avoid dangling.
628b1e83836Smrg return string_type(_S_convert(__str.data(),
629b1e83836Smrg __str.data() + __str.size()));
630b1e83836Smrg #endif
631b1e83836Smrg else
632b1e83836Smrg return _S_convert(__str.data(), __str.data() + __str.size());
63314f5a3b0Smrg }
63414f5a3b0Smrg
635b1e83836Smrg template<typename _EcharT>
636b1e83836Smrg static auto
637b1e83836Smrg _S_convert(const _EcharT* __first, const _EcharT* __last);
638b1e83836Smrg
639b1e83836Smrg // _S_convert_loc converts a range of char to string_type, using the
640b1e83836Smrg // supplied locale for encoding conversions.
64114f5a3b0Smrg
64214f5a3b0Smrg static string_type
64314f5a3b0Smrg _S_convert_loc(const char* __first, const char* __last,
64414f5a3b0Smrg const std::locale& __loc);
64514f5a3b0Smrg
64614f5a3b0Smrg template<typename _Iter>
64714f5a3b0Smrg static string_type
64814f5a3b0Smrg _S_convert_loc(_Iter __first, _Iter __last, const std::locale& __loc)
64914f5a3b0Smrg {
650b1e83836Smrg const auto __s = __detail::__string_from_range(__first, __last);
651b1e83836Smrg return _S_convert_loc(__s.data(), __s.data() + __s.size(), __loc);
65214f5a3b0Smrg }
65314f5a3b0Smrg
654b1e83836Smrg template<typename _Tp>
65514f5a3b0Smrg static string_type
656b1e83836Smrg _S_convert_loc(const _Tp& __s, const std::locale& __loc)
65714f5a3b0Smrg {
658181254a7Smrg return _S_convert_loc(__s.data(), __s.data() + __s.size(), __loc);
65914f5a3b0Smrg }
66014f5a3b0Smrg
66114f5a3b0Smrg template<typename _CharT, typename _Traits, typename _Allocator>
66214f5a3b0Smrg static basic_string<_CharT, _Traits, _Allocator>
663fb8a8121Smrg _S_str_convert(basic_string_view<value_type>, const _Allocator&);
66414f5a3b0Smrg
665b1e83836Smrg // Returns lhs.compare(rhs), but defined after path::iterator is complete.
666b1e83836Smrg __attribute__((__always_inline__))
667b1e83836Smrg static int
668b1e83836Smrg _S_compare(const path& __lhs, const path& __rhs) noexcept;
669b1e83836Smrg
67014f5a3b0Smrg void _M_split_cmpts();
671181254a7Smrg
672181254a7Smrg _Type _M_type() const noexcept { return _M_cmpts.type(); }
67314f5a3b0Smrg
67414f5a3b0Smrg string_type _M_pathname;
67514f5a3b0Smrg
67614f5a3b0Smrg struct _Cmpt;
677181254a7Smrg
678181254a7Smrg struct _List
679181254a7Smrg {
680181254a7Smrg using value_type = _Cmpt;
681181254a7Smrg using iterator = value_type*;
682181254a7Smrg using const_iterator = const value_type*;
683181254a7Smrg
684181254a7Smrg _List();
685181254a7Smrg _List(const _List&);
686181254a7Smrg _List(_List&&) = default;
687181254a7Smrg _List& operator=(const _List&);
688181254a7Smrg _List& operator=(_List&&) = default;
689181254a7Smrg ~_List() = default;
690181254a7Smrg
691181254a7Smrg _Type type() const noexcept
692fb8a8121Smrg { return _Type(reinterpret_cast<uintptr_t>(_M_impl.get()) & 0x3); }
693181254a7Smrg
694181254a7Smrg void type(_Type) noexcept;
695181254a7Smrg
696181254a7Smrg int size() const noexcept; // zero unless type() == _Type::_Multi
697181254a7Smrg bool empty() const noexcept; // true unless type() == _Type::_Multi
698181254a7Smrg void clear();
699181254a7Smrg void swap(_List& __l) noexcept { _M_impl.swap(__l._M_impl); }
700181254a7Smrg int capacity() const noexcept;
701181254a7Smrg void reserve(int, bool); ///< @pre type() == _Type::_Multi
702181254a7Smrg
703181254a7Smrg // All the member functions below here have a precondition !empty()
704181254a7Smrg // (and they should only be called from within the library).
705181254a7Smrg
706fb8a8121Smrg iterator begin() noexcept;
707fb8a8121Smrg iterator end() noexcept;
708fb8a8121Smrg const_iterator begin() const noexcept;
709fb8a8121Smrg const_iterator end() const noexcept;
710181254a7Smrg
711181254a7Smrg value_type& front() noexcept;
712181254a7Smrg value_type& back() noexcept;
713181254a7Smrg const value_type& front() const noexcept;
714181254a7Smrg const value_type& back() const noexcept;
715181254a7Smrg
716181254a7Smrg void pop_back();
717181254a7Smrg void _M_erase_from(const_iterator __pos); // erases [__pos,end())
718181254a7Smrg
719181254a7Smrg struct _Impl;
720181254a7Smrg struct _Impl_deleter
721181254a7Smrg {
722181254a7Smrg void operator()(_Impl*) const noexcept;
72314f5a3b0Smrg };
724181254a7Smrg unique_ptr<_Impl, _Impl_deleter> _M_impl;
725181254a7Smrg };
726181254a7Smrg _List _M_cmpts;
72714f5a3b0Smrg
728181254a7Smrg struct _Parser;
729b1e83836Smrg
730b1e83836Smrg template<typename _EcharT> struct _Codecvt;
731181254a7Smrg };
73214f5a3b0Smrg
733b1e83836Smrg /// @{
734b1e83836Smrg /// @relates std::filesystem::path
735fb8a8121Smrg
736b1e83836Smrg #if __cpp_concepts >= 201907L
737b1e83836Smrg // Workaround for PR libstdc++/106201
738b1e83836Smrg inline void
739b1e83836Smrg swap(same_as<path> auto& __lhs, same_as<path> auto& __rhs) noexcept
740b1e83836Smrg { __lhs.swap(__rhs); }
741b1e83836Smrg #else
74214f5a3b0Smrg inline void swap(path& __lhs, path& __rhs) noexcept { __lhs.swap(__rhs); }
743b1e83836Smrg #endif
74414f5a3b0Smrg
74514f5a3b0Smrg size_t hash_value(const path& __p) noexcept;
74614f5a3b0Smrg
747fb8a8121Smrg /// @}
748fb8a8121Smrg
749181254a7Smrg /// Exception type thrown by the Filesystem library
750*0a307195Smrg /**
751*0a307195Smrg * @headerfile filesystem
752*0a307195Smrg * @since C++17
753*0a307195Smrg */
754181254a7Smrg class filesystem_error : public std::system_error
75514f5a3b0Smrg {
756181254a7Smrg public:
757181254a7Smrg filesystem_error(const string& __what_arg, error_code __ec);
75814f5a3b0Smrg
759181254a7Smrg filesystem_error(const string& __what_arg, const path& __p1,
760181254a7Smrg error_code __ec);
76114f5a3b0Smrg
762181254a7Smrg filesystem_error(const string& __what_arg, const path& __p1,
763181254a7Smrg const path& __p2, error_code __ec);
76414f5a3b0Smrg
765181254a7Smrg filesystem_error(const filesystem_error&) = default;
766181254a7Smrg filesystem_error& operator=(const filesystem_error&) = default;
76714f5a3b0Smrg
768181254a7Smrg // No move constructor or assignment operator.
769181254a7Smrg // Copy rvalues instead, so that _M_impl is not left empty.
770181254a7Smrg
771181254a7Smrg ~filesystem_error();
772181254a7Smrg
773181254a7Smrg const path& path1() const noexcept;
774181254a7Smrg const path& path2() const noexcept;
775181254a7Smrg const char* what() const noexcept;
776181254a7Smrg
777181254a7Smrg private:
778181254a7Smrg struct _Impl;
779181254a7Smrg std::__shared_ptr<const _Impl> _M_impl;
780181254a7Smrg };
781181254a7Smrg
782b1e83836Smrg /// @cond undocumented
783b1e83836Smrg namespace __detail
784b1e83836Smrg {
785b1e83836Smrg [[noreturn]] inline void
786b1e83836Smrg __throw_conversion_error()
787b1e83836Smrg {
788b1e83836Smrg _GLIBCXX_THROW_OR_ABORT(filesystem_error(
789b1e83836Smrg "Cannot convert character sequence",
790b1e83836Smrg std::make_error_code(errc::illegal_byte_sequence)));
791b1e83836Smrg }
792b1e83836Smrg
793b1e83836Smrg #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
794b1e83836Smrg template<typename _Tp>
795b1e83836Smrg inline std::wstring
796b1e83836Smrg __wstr_from_utf8(const _Tp& __str)
797b1e83836Smrg {
798b1e83836Smrg static_assert(std::is_same_v<typename _Tp::value_type, char>);
799b1e83836Smrg std::wstring __wstr;
800b1e83836Smrg // XXX This assumes native wide encoding is UTF-16.
801b1e83836Smrg std::codecvt_utf8_utf16<wchar_t> __wcvt;
802b1e83836Smrg const auto __p = __str.data();
803b1e83836Smrg if (!__str_codecvt_in_all(__p, __p + __str.size(), __wstr, __wcvt))
804b1e83836Smrg __detail::__throw_conversion_error();
805b1e83836Smrg return __wstr;
806b1e83836Smrg }
807b1e83836Smrg #endif
808b1e83836Smrg
809b1e83836Smrg } // namespace __detail
810b1e83836Smrg /// @endcond
811b1e83836Smrg
812b1e83836Smrg
813fb8a8121Smrg /** Create a path from a UTF-8-encoded sequence of char
814fb8a8121Smrg *
815fb8a8121Smrg * @relates std::filesystem::path
816*0a307195Smrg * @headerfile filesystem
817*0a307195Smrg * @since C++17
818fb8a8121Smrg */
819fb8a8121Smrg template<typename _InputIterator,
820b1e83836Smrg typename _Require = __detail::_Path2<_InputIterator>,
821fb8a8121Smrg typename _CharT
822fb8a8121Smrg = __detail::__value_type_is_char_or_char8_t<_InputIterator>>
823fb8a8121Smrg inline path
82414f5a3b0Smrg u8path(_InputIterator __first, _InputIterator __last)
82514f5a3b0Smrg {
82614f5a3b0Smrg #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
827fb8a8121Smrg if constexpr (is_same_v<_CharT, char>)
828b1e83836Smrg return path{ __detail::__wstr_from_utf8(
829b1e83836Smrg __detail::__string_from_range(__first, __last)) };
83014f5a3b0Smrg else
831b1e83836Smrg return path{ __first, __last }; // constructor handles char8_t
83214f5a3b0Smrg #else
833181254a7Smrg // This assumes native normal encoding is UTF-8.
83414f5a3b0Smrg return path{ __first, __last };
83514f5a3b0Smrg #endif
83614f5a3b0Smrg }
83714f5a3b0Smrg
838fb8a8121Smrg /** Create a path from a UTF-8-encoded sequence of char
839fb8a8121Smrg *
840fb8a8121Smrg * @relates std::filesystem::path
841*0a307195Smrg * @headerfile filesystem
842*0a307195Smrg * @since C++17
843fb8a8121Smrg */
844fb8a8121Smrg template<typename _Source,
845fb8a8121Smrg typename _Require = __detail::_Path<_Source>,
846fb8a8121Smrg typename _CharT = __detail::__value_type_is_char_or_char8_t<_Source>>
847fb8a8121Smrg inline path
848181254a7Smrg u8path(const _Source& __source)
84914f5a3b0Smrg {
850181254a7Smrg #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
851fb8a8121Smrg if constexpr (is_same_v<_CharT, char>)
852b1e83836Smrg return path{ __detail::__wstr_from_utf8(
853b1e83836Smrg __detail::__effective_range(__source)) };
854181254a7Smrg else
855b1e83836Smrg return path{ __source }; // constructor handles char8_t
856181254a7Smrg #else
857b1e83836Smrg // This assumes native normal encoding is UTF-8.
858181254a7Smrg return path{ __source };
859181254a7Smrg #endif
860181254a7Smrg }
86114f5a3b0Smrg
862fb8a8121Smrg /// @cond undocumented
863fb8a8121Smrg
86414f5a3b0Smrg struct path::_Cmpt : path
86514f5a3b0Smrg {
8667d4dc15bSmrg _Cmpt(basic_string_view<value_type> __s, _Type __t, size_t __pos);
86714f5a3b0Smrg
86814f5a3b0Smrg _Cmpt() : _M_pos(-1) { }
86914f5a3b0Smrg
87014f5a3b0Smrg size_t _M_pos;
87114f5a3b0Smrg };
87214f5a3b0Smrg
873b1e83836Smrg // path::_Codecvt<C> Performs conversions between C and path::string_type.
874b1e83836Smrg // The native encoding of char strings is the OS-dependent current
875b1e83836Smrg // encoding for pathnames. FIXME: We assume this is UTF-8 everywhere,
876b1e83836Smrg // but should use a Windows API to query it.
87714f5a3b0Smrg
878b1e83836Smrg // Converts between native pathname encoding and char16_t or char32_t.
879b1e83836Smrg template<typename _EcharT>
880b1e83836Smrg struct path::_Codecvt
881b1e83836Smrg // Need derived class here because std::codecvt has protected destructor.
882b1e83836Smrg : std::codecvt<_EcharT, char, mbstate_t>
883b1e83836Smrg { };
884b1e83836Smrg
885b1e83836Smrg // Converts between native pathname encoding and native wide encoding.
886b1e83836Smrg // The native encoding for wide strings is the execution wide-character
887b1e83836Smrg // set encoding. FIXME: We assume that this is either UTF-32 or UTF-16
888b1e83836Smrg // (depending on the width of wchar_t). That matches GCC's default,
889b1e83836Smrg // but can be changed with -fwide-exec-charset.
890b1e83836Smrg // We need a custom codecvt converting the native pathname encoding
891b1e83836Smrg // to/from the native wide encoding.
892181254a7Smrg template<>
893b1e83836Smrg struct path::_Codecvt<wchar_t>
894b1e83836Smrg : __conditional_t<sizeof(wchar_t) == sizeof(char32_t),
895b1e83836Smrg std::codecvt_utf8<wchar_t>, // UTF-8 <-> UTF-32
896b1e83836Smrg std::codecvt_utf8_utf16<wchar_t>> // UTF-8 <-> UTF-16
897b1e83836Smrg { };
898b1e83836Smrg
899b1e83836Smrg template<typename _EcharT>
900b1e83836Smrg auto
901b1e83836Smrg path::_S_convert(const _EcharT* __f, const _EcharT* __l)
902181254a7Smrg {
903b1e83836Smrg static_assert(__detail::__is_encoded_char<_EcharT>);
904b1e83836Smrg
905b1e83836Smrg #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
906b1e83836Smrg # define _GLIBCXX_CONV_FROM_UTF8(S) __detail::__wstr_from_utf8(S)
907b1e83836Smrg #else
908b1e83836Smrg # define _GLIBCXX_CONV_FROM_UTF8(S) S
909181254a7Smrg #endif
910181254a7Smrg
911b1e83836Smrg if constexpr (is_same_v<_EcharT, value_type>)
912b1e83836Smrg return basic_string_view<value_type>(__f, __l - __f);
913b1e83836Smrg #ifdef _GLIBCXX_USE_CHAR8_T
914b1e83836Smrg else if constexpr (is_same_v<_EcharT, char8_t>)
91514f5a3b0Smrg {
916b1e83836Smrg string_view __str(reinterpret_cast<const char*>(__f), __l - __f);
917b1e83836Smrg return _GLIBCXX_CONV_FROM_UTF8(__str);
918b1e83836Smrg }
919b1e83836Smrg #endif
92014f5a3b0Smrg #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
921b1e83836Smrg else if constexpr (is_same_v<_EcharT, char>)
92214f5a3b0Smrg {
923b1e83836Smrg std::wstring __wstr;
924b1e83836Smrg path::_Codecvt<wchar_t> __cvt;
925181254a7Smrg if (__str_codecvt_in_all(__f, __l, __wstr, __cvt))
926181254a7Smrg return __wstr;
927181254a7Smrg }
928181254a7Smrg #endif
929b1e83836Smrg else
930181254a7Smrg {
931b1e83836Smrg path::_Codecvt<_EcharT> __cvt;
93214f5a3b0Smrg std::string __str;
933181254a7Smrg if (__str_codecvt_out_all(__f, __l, __str, __cvt))
934b1e83836Smrg return _GLIBCXX_CONV_FROM_UTF8(__str);
93514f5a3b0Smrg }
936b1e83836Smrg __detail::__throw_conversion_error();
93714f5a3b0Smrg }
938b1e83836Smrg #undef _GLIBCXX_CONV_FROM_UTF8
93914f5a3b0Smrg
940fb8a8121Smrg /// @endcond
941fb8a8121Smrg
94214f5a3b0Smrg /// An iterator for the components of a path
943*0a307195Smrg /**
944*0a307195Smrg * @headerfile filesystem
945*0a307195Smrg * @since C++17
946*0a307195Smrg */
94714f5a3b0Smrg class path::iterator
94814f5a3b0Smrg {
94914f5a3b0Smrg public:
95014f5a3b0Smrg using difference_type = std::ptrdiff_t;
95114f5a3b0Smrg using value_type = path;
95214f5a3b0Smrg using reference = const path&;
95314f5a3b0Smrg using pointer = const path*;
95414f5a3b0Smrg using iterator_category = std::bidirectional_iterator_tag;
95514f5a3b0Smrg
956b1e83836Smrg iterator() noexcept : _M_path(nullptr), _M_cur(), _M_at_end() { }
95714f5a3b0Smrg
95814f5a3b0Smrg iterator(const iterator&) = default;
95914f5a3b0Smrg iterator& operator=(const iterator&) = default;
96014f5a3b0Smrg
961b1e83836Smrg reference operator*() const noexcept;
962b1e83836Smrg pointer operator->() const noexcept { return std::__addressof(**this); }
96314f5a3b0Smrg
964b1e83836Smrg iterator& operator++() noexcept;
96514f5a3b0Smrg
966b1e83836Smrg iterator operator++(int) noexcept
967b1e83836Smrg { auto __tmp = *this; ++*this; return __tmp; }
96814f5a3b0Smrg
969b1e83836Smrg iterator& operator--() noexcept;
970b1e83836Smrg
971b1e83836Smrg iterator operator--(int) noexcept
972b1e83836Smrg { auto __tmp = *this; --*this; return __tmp; }
973b1e83836Smrg
974b1e83836Smrg friend bool
975b1e83836Smrg operator==(const iterator& __lhs, const iterator& __rhs) noexcept
97614f5a3b0Smrg { return __lhs._M_equals(__rhs); }
97714f5a3b0Smrg
978b1e83836Smrg friend bool
979b1e83836Smrg operator!=(const iterator& __lhs, const iterator& __rhs) noexcept
98014f5a3b0Smrg { return !__lhs._M_equals(__rhs); }
98114f5a3b0Smrg
98214f5a3b0Smrg private:
98314f5a3b0Smrg friend class path;
98414f5a3b0Smrg
985b1e83836Smrg bool
986b1e83836Smrg _M_is_multi() const noexcept
987b1e83836Smrg { return _M_path->_M_type() == _Type::_Multi; }
988181254a7Smrg
989181254a7Smrg friend difference_type
990181254a7Smrg __path_iter_distance(const iterator& __first, const iterator& __last)
991b1e83836Smrg noexcept
992181254a7Smrg {
993181254a7Smrg __glibcxx_assert(__first._M_path != nullptr);
994181254a7Smrg __glibcxx_assert(__first._M_path == __last._M_path);
995181254a7Smrg if (__first._M_is_multi())
996181254a7Smrg return std::distance(__first._M_cur, __last._M_cur);
997181254a7Smrg else if (__first._M_at_end == __last._M_at_end)
998181254a7Smrg return 0;
999181254a7Smrg else
1000181254a7Smrg return __first._M_at_end ? -1 : 1;
1001181254a7Smrg }
1002181254a7Smrg
1003181254a7Smrg friend void
1004b1e83836Smrg __path_iter_advance(iterator& __i, difference_type __n) noexcept
1005181254a7Smrg {
1006181254a7Smrg if (__n == 1)
1007181254a7Smrg ++__i;
1008181254a7Smrg else if (__n == -1)
1009181254a7Smrg --__i;
1010181254a7Smrg else if (__n != 0)
1011181254a7Smrg {
1012181254a7Smrg __glibcxx_assert(__i._M_path != nullptr);
1013181254a7Smrg __glibcxx_assert(__i._M_is_multi());
1014181254a7Smrg // __glibcxx_assert(__i._M_path->_M_cmpts.end() - __i._M_cur >= __n);
1015181254a7Smrg __i._M_cur += __n;
1016181254a7Smrg }
1017181254a7Smrg }
1018181254a7Smrg
1019b1e83836Smrg iterator(const path* __path, path::_List::const_iterator __iter) noexcept
102014f5a3b0Smrg : _M_path(__path), _M_cur(__iter), _M_at_end()
102114f5a3b0Smrg { }
102214f5a3b0Smrg
1023b1e83836Smrg iterator(const path* __path, bool __at_end) noexcept
102414f5a3b0Smrg : _M_path(__path), _M_cur(), _M_at_end(__at_end)
102514f5a3b0Smrg { }
102614f5a3b0Smrg
1027b1e83836Smrg bool _M_equals(iterator) const noexcept;
102814f5a3b0Smrg
102914f5a3b0Smrg const path* _M_path;
103014f5a3b0Smrg path::_List::const_iterator _M_cur;
103114f5a3b0Smrg bool _M_at_end; // only used when type != _Multi
103214f5a3b0Smrg };
103314f5a3b0Smrg
103414f5a3b0Smrg
103514f5a3b0Smrg inline path&
103614f5a3b0Smrg path::operator=(path&& __p) noexcept
103714f5a3b0Smrg {
1038181254a7Smrg if (&__p == this) [[__unlikely__]]
1039003ba354Smrg return *this;
1040003ba354Smrg
104114f5a3b0Smrg _M_pathname = std::move(__p._M_pathname);
104214f5a3b0Smrg _M_cmpts = std::move(__p._M_cmpts);
104314f5a3b0Smrg __p.clear();
104414f5a3b0Smrg return *this;
104514f5a3b0Smrg }
104614f5a3b0Smrg
104714f5a3b0Smrg inline path&
104814f5a3b0Smrg path::operator=(string_type&& __source)
104914f5a3b0Smrg { return *this = path(std::move(__source)); }
105014f5a3b0Smrg
105114f5a3b0Smrg inline path&
105214f5a3b0Smrg path::assign(string_type&& __source)
105314f5a3b0Smrg { return *this = path(std::move(__source)); }
105414f5a3b0Smrg
105514f5a3b0Smrg inline path&
105614f5a3b0Smrg path::operator+=(const string_type& __x)
105714f5a3b0Smrg {
1058181254a7Smrg _M_concat(__x);
105914f5a3b0Smrg return *this;
106014f5a3b0Smrg }
106114f5a3b0Smrg
106214f5a3b0Smrg inline path&
106314f5a3b0Smrg path::operator+=(const value_type* __x)
106414f5a3b0Smrg {
1065181254a7Smrg _M_concat(__x);
106614f5a3b0Smrg return *this;
106714f5a3b0Smrg }
106814f5a3b0Smrg
106914f5a3b0Smrg inline path&
107014f5a3b0Smrg path::operator+=(value_type __x)
107114f5a3b0Smrg {
1072181254a7Smrg _M_concat(basic_string_view<value_type>(&__x, 1));
107314f5a3b0Smrg return *this;
107414f5a3b0Smrg }
107514f5a3b0Smrg
107614f5a3b0Smrg inline path&
107714f5a3b0Smrg path::operator+=(basic_string_view<value_type> __x)
107814f5a3b0Smrg {
1079181254a7Smrg _M_concat(__x);
108014f5a3b0Smrg return *this;
108114f5a3b0Smrg }
108214f5a3b0Smrg
108314f5a3b0Smrg template<typename _CharT>
1084b1e83836Smrg inline __detail::_Path2<_CharT*>&
1085b1e83836Smrg path::operator+=(const _CharT __x)
108614f5a3b0Smrg {
1087b1e83836Smrg _M_concat(_S_convert(&__x, &__x + 1));
1088b1e83836Smrg return *this;
108914f5a3b0Smrg }
109014f5a3b0Smrg
109114f5a3b0Smrg inline path&
109214f5a3b0Smrg path::make_preferred()
109314f5a3b0Smrg {
109414f5a3b0Smrg #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1095b1e83836Smrg auto __pos = _M_pathname.find(L'/');
1096b1e83836Smrg while (__pos != _M_pathname.npos)
1097b1e83836Smrg {
1098b1e83836Smrg _M_pathname[__pos] = preferred_separator;
1099b1e83836Smrg __pos = _M_pathname.find(L'/', __pos);
1100b1e83836Smrg }
110114f5a3b0Smrg #endif
110214f5a3b0Smrg return *this;
110314f5a3b0Smrg }
110414f5a3b0Smrg
110514f5a3b0Smrg inline void path::swap(path& __rhs) noexcept
110614f5a3b0Smrg {
110714f5a3b0Smrg _M_pathname.swap(__rhs._M_pathname);
110814f5a3b0Smrg _M_cmpts.swap(__rhs._M_cmpts);
110914f5a3b0Smrg }
111014f5a3b0Smrg
1111fb8a8121Smrg /// @cond undocumented
111214f5a3b0Smrg template<typename _CharT, typename _Traits, typename _Allocator>
111314f5a3b0Smrg std::basic_string<_CharT, _Traits, _Allocator>
1114fb8a8121Smrg path::_S_str_convert(basic_string_view<value_type> __str,
1115fb8a8121Smrg const _Allocator& __a)
111614f5a3b0Smrg {
1117181254a7Smrg static_assert(!is_same_v<_CharT, value_type>);
111814f5a3b0Smrg
111914f5a3b0Smrg using _WString = basic_string<_CharT, _Traits, _Allocator>;
112014f5a3b0Smrg
1121181254a7Smrg if (__str.size() == 0)
1122181254a7Smrg return _WString(__a);
1123181254a7Smrg
1124b1e83836Smrg #ifndef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1125b1e83836Smrg string_view __u8str = __str;
1126b1e83836Smrg #else
1127181254a7Smrg // First convert native string from UTF-16 to to UTF-8.
1128181254a7Smrg // XXX This assumes that the execution wide-character set is UTF-16.
1129181254a7Smrg std::codecvt_utf8_utf16<value_type> __cvt;
1130181254a7Smrg
1131181254a7Smrg using _CharAlloc = __alloc_rebind<_Allocator, char>;
1132181254a7Smrg using _String = basic_string<char, char_traits<char>, _CharAlloc>;
113314f5a3b0Smrg _String __u8str{_CharAlloc{__a}};
1134181254a7Smrg const value_type* __wfirst = __str.data();
1135181254a7Smrg const value_type* __wlast = __wfirst + __str.size();
1136b1e83836Smrg if (!__str_codecvt_out_all(__wfirst, __wlast, __u8str, __cvt))
1137b1e83836Smrg __detail::__throw_conversion_error();
113814f5a3b0Smrg if constexpr (is_same_v<_CharT, char>)
1139181254a7Smrg return __u8str; // XXX assumes native ordinary encoding is UTF-8.
1140b1e83836Smrg else
1141b1e83836Smrg #endif
1142b1e83836Smrg {
1143181254a7Smrg const char* __first = __u8str.data();
1144181254a7Smrg const char* __last = __first + __u8str.size();
1145181254a7Smrg
1146181254a7Smrg // Convert UTF-8 string to requested format.
1147181254a7Smrg #ifdef _GLIBCXX_USE_CHAR8_T
1148181254a7Smrg if constexpr (is_same_v<_CharT, char8_t>)
1149181254a7Smrg return _WString(__first, __last, __a);
1150181254a7Smrg else
1151181254a7Smrg #endif
1152181254a7Smrg {
1153181254a7Smrg // Convert UTF-8 to wide string.
1154181254a7Smrg _WString __wstr(__a);
1155b1e83836Smrg path::_Codecvt<_CharT> __cvt;
1156181254a7Smrg if (__str_codecvt_in_all(__first, __last, __wstr, __cvt))
115714f5a3b0Smrg return __wstr;
1158181254a7Smrg }
1159b1e83836Smrg }
1160b1e83836Smrg __detail::__throw_conversion_error();
116114f5a3b0Smrg }
1162fb8a8121Smrg /// @endcond
116314f5a3b0Smrg
116414f5a3b0Smrg template<typename _CharT, typename _Traits, typename _Allocator>
116514f5a3b0Smrg inline basic_string<_CharT, _Traits, _Allocator>
116614f5a3b0Smrg path::string(const _Allocator& __a) const
116714f5a3b0Smrg {
116814f5a3b0Smrg if constexpr (is_same_v<_CharT, value_type>)
1169181254a7Smrg return { _M_pathname.c_str(), _M_pathname.length(), __a };
117014f5a3b0Smrg else
117114f5a3b0Smrg return _S_str_convert<_CharT, _Traits>(_M_pathname, __a);
117214f5a3b0Smrg }
117314f5a3b0Smrg
117414f5a3b0Smrg inline std::string
117514f5a3b0Smrg path::string() const { return string<char>(); }
117614f5a3b0Smrg
117714f5a3b0Smrg #if _GLIBCXX_USE_WCHAR_T
117814f5a3b0Smrg inline std::wstring
117914f5a3b0Smrg path::wstring() const { return string<wchar_t>(); }
118014f5a3b0Smrg #endif
118114f5a3b0Smrg
1182181254a7Smrg #ifdef _GLIBCXX_USE_CHAR8_T
1183181254a7Smrg inline std::u8string
1184181254a7Smrg path::u8string() const { return string<char8_t>(); }
1185181254a7Smrg #else
118614f5a3b0Smrg inline std::string
118714f5a3b0Smrg path::u8string() const
118814f5a3b0Smrg {
118914f5a3b0Smrg #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
119014f5a3b0Smrg std::string __str;
1191181254a7Smrg // convert from native wide encoding (assumed to be UTF-16) to UTF-8
1192181254a7Smrg std::codecvt_utf8_utf16<value_type> __cvt;
119314f5a3b0Smrg const value_type* __first = _M_pathname.data();
119414f5a3b0Smrg const value_type* __last = __first + _M_pathname.size();
1195181254a7Smrg if (__str_codecvt_out_all(__first, __last, __str, __cvt))
119614f5a3b0Smrg return __str;
1197b1e83836Smrg __detail::__throw_conversion_error();
119814f5a3b0Smrg #else
119914f5a3b0Smrg return _M_pathname;
120014f5a3b0Smrg #endif
120114f5a3b0Smrg }
1202181254a7Smrg #endif // _GLIBCXX_USE_CHAR8_T
120314f5a3b0Smrg
120414f5a3b0Smrg inline std::u16string
120514f5a3b0Smrg path::u16string() const { return string<char16_t>(); }
120614f5a3b0Smrg
120714f5a3b0Smrg inline std::u32string
120814f5a3b0Smrg path::u32string() const { return string<char32_t>(); }
120914f5a3b0Smrg
121014f5a3b0Smrg template<typename _CharT, typename _Traits, typename _Allocator>
121114f5a3b0Smrg inline std::basic_string<_CharT, _Traits, _Allocator>
121214f5a3b0Smrg path::generic_string(const _Allocator& __a) const
121314f5a3b0Smrg {
121414f5a3b0Smrg #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
121514f5a3b0Smrg const value_type __slash = L'/';
121614f5a3b0Smrg #else
121714f5a3b0Smrg const value_type __slash = '/';
121814f5a3b0Smrg #endif
1219fb8a8121Smrg using _Alloc2 = typename allocator_traits<_Allocator>::template
1220fb8a8121Smrg rebind_alloc<value_type>;
1221fb8a8121Smrg basic_string<value_type, char_traits<value_type>, _Alloc2> __str(__a);
122214f5a3b0Smrg
1223181254a7Smrg if (_M_type() == _Type::_Root_dir)
122414f5a3b0Smrg __str.assign(1, __slash);
122514f5a3b0Smrg else
122614f5a3b0Smrg {
122714f5a3b0Smrg __str.reserve(_M_pathname.size());
122814f5a3b0Smrg bool __add_slash = false;
122914f5a3b0Smrg for (auto& __elem : *this)
123014f5a3b0Smrg {
1231fb8a8121Smrg #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1232fb8a8121Smrg if (__elem._M_type() == _Type::_Root_dir)
1233fb8a8121Smrg {
1234fb8a8121Smrg __str += __slash;
1235fb8a8121Smrg continue;
1236fb8a8121Smrg }
1237fb8a8121Smrg #endif
123814f5a3b0Smrg if (__add_slash)
123914f5a3b0Smrg __str += __slash;
1240fb8a8121Smrg __str += basic_string_view<value_type>(__elem._M_pathname);
1241181254a7Smrg __add_slash = __elem._M_type() == _Type::_Filename;
124214f5a3b0Smrg }
124314f5a3b0Smrg }
124414f5a3b0Smrg
124514f5a3b0Smrg if constexpr (is_same_v<_CharT, value_type>)
124614f5a3b0Smrg return __str;
124714f5a3b0Smrg else
124814f5a3b0Smrg return _S_str_convert<_CharT, _Traits>(__str, __a);
124914f5a3b0Smrg }
125014f5a3b0Smrg
125114f5a3b0Smrg inline std::string
125214f5a3b0Smrg path::generic_string() const
125314f5a3b0Smrg { return generic_string<char>(); }
125414f5a3b0Smrg
125514f5a3b0Smrg #if _GLIBCXX_USE_WCHAR_T
125614f5a3b0Smrg inline std::wstring
125714f5a3b0Smrg path::generic_wstring() const
125814f5a3b0Smrg { return generic_string<wchar_t>(); }
125914f5a3b0Smrg #endif
126014f5a3b0Smrg
1261181254a7Smrg #ifdef _GLIBCXX_USE_CHAR8_T
1262181254a7Smrg inline std::u8string
1263181254a7Smrg path::generic_u8string() const
1264181254a7Smrg { return generic_string<char8_t>(); }
1265181254a7Smrg #else
126614f5a3b0Smrg inline std::string
126714f5a3b0Smrg path::generic_u8string() const
126814f5a3b0Smrg { return generic_string(); }
1269181254a7Smrg #endif
127014f5a3b0Smrg
127114f5a3b0Smrg inline std::u16string
127214f5a3b0Smrg path::generic_u16string() const
127314f5a3b0Smrg { return generic_string<char16_t>(); }
127414f5a3b0Smrg
127514f5a3b0Smrg inline std::u32string
127614f5a3b0Smrg path::generic_u32string() const
127714f5a3b0Smrg { return generic_string<char32_t>(); }
127814f5a3b0Smrg
127914f5a3b0Smrg inline int
1280181254a7Smrg path::compare(const string_type& __s) const noexcept
1281181254a7Smrg { return compare(basic_string_view<value_type>(__s)); }
128214f5a3b0Smrg
128314f5a3b0Smrg inline int
1284181254a7Smrg path::compare(const value_type* __s) const noexcept
1285181254a7Smrg { return compare(basic_string_view<value_type>(__s)); }
128614f5a3b0Smrg
128714f5a3b0Smrg inline path
128814f5a3b0Smrg path::filename() const
128914f5a3b0Smrg {
129014f5a3b0Smrg if (empty())
129114f5a3b0Smrg return {};
1292181254a7Smrg else if (_M_type() == _Type::_Filename)
129314f5a3b0Smrg return *this;
1294181254a7Smrg else if (_M_type() == _Type::_Multi)
129514f5a3b0Smrg {
129614f5a3b0Smrg if (_M_pathname.back() == preferred_separator)
129714f5a3b0Smrg return {};
12987d4dc15bSmrg auto __last = --end();
12997d4dc15bSmrg if (__last->_M_type() == _Type::_Filename)
13007d4dc15bSmrg return *__last;
130114f5a3b0Smrg }
130214f5a3b0Smrg return {};
130314f5a3b0Smrg }
130414f5a3b0Smrg
130514f5a3b0Smrg inline path
130614f5a3b0Smrg path::stem() const
130714f5a3b0Smrg {
130814f5a3b0Smrg auto ext = _M_find_extension();
130914f5a3b0Smrg if (ext.first && ext.second != 0)
131014f5a3b0Smrg return path{ext.first->substr(0, ext.second)};
131114f5a3b0Smrg return {};
131214f5a3b0Smrg }
131314f5a3b0Smrg
131414f5a3b0Smrg inline path
131514f5a3b0Smrg path::extension() const
131614f5a3b0Smrg {
131714f5a3b0Smrg auto ext = _M_find_extension();
131814f5a3b0Smrg if (ext.first && ext.second != string_type::npos)
131914f5a3b0Smrg return path{ext.first->substr(ext.second)};
132014f5a3b0Smrg return {};
132114f5a3b0Smrg }
132214f5a3b0Smrg
132314f5a3b0Smrg inline bool
1324181254a7Smrg path::has_stem() const noexcept
132514f5a3b0Smrg {
132614f5a3b0Smrg auto ext = _M_find_extension();
132714f5a3b0Smrg return ext.first && ext.second != 0;
132814f5a3b0Smrg }
132914f5a3b0Smrg
133014f5a3b0Smrg inline bool
1331181254a7Smrg path::has_extension() const noexcept
133214f5a3b0Smrg {
133314f5a3b0Smrg auto ext = _M_find_extension();
133414f5a3b0Smrg return ext.first && ext.second != string_type::npos;
133514f5a3b0Smrg }
133614f5a3b0Smrg
1337181254a7Smrg inline bool
1338181254a7Smrg path::is_absolute() const noexcept
1339181254a7Smrg {
1340181254a7Smrg #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1341181254a7Smrg return has_root_name() && has_root_directory();
1342181254a7Smrg #else
1343181254a7Smrg return has_root_directory();
1344181254a7Smrg #endif
1345181254a7Smrg }
1346181254a7Smrg
134714f5a3b0Smrg inline path::iterator
1348b1e83836Smrg path::begin() const noexcept
134914f5a3b0Smrg {
1350181254a7Smrg if (_M_type() == _Type::_Multi)
135114f5a3b0Smrg return iterator(this, _M_cmpts.begin());
135214f5a3b0Smrg return iterator(this, empty());
135314f5a3b0Smrg }
135414f5a3b0Smrg
135514f5a3b0Smrg inline path::iterator
1356b1e83836Smrg path::end() const noexcept
135714f5a3b0Smrg {
1358181254a7Smrg if (_M_type() == _Type::_Multi)
135914f5a3b0Smrg return iterator(this, _M_cmpts.end());
136014f5a3b0Smrg return iterator(this, true);
136114f5a3b0Smrg }
136214f5a3b0Smrg
136314f5a3b0Smrg inline path::iterator&
1364b1e83836Smrg path::iterator::operator++() noexcept
136514f5a3b0Smrg {
136614f5a3b0Smrg __glibcxx_assert(_M_path != nullptr);
1367b1e83836Smrg if (_M_is_multi())
136814f5a3b0Smrg {
136914f5a3b0Smrg __glibcxx_assert(_M_cur != _M_path->_M_cmpts.end());
137014f5a3b0Smrg ++_M_cur;
137114f5a3b0Smrg }
137214f5a3b0Smrg else
137314f5a3b0Smrg {
137414f5a3b0Smrg __glibcxx_assert(!_M_at_end);
137514f5a3b0Smrg _M_at_end = true;
137614f5a3b0Smrg }
137714f5a3b0Smrg return *this;
137814f5a3b0Smrg }
137914f5a3b0Smrg
138014f5a3b0Smrg inline path::iterator&
1381b1e83836Smrg path::iterator::operator--() noexcept
138214f5a3b0Smrg {
138314f5a3b0Smrg __glibcxx_assert(_M_path != nullptr);
1384b1e83836Smrg if (_M_is_multi())
138514f5a3b0Smrg {
138614f5a3b0Smrg __glibcxx_assert(_M_cur != _M_path->_M_cmpts.begin());
138714f5a3b0Smrg --_M_cur;
138814f5a3b0Smrg }
138914f5a3b0Smrg else
139014f5a3b0Smrg {
139114f5a3b0Smrg __glibcxx_assert(_M_at_end);
139214f5a3b0Smrg _M_at_end = false;
139314f5a3b0Smrg }
139414f5a3b0Smrg return *this;
139514f5a3b0Smrg }
139614f5a3b0Smrg
139714f5a3b0Smrg inline path::iterator::reference
1398b1e83836Smrg path::iterator::operator*() const noexcept
139914f5a3b0Smrg {
140014f5a3b0Smrg __glibcxx_assert(_M_path != nullptr);
1401b1e83836Smrg if (_M_is_multi())
140214f5a3b0Smrg {
140314f5a3b0Smrg __glibcxx_assert(_M_cur != _M_path->_M_cmpts.end());
140414f5a3b0Smrg return *_M_cur;
140514f5a3b0Smrg }
140614f5a3b0Smrg return *_M_path;
140714f5a3b0Smrg }
140814f5a3b0Smrg
140914f5a3b0Smrg inline bool
1410b1e83836Smrg path::iterator::_M_equals(iterator __rhs) const noexcept
141114f5a3b0Smrg {
141214f5a3b0Smrg if (_M_path != __rhs._M_path)
141314f5a3b0Smrg return false;
141414f5a3b0Smrg if (_M_path == nullptr)
141514f5a3b0Smrg return true;
1416b1e83836Smrg if (_M_is_multi())
141714f5a3b0Smrg return _M_cur == __rhs._M_cur;
141814f5a3b0Smrg return _M_at_end == __rhs._M_at_end;
141914f5a3b0Smrg }
142014f5a3b0Smrg
1421b1e83836Smrg // Define this now that path and path::iterator are complete.
1422b1e83836Smrg // It needs to consider the string_view(Range&&) constructor during
1423b1e83836Smrg // overload resolution, which depends on whether range<path> is satisfied,
1424b1e83836Smrg // which depends on whether path::iterator is complete.
1425b1e83836Smrg inline int
1426b1e83836Smrg path::_S_compare(const path& __lhs, const path& __rhs) noexcept
1427b1e83836Smrg { return __lhs.compare(__rhs); }
1428b1e83836Smrg
1429a448f87cSmrg /// @} group filesystem
143014f5a3b0Smrg _GLIBCXX_END_NAMESPACE_CXX11
143114f5a3b0Smrg } // namespace filesystem
143214f5a3b0Smrg
1433b1e83836Smrg /// @cond undocumented
1434b1e83836Smrg
1435181254a7Smrg inline ptrdiff_t
1436181254a7Smrg distance(filesystem::path::iterator __first, filesystem::path::iterator __last)
1437b1e83836Smrg noexcept
1438181254a7Smrg { return __path_iter_distance(__first, __last); }
1439181254a7Smrg
1440a448f87cSmrg template<typename _Distance>
1441b1e83836Smrg inline void
1442b1e83836Smrg advance(filesystem::path::iterator& __i, _Distance __n) noexcept
1443181254a7Smrg { __path_iter_advance(__i, static_cast<ptrdiff_t>(__n)); }
1444181254a7Smrg
1445181254a7Smrg extern template class __shared_ptr<const filesystem::filesystem_error::_Impl>;
1446181254a7Smrg
1447b1e83836Smrg /// @endcond
1448b1e83836Smrg
1449b1e83836Smrg // _GLIBCXX_RESOLVE_LIB_DEFECTS
1450b1e83836Smrg // 3657. std::hash<std::filesystem::path> is not enabled
1451b1e83836Smrg template<>
1452b1e83836Smrg struct hash<filesystem::path>
1453b1e83836Smrg {
1454b1e83836Smrg size_t
1455b1e83836Smrg operator()(const filesystem::path& __p) const noexcept
1456b1e83836Smrg { return filesystem::hash_value(__p); }
1457b1e83836Smrg };
1458b1e83836Smrg
145914f5a3b0Smrg _GLIBCXX_END_NAMESPACE_VERSION
146014f5a3b0Smrg } // namespace std
146114f5a3b0Smrg
146214f5a3b0Smrg #endif // C++17
146314f5a3b0Smrg
146414f5a3b0Smrg #endif // _GLIBCXX_FS_PATH_H
1465