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,16 +51,16 @@ 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);
if ((this->exceptions() & ios_base::badbit) != 0) if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again; __throw_exception_again;
} }
} }
return *this; return *this;
} }
@ -73,16 +73,16 @@ 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);
if ((this->exceptions() & ios_base::badbit) != 0) if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again; __throw_exception_again;
} }
} }
return *this; return *this;
} }
@ -95,16 +95,16 @@ 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);
if ((this->exceptions() & ios_base::badbit) != 0) if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again; __throw_exception_again;
} }
} }
return *this; return *this;
} }
@ -116,17 +116,19 @@ 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);
}
catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
} if ((this->exceptions() & ios_base::badbit) != 0)
catch(exception& __fail){ __throw_exception_again;
// 27.6.1.2.1 Common requirements. }
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
} }
return *this; return *this;
} }
@ -138,25 +140,27 @@ namespace std {
sentry __cerb(*this); sentry __cerb(*this);
if (__cerb) if (__cerb)
{ {
try { try
bool __f; {
ios_base::fmtflags __fmt = this->flags() & ios_base::basefield; bool __f;
if (__fmt & ios_base::oct || __fmt & ios_base::hex) ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
__f = _M_fnumput->put(*this, *this, this->fill(), if (__fmt & ios_base::oct || __fmt & ios_base::hex)
static_cast<unsigned long>(__n)).failed(); __f = _M_fnumput->put(*this, *this, this->fill(),
else static_cast<unsigned long>(__n)).failed();
__f = _M_fnumput->put(*this, *this, this->fill(), __n).failed(); else
__f = _M_fnumput->put(*this, *this, this->fill(), __n).failed();
if (__f) if (__f)
this->setstate(ios_base::badbit);
}
catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
} if ((this->exceptions() & ios_base::badbit) != 0)
catch(exception& __fail){ __throw_exception_again;
// 27.6.1.2.1 Common requirements. }
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
} }
return *this; return *this;
} }
@ -168,17 +172,19 @@ 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);
}
catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
} if ((this->exceptions() & ios_base::badbit) != 0)
catch(exception& __fail){ __throw_exception_again;
// 27.6.1.2.1 Common requirements. }
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
} }
return *this; return *this;
} }
@ -191,23 +197,26 @@ namespace std {
sentry __cerb(*this); sentry __cerb(*this);
if (__cerb) if (__cerb)
{ {
try { try
bool __f; {
ios_base::fmtflags __fmt = this->flags() & ios_base::basefield; bool __f;
if (__fmt & ios_base::oct || __fmt & ios_base::hex) ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
__f = _M_fnumput->put(*this, *this, this->fill(), if (__fmt & ios_base::oct || __fmt & ios_base::hex)
static_cast<unsigned long long>(__n)).failed(); __f = _M_fnumput->put(*this, *this, this->fill(),
else static_cast<unsigned long long>(__n)).failed();
__f = _M_fnumput->put(*this, *this, this->fill(), __n).failed(); else
__f = _M_fnumput->put(*this, *this, this->fill(), __n).failed();
} if (__f)
catch(exception& __fail){ this->setstate(ios_base::badbit);
// 27.6.1.2.1 Common requirements. }
// Turn this on without causing an ios::failure to be thrown. catch(exception& __fail)
this->setstate(ios_base::badbit); {
if ((this->exceptions() & ios_base::badbit) != 0) // 27.6.1.2.1 Common requirements.
__throw_exception_again; // Turn this on without causing an ios::failure to be thrown.
} this->setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
} }
return *this; return *this;
} }
@ -219,22 +228,24 @@ 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);
}
catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
} if ((this->exceptions() & ios_base::badbit) != 0)
catch(exception& __fail){ __throw_exception_again;
// 27.6.1.2.1 Common requirements. }
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
} }
return *this; return *this;
} }
#endif #endif
template<typename _CharT, typename _Traits> template<typename _CharT, typename _Traits>
basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>&
basic_ostream<_CharT, _Traits>::operator<<(double __n) basic_ostream<_CharT, _Traits>::operator<<(double __n)
@ -242,21 +253,23 @@ 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);
}
catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
} if ((this->exceptions() & ios_base::badbit) != 0)
catch(exception& __fail){ __throw_exception_again;
// 27.6.1.2.1 Common requirements. }
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
} }
return *this; return *this;
} }
template<typename _CharT, typename _Traits> template<typename _CharT, typename _Traits>
basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>&
basic_ostream<_CharT, _Traits>::operator<<(long double __n) basic_ostream<_CharT, _Traits>::operator<<(long double __n)
@ -264,17 +277,19 @@ 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);
}
catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
} if ((this->exceptions() & ios_base::badbit) != 0)
catch(exception& __fail){ __throw_exception_again;
// 27.6.1.2.1 Common requirements. }
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
} }
return *this; return *this;
} }
@ -286,17 +301,19 @@ 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);
}
catch(exception& __fail)
{
// 27.6.1.2.1 Common requirements.
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit); this->setstate(ios_base::badbit);
} if ((this->exceptions() & ios_base::badbit) != 0)
catch(exception& __fail){ __throw_exception_again;
// 27.6.1.2.1 Common requirements. }
// Turn this on without causing an ios::failure to be thrown.
this->setstate(ios_base::badbit);
if ((this->exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
} }
return *this; return *this;
} }
@ -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,30 +511,32 @@ namespace std {
typename __ostream_type::sentry __cerb(__out); typename __ostream_type::sentry __cerb(__out);
if (__cerb) if (__cerb)
{ {
try { try
streamsize __w = __out.width(); {
_CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w)); streamsize __w = __out.width();
__pads[0] = __c; _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
streamsize __len = 1; __pads[0] = __c;
if (__w > __len) streamsize __len = 1;
{ if (__w > __len)
__pad_char(__out, __pads, &__c, __w, __len); {
__len = __w; __pad_char(__out, __pads, &__c, __w, __len);
} __len = __w;
__out.write(__pads, __len); }
__out.width(0); __out.write(__pads, __len);
} __out.width(0);
catch(exception& __fail){ }
// 27.6.1.2.1 Common requirements. catch(exception& __fail)
// Turn this on without causing an ios::failure to be thrown. {
__out.setstate(ios_base::badbit); // 27.6.1.2.1 Common requirements.
if ((__out.exceptions() & ios_base::badbit) != 0) // Turn this on without causing an ios::failure to be thrown.
__throw_exception_again; __out.setstate(ios_base::badbit);
} if ((__out.exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
} }
return __out; return __out;
} }
// Specialization // Specialization
template <class _Traits> template <class _Traits>
basic_ostream<char, _Traits>& basic_ostream<char, _Traits>&
@ -527,26 +546,28 @@ namespace std {
typename __ostream_type::sentry __cerb(__out); typename __ostream_type::sentry __cerb(__out);
if (__cerb) if (__cerb)
{ {
try { try
streamsize __w = __out.width(); {
char* __pads = static_cast<char*>(__builtin_alloca(__w + 1)); streamsize __w = __out.width();
__pads[0] = __c; char* __pads = static_cast<char*>(__builtin_alloca(__w + 1));
streamsize __len = 1; __pads[0] = __c;
if (__w > __len) streamsize __len = 1;
{ if (__w > __len)
__pad_char(__out, __pads, &__c, __w, __len); {
__len = __w; __pad_char(__out, __pads, &__c, __w, __len);
} __len = __w;
__out.write(__pads, __len); }
__out.width(0); __out.write(__pads, __len);
} __out.width(0);
catch(exception& __fail){ }
// 27.6.1.2.1 Common requirements. catch(exception& __fail)
// Turn this on without causing an ios::failure to be thrown. {
__out.setstate(ios_base::badbit); // 27.6.1.2.1 Common requirements.
if ((__out.exceptions() & ios_base::badbit) != 0) // Turn this on without causing an ios::failure to be thrown.
__throw_exception_again; __out.setstate(ios_base::badbit);
} if ((__out.exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
} }
return __out; return __out;
} }
@ -559,26 +580,28 @@ namespace std {
typename __ostream_type::sentry __cerb(__out); typename __ostream_type::sentry __cerb(__out);
if (__cerb) if (__cerb)
{ {
try { try
streamsize __w = __out.width(); {
_CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w)); streamsize __w = __out.width();
streamsize __len = static_cast<streamsize>(_Traits::length(__s)); _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
if (__w > __len) streamsize __len = static_cast<streamsize>(_Traits::length(__s));
{ if (__w > __len)
__pad_char(__out, __pads, __s, __w, __len); {
__s = __pads; __pad_char(__out, __pads, __s, __w, __len);
__len = __w; __s = __pads;
} __len = __w;
__out.write(__s, __len); }
__out.width(0); __out.write(__s, __len);
} __out.width(0);
catch(exception& __fail){ }
// 27.6.1.2.1 Common requirements. catch(exception& __fail)
// Turn this on without causing an ios::failure to be thrown. {
__out.setstate(ios_base::badbit); // 27.6.1.2.1 Common requirements.
if ((__out.exceptions() & ios_base::badbit) != 0) // Turn this on without causing an ios::failure to be thrown.
__throw_exception_again; __out.setstate(ios_base::badbit);
} if ((__out.exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
} }
return __out; return __out;
} }
@ -601,27 +624,29 @@ 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 __w = __out.width(); streamsize __len = static_cast<streamsize>(__clen);
_CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w)); streamsize __w = __out.width();
_CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
if (__w > __len)
{ if (__w > __len)
__pad_char(__out, __pads, __ws, __w, __len); {
__str = __pads; __pad_char(__out, __pads, __ws, __w, __len);
__len = __w; __str = __pads;
} __len = __w;
__out.write(__str, __len); }
__out.width(0); __out.write(__str, __len);
} __out.width(0);
catch(exception& __fail){ }
// 27.6.1.2.1 Common requirements. catch(exception& __fail)
// Turn this on without causing an ios::failure to be thrown. {
__out.setstate(ios_base::badbit); // 27.6.1.2.1 Common requirements.
if ((__out.exceptions() & ios_base::badbit) != 0) // Turn this on without causing an ios::failure to be thrown.
__throw_exception_again; __out.setstate(ios_base::badbit);
} if ((__out.exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
} }
return __out; return __out;
} }
@ -635,37 +660,62 @@ namespace std {
typename __ostream_type::sentry __cerb(__out); typename __ostream_type::sentry __cerb(__out);
if (__cerb) if (__cerb)
{ {
try { try
streamsize __w = __out.width(); {
char* __pads = static_cast<char*>(__builtin_alloca(__w)); streamsize __w = __out.width();
streamsize __len = static_cast<streamsize>(_Traits::length(__s)); char* __pads = static_cast<char*>(__builtin_alloca(__w));
if (__w > __len) streamsize __len = static_cast<streamsize>(_Traits::length(__s));
{ if (__w > __len)
__pad_char(__out, __pads, __s, __w, __len); {
__s = __pads; __pad_char(__out, __pads, __s, __w, __len);
__len = __w; __s = __pads;
} __len = __w;
__out.write(__s, __len); }
__out.width(0); __out.write(__s, __len);
} __out.width(0);
catch(exception& __fail){ }
// 27.6.1.2.1 Common requirements. catch(exception& __fail)
// Turn this on without causing an ios::failure to be thrown. {
__out.setstate(ios_base::badbit); // 27.6.1.2.1 Common requirements.
if ((__out.exceptions() & ios_base::badbit) != 0) // Turn this on without causing an ios::failure to be thrown.
__throw_exception_again; __out.setstate(ios_base::badbit);
} if ((__out.exceptions() & ios_base::badbit) != 0)
__throw_exception_again;
}
} }
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;
} }