[PATCH] Make filesystem::path work with basic_string_view (P0392R0)

Jonathan Wakely jwakely@redhat.com
Fri Oct 28 18:50:00 GMT 2016


This integrates string_view with experimental::filesystem::path, which
is not actually in the TS, but is required for std::filesystem. I've
made it work with experimental::string_view in C++14 mode, and with
std::string_view in C++17 mode. I suppose I could have made it work
with either in C++17 mode, but that would be a bit more complicated.

	* 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.

Tested x86_64-linux, committed to trunk.

-------------- next part --------------
commit d554b7a9a7cad560a4e483bd6fbb900d91da2655
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Oct 28 19:30:29 2016 +0100

    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.

diff --git a/libstdc++-v3/include/experimental/bits/fs_path.h b/libstdc++-v3/include/experimental/bits/fs_path.h
index f6a290d..70a5445 100644
--- a/libstdc++-v3/include/experimental/bits/fs_path.h
+++ b/libstdc++-v3/include/experimental/bits/fs_path.h
@@ -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(); }
 
diff --git a/libstdc++-v3/testsuite/experimental/filesystem/path/construct/string_view.cc b/libstdc++-v3/testsuite/experimental/filesystem/path/construct/string_view.cc
new file mode 100644
index 0000000..13ebaaa
--- /dev/null
+++ b/libstdc++-v3/testsuite/experimental/filesystem/path/construct/string_view.cc
@@ -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();
+}


More information about the Libstdc++ mailing list