diff options
author | paolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4> | 2006-10-13 09:00:31 +0000 |
---|---|---|
committer | paolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4> | 2006-10-13 09:00:31 +0000 |
commit | d978d656d8017e71fe4d2802766c4ceda99bccbf (patch) | |
tree | 62a5ac79c15b666f719419ef0629306a93f3a310 /libstdc++-v3/include | |
parent | 4913f81e03720d3d25e6b2ef777a3d764d1c2f7c (diff) | |
download | ppe42-gcc-d978d656d8017e71fe4d2802766c4ceda99bccbf.tar.gz ppe42-gcc-d978d656d8017e71fe4d2802766c4ceda99bccbf.zip |
2006-10-13 Paolo Carlini <pcarlini@suse.de>
PR libstdc++/28277 (partial: ostream bits 2)
* include/std/std_ostream.h (basic_ostream<>::_M_insert(const
char_type*, streamsize)): New.
(basic_ostream<>::_M_write(char_type, streamsize)): Likewise.
(operator<<(basic_ostream<>&, _CharT), operator<<(basic_ostream<>&,
char), operator<<(basic_ostream<>&, const _CharT*),
operator<<(basic_ostream<>&, const char*)): Use the latter.
* include/bits/ostream.tcc (basic_ostream<>::_M_insert(const
char_type*, streamsize)): Define.
(operator<<(basic_ostream<>&, const char*)): Use the latter.
(operator<<(basic_ostream<>&, _CharT), operator<<(basic_ostream<>&,
char), operator<<(basic_ostream<>&, const _CharT*),
operator<<(basic_ostream<>&, const char*),
operator<<(basic_ostream<>&, const basic_string<>&)): Remove.
* include/bits/basic_string.h (operator<<(basic_ostream<>&,
const basic_string<>&)): Use the latter, implement DR 586.
* config/abi/pre/gnu.ver: Adjust, export the new _M_insert.
* docs/html/ext/howto.html: Add an entry for DR 586.
* testsuite/21_strings/basic_string/inserters_extractors/char/
28277.cc: New.
* testsuite/21_strings/basic_string/inserters_extractors/wchar_t/
28277.cc: Likewise.
* testsuite/27_io/basic_ostream/inserters_character/char/
28277-3.cc: Likewise.
* testsuite/27_io/basic_ostream/inserters_character/char/
28277-4.cc: Likewise.
* testsuite/27_io/basic_ostream/inserters_character/wchar_t/
28277-2.cc: Likewise.
* testsuite/27_io/basic_ostream/inserters_character/wchar_t/
28277-3.cc: Likewise.
* testsuite/27_io/basic_ostream/inserters_character/wchar_t/
28277-4.cc: Likewise.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@117689 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libstdc++-v3/include')
-rw-r--r-- | libstdc++-v3/include/bits/basic_string.h | 7 | ||||
-rw-r--r-- | libstdc++-v3/include/bits/ostream.tcc | 201 | ||||
-rw-r--r-- | libstdc++-v3/include/std/std_ostream.h | 50 |
3 files changed, 76 insertions, 182 deletions
diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index 56c3fe6097a..da6ef1c2d19 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -2396,7 +2396,12 @@ _GLIBCXX_BEGIN_NAMESPACE(std) template<typename _CharT, typename _Traits, typename _Alloc> basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, - const basic_string<_CharT, _Traits, _Alloc>& __str); + const basic_string<_CharT, _Traits, _Alloc>& __str) + { + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 586. string inserter not a formatted function + return __os._M_insert(__str.data(), __str.size()); + } /** * @brief Read a line from stream into a string. diff --git a/libstdc++-v3/include/bits/ostream.tcc b/libstdc++-v3/include/bits/ostream.tcc index d53cd07579d..a52eb4e3a00 100644 --- a/libstdc++-v3/include/bits/ostream.tcc +++ b/libstdc++-v3/include/bits/ostream.tcc @@ -315,209 +315,60 @@ _GLIBCXX_BEGIN_NAMESPACE(std) return *this; } - // 27.6.2.5.4 Character inserters. template<typename _CharT, typename _Traits> basic_ostream<_CharT, _Traits>& - operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c) - { - typedef basic_ostream<_CharT, _Traits> __ostream_type; - typename __ostream_type::sentry __cerb(__out); - if (__cerb) - { - try - { - const streamsize __w = __out.width(); - streamsize __len = 1; - _CharT* __cs = &__c; - if (__w > __len) - { - __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) - * __w)); - __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __cs, - &__c, __w, __len, false); - __len = __w; - } - __out._M_write(__cs, __len); - __out.width(0); - } - catch(...) - { __out._M_setstate(ios_base::badbit); } - } - return __out; - } - - // Specializations. - template <class _Traits> - basic_ostream<char, _Traits>& - operator<<(basic_ostream<char, _Traits>& __out, char __c) + basic_ostream<_CharT, _Traits>:: + _M_insert(const char_type* __s, streamsize __n) { - typedef basic_ostream<char, _Traits> __ostream_type; - typename __ostream_type::sentry __cerb(__out); + sentry __cerb(*this); if (__cerb) { try { - const streamsize __w = __out.width(); - streamsize __len = 1; - char* __cs = &__c; - if (__w > __len) + const streamsize __w = this->width(); + if (__w > __n) { - __cs = static_cast<char*>(__builtin_alloca(__w)); - __pad<char, _Traits>::_S_pad(__out, __out.fill(), __cs, - &__c, __w, __len, false); - __len = __w; + const bool __left = ((this->flags() & ios_base::adjustfield) + == ios_base::left); + if (!__left) + _M_write(this->fill(), __w - __n); + if (this->good()) + _M_write(__s, __n); + if (__left && this->good()) + _M_write(this->fill(), __w - __n); } - __out._M_write(__cs, __len); - __out.width(0); + else + _M_write(__s, __n); + this->width(0); } catch(...) - { __out._M_setstate(ios_base::badbit); } - } - return __out; - } - - template<typename _CharT, typename _Traits> - basic_ostream<_CharT, _Traits>& - operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s) - { - typedef basic_ostream<_CharT, _Traits> __ostream_type; - typename __ostream_type::sentry __cerb(__out); - if (__cerb && __s) - { - try - { - const streamsize __w = __out.width(); - streamsize __len = static_cast<streamsize>(_Traits::length(__s)); - if (__w > __len) - { - _CharT* __cs = (static_cast< - _CharT*>(__builtin_alloca(sizeof(_CharT) - * __w))); - __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __cs, - __s, __w, __len, false); - __s = __cs; - __len = __w; - } - __out._M_write(__s, __len); - __out.width(0); - } - catch(...) - { __out._M_setstate(ios_base::badbit); } + { this->_M_setstate(ios_base::badbit); } } - else if (!__s) - __out.setstate(ios_base::badbit); - return __out; + return *this; } template<typename _CharT, typename _Traits> basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s) { - typedef basic_ostream<_CharT, _Traits> __ostream_type; - // _GLIBCXX_RESOLVE_LIB_DEFECTS - // 167. Improper use of traits_type::length() - // Note that this is only in 'Review' status. - typedef char_traits<char> __traits_type; - typename __ostream_type::sentry __cerb(__out); - if (__cerb && __s) + if (!__s) + __out.setstate(ios_base::badbit); + else { - _CharT* __ws = 0; try { - const size_t __clen = __traits_type::length(__s); - __ws = new _CharT[__clen]; + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 167. Improper use of traits_type::length() + const size_t __clen = char_traits<char>::length(__s); + _CharT* __ws = new _CharT[__clen]; for (size_t __i = 0; __i < __clen; ++__i) __ws[__i] = __out.widen(__s[__i]); - _CharT* __str = __ws; - - const streamsize __w = __out.width(); - streamsize __len = static_cast<streamsize>(__clen); - if (__w > __len) - { - _CharT* __cs = (static_cast< - _CharT*>(__builtin_alloca(sizeof(_CharT) - * __w))); - __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __cs, - __ws, __w, __len, false); - __str = __cs; - __len = __w; - } - __out._M_write(__str, __len); - __out.width(0); - + __out._M_insert(__ws, __clen); delete [] __ws; } catch(...) - { - delete [] __ws; - __out._M_setstate(ios_base::badbit); - } - } - else if (!__s) - __out.setstate(ios_base::badbit); - return __out; - } - - // Partial specializations. - template<class _Traits> - basic_ostream<char, _Traits>& - operator<<(basic_ostream<char, _Traits>& __out, const char* __s) - { - typedef basic_ostream<char, _Traits> __ostream_type; - typename __ostream_type::sentry __cerb(__out); - if (__cerb && __s) - { - try - { - const streamsize __w = __out.width(); - streamsize __len = static_cast<streamsize>(_Traits::length(__s)); - if (__w > __len) - { - char* __cs = static_cast<char*>(__builtin_alloca(__w)); - __pad<char, _Traits>::_S_pad(__out, __out.fill(), __cs, - __s, __w, __len, false); - __s = __cs; - __len = __w; - } - __out._M_write(__s, __len); - __out.width(0); - } - catch(...) { __out._M_setstate(ios_base::badbit); } } - else if (!__s) - __out.setstate(ios_base::badbit); - return __out; - } - - // 21.3.7.9 basic_string::operator<< - template<typename _CharT, typename _Traits, typename _Alloc> - basic_ostream<_CharT, _Traits>& - operator<<(basic_ostream<_CharT, _Traits>& __out, - const basic_string<_CharT, _Traits, _Alloc>& __str) - { - typedef basic_ostream<_CharT, _Traits> __ostream_type; - typename __ostream_type::sentry __cerb(__out); - if (__cerb) - { - const streamsize __w = __out.width(); - streamsize __len = static_cast<streamsize>(__str.size()); - const _CharT* __s = __str.data(); - - // _GLIBCXX_RESOLVE_LIB_DEFECTS - // 25. String operator<< uses width() value wrong - if (__w > __len) - { - _CharT* __cs = (static_cast< - _CharT*>(__builtin_alloca(sizeof(_CharT) * __w))); - __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __cs, __s, - __w, __len, false); - __s = __cs; - __len = __w; - } - __out._M_write(__s, __len); - __out.width(0); - } return __out; } diff --git a/libstdc++-v3/include/std/std_ostream.h b/libstdc++-v3/include/std/std_ostream.h index 23a19c98d32..6b4d389ac1f 100644 --- a/libstdc++-v3/include/std/std_ostream.h +++ b/libstdc++-v3/include/std/std_ostream.h @@ -92,6 +92,11 @@ _GLIBCXX_BEGIN_NAMESPACE(std) friend basic_ostream<_CharT2, _Traits2>& operator<<(basic_ostream<_CharT2, _Traits2>&, const char*); + template<typename _CharT2, typename _Traits2, typename _Alloc> + friend basic_ostream<_CharT2, _Traits2>& + operator<<(basic_ostream<_CharT2, _Traits2>&, + const basic_string<_CharT2, _Traits2, _Alloc>&); + // [27.6.2.2] constructor/destructor /** * @brief Base constructor. @@ -288,11 +293,25 @@ _GLIBCXX_BEGIN_NAMESPACE(std) void _M_write(const char_type* __s, streamsize __n) { - streamsize __put = this->rdbuf()->sputn(__s, __n); + const streamsize __put = this->rdbuf()->sputn(__s, __n); if (__put != __n) this->setstate(ios_base::badbit); } + void + _M_write(char_type __c, streamsize __n) + { + for (; __n > 0; --__n) + { + const int_type __put = this->rdbuf()->sputc(__c); + if (traits_type::eq_int_type(__put, traits_type::eof())) + { + this->setstate(ios_base::badbit); + break; + } + } + } + /** * @brief Character string insertion. * @param s The array to insert. @@ -366,6 +385,9 @@ _GLIBCXX_BEGIN_NAMESPACE(std) template<typename _ValueT> __ostream_type& _M_insert(_ValueT __v); + + __ostream_type& + _M_insert(const char_type* __s, streamsize __n); }; /** @@ -448,7 +470,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std) */ template<typename _CharT, typename _Traits> basic_ostream<_CharT, _Traits>& - operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c); + operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c) + { return __out._M_insert(&__c, 1); } template<typename _CharT, typename _Traits> basic_ostream<_CharT, _Traits>& @@ -458,7 +481,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std) // Specialization template <class _Traits> basic_ostream<char, _Traits>& - operator<<(basic_ostream<char, _Traits>& __out, char __c); + operator<<(basic_ostream<char, _Traits>& __out, char __c) + { return __out._M_insert(&__c, 1); } // Signed and unsigned template<class _Traits> @@ -488,7 +512,14 @@ _GLIBCXX_BEGIN_NAMESPACE(std) */ template<typename _CharT, typename _Traits> basic_ostream<_CharT, _Traits>& - operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s); + operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s) + { + if (!__s) + __out.setstate(ios_base::badbit); + else + __out._M_insert(__s, static_cast<streamsize>(_Traits::length(__s))); + return __out; + } template<typename _CharT, typename _Traits> basic_ostream<_CharT, _Traits> & @@ -497,8 +528,15 @@ _GLIBCXX_BEGIN_NAMESPACE(std) // Partial specializationss template<class _Traits> basic_ostream<char, _Traits>& - operator<<(basic_ostream<char, _Traits>& __out, const char* __s); - + operator<<(basic_ostream<char, _Traits>& __out, const char* __s) + { + if (!__s) + __out.setstate(ios_base::badbit); + else + __out._M_insert(__s, static_cast<streamsize>(_Traits::length(__s))); + return __out; + } + // Signed and unsigned template<class _Traits> basic_ostream<char, _Traits>& |