From de5adcd8bf7ad5cf4de8fe6db4af917e1449e536 Mon Sep 17 00:00:00 2001 From: redi Date: Wed, 6 Jun 2018 06:05:07 +0000 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. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@261227 138bc75d-0d04-0410-961f-82ee72b054a4 --- 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 +- .../standard/char/{quoted.cc => quoted_sv.cc} | 56 +++++++++------------- .../27_io/manipulators/standard/wchar_t/quoted.cc | 2 +- .../standard/wchar_t/{quoted.cc => quoted_sv.cc} | 56 +++++++++------------- 7 files changed, 100 insertions(+), 75 deletions(-) copy libstdc++-v3/testsuite/27_io/manipulators/standard/char/{quoted.cc => quoted_sv.cc} (51%) copy libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/{quoted.cc => quoted_sv.cc} (50%) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 6bdb17b554c..680cb640b26 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 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 + 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 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 @@ -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 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 // . -// 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.cc b/libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted_sv.cc similarity index 51% copy from libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted.cc copy to libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted_sv.cc index 50f56f280d1..512bdf25533 100644 --- a/libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted.cc +++ b/libstdc++-v3/testsuite/27_io/manipulators/standard/char/quoted_sv.cc @@ -1,6 +1,7 @@ -// { dg-do run { target c++14 } } +// { dg-options "-std=gnu++17" } +// { dg-do run { target c++17 } } -// Copyright (C) 2013-2018 Free Software Foundation, Inc. +// 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 @@ -17,9 +18,9 @@ // with this library; see the file COPYING3. If not see // . -// 27.7.6 - Quoted manipulators [quoted.manip] +// C++17 30.7.8 - Quoted manipulators [quoted.manip] -#include +#include #include #include #include @@ -27,11 +28,12 @@ void test01() { - // Basic test from paper. std::stringstream ss; - std::string original = "foolish me"; - std::string round_trip; + 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 ); } @@ -39,35 +41,27 @@ test01() void test02() { - // Test skipws correctness. std::stringstream ss; - ss << std::quoted("Hello Goodbye") << ' ' << 1 << ' ' << 2; - std::string song; - int thing1, thing2; - ss >> std::quoted(song) >> thing1 >> thing2; - VERIFY( song == "Hello Goodbye" ); - VERIFY( thing1 == 1 ); - VERIFY( thing2 == 2 ); + 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() { - // Test read of unquoted string. std::stringstream ss; - ss << "Alpha Omega"; - std::string testit; - ss >> std::quoted(testit); - VERIFY( testit == "Alpha" ); -} - -auto -test04(const std::string& message) -{ - // Test 'const basic_string&' - std::stringstream ss; - ss << "** Error: " << std::quoted(message) << " **"; - return ss.str(); + 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 @@ -76,8 +70,4 @@ main() test01(); test02(); test03(); - auto ss = test04("My biscuits are burnin'!"); - VERIFY( ss == "** Error: \"My biscuits are burnin'!\" **" ); - - return 0; } 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 // . -// 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.cc b/libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted_sv.cc similarity index 50% copy from libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted.cc copy to libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted_sv.cc index 4c5896372f4..59de59c1eaf 100644 --- a/libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted.cc +++ b/libstdc++-v3/testsuite/27_io/manipulators/standard/wchar_t/quoted_sv.cc @@ -1,6 +1,7 @@ -// { dg-do run { target c++14 } } +// { dg-options "-std=gnu++17" } +// { dg-do run { target c++17 } } -// Copyright (C) 2013-2018 Free Software Foundation, Inc. +// 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 @@ -17,9 +18,9 @@ // with this library; see the file COPYING3. If not see // . -// 27.7.6 - Quoted manipulators [quoted.manip] +// C++17 30.7.8 - Quoted manipulators [quoted.manip] -#include +#include #include #include #include @@ -27,11 +28,12 @@ void test01() { - // Basic test from paper. std::wstringstream ss; - std::wstring original = L"foolish me"; - std::wstring round_trip; + 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 ); } @@ -39,35 +41,27 @@ test01() void test02() { - // Test skipws correctness. std::wstringstream ss; - ss << std::quoted(L"Hello Goodbye") << L' ' << 1 << L' ' << 2; - std::wstring song; - int thing1, thing2; - ss >> std::quoted(song) >> thing1 >> thing2; - VERIFY( song == L"Hello Goodbye" ); - VERIFY( thing1 == 1 ); - VERIFY( thing2 == 2 ); + 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() { - // Test read of unquoted string. std::wstringstream ss; - ss << L"Alpha Omega"; - std::wstring testit; - ss >> std::quoted(testit); - VERIFY( testit == L"Alpha" ); -} - -auto -test04(const std::wstring& message) -{ - // Test 'const basic_string&' - std::wstringstream ss; - ss << L"** Error: " << std::quoted(message) << L" **"; - return ss.str(); + 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 @@ -76,8 +70,4 @@ main() test01(); test02(); test03(); - auto ss = test04(L"My biscuits are burnin'!"); - VERIFY( ss == L"** Error: \"My biscuits are burnin'!\" **" ); - - return 0; } -- 2.11.4.GIT