PR libstdc++/90454.cc path construction from void*
[official-gcc.git] / libstdc++-v3 / include / experimental / bits / fs_path.h
blob588f06822beb60939a352cdb9f4d5dc41a25964d
1 // Class filesystem::path -*- C++ -*-
3 // Copyright (C) 2014-2019 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file experimental/bits/fs_path.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{experimental/filesystem}
30 #ifndef _GLIBCXX_EXPERIMENTAL_FS_PATH_H
31 #define _GLIBCXX_EXPERIMENTAL_FS_PATH_H 1
33 #if __cplusplus < 201103L
34 # include <bits/c++0x_warning.h>
35 #else
37 #include <utility>
38 #include <type_traits>
39 #include <vector>
40 #include <locale>
41 #include <iosfwd>
42 #include <codecvt>
43 #include <system_error>
44 #include <bits/stl_algobase.h>
45 #include <bits/quoted_string.h>
46 #include <bits/locale_conv.h>
47 #if __cplusplus == 201402L
48 # include <experimental/string_view>
49 #endif
51 #if defined(_WIN32) && !defined(__CYGWIN__)
52 # define _GLIBCXX_FILESYSTEM_IS_WINDOWS 1
53 # include <algorithm>
54 #endif
56 namespace std _GLIBCXX_VISIBILITY(default)
58 _GLIBCXX_BEGIN_NAMESPACE_VERSION
60 namespace experimental
62 namespace filesystem
64 inline namespace v1
66 _GLIBCXX_BEGIN_NAMESPACE_CXX11
68 #if __cplusplus == 201402L
69 using std::experimental::basic_string_view;
70 #elif __cplusplus > 201402L
71 using std::basic_string_view;
72 #endif
74 /**
75 * @addtogroup filesystem-ts
76 * @{
79 /// A filesystem path.
80 class path
82 template<typename _CharT,
83 typename _Ch = typename remove_const<_CharT>::type>
84 using __is_encoded_char
85 = __or_<is_same<_Ch, char>,
86 is_same<_Ch, wchar_t>,
87 #ifdef _GLIBCXX_USE_CHAR8_T
88 is_same<_Ch, char8_t>,
89 #endif
90 is_same<_Ch, char16_t>,
91 is_same<_Ch, char32_t>>;
93 template<typename _Iter,
94 typename _Iter_traits = std::iterator_traits<_Iter>>
95 using __is_path_iter_src
96 = __and_<__is_encoded_char<typename _Iter_traits::value_type>,
97 std::is_base_of<std::input_iterator_tag,
98 typename _Iter_traits::iterator_category>>;
100 template<typename _Iter>
101 static __is_path_iter_src<_Iter>
102 __is_path_src(_Iter, int);
104 template<typename _CharT, typename _Traits, typename _Alloc>
105 static __is_encoded_char<_CharT>
106 __is_path_src(const basic_string<_CharT, _Traits, _Alloc>&, int);
108 #if __cplusplus >= 201402L
109 template<typename _CharT, typename _Traits>
110 static __is_encoded_char<_CharT>
111 __is_path_src(const basic_string_view<_CharT, _Traits>&, int);
112 #endif
114 template<typename _Unknown>
115 static std::false_type
116 __is_path_src(const _Unknown&, ...);
118 template<typename _Tp1, typename _Tp2>
119 struct __constructible_from;
121 template<typename _Iter>
122 struct __constructible_from<_Iter, _Iter>
123 : __is_path_iter_src<_Iter>
124 { };
126 template<typename _Source>
127 struct __constructible_from<_Source, void>
128 : decltype(__is_path_src(std::declval<_Source>(), 0))
129 { };
131 template<typename _Tp1, typename _Tp2 = void,
132 typename _Tp1_nocv = typename remove_cv<_Tp1>::type,
133 typename _Tp1_noptr = typename remove_pointer<_Tp1>::type>
134 using _Path = typename
135 std::enable_if<__and_<__not_<is_same<_Tp1_nocv, path>>,
136 __not_<is_void<_Tp1_noptr>>,
137 __constructible_from<_Tp1, _Tp2>>::value,
138 path>::type;
140 template<typename _Source>
141 static _Source
142 _S_range_begin(_Source __begin) { return __begin; }
144 struct __null_terminated { };
146 template<typename _Source>
147 static __null_terminated
148 _S_range_end(_Source) { return {}; }
150 template<typename _CharT, typename _Traits, typename _Alloc>
151 static const _CharT*
152 _S_range_begin(const basic_string<_CharT, _Traits, _Alloc>& __str)
153 { return __str.data(); }
155 template<typename _CharT, typename _Traits, typename _Alloc>
156 static const _CharT*
157 _S_range_end(const basic_string<_CharT, _Traits, _Alloc>& __str)
158 { return __str.data() + __str.size(); }
160 #if __cplusplus >= 201402L
161 template<typename _CharT, typename _Traits>
162 static const _CharT*
163 _S_range_begin(const basic_string_view<_CharT, _Traits>& __str)
164 { return __str.data(); }
166 template<typename _CharT, typename _Traits>
167 static const _CharT*
168 _S_range_end(const basic_string_view<_CharT, _Traits>& __str)
169 { return __str.data() + __str.size(); }
170 #endif
172 template<typename _Tp,
173 typename _Iter = decltype(_S_range_begin(std::declval<_Tp>())),
174 typename _Val = typename std::iterator_traits<_Iter>::value_type>
175 using __value_type_is_char = typename std::enable_if<
176 std::is_same<typename std::remove_const<_Val>::type, char>::value
177 >::type;
179 public:
180 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
181 typedef wchar_t value_type;
182 static constexpr value_type preferred_separator = L'\\';
183 #else
184 typedef char value_type;
185 static constexpr value_type preferred_separator = '/';
186 #endif
187 typedef std::basic_string<value_type> string_type;
189 // constructors and destructor
191 path() noexcept { }
193 path(const path& __p) = default;
195 path(path&& __p) noexcept
196 : _M_pathname(std::move(__p._M_pathname)), _M_type(__p._M_type)
198 _M_split_cmpts();
199 __p.clear();
202 path(string_type&& __source)
203 : _M_pathname(std::move(__source))
204 { _M_split_cmpts(); }
206 template<typename _Source,
207 typename _Require = _Path<_Source>>
208 path(_Source const& __source)
209 : _M_pathname(_S_convert(_S_range_begin(__source),
210 _S_range_end(__source)))
211 { _M_split_cmpts(); }
213 template<typename _InputIterator,
214 typename _Require = _Path<_InputIterator, _InputIterator>>
215 path(_InputIterator __first, _InputIterator __last)
216 : _M_pathname(_S_convert(__first, __last))
217 { _M_split_cmpts(); }
219 template<typename _Source,
220 typename _Require = _Path<_Source>,
221 typename _Require2 = __value_type_is_char<_Source>>
222 path(_Source const& __source, const locale& __loc)
223 : _M_pathname(_S_convert_loc(_S_range_begin(__source),
224 _S_range_end(__source), __loc))
225 { _M_split_cmpts(); }
227 template<typename _InputIterator,
228 typename _Require = _Path<_InputIterator, _InputIterator>,
229 typename _Require2 = __value_type_is_char<_InputIterator>>
230 path(_InputIterator __first, _InputIterator __last, const locale& __loc)
231 : _M_pathname(_S_convert_loc(__first, __last, __loc))
232 { _M_split_cmpts(); }
234 ~path() = default;
236 // assignments
238 path& operator=(const path& __p) = default;
239 path& operator=(path&& __p) noexcept;
240 path& operator=(string_type&& __source);
241 path& assign(string_type&& __source);
243 template<typename _Source>
244 _Path<_Source>&
245 operator=(_Source const& __source)
246 { return *this = path(__source); }
248 template<typename _Source>
249 _Path<_Source>&
250 assign(_Source const& __source)
251 { return *this = path(__source); }
253 template<typename _InputIterator>
254 _Path<_InputIterator, _InputIterator>&
255 assign(_InputIterator __first, _InputIterator __last)
256 { return *this = path(__first, __last); }
258 // appends
260 path& operator/=(const path& __p) { return _M_append(__p._M_pathname); }
262 template <class _Source>
263 _Path<_Source>&
264 operator/=(_Source const& __source)
265 { return append(__source); }
267 template<typename _Source>
268 _Path<_Source>&
269 append(_Source const& __source)
271 return _M_append(_S_convert(_S_range_begin(__source),
272 _S_range_end(__source)));
275 template<typename _InputIterator>
276 _Path<_InputIterator, _InputIterator>&
277 append(_InputIterator __first, _InputIterator __last)
278 { return _M_append(_S_convert(__first, __last)); }
280 // concatenation
282 path& operator+=(const path& __x);
283 path& operator+=(const string_type& __x);
284 path& operator+=(const value_type* __x);
285 path& operator+=(value_type __x);
286 #if __cplusplus >= 201402L
287 path& operator+=(basic_string_view<value_type> __x);
288 #endif
290 template<typename _Source>
291 _Path<_Source>&
292 operator+=(_Source const& __x) { return concat(__x); }
294 template<typename _CharT>
295 _Path<_CharT*, _CharT*>&
296 operator+=(_CharT __x);
298 template<typename _Source>
299 _Path<_Source>&
300 concat(_Source const& __x)
301 { return *this += _S_convert(_S_range_begin(__x), _S_range_end(__x)); }
303 template<typename _InputIterator>
304 _Path<_InputIterator, _InputIterator>&
305 concat(_InputIterator __first, _InputIterator __last)
306 { return *this += _S_convert(__first, __last); }
308 // modifiers
310 void clear() noexcept { _M_pathname.clear(); _M_split_cmpts(); }
312 path& make_preferred();
313 path& remove_filename();
314 path& replace_filename(const path& __replacement);
315 path& replace_extension(const path& __replacement = path());
317 void swap(path& __rhs) noexcept;
319 // native format observers
321 const string_type& native() const noexcept { return _M_pathname; }
322 const value_type* c_str() const noexcept { return _M_pathname.c_str(); }
323 operator string_type() const { return _M_pathname; }
325 template<typename _CharT, typename _Traits = std::char_traits<_CharT>,
326 typename _Allocator = std::allocator<_CharT>>
327 std::basic_string<_CharT, _Traits, _Allocator>
328 string(const _Allocator& __a = _Allocator()) const;
330 std::string string() const;
331 #if _GLIBCXX_USE_WCHAR_T
332 std::wstring wstring() const;
333 #endif
334 #ifdef _GLIBCXX_USE_CHAR8_T
335 __attribute__((__abi_tag__("__u8")))
336 std::u8string u8string() const;
337 #else
338 std::string u8string() const;
339 #endif // _GLIBCXX_USE_CHAR8_T
340 std::u16string u16string() const;
341 std::u32string u32string() const;
343 // generic format observers
344 template<typename _CharT, typename _Traits = std::char_traits<_CharT>,
345 typename _Allocator = std::allocator<_CharT>>
346 std::basic_string<_CharT, _Traits, _Allocator>
347 generic_string(const _Allocator& __a = _Allocator()) const;
349 std::string generic_string() const;
350 #if _GLIBCXX_USE_WCHAR_T
351 std::wstring generic_wstring() const;
352 #endif
353 #ifdef _GLIBCXX_USE_CHAR8_T
354 __attribute__((__abi_tag__("__u8")))
355 std::u8string generic_u8string() const;
356 #else
357 std::string generic_u8string() const;
358 #endif // _GLIBCXX_USE_CHAR8_T
359 std::u16string generic_u16string() const;
360 std::u32string generic_u32string() const;
362 // compare
364 int compare(const path& __p) const noexcept;
365 int compare(const string_type& __s) const;
366 int compare(const value_type* __s) const;
367 #if __cplusplus >= 201402L
368 int compare(const basic_string_view<value_type> __s) const;
369 #endif
371 // decomposition
373 path root_name() const;
374 path root_directory() const;
375 path root_path() const;
376 path relative_path() const;
377 path parent_path() const;
378 path filename() const;
379 path stem() const;
380 path extension() const;
382 // query
384 _GLIBCXX_NODISCARD bool empty() const noexcept { return _M_pathname.empty(); }
385 bool has_root_name() const;
386 bool has_root_directory() const;
387 bool has_root_path() const;
388 bool has_relative_path() const;
389 bool has_parent_path() const;
390 bool has_filename() const;
391 bool has_stem() const;
392 bool has_extension() const;
393 bool is_absolute() const;
394 bool is_relative() const { return !is_absolute(); }
396 // iterators
397 class iterator;
398 typedef iterator const_iterator;
400 iterator begin() const;
401 iterator end() const;
403 /// @cond undocumented
404 // Create a basic_string by reading until a null character.
405 template<typename _InputIterator,
406 typename _Traits = std::iterator_traits<_InputIterator>,
407 typename _CharT
408 = typename std::remove_cv<typename _Traits::value_type>::type>
409 static std::basic_string<_CharT>
410 _S_string_from_iter(_InputIterator __source)
412 std::basic_string<_CharT> __str;
413 for (_CharT __ch = *__source; __ch != _CharT(); __ch = *++__source)
414 __str.push_back(__ch);
415 return __str;
417 /// @endcond
419 private:
420 enum class _Type : unsigned char {
421 _Multi, _Root_name, _Root_dir, _Filename
424 path(string_type __str, _Type __type) : _M_pathname(__str), _M_type(__type)
426 __glibcxx_assert(!empty());
427 __glibcxx_assert(_M_type != _Type::_Multi);
430 enum class _Split { _Stem, _Extension };
432 path& _M_append(const string_type& __str)
434 if (!_M_pathname.empty() && !_S_is_dir_sep(_M_pathname.back())
435 && !__str.empty() && !_S_is_dir_sep(__str.front()))
436 _M_pathname += preferred_separator;
437 _M_pathname += __str;
438 _M_split_cmpts();
439 return *this;
442 pair<const string_type*, size_t> _M_find_extension() const;
444 template<typename _CharT>
445 struct _Cvt;
447 static string_type
448 _S_convert(value_type* __src, __null_terminated)
449 { return string_type(__src); }
451 static string_type
452 _S_convert(const value_type* __src, __null_terminated)
453 { return string_type(__src); }
455 template<typename _Iter>
456 static string_type
457 _S_convert(_Iter __first, _Iter __last)
459 using __value_type = typename std::iterator_traits<_Iter>::value_type;
460 return _Cvt<typename remove_cv<__value_type>::type>::
461 _S_convert(__first, __last);
464 template<typename _InputIterator>
465 static string_type
466 _S_convert(_InputIterator __src, __null_terminated)
468 auto __s = _S_string_from_iter(__src);
469 return _S_convert(__s.c_str(), __s.c_str() + __s.size());
472 static string_type
473 _S_convert_loc(const char* __first, const char* __last,
474 const std::locale& __loc);
476 template<typename _Iter>
477 static string_type
478 _S_convert_loc(_Iter __first, _Iter __last, const std::locale& __loc)
480 const std::string __str(__first, __last);
481 return _S_convert_loc(__str.data(), __str.data()+__str.size(), __loc);
484 template<typename _InputIterator>
485 static string_type
486 _S_convert_loc(_InputIterator __src, __null_terminated,
487 const std::locale& __loc)
489 const std::string __s = _S_string_from_iter(__src);
490 return _S_convert_loc(__s.data(), __s.data() + __s.size(), __loc);
493 bool _S_is_dir_sep(value_type __ch)
495 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
496 return __ch == L'/' || __ch == preferred_separator;
497 #else
498 return __ch == '/';
499 #endif
502 void _M_split_cmpts();
503 void _M_trim();
504 void _M_add_root_name(size_t __n);
505 void _M_add_root_dir(size_t __pos);
506 void _M_add_filename(size_t __pos, size_t __n);
508 string_type _M_pathname;
510 struct _Cmpt;
511 using _List = _GLIBCXX_STD_C::vector<_Cmpt>;
512 _List _M_cmpts; // empty unless _M_type == _Type::_Multi
513 _Type _M_type = _Type::_Multi;
516 /// @relates std::experimental::filesystem::path @{
518 /// Swap overload for paths
519 inline void swap(path& __lhs, path& __rhs) noexcept { __lhs.swap(__rhs); }
521 /// Compute a hash value for a path
522 size_t hash_value(const path& __p) noexcept;
524 /// Compare paths
525 inline bool operator<(const path& __lhs, const path& __rhs) noexcept
526 { return __lhs.compare(__rhs) < 0; }
528 /// Compare paths
529 inline bool operator<=(const path& __lhs, const path& __rhs) noexcept
530 { return !(__rhs < __lhs); }
532 /// Compare paths
533 inline bool operator>(const path& __lhs, const path& __rhs) noexcept
534 { return __rhs < __lhs; }
536 /// Compare paths
537 inline bool operator>=(const path& __lhs, const path& __rhs) noexcept
538 { return !(__lhs < __rhs); }
540 /// Compare paths
541 inline bool operator==(const path& __lhs, const path& __rhs) noexcept
542 { return __lhs.compare(__rhs) == 0; }
544 /// Compare paths
545 inline bool operator!=(const path& __lhs, const path& __rhs) noexcept
546 { return !(__lhs == __rhs); }
548 /// Append one path to another
549 inline path operator/(const path& __lhs, const path& __rhs)
551 path __result(__lhs);
552 __result /= __rhs;
553 return __result;
556 /// Write a path to a stream
557 template<typename _CharT, typename _Traits>
558 basic_ostream<_CharT, _Traits>&
559 operator<<(basic_ostream<_CharT, _Traits>& __os, const path& __p)
561 auto __tmp = __p.string<_CharT, _Traits>();
562 using __quoted_string
563 = std::__detail::_Quoted_string<decltype(__tmp)&, _CharT>;
564 __os << __quoted_string{__tmp, _CharT('"'), _CharT('\\')};
565 return __os;
568 /// Read a path from a stream
569 template<typename _CharT, typename _Traits>
570 basic_istream<_CharT, _Traits>&
571 operator>>(basic_istream<_CharT, _Traits>& __is, path& __p)
573 basic_string<_CharT, _Traits> __tmp;
574 using __quoted_string
575 = std::__detail::_Quoted_string<decltype(__tmp)&, _CharT>;
576 if (__is >> __quoted_string{ __tmp, _CharT('"'), _CharT('\\') })
577 __p = std::move(__tmp);
578 return __is;
581 /// Create a path from a UTF-8-encoded sequence of char
582 // TODO constrain with _Path<Source> and __value_type_is_char
583 template<typename _Source>
584 inline path
585 u8path(const _Source& __source)
587 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
588 return path{ path::string_type{__source} };
589 #else
590 return path{ __source };
591 #endif
594 /// Create a path from a UTF-8-encoded sequence of char
595 // TODO constrain with _Path<InputIterator, InputIterator> and __value_type_is_char
596 template<typename _InputIterator>
597 inline path
598 u8path(_InputIterator __first, _InputIterator __last)
600 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
601 return path{ path::string_type{__first, __last} };
602 #else
603 return path{ __first, __last };
604 #endif
607 /// @}
609 /// Exception type thrown by the Filesystem TS library
610 class filesystem_error : public std::system_error
612 public:
613 filesystem_error(const string& __what_arg, error_code __ec)
614 : system_error(__ec, __what_arg) { }
616 filesystem_error(const string& __what_arg, const path& __p1,
617 error_code __ec)
618 : system_error(__ec, __what_arg), _M_path1(__p1) { }
620 filesystem_error(const string& __what_arg, const path& __p1,
621 const path& __p2, error_code __ec)
622 : system_error(__ec, __what_arg), _M_path1(__p1), _M_path2(__p2)
625 ~filesystem_error();
627 const path& path1() const noexcept { return _M_path1; }
628 const path& path2() const noexcept { return _M_path2; }
629 const char* what() const noexcept { return _M_what.c_str(); }
631 private:
632 std::string _M_gen_what();
634 path _M_path1;
635 path _M_path2;
636 std::string _M_what = _M_gen_what();
639 /// @cond undocumented
640 struct path::_Cmpt : path
642 _Cmpt(string_type __s, _Type __t, size_t __pos)
643 : path(std::move(__s), __t), _M_pos(__pos) { }
645 _Cmpt() : _M_pos(-1) { }
647 size_t _M_pos;
650 // specialize _Cvt for degenerate 'noconv' case
651 template<>
652 struct path::_Cvt<path::value_type>
654 template<typename _Iter>
655 static string_type
656 _S_convert(_Iter __first, _Iter __last)
657 { return string_type{__first, __last}; }
660 template<typename _CharT>
661 struct path::_Cvt
663 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
664 static string_type
665 _S_wconvert(const char* __f, const char* __l, true_type)
667 using _Cvt = std::codecvt<wchar_t, char, mbstate_t>;
668 const auto& __cvt = std::use_facet<_Cvt>(std::locale{});
669 std::wstring __wstr;
670 if (__str_codecvt_in(__f, __l, __wstr, __cvt))
671 return __wstr;
672 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
673 "Cannot convert character sequence",
674 std::make_error_code(errc::illegal_byte_sequence)));
677 static string_type
678 _S_wconvert(const _CharT* __f, const _CharT* __l, false_type)
680 std::codecvt_utf8<_CharT> __cvt;
681 std::string __str;
682 if (__str_codecvt_out(__f, __l, __str, __cvt))
684 const char* __f2 = __str.data();
685 const char* __l2 = __f2 + __str.size();
686 std::codecvt_utf8<wchar_t> __wcvt;
687 std::wstring __wstr;
688 if (__str_codecvt_in(__f2, __l2, __wstr, __wcvt))
689 return __wstr;
691 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
692 "Cannot convert character sequence",
693 std::make_error_code(errc::illegal_byte_sequence)));
696 static string_type
697 _S_convert(const _CharT* __f, const _CharT* __l)
699 return _S_wconvert(__f, __l, is_same<_CharT, char>{});
701 #else
702 static string_type
703 _S_convert(const _CharT* __f, const _CharT* __l)
705 #ifdef _GLIBCXX_USE_CHAR8_T
706 if constexpr (is_same<_CharT, char8_t>::value)
708 string_type __str(__f, __l);
709 return __str;
711 else
713 #endif
714 std::codecvt_utf8<_CharT> __cvt;
715 std::string __str;
716 if (__str_codecvt_out(__f, __l, __str, __cvt))
717 return __str;
718 #ifdef _GLIBCXX_USE_CHAR8_T
720 #endif
721 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
722 "Cannot convert character sequence",
723 std::make_error_code(errc::illegal_byte_sequence)));
725 #endif
727 static string_type
728 _S_convert(_CharT* __f, _CharT* __l)
730 return _S_convert(const_cast<const _CharT*>(__f),
731 const_cast<const _CharT*>(__l));
734 template<typename _Iter>
735 static string_type
736 _S_convert(_Iter __first, _Iter __last)
738 const std::basic_string<_CharT> __str(__first, __last);
739 return _S_convert(__str.data(), __str.data() + __str.size());
742 template<typename _Iter, typename _Cont>
743 static string_type
744 _S_convert(__gnu_cxx::__normal_iterator<_Iter, _Cont> __first,
745 __gnu_cxx::__normal_iterator<_Iter, _Cont> __last)
746 { return _S_convert(__first.base(), __last.base()); }
748 /// @endcond
750 /// An iterator for the components of a path
751 class path::iterator
753 public:
754 using difference_type = std::ptrdiff_t;
755 using value_type = path;
756 using reference = const path&;
757 using pointer = const path*;
758 using iterator_category = std::bidirectional_iterator_tag;
760 iterator() : _M_path(nullptr), _M_cur(), _M_at_end() { }
762 iterator(const iterator&) = default;
763 iterator& operator=(const iterator&) = default;
765 reference operator*() const;
766 pointer operator->() const { return std::__addressof(**this); }
768 iterator& operator++();
769 iterator operator++(int) { auto __tmp = *this; ++*this; return __tmp; }
771 iterator& operator--();
772 iterator operator--(int) { auto __tmp = *this; --*this; return __tmp; }
774 friend bool operator==(const iterator& __lhs, const iterator& __rhs)
775 { return __lhs._M_equals(__rhs); }
777 friend bool operator!=(const iterator& __lhs, const iterator& __rhs)
778 { return !__lhs._M_equals(__rhs); }
780 private:
781 friend class path;
783 iterator(const path* __path, path::_List::const_iterator __iter)
784 : _M_path(__path), _M_cur(__iter), _M_at_end()
787 iterator(const path* __path, bool __at_end)
788 : _M_path(__path), _M_cur(), _M_at_end(__at_end)
791 bool _M_equals(iterator) const;
793 const path* _M_path;
794 path::_List::const_iterator _M_cur;
795 bool _M_at_end; // only used when type != _Multi
799 inline path&
800 path::operator=(path&& __p) noexcept
802 _M_pathname = std::move(__p._M_pathname);
803 _M_cmpts = std::move(__p._M_cmpts);
804 _M_type = __p._M_type;
805 __p.clear();
806 return *this;
809 inline path&
810 path::operator=(string_type&& __source)
811 { return *this = path(std::move(__source)); }
813 inline path&
814 path::assign(string_type&& __source)
815 { return *this = path(std::move(__source)); }
817 inline path&
818 path::operator+=(const path& __p)
820 return operator+=(__p.native());
823 inline path&
824 path::operator+=(const string_type& __x)
826 _M_pathname += __x;
827 _M_split_cmpts();
828 return *this;
831 inline path&
832 path::operator+=(const value_type* __x)
834 _M_pathname += __x;
835 _M_split_cmpts();
836 return *this;
839 inline path&
840 path::operator+=(value_type __x)
842 _M_pathname += __x;
843 _M_split_cmpts();
844 return *this;
847 #if __cplusplus >= 201402L
848 inline path&
849 path::operator+=(basic_string_view<value_type> __x)
851 _M_pathname.append(__x.data(), __x.size());
852 _M_split_cmpts();
853 return *this;
855 #endif
857 template<typename _CharT>
858 inline path::_Path<_CharT*, _CharT*>&
859 path::operator+=(_CharT __x)
861 auto* __addr = std::__addressof(__x);
862 return concat(__addr, __addr + 1);
865 inline path&
866 path::make_preferred()
868 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
869 std::replace(_M_pathname.begin(), _M_pathname.end(), L'/',
870 preferred_separator);
871 #endif
872 return *this;
875 inline void path::swap(path& __rhs) noexcept
877 _M_pathname.swap(__rhs._M_pathname);
878 _M_cmpts.swap(__rhs._M_cmpts);
879 std::swap(_M_type, __rhs._M_type);
882 template<typename _CharT, typename _Traits, typename _Allocator>
883 inline std::basic_string<_CharT, _Traits, _Allocator>
884 path::string(const _Allocator& __a) const
886 if (is_same<_CharT, value_type>::value)
887 return { _M_pathname.begin(), _M_pathname.end(), __a };
889 const value_type* __first = _M_pathname.data();
890 const value_type* __last = __first + _M_pathname.size();
892 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
893 using _CharAlloc = __alloc_rebind<_Allocator, char>;
894 using _String = basic_string<char, char_traits<char>, _CharAlloc>;
895 using _WString = basic_string<_CharT, _Traits, _Allocator>;
897 // use codecvt_utf8<wchar_t> to convert native string to UTF-8
898 codecvt_utf8<value_type> __cvt;
899 _String __u8str{_CharAlloc{__a}};
900 if (__str_codecvt_out(__first, __last, __u8str, __cvt))
902 struct
904 const _String*
905 operator()(const _String& __from, _String&, true_type)
906 { return std::__addressof(__from); }
908 _WString*
909 operator()(const _String& __from, _WString& __to, false_type)
911 #ifdef _GLIBCXX_USE_CHAR8_T
912 if constexpr (is_same<_CharT, char8_t>::value)
914 __to.assign(__from.begin(), __from.end());
915 return std::__addressof(__to);
917 else
919 #endif
920 // use codecvt_utf8<_CharT> to convert UTF-8 to wide string
921 codecvt_utf8<_CharT> __cvt;
922 const char* __f = __from.data();
923 const char* __l = __f + __from.size();
924 if (__str_codecvt_in(__f, __l, __to, __cvt))
925 return std::__addressof(__to);
926 #ifdef _GLIBCXX_USE_CHAR8_T
928 #endif
929 return nullptr;
931 } __dispatch;
932 _WString __wstr;
933 if (auto* __p = __dispatch(__u8str, __wstr, is_same<_CharT, char>{}))
934 return *__p;
936 #else
937 #ifdef _GLIBCXX_USE_CHAR8_T
938 if constexpr (is_same<_CharT, char8_t>::value)
940 basic_string<_CharT, _Traits, _Allocator> __wstr{__first, __last, __a};
941 return __wstr;
943 else
945 #endif
946 codecvt_utf8<_CharT> __cvt;
947 basic_string<_CharT, _Traits, _Allocator> __wstr{__a};
948 if (__str_codecvt_in(__first, __last, __wstr, __cvt))
949 return __wstr;
950 #ifdef _GLIBCXX_USE_CHAR8_T
952 #endif
953 #endif
954 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
955 "Cannot convert character sequence",
956 std::make_error_code(errc::illegal_byte_sequence)));
959 inline std::string
960 path::string() const { return string<char>(); }
962 #if _GLIBCXX_USE_WCHAR_T
963 inline std::wstring
964 path::wstring() const { return string<wchar_t>(); }
965 #endif
967 #ifdef _GLIBCXX_USE_CHAR8_T
968 inline std::u8string
969 path::u8string() const { return string<char8_t>(); }
970 #else
971 inline std::string
972 path::u8string() const
974 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
975 std::string __str;
976 // convert from native encoding to UTF-8
977 codecvt_utf8<value_type> __cvt;
978 const value_type* __first = _M_pathname.data();
979 const value_type* __last = __first + _M_pathname.size();
980 if (__str_codecvt_out(__first, __last, __str, __cvt))
981 return __str;
982 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
983 "Cannot convert character sequence",
984 std::make_error_code(errc::illegal_byte_sequence)));
985 #else
986 return _M_pathname;
987 #endif
989 #endif // _GLIBCXX_USE_CHAR8_T
991 inline std::u16string
992 path::u16string() const { return string<char16_t>(); }
994 inline std::u32string
995 path::u32string() const { return string<char32_t>(); }
997 #ifndef _GLIBCXX_FILESYSTEM_IS_WINDOWS
998 template<typename _CharT, typename _Traits, typename _Allocator>
999 inline std::basic_string<_CharT, _Traits, _Allocator>
1000 path::generic_string(const _Allocator& __a) const
1001 { return string<_CharT, _Traits, _Allocator>(__a); }
1003 inline std::string
1004 path::generic_string() const { return string(); }
1006 #if _GLIBCXX_USE_WCHAR_T
1007 inline std::wstring
1008 path::generic_wstring() const { return wstring(); }
1009 #endif
1011 #ifdef _GLIBCXX_USE_CHAR8_T
1012 inline std::u8string
1013 path::generic_u8string() const { return u8string(); }
1014 #else
1015 inline std::string
1016 path::generic_u8string() const { return u8string(); }
1017 #endif
1019 inline std::u16string
1020 path::generic_u16string() const { return u16string(); }
1022 inline std::u32string
1023 path::generic_u32string() const { return u32string(); }
1024 #endif
1026 inline int
1027 path::compare(const string_type& __s) const { return compare(path(__s)); }
1029 inline int
1030 path::compare(const value_type* __s) const { return compare(path(__s)); }
1032 #if __cplusplus >= 201402L
1033 inline int
1034 path::compare(basic_string_view<value_type> __s) const
1035 { return compare(path(__s)); }
1036 #endif
1038 inline path
1039 path::filename() const { return empty() ? path() : *--end(); }
1041 inline path
1042 path::stem() const
1044 auto ext = _M_find_extension();
1045 if (ext.first && ext.second != 0)
1046 return path{ext.first->substr(0, ext.second)};
1047 return {};
1050 inline path
1051 path::extension() const
1053 auto ext = _M_find_extension();
1054 if (ext.first && ext.second != string_type::npos)
1055 return path{ext.first->substr(ext.second)};
1056 return {};
1059 inline bool
1060 path::has_stem() const
1062 auto ext = _M_find_extension();
1063 return ext.first && ext.second != 0;
1066 inline bool
1067 path::has_extension() const
1069 auto ext = _M_find_extension();
1070 return ext.first && ext.second != string_type::npos;
1073 inline bool
1074 path::is_absolute() const
1076 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1077 return has_root_name() && has_root_directory();
1078 #else
1079 return has_root_directory();
1080 #endif
1083 inline path::iterator
1084 path::begin() const
1086 if (_M_type == _Type::_Multi)
1087 return iterator(this, _M_cmpts.begin());
1088 return iterator(this, false);
1091 inline path::iterator
1092 path::end() const
1094 if (_M_type == _Type::_Multi)
1095 return iterator(this, _M_cmpts.end());
1096 return iterator(this, true);
1099 inline path::iterator&
1100 path::iterator::operator++()
1102 __glibcxx_assert(_M_path != nullptr);
1103 if (_M_path->_M_type == _Type::_Multi)
1105 __glibcxx_assert(_M_cur != _M_path->_M_cmpts.end());
1106 ++_M_cur;
1108 else
1110 __glibcxx_assert(!_M_at_end);
1111 _M_at_end = true;
1113 return *this;
1116 inline path::iterator&
1117 path::iterator::operator--()
1119 __glibcxx_assert(_M_path != nullptr);
1120 if (_M_path->_M_type == _Type::_Multi)
1122 __glibcxx_assert(_M_cur != _M_path->_M_cmpts.begin());
1123 --_M_cur;
1125 else
1127 __glibcxx_assert(_M_at_end);
1128 _M_at_end = false;
1130 return *this;
1133 inline path::iterator::reference
1134 path::iterator::operator*() const
1136 __glibcxx_assert(_M_path != nullptr);
1137 if (_M_path->_M_type == _Type::_Multi)
1139 __glibcxx_assert(_M_cur != _M_path->_M_cmpts.end());
1140 return *_M_cur;
1142 return *_M_path;
1145 inline bool
1146 path::iterator::_M_equals(iterator __rhs) const
1148 if (_M_path != __rhs._M_path)
1149 return false;
1150 if (_M_path == nullptr)
1151 return true;
1152 if (_M_path->_M_type == path::_Type::_Multi)
1153 return _M_cur == __rhs._M_cur;
1154 return _M_at_end == __rhs._M_at_end;
1157 // @} group filesystem-ts
1158 _GLIBCXX_END_NAMESPACE_CXX11
1159 } // namespace v1
1160 } // namespace filesystem
1161 } // namespace experimental
1163 _GLIBCXX_END_NAMESPACE_VERSION
1164 } // namespace std
1166 #endif // C++11
1168 #endif // _GLIBCXX_EXPERIMENTAL_FS_PATH_H