From 7db54ccd8a2b8a2c89f9cf38f716974aac27fa62 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Wed, 6 Jun 2018 07:05:07 +0100 Subject: [PATCH] PR libstdc++/86008 add std::quoted support for string_view PR libstdc++/86008 * include/bits/quoted_string.h (_Quoted_string): Define new partial specialization. * include/std/iomanip (quoted(basic_string_view, C, C)): Define new overload. (operator<<(basic_ostream&, const _Quoted_string&)): 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 | 15 +++++ libstdc++-v3/include/bits/quoted_string.h | 20 +++++- libstdc++-v3/include/std/iomanip | 24 +++++-- .../27_io/manipulators/standard/char/quoted.cc | 2 +- .../27_io/manipulators/standard/char/quoted_sv.cc | 73 ++++++++++++++++++++++ .../27_io/manipulators/standard/wchar_t/quoted.cc | 2 +- .../manipulators/standard/wchar_t/quoted_sv.cc | 73 ++++++++++++++++++++++ 7 files changed, 200 insertions(+), 9 deletions(-) create mode 100644 libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted_sv.cc create mode 100644 libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted_sv.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 6bdb17b..680cb64 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,18 @@ +2018-06-06 Jonathan Wakely + + PR libstdc++/86008 + * include/bits/quoted_string.h (_Quoted_string): + Define new partial specialization. + * include/std/iomanip (quoted(basic_string_view, C, C)): Define + new overload. + (operator<<(basic_ostream&, const _Quoted_string&)): 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 * include/std/type_traits: Fix comment typos. diff --git a/libstdc++-v3/include/bits/quoted_string.h b/libstdc++-v3/include/bits/quoted_string.h index b6e707a..f6134da5 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 + struct _Quoted_string, _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 db8670e..9c1f63e 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 @@ -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 + 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 50f56f2..7d1da6b 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 // . -// 27.7.6 - Quoted manipulators [quoted.manip] +// C++14 27.7.6 - Quoted manipulators [quoted.manip] #include #include 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 0000000..512bdf2 --- /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 +// . + +// C++17 30.7.8 - Quoted manipulators [quoted.manip] + +#include +#include +#include +#include + +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 4c58963..d8b0503 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 // . -// 27.7.6 - Quoted manipulators [quoted.manip] +// C++14 27.7.6 - Quoted manipulators [quoted.manip] #include #include 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 0000000..59de59c --- /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 +// . + +// C++17 30.7.8 - Quoted manipulators [quoted.manip] + +#include +#include +#include +#include + +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(); +} -- 2.7.4