mirror of
https://github.com/boostorg/utility.git
synced 2025-05-08 18:34:02 +00:00
Use ostream_put from Boost.IO
This commit is contained in:
parent
882c9c86c4
commit
2b436d7d50
@ -99,19 +99,6 @@ boostbook standalone_declval
|
||||
<xsl:param>generate.section.toc.level=1
|
||||
;
|
||||
|
||||
xml ostream_string : ostream_string.qbk ;
|
||||
boostbook standalone_ostream_string
|
||||
:
|
||||
ostream_string
|
||||
:
|
||||
<xsl:param>root.filename=ostream_string
|
||||
<xsl:param>chunk.section.depth=0
|
||||
<xsl:param>chunk.first.sections=0
|
||||
<xsl:param>toc.section.depth=1
|
||||
<xsl:param>toc.max.depth=1
|
||||
<xsl:param>generate.section.toc.level=1
|
||||
;
|
||||
|
||||
xml string_ref : string_ref.qbk ;
|
||||
boostbook standalone_string_ref
|
||||
:
|
||||
@ -136,5 +123,5 @@ alias boostdoc ;
|
||||
explicit boostdoc ;
|
||||
alias boostrelease :
|
||||
standalone_base_from_member standalone_compressed_pair
|
||||
standalone_declval standalone_ostream_string standalone_string_ref ;
|
||||
standalone_declval standalone_string_ref ;
|
||||
explicit boostrelease ;
|
||||
|
@ -1,78 +0,0 @@
|
||||
[/
|
||||
Copyright 2019 Glen Joseph Fernandes
|
||||
(glenjofe@gmail.com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(http://www.boost.org/LICENSE_1_0.txt)
|
||||
]
|
||||
|
||||
[article ostream_string
|
||||
[quickbook 1.5]
|
||||
[authors [Fernandes, Glen]]
|
||||
[copyright 2019 Glen Joseph Fernandes]
|
||||
[license Distributed under the Boost Software License, Version 1.0.]]
|
||||
|
||||
[section Overview]
|
||||
|
||||
The header <boost/utility/ostream_string.hpp> provides the function template
|
||||
`boost::ostream_string` for formatted output that satisfies the requirements of
|
||||
\[ostream.formatted.reqmts\].
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Examples]
|
||||
|
||||
The inserter for class template `basic_string_view` could be implemented as
|
||||
follows:
|
||||
|
||||
```
|
||||
template<class charT, class traits>
|
||||
std::basic_ostream<charT, traits>&
|
||||
operator<<(std::basic_ostream<charT, traits>& os,
|
||||
const basic_string_view<charT, traits>& str)
|
||||
{
|
||||
return boost::ostream_string(os, str.data(), str.size());
|
||||
}
|
||||
```
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Reference]
|
||||
|
||||
```
|
||||
namespace boost {
|
||||
|
||||
template<class charT, class traits>
|
||||
std::basic_ostream<charT, traits>&
|
||||
ostream_string(std::basic_ostream<charT, traits>& os,
|
||||
const charT* data, std::size_t size);
|
||||
|
||||
} /* boost */
|
||||
```
|
||||
|
||||
[heading Free functions]
|
||||
|
||||
[variablelist
|
||||
[[`template<class charT, class traits> std::basic_ostream<charT, traits>&
|
||||
ostream_string(std::basic_ostream<charT, traits>& os, const charT* data,
|
||||
std::size_t size);`]
|
||||
[[variablelist
|
||||
[[Effects]
|
||||
[Behaves like a formatted inserter (as described in
|
||||
\[ostream.formatted.reqmts\]) of `os`. Creates a character sequence `seq` of
|
||||
`size` characters starting at `data`, each widened using `os.widen()`
|
||||
(\[basic.ios.members\]). Determines padding for `seq` as described in
|
||||
\[ostream.formatted.reqmts\]. Inserts `seq` into `os`. Calls `width(0)`.]]
|
||||
[[Returns][`os`.]]]]]]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section History]
|
||||
|
||||
[heading boost 1.71]
|
||||
|
||||
* Glen Fernandes updated the implementation of the `basic_string_ref` and
|
||||
`basic_string_view` stream insertion operators to write directly to the
|
||||
`basic_streambuf` and refactored that functionality into this common utility.
|
||||
|
||||
[endsect]
|
@ -1,95 +0,0 @@
|
||||
/*
|
||||
Copyright 2019 Glen Joseph Fernandes
|
||||
(glenjofe@gmail.com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#ifndef BOOST_UTILITY_OSTREAM_STRING_HPP
|
||||
#define BOOST_UTILITY_OSTREAM_STRING_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <iosfwd>
|
||||
#include <cstddef>
|
||||
|
||||
namespace boost {
|
||||
namespace detail {
|
||||
|
||||
template<class charT, class traits>
|
||||
inline std::size_t
|
||||
oss_put(std::basic_ostream<charT, traits>& os, const charT* data,
|
||||
std::size_t size)
|
||||
{
|
||||
return static_cast<std::size_t>(os.rdbuf()->sputn(data, size));
|
||||
}
|
||||
|
||||
template<class charT, class traits>
|
||||
inline bool
|
||||
oss_fill(std::basic_ostream<charT, traits>& os, std::size_t size)
|
||||
{
|
||||
charT c = os.fill();
|
||||
charT fill[] = { c, c, c, c, c, c, c, c };
|
||||
enum {
|
||||
chunk = sizeof fill / sizeof(charT)
|
||||
};
|
||||
for (; size > chunk; size -= chunk) {
|
||||
if (boost::detail::oss_put(os, fill, chunk) != chunk) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return boost::detail::oss_put(os, fill, size) == size;
|
||||
}
|
||||
|
||||
template<class charT, class traits>
|
||||
class oss_guard {
|
||||
public:
|
||||
explicit oss_guard(std::basic_ostream<charT, traits>& os) BOOST_NOEXCEPT
|
||||
: os_(&os) { }
|
||||
~oss_guard() BOOST_NOEXCEPT_IF(false) {
|
||||
if (os_) {
|
||||
os_->setstate(std::basic_ostream<charT, traits>::badbit);
|
||||
}
|
||||
}
|
||||
void release() BOOST_NOEXCEPT {
|
||||
os_ = 0;
|
||||
}
|
||||
private:
|
||||
oss_guard(const oss_guard&);
|
||||
oss_guard& operator=(const oss_guard&);
|
||||
std::basic_ostream<charT, traits>* os_;
|
||||
};
|
||||
|
||||
} /* detail */
|
||||
|
||||
template<class charT, class traits>
|
||||
inline std::basic_ostream<charT, traits>&
|
||||
ostream_string(std::basic_ostream<charT, traits>& os, const charT* data,
|
||||
std::size_t size)
|
||||
{
|
||||
typedef std::basic_ostream<charT, traits> stream;
|
||||
detail::oss_guard<charT, traits> guard(os);
|
||||
typename stream::sentry entry(os);
|
||||
if (entry) {
|
||||
std::size_t width = static_cast<std::size_t>(os.width());
|
||||
if (width <= size) {
|
||||
if (detail::oss_put(os, data, size) != size) {
|
||||
return os;
|
||||
}
|
||||
} else if ((os.flags() & stream::adjustfield) == stream::left) {
|
||||
if (detail::oss_put(os, data, size) != size ||
|
||||
!detail::oss_fill(os, width - size)) {
|
||||
return os;
|
||||
}
|
||||
} else if (!detail::oss_fill(os, width - size) ||
|
||||
detail::oss_put(os, data, size) != size) {
|
||||
return os;
|
||||
}
|
||||
os.width(0);
|
||||
}
|
||||
guard.release();
|
||||
return os;
|
||||
}
|
||||
|
||||
} /* boost */
|
||||
|
||||
#endif
|
@ -18,7 +18,7 @@
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
#include <boost/utility/ostream_string.hpp>
|
||||
#include <boost/io/ostream_put.hpp>
|
||||
#include <boost/utility/string_ref_fwd.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
|
||||
@ -427,7 +427,7 @@ namespace boost {
|
||||
template<class charT, class traits>
|
||||
inline std::basic_ostream<charT, traits>&
|
||||
operator<<(std::basic_ostream<charT, traits>& os, const basic_string_ref<charT,traits>& str) {
|
||||
return boost::ostream_string(os, str.data(), str.size());
|
||||
return boost::io::ostream_put(os, str.data(), str.size());
|
||||
}
|
||||
|
||||
#if 0
|
||||
|
@ -20,7 +20,7 @@
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
#include <boost/utility/ostream_string.hpp>
|
||||
#include <boost/io/ostream_put.hpp>
|
||||
#include <boost/utility/string_view_fwd.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <boost/container_hash/hash_fwd.hpp>
|
||||
@ -578,7 +578,7 @@ namespace boost {
|
||||
inline std::basic_ostream<charT, traits>&
|
||||
operator<<(std::basic_ostream<charT, traits>& os,
|
||||
const basic_string_view<charT,traits>& str) {
|
||||
return boost::ostream_string(os, str.data(), str.size());
|
||||
return boost::io::ostream_put(os, str.data(), str.size());
|
||||
}
|
||||
|
||||
#if 0
|
||||
|
49
index.html
49
index.html
@ -12,30 +12,31 @@
|
||||
<p>The Boost Utility Library isn't really a single library at all. It is just a
|
||||
collection for components too small to be called libraries in their own right.</p>
|
||||
<p>But that doesn't mean there isn't useful stuff here. Take a look:</p>
|
||||
<blockquote>
|
||||
<p>
|
||||
<a href="../core/doc/html/core/addressof.html">addressof</a> (moved to the Boost.Core library)<br>
|
||||
<a href="doc/html/base_from_member.html">base_from_member</a><br>
|
||||
<a href="utility.htm#BOOST_BINARY">BOOST_BINARY</a><br>
|
||||
<a href="call_traits.htm">call_traits</a><br>
|
||||
<a href="../core/doc/html/core/checked_delete.html">checked_delete</a> (moved to the Boost.Core library)<br>
|
||||
<a href="doc/html/compressed_pair.html">compressed_pair</a><br>
|
||||
<a href="../type_traits/doc/html/boost_typetraits/reference/declval.html">declval</a> (moved to the Boost.TypeTraits library)<br>
|
||||
<a href="../core/doc/html/core/enable_if.html">enable_if</a> (moved to the Boost.Core library)<br>
|
||||
<a href="in_place_factories.html">in_place_factory</a><br>
|
||||
<a href="iterator_adaptors.htm">iterator_adaptors</a><br>
|
||||
<a href="../iterator/doc/generator_iterator.htm">generator iterator adaptors</a> (moved to the Boost.Iterator library)<br>
|
||||
<a href="../iterator/doc/html/iterator/algorithms/next_prior.html">next/prior</a> (moved to the Boost.Iterator library)<br>
|
||||
<a href="../core/doc/html/core/noncopyable.html">noncopyable</a> (moved to the Boost.Core library)<br>
|
||||
<a href="operators.htm">operators</a><br>
|
||||
<a href="utility.htm#result_of">result_of</a><br>
|
||||
<a href="throw_exception.html">throw_exception</a><br>
|
||||
<a href="utility.htm">utility</a><br>
|
||||
<a href="doc/html/ostream_string.html">ostream_string</a><br>
|
||||
<a href="doc/html/string_ref.html">string_ref</a><br>
|
||||
<a href="value_init.htm">value_init</a><br>
|
||||
</p>
|
||||
</blockquote>
|
||||
<ul>
|
||||
<li><a href="doc/html/base_from_member.html">base_from_member</a></li>
|
||||
<li><a href="utility.htm#BOOST_BINARY">BOOST_BINARY</a></li>
|
||||
<li><a href="call_traits.htm">call_traits</a></li>
|
||||
<li><a href="doc/html/compressed_pair.html">compressed_pair</a></li>
|
||||
<li><a href="in_place_factories.html">in_place_factory</a></li>
|
||||
<li><a href="iterator_adaptors.htm">iterator_adaptors</a></li>
|
||||
<li><a href="operators.htm">operators</a></li>
|
||||
<li><a href="utility.htm#result_of">result_of</a></li>
|
||||
<li><a href="throw_exception.html">throw_exception</a></li>
|
||||
<li><a href="utility.htm">utility</a></li>
|
||||
<li><a href="doc/html/string_ref.html">string_ref</a></li>
|
||||
<li><a href="value_init.htm">value_init</a></li>
|
||||
</ul>
|
||||
<p>Over time useful stuff here has moved to more appropriate Boost libraries:</p>
|
||||
<ul>
|
||||
<li><a href="../core/doc/html/core/addressof.html">addressof</a> (moved to Boost.Core)</li>
|
||||
<li><a href="../core/doc/html/core/checked_delete.html">checked_delete</a> (moved to Boost.Core)</li>
|
||||
<li><a href="../type_traits/doc/html/boost_typetraits/reference/declval.html">declval</a> (moved to Boost.TypeTraits)</li>
|
||||
<li><a href="../core/doc/html/core/enable_if.html">enable_if</a> (moved to Boost.Core)</li>
|
||||
<li><a href="../iterator/doc/generator_iterator.htm">generator iterator adaptors</a> (moved to Boost.Iterator)</li>
|
||||
<li><a href="../iterator/doc/html/iterator/algorithms/next_prior.html">next/prior</a> (moved to Boost.Iterator)</li>
|
||||
<li><a href="../core/doc/html/core/noncopyable.html">noncopyable</a> (moved to Boost.Core)</li>
|
||||
<li><a href="../io/doc/html/io.html">ostream_string</a> (moved to Boost.IO)</li>
|
||||
</ul>
|
||||
<hr>
|
||||
<p>© Copyright Beman Dawes, 2001</p>
|
||||
<p>Distributed under the Boost Software License, Version 1.0. (See
|
||||
|
@ -85,19 +85,6 @@
|
||||
"Daniel Frey <d.frey -at- gmx.de>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"key": "utility/ostream_string",
|
||||
"name": "ostream_string",
|
||||
"description": "String formatted output function.",
|
||||
"documentation": "doc/html/ostream_string.html",
|
||||
"category": [
|
||||
"IO"
|
||||
],
|
||||
"authors": "Glen Fernandes",
|
||||
"maintainers": [
|
||||
"Glen Fernandes <glenjofe -at- gmail.com>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"key": "utility/result_of",
|
||||
"name": "Result Of",
|
||||
|
@ -42,5 +42,3 @@ compile-fail value_init_test_fail2.cpp ;
|
||||
compile-fail value_init_test_fail3.cpp ;
|
||||
compile-fail initialized_test_fail1.cpp ;
|
||||
compile-fail initialized_test_fail2.cpp ;
|
||||
|
||||
run ostream_string_test.cpp ;
|
||||
|
@ -1,136 +0,0 @@
|
||||
/*
|
||||
Copyright 2019 Glen Joseph Fernandes
|
||||
(glenjofe@gmail.com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <boost/utility/ostream_string.hpp>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::ostringstream os;
|
||||
os.width(1);
|
||||
os.fill('.');
|
||||
os.setf(std::ios_base::left, std::ios_base::adjustfield);
|
||||
boost::ostream_string(os, "xy", 2);
|
||||
BOOST_TEST(os.good());
|
||||
BOOST_TEST(os.width() == 0);
|
||||
BOOST_TEST(os.str() == "xy");
|
||||
}
|
||||
{
|
||||
std::wostringstream os;
|
||||
os.width(1);
|
||||
os.fill('.');
|
||||
os.setf(std::ios_base::left, std::ios_base::adjustfield);
|
||||
boost::ostream_string(os, L"xy", 2);
|
||||
BOOST_TEST(os.good());
|
||||
BOOST_TEST(os.width() == 0);
|
||||
BOOST_TEST(os.str() == L"xy");
|
||||
}
|
||||
{
|
||||
std::ostringstream os;
|
||||
os.width(1);
|
||||
os.fill('.');
|
||||
os.setf(std::ios_base::right, std::ios_base::adjustfield);
|
||||
boost::ostream_string(os, "xy", 2);
|
||||
BOOST_TEST(os.good());
|
||||
BOOST_TEST(os.width() == 0);
|
||||
BOOST_TEST(os.str() == "xy");
|
||||
}
|
||||
{
|
||||
std::wostringstream os;
|
||||
os.width(1);
|
||||
os.fill('.');
|
||||
os.setf(std::ios_base::right, std::ios_base::adjustfield);
|
||||
boost::ostream_string(os, L"xy", 2);
|
||||
BOOST_TEST(os.good());
|
||||
BOOST_TEST(os.width() == 0);
|
||||
BOOST_TEST(os.str() == L"xy");
|
||||
}
|
||||
{
|
||||
std::ostringstream os;
|
||||
os.width(4);
|
||||
os.fill('.');
|
||||
os.setf(std::ios_base::left, std::ios_base::adjustfield);
|
||||
boost::ostream_string(os, "xy", 2);
|
||||
BOOST_TEST(os.good());
|
||||
BOOST_TEST(os.width() == 0);
|
||||
BOOST_TEST(os.str() == "xy..");
|
||||
}
|
||||
{
|
||||
std::wostringstream os;
|
||||
os.width(4);
|
||||
os.fill(L'.');
|
||||
os.setf(std::ios_base::left, std::ios_base::adjustfield);
|
||||
boost::ostream_string(os, L"xy", 2);
|
||||
BOOST_TEST(os.good());
|
||||
BOOST_TEST(os.width() == 0);
|
||||
BOOST_TEST(os.str() == L"xy..");
|
||||
}
|
||||
{
|
||||
std::ostringstream os;
|
||||
os.width(4);
|
||||
os.fill('.');
|
||||
os.setf(std::ios_base::right, std::ios_base::adjustfield);
|
||||
boost::ostream_string(os, "xy", 2);
|
||||
BOOST_TEST(os.good());
|
||||
BOOST_TEST(os.width() == 0);
|
||||
BOOST_TEST(os.str() == "..xy");
|
||||
}
|
||||
{
|
||||
std::wostringstream os;
|
||||
os.width(4);
|
||||
os.fill(L'.');
|
||||
os.setf(std::ios_base::right, std::ios_base::adjustfield);
|
||||
boost::ostream_string(os, L"xy", 2);
|
||||
BOOST_TEST(os.good());
|
||||
BOOST_TEST(os.width() == 0);
|
||||
BOOST_TEST(os.str() == L"..xy");
|
||||
}
|
||||
{
|
||||
std::ostringstream os;
|
||||
os.width(12);
|
||||
os.fill('.');
|
||||
os.setf(std::ios_base::left, std::ios_base::adjustfield);
|
||||
boost::ostream_string(os, "xy", 2);
|
||||
BOOST_TEST(os.good());
|
||||
BOOST_TEST(os.width() == 0);
|
||||
BOOST_TEST(os.str() == "xy..........");
|
||||
}
|
||||
{
|
||||
std::wostringstream os;
|
||||
os.width(12);
|
||||
os.fill(L'.');
|
||||
os.setf(std::ios_base::left, std::ios_base::adjustfield);
|
||||
boost::ostream_string(os, L"xy", 2);
|
||||
BOOST_TEST(os.good());
|
||||
BOOST_TEST(os.width() == 0);
|
||||
BOOST_TEST(os.str() == L"xy..........");
|
||||
}
|
||||
{
|
||||
std::ostringstream os;
|
||||
os.width(12);
|
||||
os.fill('.');
|
||||
os.setf(std::ios_base::right, std::ios_base::adjustfield);
|
||||
boost::ostream_string(os, "xy", 2);
|
||||
BOOST_TEST(os.good());
|
||||
BOOST_TEST(os.width() == 0);
|
||||
BOOST_TEST(os.str() == "..........xy");
|
||||
}
|
||||
{
|
||||
std::wostringstream os;
|
||||
os.width(12);
|
||||
os.fill(L'.');
|
||||
os.setf(std::ios_base::right, std::ios_base::adjustfield);
|
||||
boost::ostream_string(os, L"xy", 2);
|
||||
BOOST_TEST(os.good());
|
||||
BOOST_TEST(os.width() == 0);
|
||||
BOOST_TEST(os.str() == L"..........xy");
|
||||
}
|
||||
return boost::report_errors();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user