PR libstdc++/86008 add std::quoted support for string_view

Message ID 20180606060453.GA17988@redhat.com
State New
Headers show
Series
  • PR libstdc++/86008 add std::quoted support for string_view
Related show

Commit Message

Jonathan Wakely June 6, 2018, 6:04 a.m.
PR libstdc++/86008
	* include/bits/quoted_string.h (_Quoted_string<basic_string_view, C>):
	Define new partial specialization.
	* include/std/iomanip (quoted(basic_string_view<C,T>, C, C)): Define
	new overload.
	(operator<<(basic_ostream<C,T>&, const _Quoted_string<S,C>&)): Use
	value not reference for iteration.
	* testsuite/27_io/manipulators/standard/char/quoted.cc: Adjust
	comment.
	* testsuite/27_io/manipulators/standard/char/quoted_sv.cc: New test.
	* testsuite/27_io/manipulators/standard/wchar_t/quoted.cc: Adjust
	comment.

Tested powerpc64le-linux, committed to trunk.
commit 3914358048981798c18801b9885576529534d2bd
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Jun 5 14:28:12 2018 +0100

    PR libstdc++/86008 add std::quoted support for string_view
    
            PR libstdc++/86008
            * include/bits/quoted_string.h (_Quoted_string<basic_string_view, C>):
            Define new partial specialization.
            * include/std/iomanip (quoted(basic_string_view<C,T>, C, C)): Define
            new overload.
            (operator<<(basic_ostream<C,T>&, const _Quoted_string<S,C>&)): Use
            value not reference for iteration.
            * testsuite/27_io/manipulators/standard/char/quoted.cc: Adjust
            comment.
            * testsuite/27_io/manipulators/standard/char/quoted_sv.cc: New test.
            * testsuite/27_io/manipulators/standard/wchar_t/quoted.cc: Adjust
            comment.

Patch

diff --git a/libstdc++-v3/include/bits/quoted_string.h b/libstdc++-v3/include/bits/quoted_string.h
index b6e707a8a6e..f6134da5a43 100644
--- a/libstdc++-v3/include/bits/quoted_string.h
+++ b/libstdc++-v3/include/bits/quoted_string.h
@@ -64,6 +64,24 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	_CharT _M_escape;
       };
 
+#if __cplusplus >= 201703L
+    template<typename _CharT, typename _Traits>
+      struct _Quoted_string<basic_string_view<_CharT, _Traits>, _CharT>
+      {
+	_Quoted_string(basic_string_view<_CharT, _Traits> __str,
+		       _CharT __del, _CharT __esc)
+	: _M_string(__str), _M_delim{__del}, _M_escape{__esc}
+	{ }
+
+	_Quoted_string&
+	operator=(_Quoted_string&) = delete;
+
+	basic_string_view<_CharT, _Traits> _M_string;
+	_CharT _M_delim;
+	_CharT _M_escape;
+      };
+#endif // C++17
+
     /**
      * @brief Inserter for quoted strings.
      *
@@ -101,7 +119,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       {
 	std::basic_ostringstream<_CharT, _Traits> __ostr;
 	__ostr << __str._M_delim;
-	for (auto& __c : __str._M_string)
+	for (auto __c : __str._M_string)
 	  {
 	    if (__c == __str._M_delim || __c == __str._M_escape)
 	      __ostr << __str._M_escape;
diff --git a/libstdc++-v3/include/std/iomanip b/libstdc++-v3/include/std/iomanip
index db8670ed56a..9c1f63edb61 100644
--- a/libstdc++-v3/include/std/iomanip
+++ b/libstdc++-v3/include/std/iomanip
@@ -446,7 +446,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       return __is;
     }
 
-#if __cplusplus > 201103L
+#if __cplusplus >= 201402L
 
 #define __cpp_lib_quoted_string_io 201304
 
@@ -471,8 +471,8 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	   _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
     {
       return __detail::_Quoted_string<
-			const basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
-				__string, __delim, __escape);
+	const basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
+	    __string, __delim, __escape);
     }
 
   template<typename _CharT, typename _Traits, typename _Alloc>
@@ -481,11 +481,23 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	   _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
     {
       return __detail::_Quoted_string<
-			basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
-				__string, __delim, __escape);
+	basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
+	    __string, __delim, __escape);
     }
 
-#endif // __cplusplus > 201103L
+#if __cplusplus >= 201703L
+  // _GLIBCXX_RESOLVE_LIB_DEFECTS
+  // 2785. quoted should work with basic_string_view
+  template<typename _CharT, typename _Traits>
+    inline auto
+    quoted(basic_string_view<_CharT, _Traits> __sv,
+	   _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
+    {
+      return __detail::_Quoted_string<
+	basic_string_view<_CharT, _Traits>, _CharT>(__sv, __delim, __escape);
+    }
+#endif // C++17
+#endif // C++14
 
 #endif // __cplusplus >= 201103L
 
diff --git a/libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted.cc b/libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted.cc
index 50f56f280d1..7d1da6b731e 100644
--- a/libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted.cc
+++ b/libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted.cc
@@ -17,7 +17,7 @@ 
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// 27.7.6 - Quoted manipulators		[quoted.manip]
+// C++14 27.7.6 - Quoted manipulators		[quoted.manip]
 
 #include <string>
 #include <sstream>
diff --git a/libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted_sv.cc b/libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted_sv.cc
new file mode 100644
index 00000000000..512bdf25533
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted_sv.cc
@@ -0,0 +1,73 @@ 
+// { dg-options "-std=gnu++17" }
+// { dg-do run { target c++17 } }
+
+// Copyright (C) 2018 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/>.
+
+// C++17 30.7.8 - Quoted manipulators		[quoted.manip]
+
+#include <string_view>
+#include <sstream>
+#include <iomanip>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  std::stringstream ss;
+  const std::string_view original = R"(This "string" will be \"quoted\")";
+  std::string raw, round_trip;
+  ss << std::quoted(original);
+  raw = ss.str();
+  VERIFY( raw == R"("This \"string\" will be \\\"quoted\\\"")" );
+  ss >> std::quoted(round_trip);
+  VERIFY( original == round_trip );
+}
+
+void
+test02()
+{
+  std::stringstream ss;
+  const std::string_view original = R"(This "string" will be \"quoted\")";
+  std::string raw, round_trip;
+  ss << std::quoted(original, '\'', '!');
+  raw = ss.str();
+  VERIFY( raw == R"('This "string" will be \"quoted\"')" );
+  ss >> std::quoted(round_trip, '\'', '!');
+  VERIFY( original == round_trip );
+}
+
+void
+test03()
+{
+  std::stringstream ss;
+  const std::string_view original = R"(This 'string' will be !'quoted!')";
+  std::string raw, round_trip;
+  ss << std::quoted(original, '\'', '!');
+  raw = ss.str();
+  VERIFY( raw == R"('This !'string!' will be !!!'quoted!!!'')" );
+  ss >> std::quoted(round_trip, '\'', '!');
+  VERIFY( original == round_trip );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+  test03();
+}
diff --git a/libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted.cc b/libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted.cc
index 4c5896372f4..d8b05037aae 100644
--- a/libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted.cc
+++ b/libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted.cc
@@ -17,7 +17,7 @@ 
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// 27.7.6 - Quoted manipulators		[quoted.manip]
+// C++14 27.7.6 - Quoted manipulators		[quoted.manip]
 
 #include <string>
 #include <sstream>
diff --git a/libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted_sv.cc b/libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted_sv.cc
new file mode 100644
index 00000000000..59de59c1eaf
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted_sv.cc
@@ -0,0 +1,73 @@ 
+// { dg-options "-std=gnu++17" }
+// { dg-do run { target c++17 } }
+
+// Copyright (C) 2018 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/>.
+
+// C++17 30.7.8 - Quoted manipulators		[quoted.manip]
+
+#include <string_view>
+#include <sstream>
+#include <iomanip>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  std::wstringstream ss;
+  const std::wstring_view original = LR"(This "string" will be \"quoted\")";
+  std::wstring raw, round_trip;
+  ss << std::quoted(original);
+  raw = ss.str();
+  VERIFY( raw == LR"("This \"string\" will be \\\"quoted\\\"")" );
+  ss >> std::quoted(round_trip);
+  VERIFY( original == round_trip );
+}
+
+void
+test02()
+{
+  std::wstringstream ss;
+  const std::wstring_view original = LR"(This "string" will be \"quoted\")";
+  std::wstring raw, round_trip;
+  ss << std::quoted(original, L'\'', L'!');
+  raw = ss.str();
+  VERIFY( raw == LR"('This "string" will be \"quoted\"')" );
+  ss >> std::quoted(round_trip, L'\'', L'!');
+  VERIFY( original == round_trip );
+}
+
+void
+test03()
+{
+  std::wstringstream ss;
+  const std::wstring_view original = LR"(This 'string' will be !'quoted!')";
+  std::wstring raw, round_trip;
+  ss << std::quoted(original, L'\'', L'!');
+  raw = ss.str();
+  VERIFY( raw == LR"('This !'string!' will be !!!'quoted!!!'')" );
+  ss >> std::quoted(round_trip, L'\'', L'!');
+  VERIFY( original == round_trip );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+  test03();
+}