mirror of
https://github.com/boostorg/io.git
synced 2025-07-29 20:07:13 +02:00
Reduce calls to rdbuf() in ostream_put
This commit is contained in:
@ -8,7 +8,7 @@ Distributed under the Boost Software License, Version 1.0.
|
|||||||
#define BOOST_IO_IOS_STATE_HPP
|
#define BOOST_IO_IOS_STATE_HPP
|
||||||
|
|
||||||
#include <boost/io_fwd.hpp>
|
#include <boost/io_fwd.hpp>
|
||||||
#include <boost/detail/workaround.hpp>
|
#include <boost/config/workaround.hpp>
|
||||||
#include <ios>
|
#include <ios>
|
||||||
#ifndef BOOST_NO_STD_LOCALE
|
#ifndef BOOST_NO_STD_LOCALE
|
||||||
#include <locale>
|
#include <locale>
|
||||||
|
@ -18,27 +18,26 @@ namespace detail {
|
|||||||
|
|
||||||
template<class charT, class traits>
|
template<class charT, class traits>
|
||||||
inline std::size_t
|
inline std::size_t
|
||||||
osp_put(std::basic_ostream<charT, traits>& os, const charT* data,
|
osp_put(std::basic_streambuf<charT, traits>& out, const charT* data,
|
||||||
std::size_t size)
|
std::size_t size)
|
||||||
{
|
{
|
||||||
return static_cast<std::size_t>(os.rdbuf()->sputn(data, size));
|
return static_cast<std::size_t>(out.sputn(data, size));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class charT, class traits>
|
template<class charT, class traits>
|
||||||
inline bool
|
inline bool
|
||||||
osp_fill(std::basic_ostream<charT, traits>& os, std::size_t size)
|
osp_fill(std::basic_streambuf<charT, traits>& out, charT c, std::size_t size)
|
||||||
{
|
{
|
||||||
charT c = os.fill();
|
|
||||||
charT fill[] = { c, c, c, c, c, c, c, c };
|
charT fill[] = { c, c, c, c, c, c, c, c };
|
||||||
enum {
|
enum {
|
||||||
chunk = sizeof fill / sizeof(charT)
|
chunk = sizeof fill / sizeof(charT)
|
||||||
};
|
};
|
||||||
for (; size > chunk; size -= chunk) {
|
for (; size > chunk; size -= chunk) {
|
||||||
if (boost::io::detail::osp_put(os, fill, chunk) != chunk) {
|
if (boost::io::detail::osp_put(out, fill, chunk) != chunk) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return boost::io::detail::osp_put(os, fill, size) == size;
|
return boost::io::detail::osp_put(out, fill, size) == size;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class charT, class traits>
|
template<class charT, class traits>
|
||||||
@ -75,18 +74,19 @@ ostream_put(std::basic_ostream<charT, traits>& os, const charT* data,
|
|||||||
detail::osp_guard<charT, traits> guard(os);
|
detail::osp_guard<charT, traits> guard(os);
|
||||||
typename stream::sentry entry(os);
|
typename stream::sentry entry(os);
|
||||||
if (entry) {
|
if (entry) {
|
||||||
|
std::basic_streambuf<charT, traits>& out = *os.rdbuf();
|
||||||
std::size_t width = static_cast<std::size_t>(os.width());
|
std::size_t width = static_cast<std::size_t>(os.width());
|
||||||
if (width <= size) {
|
if (width <= size) {
|
||||||
if (detail::osp_put(os, data, size) != size) {
|
if (detail::osp_put(out, data, size) != size) {
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
} else if ((os.flags() & stream::adjustfield) == stream::left) {
|
} else if ((os.flags() & stream::adjustfield) == stream::left) {
|
||||||
if (detail::osp_put(os, data, size) != size ||
|
if (detail::osp_put(out, data, size) != size ||
|
||||||
!detail::osp_fill(os, width - size)) {
|
!detail::osp_fill(out, os.fill(), width - size)) {
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
} else if (!detail::osp_fill(os, width - size) ||
|
} else if (!detail::osp_fill(out, os.fill(), width - size) ||
|
||||||
detail::osp_put(os, data, size) != size) {
|
detail::osp_put(out, data, size) != size) {
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
os.width(0);
|
os.width(0);
|
||||||
|
Reference in New Issue
Block a user