inserters_extractors.cc (test09): New test.

2001-05-24  Benjamin Kosnik  <bkoz@redhat.com>

	libstdc++/2830
	* testsuite/21_strings/inserters_extractors.cc (test09): New test.
	* include/bits/ostream.tcc: Format to match istream.tcc.
	(operator<<(basic_ostream __out, const basic_string __s)): Fix.

From-SVN: r42557
This commit is contained in:
Benjamin Kosnik 2001-05-25 04:24:12 +00:00 committed by Benjamin Kosnik
parent 1044b043c2
commit a16ad77946
3 changed files with 314 additions and 235 deletions

View File

@ -1,3 +1,10 @@
2001-05-24 Benjamin Kosnik <bkoz@redhat.com>
libstdc++/2830
* testsuite/21_strings/inserters_extractors.cc (test09): New test.
* include/bits/ostream.tcc: Format to match istream.tcc.
(operator<<(basic_ostream __out, const basic_string __s)): Fix.
2001-05-24 Phil Edwards <pme@sources.redhat.com> 2001-05-24 Phil Edwards <pme@sources.redhat.com>
* libsupc++/eh_alloc.cc (__cxa_allocate_exception): Qualify * libsupc++/eh_alloc.cc (__cxa_allocate_exception): Qualify

View File

@ -31,8 +31,8 @@
#include <bits/std_locale.h> #include <bits/std_locale.h>
namespace std { namespace std
{
template<typename _CharT, typename _Traits> template<typename _CharT, typename _Traits>
basic_ostream<_CharT, _Traits>::sentry:: basic_ostream<_CharT, _Traits>::sentry::
sentry(basic_ostream<_CharT,_Traits>& __os) sentry(basic_ostream<_CharT,_Traits>& __os)
@ -51,10 +51,10 @@ namespace std {
sentry __cerb(*this); sentry __cerb(*this);
if (__cerb) if (__cerb)
{ {
try { try
__pf(*this); { __pf(*this); }
} catch(exception& __fail)
catch(exception& __fail){ {
// 27.6.2.5.1 Common requirements. // 27.6.2.5.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown. // Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
@ -73,10 +73,10 @@ namespace std {
sentry __cerb(*this); sentry __cerb(*this);
if (__cerb) if (__cerb)
{ {
try { try
__pf(*this); { __pf(*this); }
} catch(exception& __fail)
catch(exception& __fail){ {
// 27.6.2.5.1 Common requirements. // 27.6.2.5.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown. // Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
@ -95,10 +95,10 @@ namespace std {
sentry __cerb(*this); sentry __cerb(*this);
if (__cerb) if (__cerb)
{ {
try { try
__pf(*this); { __pf(*this); }
} catch(exception& __fail)
catch(exception& __fail){ {
// 27.6.2.5.1 Common requirements. // 27.6.2.5.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown. // Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
@ -116,11 +116,13 @@ namespace std {
sentry __cerb(*this); sentry __cerb(*this);
if (__cerb) if (__cerb)
{ {
try { try
{
if (_M_fnumput->put(*this, *this, this->fill(), __n).failed()) if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
} }
catch(exception& __fail){ catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements. // 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown. // Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
@ -138,7 +140,8 @@ namespace std {
sentry __cerb(*this); sentry __cerb(*this);
if (__cerb) if (__cerb)
{ {
try { try
{
bool __f; bool __f;
ios_base::fmtflags __fmt = this->flags() & ios_base::basefield; ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
if (__fmt & ios_base::oct || __fmt & ios_base::hex) if (__fmt & ios_base::oct || __fmt & ios_base::hex)
@ -150,7 +153,8 @@ namespace std {
if (__f) if (__f)
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
} }
catch(exception& __fail){ catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements. // 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown. // Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
@ -168,11 +172,13 @@ namespace std {
sentry __cerb(*this); sentry __cerb(*this);
if (__cerb) if (__cerb)
{ {
try { try
{
if (_M_fnumput->put(*this, *this, this->fill(), __n).failed()) if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
} }
catch(exception& __fail){ catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements. // 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown. // Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
@ -191,7 +197,8 @@ namespace std {
sentry __cerb(*this); sentry __cerb(*this);
if (__cerb) if (__cerb)
{ {
try { try
{
bool __f; bool __f;
ios_base::fmtflags __fmt = this->flags() & ios_base::basefield; ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
if (__fmt & ios_base::oct || __fmt & ios_base::hex) if (__fmt & ios_base::oct || __fmt & ios_base::hex)
@ -199,9 +206,11 @@ namespace std {
static_cast<unsigned long long>(__n)).failed(); static_cast<unsigned long long>(__n)).failed();
else else
__f = _M_fnumput->put(*this, *this, this->fill(), __n).failed(); __f = _M_fnumput->put(*this, *this, this->fill(), __n).failed();
if (__f)
this->setstate(ios_base::badbit);
} }
catch(exception& __fail){ catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements. // 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown. // Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
@ -219,11 +228,13 @@ namespace std {
sentry __cerb(*this); sentry __cerb(*this);
if (__cerb) if (__cerb)
{ {
try { try
{
if (_M_fnumput->put(*this, *this, this->fill(), __n).failed()) if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
} }
catch(exception& __fail){ catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements. // 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown. // Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
@ -242,11 +253,13 @@ namespace std {
sentry __cerb(*this); sentry __cerb(*this);
if (__cerb) if (__cerb)
{ {
try { try
{
if (_M_fnumput->put(*this, *this, this->fill(), __n).failed()) if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
} }
catch(exception& __fail){ catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements. // 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown. // Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
@ -264,11 +277,13 @@ namespace std {
sentry __cerb(*this); sentry __cerb(*this);
if (__cerb) if (__cerb)
{ {
try { try
{
if (_M_fnumput->put(*this, *this, this->fill(), __n).failed()) if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
} }
catch(exception& __fail){ catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements. // 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown. // Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
@ -286,11 +301,13 @@ namespace std {
sentry __cerb(*this); sentry __cerb(*this);
if (__cerb) if (__cerb)
{ {
try { try
{
if (_M_fnumput->put(*this, *this, this->fill(), __n).failed()) if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
} }
catch(exception& __fail){ catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements. // 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown. // Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
@ -393,7 +410,7 @@ namespace std {
if (__testok) if (__testok)
#ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
// 136. seekp, seekg setting wrong streams? // 136. seekp, seekg setting wrong streams?
rdbuf()->pubseekoff(__off, __d, ios_base::out); this->rdbuf()->pubseekoff(__off, __d, ios_base::out);
#endif #endif
return *this; return *this;
} }
@ -494,7 +511,8 @@ namespace std {
typename __ostream_type::sentry __cerb(__out); typename __ostream_type::sentry __cerb(__out);
if (__cerb) if (__cerb)
{ {
try { try
{
streamsize __w = __out.width(); streamsize __w = __out.width();
_CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w)); _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
__pads[0] = __c; __pads[0] = __c;
@ -507,7 +525,8 @@ namespace std {
__out.write(__pads, __len); __out.write(__pads, __len);
__out.width(0); __out.width(0);
} }
catch(exception& __fail){ catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements. // 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown. // Turn this on without causing an ios::failure to be thrown.
__out.setstate(ios_base::badbit); __out.setstate(ios_base::badbit);
@ -527,7 +546,8 @@ namespace std {
typename __ostream_type::sentry __cerb(__out); typename __ostream_type::sentry __cerb(__out);
if (__cerb) if (__cerb)
{ {
try { try
{
streamsize __w = __out.width(); streamsize __w = __out.width();
char* __pads = static_cast<char*>(__builtin_alloca(__w + 1)); char* __pads = static_cast<char*>(__builtin_alloca(__w + 1));
__pads[0] = __c; __pads[0] = __c;
@ -540,7 +560,8 @@ namespace std {
__out.write(__pads, __len); __out.write(__pads, __len);
__out.width(0); __out.width(0);
} }
catch(exception& __fail){ catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements. // 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown. // Turn this on without causing an ios::failure to be thrown.
__out.setstate(ios_base::badbit); __out.setstate(ios_base::badbit);
@ -559,7 +580,8 @@ namespace std {
typename __ostream_type::sentry __cerb(__out); typename __ostream_type::sentry __cerb(__out);
if (__cerb) if (__cerb)
{ {
try { try
{
streamsize __w = __out.width(); streamsize __w = __out.width();
_CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w)); _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
streamsize __len = static_cast<streamsize>(_Traits::length(__s)); streamsize __len = static_cast<streamsize>(_Traits::length(__s));
@ -572,7 +594,8 @@ namespace std {
__out.write(__s, __len); __out.write(__s, __len);
__out.width(0); __out.width(0);
} }
catch(exception& __fail){ catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements. // 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown. // Turn this on without causing an ios::failure to be thrown.
__out.setstate(ios_base::badbit); __out.setstate(ios_base::badbit);
@ -601,7 +624,8 @@ namespace std {
__ws[__i] = __out.widen(__s[__i]); __ws[__i] = __out.widen(__s[__i]);
_CharT* __str = __ws; _CharT* __str = __ws;
try { try
{
streamsize __len = static_cast<streamsize>(__clen); streamsize __len = static_cast<streamsize>(__clen);
streamsize __w = __out.width(); streamsize __w = __out.width();
_CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w)); _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
@ -615,7 +639,8 @@ namespace std {
__out.write(__str, __len); __out.write(__str, __len);
__out.width(0); __out.width(0);
} }
catch(exception& __fail){ catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements. // 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown. // Turn this on without causing an ios::failure to be thrown.
__out.setstate(ios_base::badbit); __out.setstate(ios_base::badbit);
@ -635,7 +660,8 @@ namespace std {
typename __ostream_type::sentry __cerb(__out); typename __ostream_type::sentry __cerb(__out);
if (__cerb) if (__cerb)
{ {
try { try
{
streamsize __w = __out.width(); streamsize __w = __out.width();
char* __pads = static_cast<char*>(__builtin_alloca(__w)); char* __pads = static_cast<char*>(__builtin_alloca(__w));
streamsize __len = static_cast<streamsize>(_Traits::length(__s)); streamsize __len = static_cast<streamsize>(_Traits::length(__s));
@ -648,7 +674,8 @@ namespace std {
__out.write(__s, __len); __out.write(__s, __len);
__out.width(0); __out.width(0);
} }
catch(exception& __fail){ catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements. // 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown. // Turn this on without causing an ios::failure to be thrown.
__out.setstate(ios_base::badbit); __out.setstate(ios_base::badbit);
@ -659,13 +686,36 @@ namespace std {
return __out; return __out;
} }
// 21.3.7.8 basic_string::operator<< // 21.3.7.9 basic_string::operator<<
template<typename _CharT, typename _Traits, typename _Alloc> template<typename _CharT, typename _Traits, typename _Alloc>
basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __out, operator<<(basic_ostream<_CharT, _Traits>& __out,
const basic_string<_CharT, _Traits, _Alloc>& __s) const basic_string<_CharT, _Traits, _Alloc>& __str)
{ return (__out << __s.c_str()); } {
typedef basic_ostream<_CharT, _Traits> __ostream_type;
typename __ostream_type::sentry __cerb(__out);
if (__cerb)
{
const _CharT* __s = __str.data();
streamsize __w = __out.width();
_CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
streamsize __len = static_cast<streamsize>(__str.size());
#ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
// 25. String operator<< uses width() value wrong
#endif
if (__w > __len)
{
__pad_char(__out, __pads, __s, __w, __len);
__s = __pads;
__len = __w;
}
streamsize __res = __out.rdbuf()->sputn(__s, __len);
__out.width(0);
if (__res != __len)
__out.setstate(ios_base::failbit);
}
return __out;
}
} // namespace std } // namespace std
// Local Variables: // Local Variables:

View File

@ -304,6 +304,26 @@ void test08()
VERIFY( year == 2001 ); VERIFY( year == 2001 );
} }
// libstdc++/2830
void test09()
{
bool test = true;
std::string blanks( 3, '\0');
std::string foo = "peace";
foo += blanks;
foo += "& love";
std::ostringstream oss1;
oss1 << foo;
VERIFY( oss1.str() == foo );
std::ostringstream oss2;
oss2.width(20);
oss2 << foo;
VERIFY( oss2.str() != foo );
VERIFY( oss2.str().size() == 20 );
}
int main() int main()
{ {
test01(); test01();
@ -320,5 +340,7 @@ int main()
test07(); test07();
test08(); test08();
test09();
return 0; return 0;
} }