libstdc++: add range constructor for std::string_view (P1391R4)
diff mbox series

Message ID 20191117013317.GA20202@redhat.com
State New
Headers show
Series
  • libstdc++: add range constructor for std::string_view (P1391R4)
Related show

Commit Message

Jonathan Wakely Nov. 17, 2019, 1:33 a.m. UTC
* include/std/string_view (basic_string_view(It, End)): Add range
	constructor and deduction guide from P1391R4.
	* testsuite/21_strings/basic_string_view/cons/char/range.cc: New test.

Tested powerpc64le-linux, committed to trunk.
commit e0c554e4da7310df83bb1dcc7b8e6c4c9c5a2a4f
Author: redi <redi@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Sun Nov 17 01:32:55 2019 +0000

    libstdc++: add range constructor for std::string_view (P1391R4)
    
            * include/std/string_view (basic_string_view(It, End)): Add range
            constructor and deduction guide from P1391R4.
            * testsuite/21_strings/basic_string_view/cons/char/range.cc: New test.
    
    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@278371 138bc75d-0d04-0410-961f-82ee72b054a4

Patch
diff mbox series

diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view
index ff1c0c3f36f..9d2a8e8e0c2 100644
--- a/libstdc++-v3/include/std/string_view
+++ b/libstdc++-v3/include/std/string_view
@@ -130,6 +130,16 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       : _M_len{__len}, _M_str{__str}
       { }
 
+#if __cplusplus > 201703L && __cpp_lib_concepts
+      template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
+	requires same_as<iter_value_t<_It>, _CharT>
+	  && (!convertible_to<_End, size_type>)
+	constexpr
+	basic_string_view(_It __first, _End __last)
+	: _M_len(__last - __first), _M_str(std::to_address(__first))
+	{ }
+#endif
+
       constexpr basic_string_view&
       operator=(const basic_string_view&) noexcept = default;
 
@@ -457,6 +467,11 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       const _CharT* _M_str;
     };
 
+#if __cplusplus > 201703L && __cpp_lib_concepts && __cpp_deduction_guides
+  template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
+    basic_string_view(_It, _End) -> basic_string_view<iter_value_t<_It>>;
+#endif
+
   // [string.view.comparison], non-member basic_string_view comparison function
 
   // Several of these functions use type_identity_t to create a non-deduced
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/range.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/range.cc
new file mode 100644
index 00000000000..d554b77c874
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/range.cc
@@ -0,0 +1,42 @@ 
+// Copyright (C) 2019 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/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <string_view>
+#include <vector>
+#include <testsuite_hooks.h>
+
+constexpr char str[] = "abcdefg";
+constexpr std::basic_string_view s(std::begin(str), std::cend(str) - 1);
+static_assert( s == str );
+static_assert( s.data() == str );
+
+void
+test01()
+{
+  std::vector<char> v{'a', 'b', 'c'};
+  std::basic_string_view s(v.begin(), v.end());
+  VERIFY( s.data() == v.data() );
+}
+
+int
+main()
+{
+  test01();
+}