Refactored a bunch of duplicated code in <ostream>. Made a new routine called __put_character_sequence, and made nine places call it.
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@196951 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/ostream b/include/ostream
index 041314a..20587a1 100644
--- a/include/ostream
+++ b/include/ostream
@@ -729,7 +729,8 @@
template<class _CharT, class _Traits>
basic_ostream<_CharT, _Traits>&
-operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c)
+__put_character_sequence(basic_ostream<_CharT, _Traits>& __os,
+ const _CharT* __str, size_t __len)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
try
@@ -740,11 +741,11 @@
{
typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
if (__pad_and_output(_Ip(__os),
- &__c,
+ __str,
(__os.flags() & ios_base::adjustfield) == ios_base::left ?
- &__c + 1 :
- &__c,
- &__c + 1,
+ __str + __len :
+ __str,
+ __str + __len,
__os,
__os.fill()).failed())
__os.setstate(ios_base::badbit | ios_base::failbit);
@@ -759,6 +760,14 @@
return __os;
}
+
+template<class _CharT, class _Traits>
+basic_ostream<_CharT, _Traits>&
+operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c)
+{
+ return _VSTD::__put_character_sequence(__os, &__c, 1);
+}
+
template<class _CharT, class _Traits>
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn)
@@ -796,129 +805,28 @@
basic_ostream<char, _Traits>&
operator<<(basic_ostream<char, _Traits>& __os, char __c)
{
-#ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
-#endif // _LIBCPP_NO_EXCEPTIONS
- typename basic_ostream<char, _Traits>::sentry __s(__os);
- if (__s)
- {
- typedef ostreambuf_iterator<char, _Traits> _Ip;
- if (__pad_and_output(_Ip(__os),
- &__c,
- (__os.flags() & ios_base::adjustfield) == ios_base::left ?
- &__c + 1 :
- &__c,
- &__c + 1,
- __os,
- __os.fill()).failed())
- __os.setstate(ios_base::badbit | ios_base::failbit);
- }
-#ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- __os.__set_badbit_and_consider_rethrow();
- }
-#endif // _LIBCPP_NO_EXCEPTIONS
- return __os;
+ return _VSTD::__put_character_sequence(__os, &__c, 1);
}
template<class _Traits>
basic_ostream<char, _Traits>&
operator<<(basic_ostream<char, _Traits>& __os, signed char __c)
{
-#ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
-#endif // _LIBCPP_NO_EXCEPTIONS
- typename basic_ostream<char, _Traits>::sentry __s(__os);
- if (__s)
- {
- typedef ostreambuf_iterator<char, _Traits> _Ip;
- if (__pad_and_output(_Ip(__os),
- (char*)&__c,
- (__os.flags() & ios_base::adjustfield) == ios_base::left ?
- (char*)&__c + 1 :
- (char*)&__c,
- (char*)&__c + 1,
- __os,
- __os.fill()).failed())
- __os.setstate(ios_base::badbit | ios_base::failbit);
- }
-#ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- __os.__set_badbit_and_consider_rethrow();
- }
-#endif // _LIBCPP_NO_EXCEPTIONS
- return __os;
+ return _VSTD::__put_character_sequence(__os, (char *) &__c, 1);
}
template<class _Traits>
basic_ostream<char, _Traits>&
operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c)
{
-#ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
-#endif // _LIBCPP_NO_EXCEPTIONS
- typename basic_ostream<char, _Traits>::sentry __s(__os);
- if (__s)
- {
- typedef ostreambuf_iterator<char, _Traits> _Ip;
- if (__pad_and_output(_Ip(__os),
- (char*)&__c,
- (__os.flags() & ios_base::adjustfield) == ios_base::left ?
- (char*)&__c + 1 :
- (char*)&__c,
- (char*)&__c + 1,
- __os,
- __os.fill()).failed())
- __os.setstate(ios_base::badbit | ios_base::failbit);
- }
-#ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- __os.__set_badbit_and_consider_rethrow();
- }
-#endif // _LIBCPP_NO_EXCEPTIONS
- return __os;
+ return _VSTD::__put_character_sequence(__os, (char *) &__c, 1);
}
template<class _CharT, class _Traits>
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str)
{
-#ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
-#endif // _LIBCPP_NO_EXCEPTIONS
- typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
- if (__s)
- {
- typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
- size_t __len = _Traits::length(__str);
- if (__pad_and_output(_Ip(__os),
- __str,
- (__os.flags() & ios_base::adjustfield) == ios_base::left ?
- __str + __len :
- __str,
- __str + __len,
- __os,
- __os.fill()).failed())
- __os.setstate(ios_base::badbit | ios_base::failbit);
- }
-#ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- __os.__set_badbit_and_consider_rethrow();
- }
-#endif // _LIBCPP_NO_EXCEPTIONS
- return __os;
+ return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str));
}
template<class _CharT, class _Traits>
@@ -971,99 +879,23 @@
basic_ostream<char, _Traits>&
operator<<(basic_ostream<char, _Traits>& __os, const char* __str)
{
-#ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
-#endif // _LIBCPP_NO_EXCEPTIONS
- typename basic_ostream<char, _Traits>::sentry __s(__os);
- if (__s)
- {
- typedef ostreambuf_iterator<char, _Traits> _Ip;
- size_t __len = _Traits::length(__str);
- if (__pad_and_output(_Ip(__os),
- __str,
- (__os.flags() & ios_base::adjustfield) == ios_base::left ?
- __str + __len :
- __str,
- __str + __len,
- __os,
- __os.fill()).failed())
- __os.setstate(ios_base::badbit | ios_base::failbit);
- }
-#ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- __os.__set_badbit_and_consider_rethrow();
- }
-#endif // _LIBCPP_NO_EXCEPTIONS
- return __os;
+ return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str));
}
template<class _Traits>
basic_ostream<char, _Traits>&
operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str)
{
-#ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
-#endif // _LIBCPP_NO_EXCEPTIONS
- typename basic_ostream<char, _Traits>::sentry __s(__os);
- if (__s)
- {
- typedef ostreambuf_iterator<char, _Traits> _Ip;
- size_t __len = _Traits::length((const char*)__str);
- if (__pad_and_output(_Ip(__os),
- (const char*)__str,
- (__os.flags() & ios_base::adjustfield) == ios_base::left ?
- (const char*)__str + __len :
- (const char*)__str,
- (const char*)__str + __len,
- __os,
- __os.fill()).failed())
- __os.setstate(ios_base::badbit | ios_base::failbit);
- }
-#ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- __os.__set_badbit_and_consider_rethrow();
- }
-#endif // _LIBCPP_NO_EXCEPTIONS
- return __os;
+ const char *__s = (const char *) __str;
+ return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s));
}
template<class _Traits>
basic_ostream<char, _Traits>&
operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str)
{
-#ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
-#endif // _LIBCPP_NO_EXCEPTIONS
- typename basic_ostream<char, _Traits>::sentry __s(__os);
- if (__s)
- {
- typedef ostreambuf_iterator<char, _Traits> _Ip;
- size_t __len = _Traits::length((const char*)__str);
- if (__pad_and_output(_Ip(__os),
- (const char*)__str,
- (__os.flags() & ios_base::adjustfield) == ios_base::left ?
- (const char*)__str + __len :
- (const char*)__str,
- (const char*)__str + __len,
- __os,
- __os.fill()).failed())
- __os.setstate(ios_base::badbit | ios_base::failbit);
- }
-#ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- __os.__set_badbit_and_consider_rethrow();
- }
-#endif // _LIBCPP_NO_EXCEPTIONS
- return __os;
+ const char *__s = (const char *) __str;
+ return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s));
}
template <class _CharT, class _Traits>
@@ -1233,33 +1065,7 @@
operator<<(basic_ostream<_CharT, _Traits>& __os,
const basic_string<_CharT, _Traits, _Allocator>& __str)
{
-#ifndef _LIBCPP_NO_EXCEPTIONS
- try
- {
-#endif // _LIBCPP_NO_EXCEPTIONS
- typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
- if (__s)
- {
- typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
- size_t __len = __str.size();
- if (__pad_and_output(_Ip(__os),
- __str.data(),
- (__os.flags() & ios_base::adjustfield) == ios_base::left ?
- __str.data() + __len :
- __str.data(),
- __str.data() + __len,
- __os,
- __os.fill()).failed())
- __os.setstate(ios_base::badbit | ios_base::failbit);
- }
-#ifndef _LIBCPP_NO_EXCEPTIONS
- }
- catch (...)
- {
- __os.__set_badbit_and_consider_rethrow();
- }
-#endif // _LIBCPP_NO_EXCEPTIONS
- return __os;
+ return _VSTD::__put_character_sequence(__os, __str.data(), __str.size());
}
template <class _CharT, class _Traits>