mirror of
https://github.com/boostorg/io.git
synced 2025-06-25 03:51:36 +02:00
Merge pull request #7 from glenfe/develop
Move my ostream_string function to the IO library
This commit is contained in:
@ -20,6 +20,7 @@ This library contains various utilities for the standard I/O library.
|
||||
include::ios_state.adoc[]
|
||||
include::quoted.adoc[]
|
||||
include::ostream_joiner.adoc[]
|
||||
include::ostream_put.adoc[]
|
||||
|
||||
:leveloffset: -1
|
||||
|
||||
|
73
doc/ostream_put.adoc
Normal file
73
doc/ostream_put.adoc
Normal file
@ -0,0 +1,73 @@
|
||||
////
|
||||
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)
|
||||
////
|
||||
|
||||
# Insert Formatted Output, <boost/io/ostream_put.hpp>
|
||||
:toc:
|
||||
:toc-title:
|
||||
:idprefix:
|
||||
|
||||
## Description
|
||||
|
||||
The header `<boost/io/ostream_put.hpp>` provides the function template
|
||||
`boost::io::ostream_put` for formatted output that satisfies the requirements
|
||||
of [ostream.formatted.reqmts].
|
||||
|
||||
## Example
|
||||
|
||||
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::io::ostream_put(os, str.data(), str.size());
|
||||
}
|
||||
```
|
||||
|
||||
## Reference
|
||||
|
||||
### Header Synopsis
|
||||
|
||||
```
|
||||
namespace boost {
|
||||
namespace io {
|
||||
|
||||
template<class charT, class traits>
|
||||
std::basic_ostream<charT, traits>&
|
||||
ostream_put(std::basic_ostream<charT, traits>& os,
|
||||
const charT* data, std::size_t size);
|
||||
|
||||
} // io
|
||||
} // boost
|
||||
```
|
||||
|
||||
### Free functions
|
||||
|
||||
```
|
||||
template<class charT, class traits>
|
||||
std::basic_ostream<charT, traits>&
|
||||
ostream_put(std::basic_ostream<charT, traits>& os,
|
||||
const charT* data, std::size_t size);
|
||||
```
|
||||
|
||||
[.specification]
|
||||
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`.
|
||||
|
||||
## Acknowledgments
|
||||
|
||||
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.
|
97
include/boost/io/ostream_put.hpp
Normal file
97
include/boost/io/ostream_put.hpp
Normal file
@ -0,0 +1,97 @@
|
||||
/*
|
||||
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_IO_OSTREAM_PUT_HPP
|
||||
#define BOOST_IO_OSTREAM_PUT_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <iosfwd>
|
||||
#include <cstddef>
|
||||
|
||||
namespace boost {
|
||||
namespace io {
|
||||
namespace detail {
|
||||
|
||||
template<class charT, class traits>
|
||||
inline std::size_t
|
||||
osp_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
|
||||
osp_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::io::detail::osp_put(os, fill, chunk) != chunk) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return boost::io::detail::osp_put(os, fill, size) == size;
|
||||
}
|
||||
|
||||
template<class charT, class traits>
|
||||
class osp_guard {
|
||||
public:
|
||||
explicit osp_guard(std::basic_ostream<charT, traits>& os) BOOST_NOEXCEPT
|
||||
: os_(&os) { }
|
||||
~osp_guard() BOOST_NOEXCEPT_IF(false) {
|
||||
if (os_) {
|
||||
os_->setstate(std::basic_ostream<charT, traits>::badbit);
|
||||
}
|
||||
}
|
||||
void release() BOOST_NOEXCEPT {
|
||||
os_ = 0;
|
||||
}
|
||||
private:
|
||||
osp_guard(const osp_guard&);
|
||||
osp_guard& operator=(const osp_guard&);
|
||||
std::basic_ostream<charT, traits>* os_;
|
||||
};
|
||||
|
||||
} /* detail */
|
||||
|
||||
template<class charT, class traits>
|
||||
inline std::basic_ostream<charT, traits>&
|
||||
ostream_put(std::basic_ostream<charT, traits>& os, const charT* data,
|
||||
std::size_t size)
|
||||
{
|
||||
typedef std::basic_ostream<charT, traits> stream;
|
||||
detail::osp_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::osp_put(os, data, size) != size) {
|
||||
return os;
|
||||
}
|
||||
} else if ((os.flags() & stream::adjustfield) == stream::left) {
|
||||
if (detail::osp_put(os, data, size) != size ||
|
||||
!detail::osp_fill(os, width - size)) {
|
||||
return os;
|
||||
}
|
||||
} else if (!detail::osp_fill(os, width - size) ||
|
||||
detail::osp_put(os, data, size) != size) {
|
||||
return os;
|
||||
}
|
||||
os.width(0);
|
||||
}
|
||||
guard.release();
|
||||
return os;
|
||||
}
|
||||
|
||||
} /* io */
|
||||
} /* boost */
|
||||
|
||||
#endif
|
@ -13,3 +13,4 @@ run ios_state_test.cpp ;
|
||||
run quoted_test.cpp ;
|
||||
run ostream_joiner_test.cpp ;
|
||||
run make_ostream_joiner_test.cpp ;
|
||||
run ostream_put_test.cpp ;
|
||||
|
136
test/ostream_put_test.cpp
Normal file
136
test/ostream_put_test.cpp
Normal file
@ -0,0 +1,136 @@
|
||||
/*
|
||||
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/io/ostream_put.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::io::ostream_put(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::io::ostream_put(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::io::ostream_put(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::io::ostream_put(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::io::ostream_put(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::io::ostream_put(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::io::ostream_put(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::io::ostream_put(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::io::ostream_put(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::io::ostream_put(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::io::ostream_put(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::io::ostream_put(os, L"xy", 2);
|
||||
BOOST_TEST(os.good());
|
||||
BOOST_TEST(os.width() == 0);
|
||||
BOOST_TEST(os.str() == L"..........xy");
|
||||
}
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user