Compare commits

...

4 Commits

Author SHA1 Message Date
Glen Fernandes
dabf53a703 Refactor stream write functionality into a standalone utility 2019-04-19 09:53:29 -04:00
Marshall Clow
ff56b3649e Merge pull request #57 from glenfe/develop
Make string_view operator<< use rdbuf directly
2019-04-18 09:00:53 -07:00
Peter Dimov
31e0ae4c37 Switch Appveyor to 2015 image 2019-04-14 18:13:56 +03:00
Glen Fernandes
5fe9df91c0 Make string_view operator<< use rdbuf directly 2019-04-12 18:06:35 -04:00
9 changed files with 325 additions and 91 deletions

View File

@@ -1,4 +1,4 @@
# Copyright 2016, 2017 Peter Dimov
# Copyright 2016-2019 Peter Dimov
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at http://boost.org/LICENSE_1_0.txt)
@@ -14,12 +14,14 @@ branches:
environment:
matrix:
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2013
TOOLSET: msvc-9.0,msvc-10.0,msvc-11.0,msvc-12.0
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
TOOLSET: msvc-14.0
TOOLSET: msvc-9.0,msvc-10.0,msvc-11.0
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
TOOLSET: msvc-12.0,msvc-14.0
ADDRMD: 32,64
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
TOOLSET: msvc-14.1
ADDRMD: 32,64
CXXSTD: 14,17
install:
@@ -43,7 +45,7 @@ install:
- git submodule init libs/integer
- git submodule init libs/detail
- git submodule update --jobs %GIT_FETCH_JOBS%
- xcopy /s /e /q %APPVEYOR_BUILD_FOLDER% libs\utility
- xcopy /s /e /q %APPVEYOR_BUILD_FOLDER% libs\utility\
- cmd /c bootstrap
- b2 -d0 headers
@@ -52,4 +54,5 @@ build: off
test_script:
- PATH=%ADDPATH%%PATH%
- if not "%CXXSTD%" == "" set CXXSTD=cxxstd=%CXXSTD%
- b2 -j %NUMBER_OF_PROCESSORS% libs/utility/test toolset=%TOOLSET% %CXXSTD%
- if not "%ADDRMD%" == "" set ADDRMD=address-model=%ADDRMD%
- b2 -j %NUMBER_OF_PROCESSORS% libs/utility/test toolset=%TOOLSET% %CXXSTD% %ADDRMD% variant=debug,release

View File

@@ -99,6 +99,19 @@ 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
:
@@ -123,5 +136,5 @@ alias boostdoc ;
explicit boostdoc ;
alias boostrelease :
standalone_base_from_member standalone_compressed_pair
standalone_declval standalone_string_ref ;
standalone_declval standalone_ostream_string standalone_string_ref ;
explicit boostrelease ;

62
doc/ostream_string.qbk Normal file
View File

@@ -0,0 +1,62 @@
[/
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 */
```
[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]

View File

@@ -0,0 +1,95 @@
/*
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

View File

@@ -1,5 +1,6 @@
/*
Copyright (c) Marshall Clow 2012-2015.
Copyright (c) Glen Joseph Fernandes 2019 (glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -17,6 +18,7 @@
#include <boost/config.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/utility/ostream_string.hpp>
#include <boost/utility/string_ref_fwd.hpp>
#include <boost/throw_exception.hpp>
@@ -421,52 +423,11 @@ namespace boost {
return basic_string_ref<charT, traits>(x) >= y;
}
namespace detail {
template<class charT, class traits>
inline void sr_insert_fill_chars(std::basic_ostream<charT, traits>& os, std::size_t n) {
enum { chunk_size = 8 };
charT fill_chars[chunk_size];
std::fill_n(fill_chars, static_cast< std::size_t >(chunk_size), os.fill());
for (; n >= chunk_size && os.good(); n -= chunk_size)
os.write(fill_chars, static_cast< std::size_t >(chunk_size));
if (n > 0 && os.good())
os.write(fill_chars, n);
}
template<class charT, class traits>
void sr_insert_aligned(std::basic_ostream<charT, traits>& os, const basic_string_ref<charT,traits>& str) {
const std::size_t size = str.size();
const std::size_t alignment_size = static_cast< std::size_t >(os.width()) - size;
const bool align_left = (os.flags() & std::basic_ostream<charT, traits>::adjustfield) == std::basic_ostream<charT, traits>::left;
if (!align_left) {
detail::sr_insert_fill_chars(os, alignment_size);
if (os.good())
os.write(str.data(), size);
}
else {
os.write(str.data(), size);
if (os.good())
detail::sr_insert_fill_chars(os, alignment_size);
}
}
} // namespace detail
// Inserter
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) {
if (os.good()) {
const std::size_t size = str.size();
const std::size_t w = static_cast< std::size_t >(os.width());
if (w <= size)
os.write(str.data(), size);
else
detail::sr_insert_aligned(os, str);
os.width(0);
}
return os;
return boost::ostream_string(os, str.data(), str.size());
}
#if 0

View File

@@ -1,6 +1,7 @@
/*
Copyright (c) Marshall Clow 2012-2015.
Copyright (c) Beman Dawes 2015
Copyright (c) Glen Joseph Fernandes 2019 (glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -19,6 +20,7 @@
#include <boost/config.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/utility/ostream_string.hpp>
#include <boost/utility/string_view_fwd.hpp>
#include <boost/throw_exception.hpp>
#include <boost/container_hash/hash_fwd.hpp>
@@ -571,53 +573,12 @@ namespace boost {
return basic_string_view<charT, traits>(x) >= y;
}
namespace detail {
template<class charT, class traits>
inline void sv_insert_fill_chars(std::basic_ostream<charT, traits>& os, std::size_t n) {
enum { chunk_size = 8 };
charT fill_chars[chunk_size];
std::fill_n(fill_chars, static_cast< std::size_t >(chunk_size), os.fill());
for (; n >= chunk_size && os.good(); n -= chunk_size)
os.write(fill_chars, static_cast< std::size_t >(chunk_size));
if (n > 0 && os.good())
os.write(fill_chars, n);
}
template<class charT, class traits>
void sv_insert_aligned(std::basic_ostream<charT, traits>& os, const basic_string_view<charT,traits>& str) {
const std::size_t size = str.size();
const std::size_t alignment_size = static_cast< std::size_t >(os.width()) - size;
const bool align_left = (os.flags() & std::basic_ostream<charT, traits>::adjustfield) == std::basic_ostream<charT, traits>::left;
if (!align_left) {
detail::sv_insert_fill_chars(os, alignment_size);
if (os.good())
os.write(str.data(), size);
}
else {
os.write(str.data(), size);
if (os.good())
detail::sv_insert_fill_chars(os, alignment_size);
}
}
} // namespace detail
// Inserter
template<class charT, class traits>
inline std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os,
const basic_string_view<charT,traits>& str) {
if (os.good()) {
const std::size_t size = str.size();
const std::size_t w = static_cast< std::size_t >(os.width());
if (w <= size)
os.write(str.data(), size);
else
detail::sv_insert_aligned(os, str);
os.width(0);
}
return os;
return boost::ostream_string(os, str.data(), str.size());
}
#if 0

View File

@@ -31,6 +31,7 @@
<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>

View File

@@ -42,3 +42,5 @@ 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 ;

View 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/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();
}