2 Copyright 2019 Glen Joseph Fernandes
5 Distributed under the Boost Software License, Version 1.0.
6 (http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_UTILITY_OSTREAM_STRING_HPP
9 #define BOOST_UTILITY_OSTREAM_STRING_HPP
11 #include <boost/config.hpp>
18 template<class charT, class traits>
20 oss_put(std::basic_ostream<charT, traits>& os, const charT* data,
23 return static_cast<std::size_t>(os.rdbuf()->sputn(data, size));
26 template<class charT, class traits>
28 oss_fill(std::basic_ostream<charT, traits>& os, std::size_t size)
31 charT fill[] = { c, c, c, c, c, c, c, c };
33 chunk = sizeof fill / sizeof(charT)
35 for (; size > chunk; size -= chunk) {
36 if (boost::detail::oss_put(os, fill, chunk) != chunk) {
40 return boost::detail::oss_put(os, fill, size) == size;
43 template<class charT, class traits>
46 explicit oss_guard(std::basic_ostream<charT, traits>& os) BOOST_NOEXCEPT
48 ~oss_guard() BOOST_NOEXCEPT_IF(false) {
50 os_->setstate(std::basic_ostream<charT, traits>::badbit);
53 void release() BOOST_NOEXCEPT {
57 oss_guard(const oss_guard&);
58 oss_guard& operator=(const oss_guard&);
59 std::basic_ostream<charT, traits>* os_;
64 template<class charT, class traits>
65 inline std::basic_ostream<charT, traits>&
66 ostream_string(std::basic_ostream<charT, traits>& os, const charT* data,
69 typedef std::basic_ostream<charT, traits> stream;
70 detail::oss_guard<charT, traits> guard(os);
71 typename stream::sentry entry(os);
73 std::size_t width = static_cast<std::size_t>(os.width());
75 if (detail::oss_put(os, data, size) != size) {
78 } else if ((os.flags() & stream::adjustfield) == stream::left) {
79 if (detail::oss_put(os, data, size) != size ||
80 !detail::oss_fill(os, width - size)) {
83 } else if (!detail::oss_fill(os, width - size) ||
84 detail::oss_put(os, data, size) != size) {