PR libstdc++/81381 support stateful allocators in basic_stringbuf

PR libstdc++/81381
	* include/bits/sstream.tcc (basic_stringbuf::overflow)
	(basic_stringbuf::basic_stringbuf(const __string_type&, ios::mode))
	(basic_stringbuf::str()): Construct new strings with an allocator.
	* testsuite/27_io/basic_stringbuf/cons/81381.cc: New.

From-SVN: r250101
This commit is contained in:
Jonathan Wakely 2017-07-10 18:59:02 +01:00 committed by Jonathan Wakely
parent 93ef155b3d
commit 387c17d312
4 changed files with 54 additions and 5 deletions

View File

@ -1,5 +1,11 @@
2017-07-10 Jonathan Wakely <jwakely@redhat.com>
PR libstdc++/81381
* include/bits/sstream.tcc (basic_stringbuf::overflow)
(basic_stringbuf::basic_stringbuf(const __string_type&, ios::mode))
(basic_stringbuf::str()): Construct new strings with an allocator.
* testsuite/27_io/basic_stringbuf/cons/81381.cc: New.
PR libstdc++/81338
* include/bits/basic_string.h [_GLIBCXX_USE_CXX11_ABI] (basic_string):
Declare basic_stringbuf to be a friend.

View File

@ -129,7 +129,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
const __size_type __opt_len = std::max(__size_type(2 * __capacity),
__size_type(512));
const __size_type __len = std::min(__opt_len, __max_size);
__string_type __tmp;
__string_type __tmp(_M_string.get_allocator());
__tmp.reserve(__len);
if (this->pbase())
__tmp.assign(this->pbase(), this->epptr() - this->pbase());

View File

@ -112,7 +112,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
explicit
basic_stringbuf(const __string_type& __str,
ios_base::openmode __mode = ios_base::in | ios_base::out)
: __streambuf_type(), _M_mode(), _M_string(__str.data(), __str.size())
: __streambuf_type(), _M_mode(),
_M_string(__str.data(), __str.size(), __str.get_allocator())
{ _M_stringbuf_init(__mode); }
#if __cplusplus >= 201103L
@ -165,14 +166,14 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
__string_type
str() const
{
__string_type __ret;
__string_type __ret(_M_string.get_allocator());
if (this->pptr())
{
// The current egptr() may not be the actual string end.
if (this->pptr() > this->egptr())
__ret = __string_type(this->pbase(), this->pptr());
__ret.assign(this->pbase(), this->pptr());
else
__ret = __string_type(this->pbase(), this->egptr());
__ret.assign(this->pbase(), this->egptr());
}
else
__ret = _M_string;

View File

@ -0,0 +1,42 @@
// Copyright (C) 2017 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/>.
// { dg-do run { target c++11 } }
// PR libstdc++/81381
#include <memory>
#include <sstream>
#include <testsuite_allocator.h>
using Alloc = __gnu_test::uneq_allocator<char>;
using String = std::basic_string<char, std::char_traits<char>, Alloc>;
struct SB : std::basic_stringbuf<char, std::char_traits<char>, Alloc>
{
SB(const String& s) : basic_stringbuf(s) { }
using basic_stringbuf::overflow;
};
int main()
{
String s(Alloc(23));
SB b(s);
b.overflow('a');
VERIFY( b.str().get_allocator() == s.get_allocator() );
}