Make filesystem::path work with basic_string_view (P0392R0)

* include/experimental/bits/fs_path.h (__is_path_src)
	(_S_range_begin, _S_range_end): Overload to treat string_view as a
	Source object.
	(path::operator+=, path::compare): Overload for basic_string_view.
	* testsuite/experimental/filesystem/path/construct/string_view.cc:
	New test.
	* testsuite/experimental/filesystem/path/construct/
	string_view_cxx17.cc: New test.

From-SVN: r241658
This commit is contained in:
Jonathan Wakely 2016-10-28 19:48:43 +01:00 committed by Jonathan Wakely
parent 4713516712
commit f0414b973f
3 changed files with 114 additions and 0 deletions

View File

@ -1,5 +1,14 @@
2016-10-28 Jonathan Wakely <jwakely@redhat.com>
* include/experimental/bits/fs_path.h (__is_path_src)
(_S_range_begin, _S_range_end): Overload to treat string_view as a
Source object.
(path::operator+=, path::compare): Overload for basic_string_view.
* testsuite/experimental/filesystem/path/construct/string_view.cc:
New test.
* testsuite/experimental/filesystem/path/construct/
string_view_cxx17.cc: New test.
* include/experimental/bits/fs_path.h
(path::_S_convert<_Iter>(_Iter, _Iter)): Remove cv-qualifiers from
iterator's value_type.

View File

@ -44,6 +44,9 @@
#include <bits/stl_algobase.h>
#include <bits/quoted_string.h>
#include <bits/locale_conv.h>
#if __cplusplus == 201402L
# include <experimental/string_view>
#endif
#if defined(_WIN32) && !defined(__CYGWIN__)
# define _GLIBCXX_FILESYSTEM_IS_WINDOWS 1
@ -61,6 +64,12 @@ inline namespace v1
_GLIBCXX_BEGIN_NAMESPACE_VERSION
_GLIBCXX_BEGIN_NAMESPACE_CXX11
#if __cplusplus == 201402L
using std::experimental::basic_string_view;
#elif __cplusplus > 201402L
using std::basic_string_view;
#endif
/**
* @ingroup filesystem
* @{
@ -87,6 +96,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
static __is_encoded_char<_CharT>
__is_path_src(const basic_string<_CharT, _Traits, _Alloc>&, int);
#if __cplusplus >= 201402L
template<typename _CharT, typename _Traits>
static __is_encoded_char<_CharT>
__is_path_src(const basic_string_view<_CharT, _Traits>&, int);
#endif
template<typename _Unknown>
static std::false_type
__is_path_src(const _Unknown&, ...);
@ -130,6 +145,18 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
_S_range_end(const basic_string<_CharT, _Traits, _Alloc>& __str)
{ return __str.data() + __str.size(); }
#if __cplusplus >= 201402L
template<typename _CharT, typename _Traits>
static const _CharT*
_S_range_begin(const basic_string_view<_CharT, _Traits>& __str)
{ return __str.data(); }
template<typename _CharT, typename _Traits>
static const _CharT*
_S_range_end(const basic_string_view<_CharT, _Traits>& __str)
{ return __str.data() + __str.size(); }
#endif
template<typename _Tp,
typename _Iter = decltype(_S_range_begin(std::declval<_Tp>())),
typename _Val = typename std::iterator_traits<_Iter>::value_type>
@ -243,6 +270,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
path& operator+=(const string_type& __x);
path& operator+=(const value_type* __x);
path& operator+=(value_type __x);
#if __cplusplus >= 201402L
path& operator+=(basic_string_view<value_type> __x);
#endif
template<typename _Source>
_Path<_Source>&
@ -311,6 +341,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
int compare(const path& __p) const noexcept;
int compare(const string_type& __s) const;
int compare(const value_type* __s) const;
#if __cplusplus >= 201402L
int compare(const basic_string_view<value_type> __s) const;
#endif
// decomposition
@ -768,6 +801,16 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
return *this;
}
#if __cplusplus >= 201402L
inline path&
path::operator+=(basic_string_view<value_type> __x)
{
_M_pathname.append(__x.data(), __x.size());
_M_split_cmpts();
return *this;
}
#endif
template<typename _CharT>
inline path::_Path<_CharT*, _CharT*>&
path::operator+=(_CharT __x)
@ -909,6 +952,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
inline int
path::compare(const value_type* __s) const { return compare(path(__s)); }
#if __cplusplus >= 201402L
inline int
path::compare(basic_string_view<value_type> __s) const
{ return compare(path(__s)); }
#endif
inline path
path::filename() const { return empty() ? path() : *--end(); }

View File

@ -0,0 +1,56 @@
// { dg-options "-lstdc++fs -std=gnu++1z" }
// { dg-do run { target c++1z } }
// { dg-require-filesystem-ts "" }
// Copyright (C) 2016 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// 8.4.1 path constructors [path.construct]
#include <experimental/filesystem>
#include <string_view>
#include <string>
#include <testsuite_fs.h>
using std::experimental::filesystem::path;
using __gnu_test::compare_paths;
void
test01()
{
for (std::string s : __gnu_test::test_paths)
{
path p1 = s;
std::string_view sv(s);
path p2 = sv;
compare_paths(p1, p2);
#if _GLIBCXX_USE_WCHAR_T
std::wstring ws(s.begin(), s.end());
path p3 = ws;
std::wstring_view wsv(ws);
path p4 = wsv;
compare_paths(p1, p4);
#endif
}
}
int
main()
{
test01();
}