Commit fced34e3 authored by gabime's avatar gabime

bumped fmt version to 4.0.0

parent 268222e4
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -11,9 +11,8 @@ ...@@ -11,9 +11,8 @@
namespace fmt { namespace fmt {
namespace { namespace internal {
// Write the content of w to os. FMT_FUNC void write(std::ostream &os, Writer &w) {
void write(std::ostream &os, Writer &w) {
const char *data = w.data(); const char *data = w.data();
typedef internal::MakeUnsigned<std::streamsize>::Type UnsignedStreamSize; typedef internal::MakeUnsigned<std::streamsize>::Type UnsignedStreamSize;
UnsignedStreamSize size = w.size(); UnsignedStreamSize size = w.size();
...@@ -31,13 +30,6 @@ void write(std::ostream &os, Writer &w) { ...@@ -31,13 +30,6 @@ void write(std::ostream &os, Writer &w) {
FMT_FUNC void print(std::ostream &os, CStringRef format_str, ArgList args) { FMT_FUNC void print(std::ostream &os, CStringRef format_str, ArgList args) {
MemoryWriter w; MemoryWriter w;
w.write(format_str, args); w.write(format_str, args);
write(os, w); internal::write(os, w);
}
FMT_FUNC int fprintf(std::ostream &os, CStringRef format, ArgList args) {
MemoryWriter w;
printf(w, format, args);
write(os, w);
return static_cast<int>(w.size());
} }
} // namespace fmt } // namespace fmt
...@@ -13,53 +13,44 @@ ...@@ -13,53 +13,44 @@
#include "format.h" #include "format.h"
#include <ostream> #include <ostream>
namespace fmt namespace fmt {
{
namespace internal namespace internal {
{
template <class Char> template <class Char>
class FormatBuf : public std::basic_streambuf<Char> class FormatBuf : public std::basic_streambuf<Char> {
{ private:
private:
typedef typename std::basic_streambuf<Char>::int_type int_type; typedef typename std::basic_streambuf<Char>::int_type int_type;
typedef typename std::basic_streambuf<Char>::traits_type traits_type; typedef typename std::basic_streambuf<Char>::traits_type traits_type;
Buffer<Char> &buffer_; Buffer<Char> &buffer_;
Char *start_;
public: public:
FormatBuf(Buffer<Char> &buffer) : buffer_(buffer), start_(&buffer[0]) FormatBuf(Buffer<Char> &buffer) : buffer_(buffer) {}
{
this->setp(start_, start_ + buffer_.capacity()); protected:
} // The put-area is actually always empty. This makes the implementation
// simpler and has the advantage that the streambuf and the buffer are always
// in sync and sputc never writes into uninitialized memory. The obvious
// disadvantage is that each call to sputc always results in a (virtual) call
// to overflow. There is no disadvantage here for sputn since this always
// results in a call to xsputn.
int_type overflow(int_type ch = traits_type::eof()) int_type overflow(int_type ch = traits_type::eof()) FMT_OVERRIDE {
{
if (!traits_type::eq_int_type(ch, traits_type::eof())) if (!traits_type::eq_int_type(ch, traits_type::eof()))
{ buffer_.push_back(static_cast<Char>(ch));
size_t buf_size = size();
buffer_.resize(buf_size);
buffer_.reserve(buf_size * 2);
start_ = &buffer_[0];
start_[buf_size] = traits_type::to_char_type(ch);
this->setp(start_+ buf_size + 1, start_ + buf_size * 2);
}
return ch; return ch;
} }
size_t size() const std::streamsize xsputn(const Char *s, std::streamsize count) FMT_OVERRIDE {
{ buffer_.append(s, s + count);
return to_unsigned(this->pptr() - start_); return count;
} }
}; };
Yes &convert(std::ostream &); Yes &convert(std::ostream &);
struct DummyStream : std::ostream struct DummyStream : std::ostream {
{
DummyStream(); // Suppress a bogus warning in MSVC. DummyStream(); // Suppress a bogus warning in MSVC.
// Hide all operator<< overloads from std::ostream. // Hide all operator<< overloads from std::ostream.
void operator<<(Null<>); void operator<<(Null<>);
...@@ -68,28 +59,28 @@ struct DummyStream : std::ostream ...@@ -68,28 +59,28 @@ struct DummyStream : std::ostream
No &operator<<(std::ostream &, int); No &operator<<(std::ostream &, int);
template<typename T> template<typename T>
struct ConvertToIntImpl<T, true> struct ConvertToIntImpl<T, true> {
{
// Convert to int only if T doesn't have an overloaded operator<<. // Convert to int only if T doesn't have an overloaded operator<<.
enum enum {
{
value = sizeof(convert(get<DummyStream>() << get<T>())) == sizeof(No) value = sizeof(convert(get<DummyStream>() << get<T>())) == sizeof(No)
}; };
}; };
// Write the content of w to os.
FMT_API void write(std::ostream &os, Writer &w);
} // namespace internal } // namespace internal
// Formats a value. // Formats a value.
template <typename Char, typename ArgFormatter, typename T> template <typename Char, typename ArgFormatter_, typename T>
void format(BasicFormatter<Char, ArgFormatter> &f, void format_arg(BasicFormatter<Char, ArgFormatter_> &f,
const Char *&format_str, const T &value) const Char *&format_str, const T &value) {
{
internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer; internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
internal::FormatBuf<Char> format_buf(buffer); internal::FormatBuf<Char> format_buf(buffer);
std::basic_ostream<Char> output(&format_buf); std::basic_ostream<Char> output(&format_buf);
output << value; output << value;
BasicStringRef<Char> str(&buffer[0], format_buf.size()); BasicStringRef<Char> str(&buffer[0], buffer.size());
typedef internal::MakeArg< BasicFormatter<Char> > MakeArg; typedef internal::MakeArg< BasicFormatter<Char> > MakeArg;
format_str = f.format(format_str, MakeArg(str)); format_str = f.format(format_str, MakeArg(str));
} }
...@@ -105,18 +96,6 @@ void format(BasicFormatter<Char, ArgFormatter> &f, ...@@ -105,18 +96,6 @@ void format(BasicFormatter<Char, ArgFormatter> &f,
*/ */
FMT_API void print(std::ostream &os, CStringRef format_str, ArgList args); FMT_API void print(std::ostream &os, CStringRef format_str, ArgList args);
FMT_VARIADIC(void, print, std::ostream &, CStringRef) FMT_VARIADIC(void, print, std::ostream &, CStringRef)
/**
\rst
Prints formatted data to the stream *os*.
**Example**::
fprintf(cerr, "Don't %s!", "panic");
\endrst
*/
FMT_API int fprintf(std::ostream &os, CStringRef format_str, ArgList args);
FMT_VARIADIC(int, fprintf, std::ostream &, CStringRef)
} // namespace fmt } // namespace fmt
#ifdef FMT_HEADER_ONLY #ifdef FMT_HEADER_ONLY
......
...@@ -21,6 +21,9 @@ ...@@ -21,6 +21,9 @@
#ifndef _WIN32 #ifndef _WIN32
# include <unistd.h> # include <unistd.h>
#else #else
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h> # include <windows.h>
# include <io.h> # include <io.h>
...@@ -79,7 +82,7 @@ void fmt::BufferedFile::close() { ...@@ -79,7 +82,7 @@ void fmt::BufferedFile::close() {
if (!file_) if (!file_)
return; return;
int result = FMT_SYSTEM(fclose(file_)); int result = FMT_SYSTEM(fclose(file_));
file_ = 0; file_ = FMT_NULL;
if (result != 0) if (result != 0)
FMT_THROW(SystemError(errno, "cannot close file")); FMT_THROW(SystemError(errno, "cannot close file"));
} }
......
This diff is collapsed.
...@@ -13,12 +13,16 @@ ...@@ -13,12 +13,16 @@
#include "format.h" #include "format.h"
#include <ctime> #include <ctime>
namespace fmt #ifdef _MSC_VER
{ # pragma warning(push)
# pragma warning(disable: 4702) // unreachable code
# pragma warning(disable: 4996) // "deprecated" functions
#endif
namespace fmt {
template <typename ArgFormatter> template <typename ArgFormatter>
void format(BasicFormatter<char, ArgFormatter> &f, void format_arg(BasicFormatter<char, ArgFormatter> &f,
const char *&format_str, const std::tm &tm) const char *&format_str, const std::tm &tm) {
{
if (*format_str == ':') if (*format_str == ':')
++format_str; ++format_str;
const char *end = format_str; const char *end = format_str;
...@@ -31,17 +35,14 @@ void format(BasicFormatter<char, ArgFormatter> &f, ...@@ -31,17 +35,14 @@ void format(BasicFormatter<char, ArgFormatter> &f,
format[format.size() - 1] = '\0'; format[format.size() - 1] = '\0';
Buffer<char> &buffer = f.writer().buffer(); Buffer<char> &buffer = f.writer().buffer();
std::size_t start = buffer.size(); std::size_t start = buffer.size();
for (;;) for (;;) {
{
std::size_t size = buffer.capacity() - start; std::size_t size = buffer.capacity() - start;
std::size_t count = std::strftime(&buffer[start], size, &format[0], &tm); std::size_t count = std::strftime(&buffer[start], size, &format[0], &tm);
if (count != 0) if (count != 0) {
{
buffer.resize(start + count); buffer.resize(start + count);
break; break;
} }
if (size >= format.size() * 256) if (size >= format.size() * 256) {
{
// If the buffer is 256 times larger than the format string, assume // If the buffer is 256 times larger than the format string, assume
// that `strftime` gives an empty result. There doesn't seem to be a // that `strftime` gives an empty result. There doesn't seem to be a
// better way to distinguish the two cases: // better way to distinguish the two cases:
...@@ -53,6 +54,90 @@ void format(BasicFormatter<char, ArgFormatter> &f, ...@@ -53,6 +54,90 @@ void format(BasicFormatter<char, ArgFormatter> &f,
} }
format_str = end + 1; format_str = end + 1;
} }
namespace internal{
inline Null<> localtime_r(...) { return Null<>(); }
inline Null<> localtime_s(...) { return Null<>(); }
inline Null<> gmtime_r(...) { return Null<>(); }
inline Null<> gmtime_s(...) { return Null<>(); }
}
// Thread-safe replacement for std::localtime
inline std::tm localtime(std::time_t time) {
struct LocalTime {
std::time_t time_;
std::tm tm_;
LocalTime(std::time_t t): time_(t) {}
bool run() {
using namespace fmt::internal;
return handle(localtime_r(&time_, &tm_));
}
bool handle(std::tm *tm) { return tm != FMT_NULL; }
bool handle(internal::Null<>) {
using namespace fmt::internal;
return fallback(localtime_s(&tm_, &time_));
}
bool fallback(int res) { return res == 0; }
bool fallback(internal::Null<>) {
using namespace fmt::internal;
std::tm *tm = std::localtime(&time_);
if (tm) tm_ = *tm;
return tm != FMT_NULL;
}
};
LocalTime lt(time);
if (lt.run())
return lt.tm_;
// Too big time values may be unsupported.
FMT_THROW(fmt::FormatError("time_t value out of range"));
return std::tm();
} }
// Thread-safe replacement for std::gmtime
inline std::tm gmtime(std::time_t time) {
struct GMTime {
std::time_t time_;
std::tm tm_;
GMTime(std::time_t t): time_(t) {}
bool run() {
using namespace fmt::internal;
return handle(gmtime_r(&time_, &tm_));
}
bool handle(std::tm *tm) { return tm != FMT_NULL; }
bool handle(internal::Null<>) {
using namespace fmt::internal;
return fallback(gmtime_s(&tm_, &time_));
}
bool fallback(int res) { return res == 0; }
bool fallback(internal::Null<>) {
std::tm *tm = std::gmtime(&time_);
if (tm != FMT_NULL) tm_ = *tm;
return tm != FMT_NULL;
}
};
GMTime gt(time);
if (gt.run())
return gt.tm_;
// Too big time values may be unsupported.
FMT_THROW(fmt::FormatError("time_t value out of range"));
return std::tm();
}
} //namespace fmt
#ifdef _MSC_VER
# pragma warning(pop)
#endif
#endif // FMT_TIME_H_ #endif // FMT_TIME_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment