Merge ostream.cc into ostream.h

This commit is contained in:
Victor Zverovich 2017-12-17 09:33:12 -08:00
parent 955062da2e
commit 941663d038
3 changed files with 21 additions and 44 deletions

View File

@ -82,8 +82,8 @@ endfunction()
# Define the fmt library, its includes and the needed defines.
# format.cc is added to FMT_HEADERS for the header-only configuration.
add_headers(FMT_HEADERS core.h format.h format.cc locale.h ostream.h ostream.cc
printf.h string.h time.h)
add_headers(FMT_HEADERS core.h format.h format.cc locale.h ostream.h printf.h
string.h time.h)
if (HAVE_OPEN)
add_headers(FMT_HEADERS posix.h)
add_headers(FMT_SOURCES posix.cc)

View File

@ -1,36 +0,0 @@
/*
Formatting library for C++ - std::ostream support
Copyright (c) 2012 - 2016, Victor Zverovich
All rights reserved.
For the license information refer to format.h.
*/
#include "fmt/ostream.h"
namespace fmt {
namespace internal {
FMT_FUNC void write(std::ostream &os, buffer &buf) {
const char *data = buf.data();
typedef std::make_unsigned<std::streamsize>::type UnsignedStreamSize;
UnsignedStreamSize size = buf.size();
UnsignedStreamSize max_size =
internal::to_unsigned((std::numeric_limits<std::streamsize>::max)());
do {
UnsignedStreamSize n = size <= max_size ? size : max_size;
os.write(data, static_cast<std::streamsize>(n));
data += n;
size -= n;
} while (size != 0);
}
}
FMT_FUNC void vprint(std::ostream &os, string_view format_str,
format_args args) {
memory_buffer buffer;
vformat_to(buffer, format_str, args);
internal::write(os, buffer);
}
} // namespace fmt

View File

@ -69,7 +69,20 @@ struct convert_to_int_impl<T, true> {
};
// Write the content of buf to os.
void write(std::ostream &os, buffer &buf);
template <typename Char>
void write(std::basic_ostream<Char> &os, basic_buffer<Char> &buf) {
const Char *data = buf.data();
typedef std::make_unsigned<std::streamsize>::type UnsignedStreamSize;
UnsignedStreamSize size = buf.size();
UnsignedStreamSize max_size =
internal::to_unsigned((std::numeric_limits<std::streamsize>::max)());
do {
UnsignedStreamSize n = size <= max_size ? size : max_size;
os.write(data, static_cast<std::streamsize>(n));
data += n;
size -= n;
} while (size != 0);
}
template <typename Char, typename T>
void format_value(basic_buffer<Char> &buffer, const T &value) {
@ -100,7 +113,11 @@ struct formatter<T, Char,
}
};
FMT_API void vprint(std::ostream &os, string_view format_str, format_args args);
inline void vprint(std::ostream &os, string_view format_str, format_args args) {
memory_buffer buffer;
vformat_to(buffer, format_str, args);
internal::write(os, buffer);
}
/**
\rst
@ -118,8 +135,4 @@ inline void print(std::ostream &os, string_view format_str,
}
} // namespace fmt
#ifdef FMT_HEADER_ONLY
# include "ostream.cc"
#endif
#endif // FMT_OSTREAM_H_