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.

From-SVN: r261227
This commit is contained in:
Jonathan Wakely 2018-06-06 07:05:07 +01:00 committed by Jonathan Wakely
parent 5ae566b665
commit 7db54ccd8a
7 changed files with 200 additions and 9 deletions

View 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.

View 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;

View 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

View 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>

View File

@ -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();
}

View 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>

View File

@ -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();
}