PR libstdc++/86008 add std::quoted support for string_view
authorJonathan Wakely <jwakely@redhat.com>
Wed, 6 Jun 2018 06:05:07 +0000 (07:05 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Wed, 6 Jun 2018 06:05:07 +0000 (07:05 +0100)
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.

From-SVN: r261227

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/quoted_string.h
libstdc++-v3/include/std/iomanip
libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted.cc
libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted_sv.cc [new file with mode: 0644]
libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted.cc
libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted_sv.cc [new file with mode: 0644]

index 6bdb17b554c8eb2ede00b67cb28b1df7a69e96b9..680cb640b2612db9d9f9e78f5f3f69e22f814ca9 100644 (file)
@@ -1,3 +1,18 @@
+2018-06-06  Jonathan Wakely  <jwakely@redhat.com>
+
+       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.
+
 2018-06-05  Jonathan Wakely  <jwakely@redhat.com>
 
        * include/std/type_traits: Fix comment typos.
index b6e707a8a6e485da3964d4f0813d1d6d5beae717..f6134da5a432c4b8f7efdc5bd6e274e5b83063e0 100644 (file)
@@ -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;
index db8670ed56adf95b16a17c5928e3fae88d9e6c95..9c1f63edb618d00ce9e101c1124c8fe0de918125 100644 (file)
@@ -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
 
index 50f56f280d160afde1f397a0209c96930e64c0b6..7d1da6b731e013fe2d9685d6ea910a18707f271b 100644 (file)
@@ -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 (file)
index 0000000..512bdf2
--- /dev/null
@@ -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();
+}
index 4c5896372f418c919ceb70a8dd12bda60fca941a..d8b05037aaed676ef15d74f98ba82f0efed6daf9 100644 (file)
@@ -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 (file)
index 0000000..59de59c
--- /dev/null
@@ -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();
+}