Commit e5376c3c authored by gabime's avatar gabime

updated cppformat to latest version (52f89065e1843f4123198df326b480380d993312)

parent 8dbf88f9
...@@ -77,26 +77,29 @@ using fmt::internal::Arg; ...@@ -77,26 +77,29 @@ using fmt::internal::Arg;
// Dummy implementations of strerror_r and strerror_s called if corresponding // Dummy implementations of strerror_r and strerror_s called if corresponding
// system functions are not available. // system functions are not available.
static inline fmt::internal::Null<> strerror_r(int, char *, ...) { static inline fmt::internal::Null<> strerror_r(int, char *, ...)
return fmt::internal::Null<>(); {
return fmt::internal::Null<>();
} }
static inline fmt::internal::Null<> strerror_s(char *, std::size_t, ...) { static inline fmt::internal::Null<> strerror_s(char *, std::size_t, ...)
return fmt::internal::Null<>(); {
return fmt::internal::Null<>();
} }
namespace fmt { namespace fmt {
namespace { namespace {
#ifndef _MSC_VER #ifndef _MSC_VER
# define FMT_SNPRINTF snprintf # define FMT_SNPRINTF snprintf
#else // _MSC_VER #else // _MSC_VER
inline int fmt_snprintf(char *buffer, size_t size, const char *format, ...) { inline int fmt_snprintf(char *buffer, size_t size, const char *format, ...)
va_list args; {
va_start(args, format); va_list args;
int result = vsnprintf_s(buffer, size, _TRUNCATE, format, args); va_start(args, format);
va_end(args); int result = vsnprintf_s(buffer, size, _TRUNCATE, format, args);
return result; va_end(args);
} return result;
}
# define FMT_SNPRINTF fmt_snprintf # define FMT_SNPRINTF fmt_snprintf
#endif // _MSC_VER #endif // _MSC_VER
...@@ -106,370 +109,461 @@ inline int fmt_snprintf(char *buffer, size_t size, const char *format, ...) { ...@@ -106,370 +109,461 @@ inline int fmt_snprintf(char *buffer, size_t size, const char *format, ...) {
# define FMT_SWPRINTF swprintf # define FMT_SWPRINTF swprintf
#endif // defined(_WIN32) && defined(__MINGW32__) && !defined(__NO_ISOCEXT) #endif // defined(_WIN32) && defined(__MINGW32__) && !defined(__NO_ISOCEXT)
// Checks if a value fits in int - used to avoid warnings about comparing // Checks if a value fits in int - used to avoid warnings about comparing
// signed and unsigned integers. // signed and unsigned integers.
template <bool IsSigned> template <bool IsSigned>
struct IntChecker { struct IntChecker
template <typename T> {
static bool fits_in_int(T value) { template <typename T>
unsigned max = INT_MAX; static bool fits_in_int(T value)
return value <= max; {
} unsigned max = INT_MAX;
static bool fits_in_int(bool) { return value <= max;
return true; }
} static bool fits_in_int(bool)
}; {
return true;
template <> }
struct IntChecker<true> { };
template <typename T>
static bool fits_in_int(T value) { template <>
return value >= INT_MIN && value <= INT_MAX; struct IntChecker<true>
} {
static bool fits_in_int(int) { template <typename T>
return true; static bool fits_in_int(T value)
} {
}; return value >= INT_MIN && value <= INT_MAX;
}
const char RESET_COLOR[] = "\x1b[0m"; static bool fits_in_int(int)
{
typedef void(*FormatFunc)(fmt::Writer &, int, fmt::StringRef); return true;
}
// Portable thread-safe version of strerror. };
// Sets buffer to point to a string describing the error code.
// This can be either a pointer to a string stored in buffer, const char RESET_COLOR[] = "\x1b[0m";
// or a pointer to some static immutable string.
// Returns one of the following values: typedef void(*FormatFunc)(fmt::Writer &, int, fmt::StringRef);
// 0 - success
// ERANGE - buffer is not large enough to store the error message // Portable thread-safe version of strerror.
// other - failure // Sets buffer to point to a string describing the error code.
// Buffer should be at least of size 1. // This can be either a pointer to a string stored in buffer,
int safe_strerror( // or a pointer to some static immutable string.
int error_code, char *&buffer, std::size_t buffer_size) FMT_NOEXCEPT{ // Returns one of the following values:
FMT_ASSERT(buffer != 0 && buffer_size != 0, "invalid buffer"); // 0 - success
// ERANGE - buffer is not large enough to store the error message
class StrError { // other - failure
private: // Buffer should be at least of size 1.
int error_code_; int safe_strerror(
char *&buffer_; int error_code, char *&buffer, std::size_t buffer_size) FMT_NOEXCEPT
std::size_t buffer_size_; {
FMT_ASSERT(buffer != 0 && buffer_size != 0, "invalid buffer");
// A noop assignment operator to avoid bogus warnings.
void operator=(const StrError &) {} class StrError
{
// Handle the result of XSI-compliant version of strerror_r. private:
int handle(int result) { int error_code_;
// glibc versions before 2.13 return result in errno. char *&buffer_;
return result == -1 ? errno : result; std::size_t buffer_size_;
}
// A noop assignment operator to avoid bogus warnings.
// Handle the result of GNU-specific version of strerror_r. void operator=(const StrError &)
int handle(char *message) { {}
// If the buffer is full then the message is probably truncated.
if (message == buffer_ && strlen(buffer_) == buffer_size_ - 1) // Handle the result of XSI-compliant version of strerror_r.
return ERANGE; int handle(int result)
buffer_ = message; {
return 0; // glibc versions before 2.13 return result in errno.
} return result == -1 ? errno : result;
}
// Handle the case when strerror_r is not available.
int handle(fmt::internal::Null<>) { // Handle the result of GNU-specific version of strerror_r.
return fallback(strerror_s(buffer_, buffer_size_, error_code_)); int handle(char *message)
} {
// If the buffer is full then the message is probably truncated.
// Fallback to strerror_s when strerror_r is not available. if (message == buffer_ && strlen(buffer_) == buffer_size_ - 1)
int fallback(int result) { return ERANGE;
// If the buffer is full then the message is probably truncated. buffer_ = message;
return result == 0 && strlen(buffer_) == buffer_size_ - 1 ? return 0;
ERANGE : result; }
}
// Handle the case when strerror_r is not available.
// Fallback to strerror if strerror_r and strerror_s are not available. int handle(fmt::internal::Null<>)
int fallback(fmt::internal::Null<>) { {
errno = 0; return fallback(strerror_s(buffer_, buffer_size_, error_code_));
buffer_ = strerror(error_code_); }
return errno;
} // Fallback to strerror_s when strerror_r is not available.
int fallback(int result)
public: {
StrError(int err_code, char *&buf, std::size_t buf_size) // If the buffer is full then the message is probably truncated.
: error_code_(err_code), buffer_(buf), buffer_size_(buf_size) {} return result == 0 && strlen(buffer_) == buffer_size_ - 1 ?
ERANGE : result;
int run() { }
strerror_r(0, 0, ""); // Suppress a warning about unused strerror_r.
return handle(strerror_r(error_code_, buffer_, buffer_size_)); // Fallback to strerror if strerror_r and strerror_s are not available.
} int fallback(fmt::internal::Null<>)
}; {
return StrError(error_code, buffer, buffer_size).run(); errno = 0;
} buffer_ = strerror(error_code_);
return errno;
void format_error_code(fmt::Writer &out, int error_code, }
fmt::StringRef message) FMT_NOEXCEPT{
// Report error code making sure that the output fits into public:
// INLINE_BUFFER_SIZE to avoid dynamic memory allocation and potential StrError(int err_code, char *&buf, std::size_t buf_size)
// bad_alloc. : error_code_(err_code), buffer_(buf), buffer_size_(buf_size)
out.clear(); {}
static const char SEP[] = ": ";
static const char ERROR_STR[] = "error "; int run()
fmt::internal::IntTraits<int>::MainType ec_value = error_code; {
// Subtract 2 to account for terminating null characters in SEP and ERROR_STR. strerror_r(0, 0, ""); // Suppress a warning about unused strerror_r.
std::size_t error_code_size = sizeof(SEP) + sizeof(ERROR_STR) - 2; return handle(strerror_r(error_code_, buffer_, buffer_size_));
error_code_size += fmt::internal::count_digits(ec_value); }
if (message.size() <= fmt::internal::INLINE_BUFFER_SIZE - error_code_size) };
out << message << SEP; return StrError(error_code, buffer, buffer_size).run();
out << ERROR_STR << error_code; }
assert(out.size() <= fmt::internal::INLINE_BUFFER_SIZE);
} void format_error_code(fmt::Writer &out, int error_code,
fmt::StringRef message) FMT_NOEXCEPT
void report_error(FormatFunc func, {
int error_code, fmt::StringRef message) FMT_NOEXCEPT{ // Report error code making sure that the output fits into
fmt::MemoryWriter full_message; // INLINE_BUFFER_SIZE to avoid dynamic memory allocation and potential
func(full_message, error_code, message); // bad_alloc.
// Use Writer::data instead of Writer::c_str to avoid potential memory out.clear();
// allocation. static const char SEP[] = ": ";
std::fwrite(full_message.data(), full_message.size(), 1, stderr); static const char ERROR_STR[] = "error ";
std::fputc('\n', stderr); // Subtract 2 to account for terminating null characters in SEP and ERROR_STR.
} std::size_t error_code_size = sizeof(SEP) + sizeof(ERROR_STR) - 2;
typedef fmt::internal::IntTraits<int>::MainType MainType;
// IsZeroInt::visit(arg) returns true iff arg is a zero integer. MainType abs_value = static_cast<MainType>(error_code);
class IsZeroInt : public fmt::internal::ArgVisitor<IsZeroInt, bool> { if (internal::is_negative(error_code)) {
public: abs_value = 0 - abs_value;
template <typename T> ++error_code_size;
bool visit_any_int(T value) { }
return value == 0; error_code_size += fmt::internal::count_digits(abs_value);
} if (message.size() <= fmt::internal::INLINE_BUFFER_SIZE - error_code_size)
}; out << message << SEP;
out << ERROR_STR << error_code;
// Checks if an argument is a valid printf width specifier and sets assert(out.size() <= fmt::internal::INLINE_BUFFER_SIZE);
// left alignment if it is negative. }
class WidthHandler : public fmt::internal::ArgVisitor<WidthHandler, unsigned> {
private: void report_error(FormatFunc func,
fmt::FormatSpec &spec_; int error_code, fmt::StringRef message) FMT_NOEXCEPT
{
FMT_DISALLOW_COPY_AND_ASSIGN(WidthHandler); fmt::MemoryWriter full_message;
func(full_message, error_code, message);
public: // Use Writer::data instead of Writer::c_str to avoid potential memory
explicit WidthHandler(fmt::FormatSpec &spec) : spec_(spec) {} // allocation.
std::fwrite(full_message.data(), full_message.size(), 1, stderr);
void report_unhandled_arg() { std::fputc('\n', stderr);
FMT_THROW(fmt::FormatError("width is not integer")); }
}
// IsZeroInt::visit(arg) returns true iff arg is a zero integer.
template <typename T> class IsZeroInt: public fmt::internal::ArgVisitor<IsZeroInt, bool>
unsigned visit_any_int(T value) { {
typedef typename fmt::internal::IntTraits<T>::MainType UnsignedType; public:
UnsignedType width = value; template <typename T>
if (fmt::internal::is_negative(value)) { bool visit_any_int(T value)
spec_.align_ = fmt::ALIGN_LEFT; {
width = 0 - width; return value == 0;
} }
if (width > INT_MAX) };
FMT_THROW(fmt::FormatError("number is too big"));
return static_cast<unsigned>(width); // Checks if an argument is a valid printf width specifier and sets
} // left alignment if it is negative.
}; class WidthHandler: public fmt::internal::ArgVisitor<WidthHandler, unsigned>
{
class PrecisionHandler : private:
public fmt::internal::ArgVisitor<PrecisionHandler, int> { fmt::FormatSpec &spec_;
public:
void report_unhandled_arg() { FMT_DISALLOW_COPY_AND_ASSIGN(WidthHandler);
FMT_THROW(fmt::FormatError("precision is not integer"));
} public:
explicit WidthHandler(fmt::FormatSpec &spec): spec_(spec)
template <typename T> {}
int visit_any_int(T value) {
if (!IntChecker<std::numeric_limits<T>::is_signed>::fits_in_int(value)) void report_unhandled_arg()
FMT_THROW(fmt::FormatError("number is too big")); {
return static_cast<int>(value); FMT_THROW(fmt::FormatError("width is not integer"));
} }
};
template <typename T>
// Converts an integer argument to an integral type T for printf. unsigned visit_any_int(T value)
template <typename T> {
class ArgConverter : public fmt::internal::ArgVisitor<ArgConverter<T>, void> { typedef typename fmt::internal::IntTraits<T>::MainType UnsignedType;
private: UnsignedType width = static_cast<UnsignedType>(value);
fmt::internal::Arg &arg_; if (fmt::internal::is_negative(value)) {
wchar_t type_; spec_.align_ = fmt::ALIGN_LEFT;
width = 0 - width;
FMT_DISALLOW_COPY_AND_ASSIGN(ArgConverter); }
if (width > INT_MAX)
public: FMT_THROW(fmt::FormatError("number is too big"));
ArgConverter(fmt::internal::Arg &arg, wchar_t type) return static_cast<unsigned>(width);
: arg_(arg), type_(type) {} }
};
void visit_bool(bool value) {
if (type_ != 's') class PrecisionHandler:
visit_any_int(value); public fmt::internal::ArgVisitor<PrecisionHandler, int>
} {
public:
template <typename U> void report_unhandled_arg()
void visit_any_int(U value) { {
bool is_signed = type_ == 'd' || type_ == 'i'; FMT_THROW(fmt::FormatError("precision is not integer"));
using fmt::internal::Arg; }
if (sizeof(T) <= sizeof(int)) {
// Extra casts are used to silence warnings. template <typename T>
if (is_signed) { int visit_any_int(T value)
arg_.type = Arg::INT; {
arg_.int_value = static_cast<int>(static_cast<T>(value)); if (!IntChecker<std::numeric_limits<T>::is_signed>::fits_in_int(value))
} FMT_THROW(fmt::FormatError("number is too big"));
else { return static_cast<int>(value);
arg_.type = Arg::UINT; }
arg_.uint_value = static_cast<unsigned>( };
static_cast<typename fmt::internal::MakeUnsigned<T>::Type>(value));
} template <typename T, typename U>
} struct is_same
else { {
if (is_signed) { enum
arg_.type = Arg::LONG_LONG; {
arg_.long_long_value = value = 0
static_cast<typename fmt::internal::MakeUnsigned<U>::Type>(value); };
} };
else {
arg_.type = Arg::ULONG_LONG; template <typename T>
arg_.ulong_long_value = struct is_same<T, T>
static_cast<typename fmt::internal::MakeUnsigned<U>::Type>(value); {
} enum
} {
} value = 1
}; };
};
// Converts an integer argument to char for printf.
class CharConverter : public fmt::internal::ArgVisitor<CharConverter, void> { // An argument visitor that converts an integer argument to T for printf,
private: // if T is an integral type. If T is void, the argument is converted to
fmt::internal::Arg &arg_; // corresponding signed or unsigned type depending on the type specifier:
// 'd' and 'i' - signed, other - unsigned)
FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter); template <typename T = void>
class ArgConverter: public fmt::internal::ArgVisitor<ArgConverter<T>, void>
public: {
explicit CharConverter(fmt::internal::Arg &arg) : arg_(arg) {} private:
fmt::internal::Arg &arg_;
template <typename T> wchar_t type_;
void visit_any_int(T value) {
arg_.type = Arg::CHAR; FMT_DISALLOW_COPY_AND_ASSIGN(ArgConverter);
arg_.int_value = static_cast<char>(value);
} public:
}; ArgConverter(fmt::internal::Arg &arg, wchar_t type)
} // namespace : arg_(arg), type_(type)
{}
namespace internal {
void visit_bool(bool value)
template <typename Char> {
class PrintfArgFormatter : if (type_ != 's')
public ArgFormatterBase<PrintfArgFormatter<Char>, Char> { visit_any_int(value);
}
void write_null_pointer() {
this->spec().type_ = 0; template <typename U>
this->write("(nil)"); void visit_any_int(U value)
} {
bool is_signed = type_ == 'd' || type_ == 'i';
typedef ArgFormatterBase<PrintfArgFormatter<Char>, Char> Base; using fmt::internal::Arg;
typedef typename fmt::internal::Conditional<
public: is_same<T, void>::value, U, T>::type TargetType;
PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s) if (sizeof(TargetType) <= sizeof(int)) {
: ArgFormatterBase<PrintfArgFormatter<Char>, Char>(w, s) {} // Extra casts are used to silence warnings.
if (is_signed) {
void visit_bool(bool value) { arg_.type = Arg::INT;
FormatSpec &fmt_spec = this->spec(); arg_.int_value = static_cast<int>(static_cast<TargetType>(value));
if (fmt_spec.type_ != 's') }
return this->visit_any_int(value); else {
fmt_spec.type_ = 0; arg_.type = Arg::UINT;
this->write(value); typedef typename fmt::internal::MakeUnsigned<TargetType>::Type Unsigned;
} arg_.uint_value = static_cast<unsigned>(static_cast<Unsigned>(value));
}
void visit_char(int value) { }
const FormatSpec &fmt_spec = this->spec(); else {
BasicWriter<Char> &w = this->writer(); if (is_signed) {
if (fmt_spec.type_ && fmt_spec.type_ != 'c') arg_.type = Arg::LONG_LONG;
w.write_int(value, fmt_spec); // glibc's printf doesn't sign extend arguments of smaller types:
typedef typename BasicWriter<Char>::CharPtr CharPtr; // std::printf("%lld", -42); // prints "4294967254"
CharPtr out = CharPtr(); // but we don't have to do the same because it's a UB.
if (fmt_spec.width_ > 1) { arg_.long_long_value = static_cast<fmt::LongLong>(value);
Char fill = ' '; }
out = w.grow_buffer(fmt_spec.width_); else {
if (fmt_spec.align_ != ALIGN_LEFT) { arg_.type = Arg::ULONG_LONG;
std::fill_n(out, fmt_spec.width_ - 1, fill); arg_.ulong_long_value =
out += fmt_spec.width_ - 1; static_cast<typename fmt::internal::MakeUnsigned<U>::Type>(value);
} }
else { }
std::fill_n(out + 1, fmt_spec.width_ - 1, fill); }
} };
}
else { // Converts an integer argument to char for printf.
out = w.grow_buffer(1); class CharConverter: public fmt::internal::ArgVisitor<CharConverter, void>
} {
*out = static_cast<Char>(value); private:
} fmt::internal::Arg &arg_;
void visit_cstring(const char *value) { FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter);
if (value)
Base::visit_cstring(value); public:
else if (this->spec().type_ == 'p') explicit CharConverter(fmt::internal::Arg &arg): arg_(arg)
write_null_pointer(); {}
else
this->write("(null)"); template <typename T>
} void visit_any_int(T value)
{
void visit_pointer(const void *value) { arg_.type = Arg::CHAR;
if (value) arg_.int_value = static_cast<char>(value);
return Base::visit_pointer(value); }
this->spec().type_ = 0; };
write_null_pointer();
} // Write the content of w to os.
void write(std::ostream &os, fmt::Writer &w)
void visit_custom(Arg::CustomValue c) { {
BasicFormatter<Char> formatter(ArgList(), this->writer()); const char *data = w.data();
const Char format_str[] = { '}', 0 }; typedef internal::MakeUnsigned<std::streamsize>::Type UnsignedStreamSize;
const Char *format = format_str; UnsignedStreamSize size = w.size();
c.format(&formatter, c.value, &format); UnsignedStreamSize max_size =
} internal::to_unsigned((std::numeric_limits<std::streamsize>::max)());
}; do {
} // namespace internal UnsignedStreamSize n = size <= max_size ? size : max_size;
os.write(data, static_cast<std::streamsize>(n));
data += n;
size -= n;
} while (size != 0);
}
} // namespace
namespace internal {
template <typename Char>
class PrintfArgFormatter:
public ArgFormatterBase<PrintfArgFormatter<Char>, Char>
{
void write_null_pointer()
{
this->spec().type_ = 0;
this->write("(nil)");
}
typedef ArgFormatterBase<PrintfArgFormatter<Char>, Char> Base;
public:
PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
: ArgFormatterBase<PrintfArgFormatter<Char>, Char>(w, s)
{}
void visit_bool(bool value)
{
FormatSpec &fmt_spec = this->spec();
if (fmt_spec.type_ != 's')
return this->visit_any_int(value);
fmt_spec.type_ = 0;
this->write(value);
}
void visit_char(int value)
{
const FormatSpec &fmt_spec = this->spec();
BasicWriter<Char> &w = this->writer();
if (fmt_spec.type_ && fmt_spec.type_ != 'c')
w.write_int(value, fmt_spec);
typedef typename BasicWriter<Char>::CharPtr CharPtr;
CharPtr out = CharPtr();
if (fmt_spec.width_ > 1) {
Char fill = ' ';
out = w.grow_buffer(fmt_spec.width_);
if (fmt_spec.align_ != ALIGN_LEFT) {
std::fill_n(out, fmt_spec.width_ - 1, fill);
out += fmt_spec.width_ - 1;
}
else {
std::fill_n(out + 1, fmt_spec.width_ - 1, fill);
}
}
else {
out = w.grow_buffer(1);
}
*out = static_cast<Char>(value);
}
void visit_cstring(const char *value)
{
if (value)
Base::visit_cstring(value);
else if (this->spec().type_ == 'p')
write_null_pointer();
else
this->write("(null)");
}
void visit_pointer(const void *value)
{
if (value)
return Base::visit_pointer(value);
this->spec().type_ = 0;
write_null_pointer();
}
void visit_custom(Arg::CustomValue c)
{
BasicFormatter<Char> formatter(ArgList(), this->writer());
const Char format_str[] = { '}', 0 };
const Char *format = format_str;
c.format(&formatter, c.value, &format);
}
};
} // namespace internal
} // namespace fmt } // namespace fmt
FMT_FUNC void fmt::SystemError::init( FMT_FUNC void fmt::SystemError::init(
int err_code, CStringRef format_str, ArgList args) { int err_code, CStringRef format_str, ArgList args)
error_code_ = err_code; {
MemoryWriter w; error_code_ = err_code;
internal::format_system_error(w, err_code, format(format_str, args)); MemoryWriter w;
std::runtime_error &base = *this; internal::format_system_error(w, err_code, format(format_str, args));
base = std::runtime_error(w.str()); std::runtime_error &base = *this;
base = std::runtime_error(w.str());
} }
template <typename T> template <typename T>
int fmt::internal::CharTraits<char>::format_float( int fmt::internal::CharTraits<char>::format_float(
char *buffer, std::size_t size, const char *format, char *buffer, std::size_t size, const char *format,
unsigned width, int precision, T value) { unsigned width, int precision, T value)
if (width == 0) { {
return precision < 0 ? if (width == 0) {
FMT_SNPRINTF(buffer, size, format, value) : return precision < 0 ?
FMT_SNPRINTF(buffer, size, format, precision, value); FMT_SNPRINTF(buffer, size, format, value) :
} FMT_SNPRINTF(buffer, size, format, precision, value);
return precision < 0 ? }
FMT_SNPRINTF(buffer, size, format, width, value) : return precision < 0 ?
FMT_SNPRINTF(buffer, size, format, width, precision, value); FMT_SNPRINTF(buffer, size, format, width, value) :
FMT_SNPRINTF(buffer, size, format, width, precision, value);
} }
template <typename T> template <typename T>
int fmt::internal::CharTraits<wchar_t>::format_float( int fmt::internal::CharTraits<wchar_t>::format_float(
wchar_t *buffer, std::size_t size, const wchar_t *format, wchar_t *buffer, std::size_t size, const wchar_t *format,
unsigned width, int precision, T value) { unsigned width, int precision, T value)
if (width == 0) { {
return precision < 0 ? if (width == 0) {
FMT_SWPRINTF(buffer, size, format, value) : return precision < 0 ?
FMT_SWPRINTF(buffer, size, format, precision, value); FMT_SWPRINTF(buffer, size, format, value) :
} FMT_SWPRINTF(buffer, size, format, precision, value);
return precision < 0 ? }
FMT_SWPRINTF(buffer, size, format, width, value) : return precision < 0 ?
FMT_SWPRINTF(buffer, size, format, width, precision, value); FMT_SWPRINTF(buffer, size, format, width, value) :
FMT_SWPRINTF(buffer, size, format, width, precision, value);
} }
template <typename T> template <typename T>
const char fmt::internal::BasicData<T>::DIGITS[] = const char fmt::internal::BasicData<T>::DIGITS[] =
"0001020304050607080910111213141516171819" "0001020304050607080910111213141516171819"
"2021222324252627282930313233343536373839" "2021222324252627282930313233343536373839"
"4041424344454647484950515253545556575859" "4041424344454647484950515253545556575859"
"6061626364656667686970717273747576777879" "6061626364656667686970717273747576777879"
"8081828384858687888990919293949596979899"; "8081828384858687888990919293949596979899";
#define FMT_POWERS_OF_10(factor) \ #define FMT_POWERS_OF_10(factor) \
factor * 10, \ factor * 10, \
...@@ -484,424 +578,447 @@ const char fmt::internal::BasicData<T>::DIGITS[] = ...@@ -484,424 +578,447 @@ const char fmt::internal::BasicData<T>::DIGITS[] =
template <typename T> template <typename T>
const uint32_t fmt::internal::BasicData<T>::POWERS_OF_10_32[] = { const uint32_t fmt::internal::BasicData<T>::POWERS_OF_10_32[] = {
0, FMT_POWERS_OF_10(1) 0, FMT_POWERS_OF_10(1)
}; };
template <typename T> template <typename T>
const uint64_t fmt::internal::BasicData<T>::POWERS_OF_10_64[] = { const uint64_t fmt::internal::BasicData<T>::POWERS_OF_10_64[] = {
0, 0,
FMT_POWERS_OF_10(1), FMT_POWERS_OF_10(1),
FMT_POWERS_OF_10(fmt::ULongLong(1000000000)), FMT_POWERS_OF_10(fmt::ULongLong(1000000000)),
// Multiply several constants instead of using a single long long constant // Multiply several constants instead of using a single long long constant
// to avoid warnings about C++98 not supporting long long. // to avoid warnings about C++98 not supporting long long.
fmt::ULongLong(1000000000) * fmt::ULongLong(1000000000) * 10 fmt::ULongLong(1000000000) * fmt::ULongLong(1000000000) * 10
}; };
FMT_FUNC void fmt::internal::report_unknown_type(char code, const char *type) { FMT_FUNC void fmt::internal::report_unknown_type(char code, const char *type)
(void)type; {
if (std::isprint(static_cast<unsigned char>(code))) { (void)type;
FMT_THROW(fmt::FormatError( if (std::isprint(static_cast<unsigned char>(code))) {
fmt::format("unknown format code '{}' for {}", code, type))); FMT_THROW(fmt::FormatError(
} fmt::format("unknown format code '{}' for {}", code, type)));
FMT_THROW(fmt::FormatError( }
fmt::format("unknown format code '\\x{:02x}' for {}", FMT_THROW(fmt::FormatError(
static_cast<unsigned>(code), type))); fmt::format("unknown format code '\\x{:02x}' for {}",
static_cast<unsigned>(code), type)));
} }
#if FMT_USE_WINDOWS_H #if FMT_USE_WINDOWS_H
FMT_FUNC fmt::internal::UTF8ToUTF16::UTF8ToUTF16(fmt::StringRef s) { FMT_FUNC fmt::internal::UTF8ToUTF16::UTF8ToUTF16(fmt::StringRef s)
static const char ERROR_MSG[] = "cannot convert string from UTF-8 to UTF-16"; {
if (s.size() > INT_MAX) static const char ERROR_MSG[] = "cannot convert string from UTF-8 to UTF-16";
FMT_THROW(WindowsError(ERROR_INVALID_PARAMETER, ERROR_MSG)); if (s.size() > INT_MAX)
int s_size = static_cast<int>(s.size()); FMT_THROW(WindowsError(ERROR_INVALID_PARAMETER, ERROR_MSG));
int length = MultiByteToWideChar( int s_size = static_cast<int>(s.size());
CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), s_size, 0, 0); int length = MultiByteToWideChar(
if (length == 0) CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), s_size, 0, 0);
FMT_THROW(WindowsError(GetLastError(), ERROR_MSG)); if (length == 0)
buffer_.resize(length + 1); FMT_THROW(WindowsError(GetLastError(), ERROR_MSG));
length = MultiByteToWideChar( buffer_.resize(length + 1);
CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), s_size, &buffer_[0], length); length = MultiByteToWideChar(
if (length == 0) CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), s_size, &buffer_[0], length);
FMT_THROW(WindowsError(GetLastError(), ERROR_MSG)); if (length == 0)
buffer_[length] = 0; FMT_THROW(WindowsError(GetLastError(), ERROR_MSG));
buffer_[length] = 0;
} }
FMT_FUNC fmt::internal::UTF16ToUTF8::UTF16ToUTF8(fmt::WStringRef s) { FMT_FUNC fmt::internal::UTF16ToUTF8::UTF16ToUTF8(fmt::WStringRef s)
if (int error_code = convert(s)) { {
FMT_THROW(WindowsError(error_code, if (int error_code = convert(s)) {
"cannot convert string from UTF-16 to UTF-8")); FMT_THROW(WindowsError(error_code,
} "cannot convert string from UTF-16 to UTF-8"));
}
} }
FMT_FUNC int fmt::internal::UTF16ToUTF8::convert(fmt::WStringRef s) { FMT_FUNC int fmt::internal::UTF16ToUTF8::convert(fmt::WStringRef s)
if (s.size() > INT_MAX) {
return ERROR_INVALID_PARAMETER; if (s.size() > INT_MAX)
int s_size = static_cast<int>(s.size()); return ERROR_INVALID_PARAMETER;
int length = WideCharToMultiByte(CP_UTF8, 0, s.data(), s_size, 0, 0, 0, 0); int s_size = static_cast<int>(s.size());
if (length == 0) int length = WideCharToMultiByte(CP_UTF8, 0, s.data(), s_size, 0, 0, 0, 0);
return GetLastError(); if (length == 0)
buffer_.resize(length + 1); return GetLastError();
length = WideCharToMultiByte( buffer_.resize(length + 1);
CP_UTF8, 0, s.data(), s_size, &buffer_[0], length, 0, 0); length = WideCharToMultiByte(
if (length == 0) CP_UTF8, 0, s.data(), s_size, &buffer_[0], length, 0, 0);
return GetLastError(); if (length == 0)
buffer_[length] = 0; return GetLastError();
return 0; buffer_[length] = 0;
return 0;
} }
FMT_FUNC void fmt::WindowsError::init( FMT_FUNC void fmt::WindowsError::init(
int err_code, CStringRef format_str, ArgList args) { int err_code, CStringRef format_str, ArgList args)
error_code_ = err_code; {
MemoryWriter w; error_code_ = err_code;
internal::format_windows_error(w, err_code, format(format_str, args)); MemoryWriter w;
std::runtime_error &base = *this; internal::format_windows_error(w, err_code, format(format_str, args));
base = std::runtime_error(w.str()); std::runtime_error &base = *this;
base = std::runtime_error(w.str());
} }
FMT_FUNC void fmt::internal::format_windows_error( FMT_FUNC void fmt::internal::format_windows_error(
fmt::Writer &out, int error_code, fmt::Writer &out, int error_code,
fmt::StringRef message) FMT_NOEXCEPT{ fmt::StringRef message) FMT_NOEXCEPT
class String { {
private: FMT_TRY{
LPWSTR str_; MemoryBuffer<wchar_t, INLINE_BUFFER_SIZE> buffer;
buffer.resize(INLINE_BUFFER_SIZE);
public: for (;;) {
String() : str_() {} wchar_t *system_message = &buffer[0];
~String() { int result = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
LocalFree(str_); 0, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
} system_message, static_cast<uint32_t>(buffer.size()), 0);
LPWSTR *ptr() { if (result != 0) {
return &str_; UTF16ToUTF8 utf8_message;
} if (utf8_message.convert(system_message) == ERROR_SUCCESS) {
LPCWSTR c_str() const { return str_; } out << message << ": " << utf8_message;
}; return;
FMT_TRY{ }
String system_message; break;
if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | }
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), break; // Can't get error message, report error code instead.
reinterpret_cast<LPWSTR>(system_message.ptr()), 0, 0)) { buffer.resize(buffer.size() * 2);
UTF16ToUTF8 utf8_message; }
if (utf8_message.convert(system_message.c_str()) == ERROR_SUCCESS) { } FMT_CATCH(...)
out << message << ": " << utf8_message; {}
return; fmt::format_error_code(out, error_code, message); // 'fmt::' is for bcc32.
}
}
} FMT_CATCH(...) {}
fmt::format_error_code(out, error_code, message); // 'fmt::' is for bcc32.
} }
#endif // FMT_USE_WINDOWS_H #endif // FMT_USE_WINDOWS_H
FMT_FUNC void fmt::internal::format_system_error( FMT_FUNC void fmt::internal::format_system_error(
fmt::Writer &out, int error_code, fmt::Writer &out, int error_code,
fmt::StringRef message) FMT_NOEXCEPT{ fmt::StringRef message) FMT_NOEXCEPT
FMT_TRY{ {
MemoryBuffer<char, INLINE_BUFFER_SIZE> buffer; FMT_TRY{
buffer.resize(INLINE_BUFFER_SIZE); MemoryBuffer<char, INLINE_BUFFER_SIZE> buffer;
for (;;) { buffer.resize(INLINE_BUFFER_SIZE);
char *system_message = &buffer[0]; for (;;) {
int result = safe_strerror(error_code, system_message, buffer.size()); char *system_message = &buffer[0];
if (result == 0) { int result = safe_strerror(error_code, system_message, buffer.size());
out << message << ": " << system_message; if (result == 0) {
return; out << message << ": " << system_message;
} return;
if (result != ERANGE) }
break; // Can't get error message, report error code instead. if (result != ERANGE)
buffer.resize(buffer.size() * 2); break; // Can't get error message, report error code instead.
} buffer.resize(buffer.size() * 2);
} FMT_CATCH(...) {} }
fmt::format_error_code(out, error_code, message); // 'fmt::' is for bcc32. } FMT_CATCH(...)
{}
fmt::format_error_code(out, error_code, message); // 'fmt::' is for bcc32.
} }
template <typename Char> template <typename Char>
void fmt::internal::ArgMap<Char>::init(const ArgList &args) { void fmt::internal::ArgMap<Char>::init(const ArgList &args)
if (!map_.empty()) {
return; if (!map_.empty())
typedef internal::NamedArg<Char> NamedArg; return;
const NamedArg *named_arg = 0; typedef internal::NamedArg<Char> NamedArg;
bool use_values = const NamedArg *named_arg = 0;
args.type(ArgList::MAX_PACKED_ARGS - 1) == internal::Arg::NONE; bool use_values =
if (use_values) { args.type(ArgList::MAX_PACKED_ARGS - 1) == internal::Arg::NONE;
for (unsigned i = 0;/*nothing*/; ++i) { if (use_values) {
internal::Arg::Type arg_type = args.type(i); for (unsigned i = 0;/*nothing*/; ++i) {
switch (arg_type) { internal::Arg::Type arg_type = args.type(i);
case internal::Arg::NONE: switch (arg_type) {
return; case internal::Arg::NONE:
case internal::Arg::NAMED_ARG: return;
named_arg = static_cast<const NamedArg*>(args.values_[i].pointer); case internal::Arg::NAMED_ARG:
map_.insert(Pair(named_arg->name, *named_arg)); named_arg = static_cast<const NamedArg*>(args.values_[i].pointer);
break; map_.push_back(Pair(named_arg->name, *named_arg));
default: break;
/*nothing*/ default:
; /*nothing*/;
} }
} }
return; return;
} }
for (unsigned i = 0; i != ArgList::MAX_PACKED_ARGS; ++i) { for (unsigned i = 0; i != ArgList::MAX_PACKED_ARGS; ++i) {
internal::Arg::Type arg_type = args.type(i); internal::Arg::Type arg_type = args.type(i);
if (arg_type == internal::Arg::NAMED_ARG) { if (arg_type == internal::Arg::NAMED_ARG) {
named_arg = static_cast<const NamedArg*>(args.args_[i].pointer); named_arg = static_cast<const NamedArg*>(args.args_[i].pointer);
map_.insert(Pair(named_arg->name, *named_arg)); map_.push_back(Pair(named_arg->name, *named_arg));
} }
} }
for (unsigned i = ArgList::MAX_PACKED_ARGS;/*nothing*/; ++i) { for (unsigned i = ArgList::MAX_PACKED_ARGS;/*nothing*/; ++i) {
switch (args.args_[i].type) { switch (args.args_[i].type) {
case internal::Arg::NONE: case internal::Arg::NONE:
return; return;
case internal::Arg::NAMED_ARG: case internal::Arg::NAMED_ARG:
named_arg = static_cast<const NamedArg*>(args.args_[i].pointer); named_arg = static_cast<const NamedArg*>(args.args_[i].pointer);
map_.insert(Pair(named_arg->name, *named_arg)); map_.push_back(Pair(named_arg->name, *named_arg));
break; break;
default: default:
/*nothing*/ /*nothing*/;
; }
} }
}
} }
template <typename Char> template <typename Char>
void fmt::internal::FixedBuffer<Char>::grow(std::size_t) { void fmt::internal::FixedBuffer<Char>::grow(std::size_t)
FMT_THROW(std::runtime_error("buffer overflow")); {
FMT_THROW(std::runtime_error("buffer overflow"));
} }
FMT_FUNC Arg fmt::internal::FormatterBase::do_get_arg( FMT_FUNC Arg fmt::internal::FormatterBase::do_get_arg(
unsigned arg_index, const char *&error) { unsigned arg_index, const char *&error)
Arg arg = args_[arg_index]; {
switch (arg.type) { Arg arg = args_[arg_index];
case Arg::NONE: switch (arg.type) {
error = "argument index out of range"; case Arg::NONE:
break; error = "argument index out of range";
case Arg::NAMED_ARG: break;
arg = *static_cast<const internal::Arg*>(arg.pointer); case Arg::NAMED_ARG:
default: arg = *static_cast<const internal::Arg*>(arg.pointer);
/*nothing*/ break;
; default:
} /*nothing*/;
return arg; }
return arg;
} }
template <typename Char> template <typename Char>
void fmt::internal::PrintfFormatter<Char>::parse_flags( void fmt::internal::PrintfFormatter<Char>::parse_flags(
FormatSpec &spec, const Char *&s) { FormatSpec &spec, const Char *&s)
for (;;) { {
switch (*s++) { for (;;) {
case '-': switch (*s++) {
spec.align_ = ALIGN_LEFT; case '-':
break; spec.align_ = ALIGN_LEFT;
case '+': break;
spec.flags_ |= SIGN_FLAG | PLUS_FLAG; case '+':
break; spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
case '0': break;
spec.fill_ = '0'; case '0':
break; spec.fill_ = '0';
case ' ': break;
spec.flags_ |= SIGN_FLAG; case ' ':
break; spec.flags_ |= SIGN_FLAG;
case '#': break;
spec.flags_ |= HASH_FLAG; case '#':
break; spec.flags_ |= HASH_FLAG;
default: break;
--s; default:
return; --s;
} return;
} }
}
} }
template <typename Char> template <typename Char>
Arg fmt::internal::PrintfFormatter<Char>::get_arg( Arg fmt::internal::PrintfFormatter<Char>::get_arg(
const Char *s, unsigned arg_index) { const Char *s, unsigned arg_index)
(void)s; {
const char *error = 0; (void)s;
Arg arg = arg_index == UINT_MAX ? const char *error = 0;
next_arg(error) : FormatterBase::get_arg(arg_index - 1, error); Arg arg = arg_index == UINT_MAX ?
if (error) next_arg(error) : FormatterBase::get_arg(arg_index - 1, error);
FMT_THROW(FormatError(!*s ? "invalid format string" : error)); if (error)
return arg; FMT_THROW(FormatError(!*s ? "invalid format string" : error));
return arg;
} }
template <typename Char> template <typename Char>
unsigned fmt::internal::PrintfFormatter<Char>::parse_header( unsigned fmt::internal::PrintfFormatter<Char>::parse_header(
const Char *&s, FormatSpec &spec) { const Char *&s, FormatSpec &spec)
unsigned arg_index = UINT_MAX; {
Char c = *s; unsigned arg_index = UINT_MAX;
if (c >= '0' && c <= '9') { Char c = *s;
// Parse an argument index (if followed by '$') or a width possibly if (c >= '0' && c <= '9') {
// preceded with '0' flag(s). // Parse an argument index (if followed by '$') or a width possibly
unsigned value = parse_nonnegative_int(s); // preceded with '0' flag(s).
if (*s == '$') { // value is an argument index unsigned value = parse_nonnegative_int(s);
++s; if (*s == '$') { // value is an argument index
arg_index = value; ++s;
} arg_index = value;
else { }
if (c == '0') else {
spec.fill_ = '0'; if (c == '0')
if (value != 0) { spec.fill_ = '0';
// Nonzero value means that we parsed width and don't need to if (value != 0) {
// parse it or flags again, so return now. // Nonzero value means that we parsed width and don't need to
spec.width_ = value; // parse it or flags again, so return now.
return arg_index; spec.width_ = value;
} return arg_index;
} }
} }
parse_flags(spec, s); }
// Parse width. parse_flags(spec, s);
if (*s >= '0' && *s <= '9') { // Parse width.
spec.width_ = parse_nonnegative_int(s); if (*s >= '0' && *s <= '9') {
} spec.width_ = parse_nonnegative_int(s);
else if (*s == '*') { }
++s; else if (*s == '*') {
spec.width_ = WidthHandler(spec).visit(get_arg(s)); ++s;
} spec.width_ = WidthHandler(spec).visit(get_arg(s));
return arg_index; }
return arg_index;
} }
template <typename Char> template <typename Char>
void fmt::internal::PrintfFormatter<Char>::format( void fmt::internal::PrintfFormatter<Char>::format(
BasicWriter<Char> &writer, BasicCStringRef<Char> format_str) { BasicWriter<Char> &writer, BasicCStringRef<Char> format_str)
const Char *start = format_str.c_str(); {
const Char *s = start; const Char *start = format_str.c_str();
while (*s) { const Char *s = start;
Char c = *s++; while (*s) {
if (c != '%') continue; Char c = *s++;
if (*s == c) { if (c != '%') continue;
write(writer, start, s); if (*s == c) {
start = ++s; write(writer, start, s);
continue; start = ++s;
} continue;
write(writer, start, s - 1); }
write(writer, start, s - 1);
FormatSpec spec;
spec.align_ = ALIGN_RIGHT; FormatSpec spec;
spec.align_ = ALIGN_RIGHT;
// Parse argument index, flags and width.
unsigned arg_index = parse_header(s, spec); // Parse argument index, flags and width.
unsigned arg_index = parse_header(s, spec);
// Parse precision.
if (*s == '.') { // Parse precision.
++s; if (*s == '.') {
if ('0' <= *s && *s <= '9') { ++s;
spec.precision_ = parse_nonnegative_int(s); if ('0' <= *s && *s <= '9') {
} spec.precision_ = static_cast<int>(parse_nonnegative_int(s));
else if (*s == '*') { }
++s; else if (*s == '*') {
spec.precision_ = PrecisionHandler().visit(get_arg(s)); ++s;
} spec.precision_ = PrecisionHandler().visit(get_arg(s));
} }
}
Arg arg = get_arg(s, arg_index);
if (spec.flag(HASH_FLAG) && IsZeroInt().visit(arg)) Arg arg = get_arg(s, arg_index);
spec.flags_ &= ~HASH_FLAG; if (spec.flag(HASH_FLAG) && IsZeroInt().visit(arg))
if (spec.fill_ == '0') { spec.flags_ &= ~to_unsigned<int>(HASH_FLAG);
if (arg.type <= Arg::LAST_NUMERIC_TYPE) if (spec.fill_ == '0') {
spec.align_ = ALIGN_NUMERIC; if (arg.type <= Arg::LAST_NUMERIC_TYPE)
else spec.align_ = ALIGN_NUMERIC;
spec.fill_ = ' '; // Ignore '0' flag for non-numeric types. else
} spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
}
// Parse length and convert the argument to the required type.
switch (*s++) { // Parse length and convert the argument to the required type.
case 'h': switch (*s++) {
if (*s == 'h') case 'h':
ArgConverter<signed char>(arg, *++s).visit(arg); if (*s == 'h')
else ArgConverter<signed char>(arg, *++s).visit(arg);
ArgConverter<short>(arg, *s).visit(arg); else
break; ArgConverter<short>(arg, *s).visit(arg);
case 'l': break;
if (*s == 'l') case 'l':
ArgConverter<fmt::LongLong>(arg, *++s).visit(arg); if (*s == 'l')
else ArgConverter<fmt::LongLong>(arg, *++s).visit(arg);
ArgConverter<long>(arg, *s).visit(arg); else
break; ArgConverter<long>(arg, *s).visit(arg);
case 'j': break;
ArgConverter<intmax_t>(arg, *s).visit(arg); case 'j':
break; ArgConverter<intmax_t>(arg, *s).visit(arg);
case 'z': break;
ArgConverter<std::size_t>(arg, *s).visit(arg); case 'z':
break; ArgConverter<std::size_t>(arg, *s).visit(arg);
case 't': break;
ArgConverter<std::ptrdiff_t>(arg, *s).visit(arg); case 't':
break; ArgConverter<std::ptrdiff_t>(arg, *s).visit(arg);
case 'L': break;
// printf produces garbage when 'L' is omitted for long double, no case 'L':
// need to do the same. // printf produces garbage when 'L' is omitted for long double, no
break; // need to do the same.
default: break;
--s; default:
ArgConverter<int>(arg, *s).visit(arg); --s;
} ArgConverter<void>(arg, *s).visit(arg);
}
// Parse type.
if (!*s) // Parse type.
FMT_THROW(FormatError("invalid format string")); if (!*s)
spec.type_ = static_cast<char>(*s++); FMT_THROW(FormatError("invalid format string"));
if (arg.type <= Arg::LAST_INTEGER_TYPE) { spec.type_ = static_cast<char>(*s++);
// Normalize type. if (arg.type <= Arg::LAST_INTEGER_TYPE) {
switch (spec.type_) { // Normalize type.
case 'i': switch (spec.type_) {
case 'u': case 'i': case 'u':
spec.type_ = 'd'; spec.type_ = 'd';
break; break;
case 'c': case 'c':
// TODO: handle wchar_t // TODO: handle wchar_t
CharConverter(arg).visit(arg); CharConverter(arg).visit(arg);
break; break;
} }
} }
start = s; start = s;
// Format argument. // Format argument.
internal::PrintfArgFormatter<Char>(writer, spec).visit(arg); internal::PrintfArgFormatter<Char>(writer, spec).visit(arg);
} }
write(writer, start, s); write(writer, start, s);
} }
FMT_FUNC void fmt::report_system_error( FMT_FUNC void fmt::report_system_error(
int error_code, fmt::StringRef message) FMT_NOEXCEPT{ int error_code, fmt::StringRef message) FMT_NOEXCEPT
// 'fmt::' is for bcc32. {
fmt::report_error(internal::format_system_error, error_code, message); // 'fmt::' is for bcc32.
fmt::report_error(internal::format_system_error, error_code, message);
} }
#if FMT_USE_WINDOWS_H #if FMT_USE_WINDOWS_H
FMT_FUNC void fmt::report_windows_error( FMT_FUNC void fmt::report_windows_error(
int error_code, fmt::StringRef message) FMT_NOEXCEPT{ int error_code, fmt::StringRef message) FMT_NOEXCEPT
// 'fmt::' is for bcc32. {
fmt::report_error(internal::format_windows_error, error_code, message); // 'fmt::' is for bcc32.
fmt::report_error(internal::format_windows_error, error_code, message);
} }
#endif #endif
FMT_FUNC void fmt::print(std::FILE *f, CStringRef format_str, ArgList args) { FMT_FUNC void fmt::print(std::FILE *f, CStringRef format_str, ArgList args)
MemoryWriter w; {
w.write(format_str, args); MemoryWriter w;
std::fwrite(w.data(), 1, w.size(), f); w.write(format_str, args);
std::fwrite(w.data(), 1, w.size(), f);
}
FMT_FUNC void fmt::print(CStringRef format_str, ArgList args)
{
print(stdout, format_str, args);
} }
FMT_FUNC void fmt::print(CStringRef format_str, ArgList args) { FMT_FUNC void fmt::print(std::ostream &os, CStringRef format_str,
print(stdout, format_str, args); ArgList args)
{
MemoryWriter w;
w.write(format_str, args);
write(os, w);
} }
FMT_FUNC void fmt::print(std::ostream &os, CStringRef format_str, ArgList args) { FMT_FUNC void fmt::print_colored(Color c, CStringRef format, ArgList args)
MemoryWriter w; {
w.write(format_str, args); char escape[] = "\x1b[30m";
os.write(w.data(), w.size()); escape[3] = static_cast<char>('0' + c);
std::fputs(escape, stdout);
print(format, args);
std::fputs(RESET_COLOR, stdout);
} }
FMT_FUNC void fmt::print_colored(Color c, CStringRef format, ArgList args) { FMT_FUNC int fmt::fprintf(std::FILE *f, CStringRef format, ArgList args)
char escape[] = "\x1b[30m"; {
escape[3] = static_cast<char>('0' + c); MemoryWriter w;
std::fputs(escape, stdout); printf(w, format, args);
print(format, args); std::size_t size = w.size();
std::fputs(RESET_COLOR, stdout); return std::fwrite(w.data(), 1, size, f) < size ? -1 : static_cast<int>(size);
} }
FMT_FUNC int fmt::fprintf(std::FILE *f, CStringRef format, ArgList args) { FMT_FUNC int fmt::fprintf(std::ostream &os, CStringRef format, ArgList args)
MemoryWriter w; {
printf(w, format, args); MemoryWriter w;
std::size_t size = w.size(); printf(w, format, args);
return std::fwrite(w.data(), 1, size, f) < size ? -1 : static_cast<int>(size); write(os, w);
return static_cast<int>(w.size());
} }
#ifndef FMT_HEADER_ONLY #ifndef FMT_HEADER_ONLY
...@@ -915,15 +1032,15 @@ template void fmt::internal::FixedBuffer<char>::grow(std::size_t); ...@@ -915,15 +1032,15 @@ template void fmt::internal::FixedBuffer<char>::grow(std::size_t);
template void fmt::internal::ArgMap<char>::init(const fmt::ArgList &args); template void fmt::internal::ArgMap<char>::init(const fmt::ArgList &args);
template void fmt::internal::PrintfFormatter<char>::format( template void fmt::internal::PrintfFormatter<char>::format(
BasicWriter<char> &writer, CStringRef format); BasicWriter<char> &writer, CStringRef format);
template int fmt::internal::CharTraits<char>::format_float( template int fmt::internal::CharTraits<char>::format_float(
char *buffer, std::size_t size, const char *format, char *buffer, std::size_t size, const char *format,
unsigned width, int precision, double value); unsigned width, int precision, double value);
template int fmt::internal::CharTraits<char>::format_float( template int fmt::internal::CharTraits<char>::format_float(
char *buffer, std::size_t size, const char *format, char *buffer, std::size_t size, const char *format,
unsigned width, int precision, long double value); unsigned width, int precision, long double value);
// Explicit instantiations for wchar_t. // Explicit instantiations for wchar_t.
...@@ -932,15 +1049,15 @@ template void fmt::internal::FixedBuffer<wchar_t>::grow(std::size_t); ...@@ -932,15 +1049,15 @@ template void fmt::internal::FixedBuffer<wchar_t>::grow(std::size_t);
template void fmt::internal::ArgMap<wchar_t>::init(const fmt::ArgList &args); template void fmt::internal::ArgMap<wchar_t>::init(const fmt::ArgList &args);
template void fmt::internal::PrintfFormatter<wchar_t>::format( template void fmt::internal::PrintfFormatter<wchar_t>::format(
BasicWriter<wchar_t> &writer, WCStringRef format); BasicWriter<wchar_t> &writer, WCStringRef format);
template int fmt::internal::CharTraits<wchar_t>::format_float( template int fmt::internal::CharTraits<wchar_t>::format_float(
wchar_t *buffer, std::size_t size, const wchar_t *format, wchar_t *buffer, std::size_t size, const wchar_t *format,
unsigned width, int precision, double value); unsigned width, int precision, double value);
template int fmt::internal::CharTraits<wchar_t>::format_float( template int fmt::internal::CharTraits<wchar_t>::format_float(
wchar_t *buffer, std::size_t size, const wchar_t *format, wchar_t *buffer, std::size_t size, const wchar_t *format,
unsigned width, int precision, long double value); unsigned width, int precision, long double value);
#endif // FMT_HEADER_ONLY #endif // FMT_HEADER_ONLY
......
This source diff could not be displayed because it is too large. You can view the blob instead.
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