1
0
mirror of https://github.com/gabime/spdlog.git synced 2025-05-08 15:23:52 +00:00

astyle previous commits

This commit is contained in:
gabime 2016-11-18 17:17:09 +02:00
parent d142f13551
commit bd6fe569b5
4 changed files with 4124 additions and 3949 deletions

View File

@ -292,8 +292,10 @@ typedef __int64 intmax_t;
#if FMT_MSC_VER && !defined(FMT_BUILTIN_CLZLL) #if FMT_MSC_VER && !defined(FMT_BUILTIN_CLZLL)
# include <intrin.h> // _BitScanReverse, _BitScanReverse64 # include <intrin.h> // _BitScanReverse, _BitScanReverse64
namespace fmt { namespace fmt
namespace internal { {
namespace internal
{
# pragma intrinsic(_BitScanReverse) # pragma intrinsic(_BitScanReverse)
inline uint32_t clz(uint32_t x) inline uint32_t clz(uint32_t x)
{ {
@ -339,8 +341,10 @@ namespace fmt {
} }
#endif #endif
namespace fmt { namespace fmt
namespace internal { {
namespace internal
{
struct DummyInt struct DummyInt
{ {
int data[2]; int data[2];
@ -388,7 +392,8 @@ namespace fmt {
} }
} // namespace fmt } // namespace fmt
namespace std { namespace std
{
// Standard permits specialization of std::numeric_limits. This specialization // Standard permits specialization of std::numeric_limits. This specialization
// is used to resolve ambiguity between isinf and std::isinf in glibc: // is used to resolve ambiguity between isinf and std::isinf in glibc:
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=48891 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=48891
@ -406,7 +411,8 @@ namespace std {
// The resolution "priority" is: // The resolution "priority" is:
// isinf macro > std::isinf > ::isinf > fmt::internal::isinf // isinf macro > std::isinf > ::isinf > fmt::internal::isinf
if (const_check(sizeof(isinf(x)) == sizeof(bool) || if (const_check(sizeof(isinf(x)) == sizeof(bool) ||
sizeof(isinf(x)) == sizeof(int))) { sizeof(isinf(x)) == sizeof(int)))
{
return isinf(x) != 0; return isinf(x) != 0;
} }
return !_finite(static_cast<double>(x)); return !_finite(static_cast<double>(x));
@ -418,7 +424,8 @@ namespace std {
{ {
using namespace fmt::internal; using namespace fmt::internal;
if (const_check(sizeof(isnan(x)) == sizeof(bool) || if (const_check(sizeof(isnan(x)) == sizeof(bool) ||
sizeof(isnan(x)) == sizeof(int))) { sizeof(isnan(x)) == sizeof(int)))
{
return isnan(x) != 0; return isnan(x) != 0;
} }
return _isnan(static_cast<double>(x)) != 0; return _isnan(static_cast<double>(x)) != 0;
@ -429,7 +436,8 @@ namespace std {
{ {
using namespace fmt::internal; using namespace fmt::internal;
if (const_check(sizeof(signbit(x)) == sizeof(bool) || if (const_check(sizeof(signbit(x)) == sizeof(bool) ||
sizeof(signbit(x)) == sizeof(int))) { sizeof(signbit(x)) == sizeof(int)))
{
return signbit(x) != 0; return signbit(x) != 0;
} }
if (x < 0) return true; if (x < 0) return true;
@ -442,7 +450,8 @@ namespace std {
}; };
} // namespace std } // namespace std
namespace fmt { namespace fmt
{
// Fix the warning about long long on older versions of GCC // Fix the warning about long long on older versions of GCC
// that don't support the diagnostic pragma. // that don't support the diagnostic pragma.
@ -651,7 +660,8 @@ namespace fmt {
~FormatError() FMT_DTOR_NOEXCEPT; ~FormatError() FMT_DTOR_NOEXCEPT;
}; };
namespace internal { namespace internal
{
// MakeUnsigned<T>::Type gives an unsigned type corresponding to integer type T. // MakeUnsigned<T>::Type gives an unsigned type corresponding to integer type T.
template <typename T> template <typename T>
@ -805,7 +815,8 @@ namespace fmt {
size_ = new_size; size_ = new_size;
} }
namespace internal { namespace internal
{
// A memory buffer for trivially copyable/constructible types with the first // A memory buffer for trivially copyable/constructible types with the first
// SIZE elements stored in the object itself. // SIZE elements stored in the object itself.
@ -842,12 +853,14 @@ namespace fmt {
this_alloc = std::move(other_alloc); this_alloc = std::move(other_alloc);
this->size_ = other.size_; this->size_ = other.size_;
this->capacity_ = other.capacity_; this->capacity_ = other.capacity_;
if (other.ptr_ == other.data_) { if (other.ptr_ == other.data_)
{
this->ptr_ = data_; this->ptr_ = data_;
std::uninitialized_copy(other.data_, other.data_ + this->size_, std::uninitialized_copy(other.data_, other.data_ + this->size_,
make_ptr(data_, this->capacity_)); make_ptr(data_, this->capacity_));
} }
else { else
{
this->ptr_ = other.ptr_; this->ptr_ = other.ptr_;
// Set pointer to the inline array so that delete is not called // Set pointer to the inline array so that delete is not called
// when deallocating. // when deallocating.
@ -1067,7 +1080,8 @@ namespace fmt {
inline unsigned count_digits(uint64_t n) inline unsigned count_digits(uint64_t n)
{ {
unsigned count = 1; unsigned count = 1;
for (;;) { for (;;)
{
// Integer division is slow so do it for a group of four digits instead // Integer division is slow so do it for a group of four digits instead
// of for every digit. The idea comes from the talk by Alexandrescu // of for every digit. The idea comes from the talk by Alexandrescu
// "Three Optimization Tips for C++". See speed-test for a comparison. // "Three Optimization Tips for C++". See speed-test for a comparison.
@ -1130,7 +1144,8 @@ namespace fmt {
ThousandsSep thousands_sep) ThousandsSep thousands_sep)
{ {
buffer += num_digits; buffer += num_digits;
while (value >= 100) { while (value >= 100)
{
// Integer division is slow so do it for a group of two digits instead // Integer division is slow so do it for a group of two digits instead
// of for every digit. The idea comes from the talk by Alexandrescu // of for every digit. The idea comes from the talk by Alexandrescu
// "Three Optimization Tips for C++". See speed-test for a comparison. // "Three Optimization Tips for C++". See speed-test for a comparison.
@ -1141,7 +1156,8 @@ namespace fmt {
*--buffer = Data::DIGITS[index]; *--buffer = Data::DIGITS[index];
thousands_sep(buffer); thousands_sep(buffer);
} }
if (value < 10) { if (value < 10)
{
*--buffer = static_cast<char>('0' + value); *--buffer = static_cast<char>('0' + value);
return; return;
} }
@ -1766,7 +1782,8 @@ namespace fmt {
using internal::Arg; using internal::Arg;
Arg arg; Arg arg;
bool use_values = type(MAX_PACKED_ARGS - 1) == Arg::NONE; bool use_values = type(MAX_PACKED_ARGS - 1) == Arg::NONE;
if (index < MAX_PACKED_ARGS) { if (index < MAX_PACKED_ARGS)
{
Arg::Type arg_type = type(index); Arg::Type arg_type = type(index);
internal::Value &val = arg; internal::Value &val = arg;
if (arg_type != Arg::NONE) if (arg_type != Arg::NONE)
@ -1774,13 +1791,15 @@ namespace fmt {
arg.type = arg_type; arg.type = arg_type;
return arg; return arg;
} }
if (use_values) { if (use_values)
{
// The index is greater than the number of arguments that can be stored // The index is greater than the number of arguments that can be stored
// in values, so return a "none" argument. // in values, so return a "none" argument.
arg.type = Arg::NONE; arg.type = Arg::NONE;
return arg; return arg;
} }
for (unsigned i = MAX_PACKED_ARGS; i <= index; ++i) { for (unsigned i = MAX_PACKED_ARGS; i <= index; ++i)
{
if (args_[i].type == Arg::NONE) if (args_[i].type == Arg::NONE)
return args_[i]; return args_[i];
} }
@ -1940,7 +1959,8 @@ namespace fmt {
*/ */
Result visit(const Arg &arg) Result visit(const Arg &arg)
{ {
switch (arg.type) { switch (arg.type)
{
case Arg::NONE: case Arg::NONE:
case Arg::NAMED_ARG: case Arg::NAMED_ARG:
FMT_ASSERT(false, "invalid argument type"); FMT_ASSERT(false, "invalid argument type");
@ -2266,7 +2286,8 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
return StrFormatSpec<wchar_t>(str, width, fill); return StrFormatSpec<wchar_t>(str, width, fill);
} }
namespace internal { namespace internal
{
template <typename Char> template <typename Char>
class ArgMap class ArgMap
@ -2285,7 +2306,8 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
{ {
// The list is unsorted, so just return the first matching name. // The list is unsorted, so just return the first matching name.
for (typename MapType::const_iterator it = map_.begin(), end = map_.end(); for (typename MapType::const_iterator it = map_.begin(), end = map_.end();
it != end; ++it) { it != end; ++it)
{
if (it->first == name) if (it->first == name)
return &it->second; return &it->second;
} }
@ -2351,7 +2373,8 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
void visit_bool(bool value) void visit_bool(bool value)
{ {
if (spec_.type_) { if (spec_.type_)
{
visit_any_int(value); visit_any_int(value);
return; return;
} }
@ -2360,7 +2383,8 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
void visit_char(int value) void visit_char(int value)
{ {
if (spec_.type_ && spec_.type_ != 'c') { if (spec_.type_ && spec_.type_ != 'c')
{
spec_.flags_ |= CHAR_FLAG; spec_.flags_ |= CHAR_FLAG;
writer_.write_int(value, spec_); writer_.write_int(value, spec_);
return; return;
@ -2371,22 +2395,27 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
Char fill = internal::CharTraits<Char>::cast(spec_.fill()); Char fill = internal::CharTraits<Char>::cast(spec_.fill());
CharPtr out = CharPtr(); CharPtr out = CharPtr();
const unsigned CHAR_SIZE = 1; const unsigned CHAR_SIZE = 1;
if (spec_.width_ > CHAR_SIZE) { if (spec_.width_ > CHAR_SIZE)
{
out = writer_.grow_buffer(spec_.width_); out = writer_.grow_buffer(spec_.width_);
if (spec_.align_ == ALIGN_RIGHT) { if (spec_.align_ == ALIGN_RIGHT)
{
std::uninitialized_fill_n(out, spec_.width_ - CHAR_SIZE, fill); std::uninitialized_fill_n(out, spec_.width_ - CHAR_SIZE, fill);
out += spec_.width_ - CHAR_SIZE; out += spec_.width_ - CHAR_SIZE;
} }
else if (spec_.align_ == ALIGN_CENTER) { else if (spec_.align_ == ALIGN_CENTER)
{
out = writer_.fill_padding(out, spec_.width_, out = writer_.fill_padding(out, spec_.width_,
internal::const_check(CHAR_SIZE), fill); internal::const_check(CHAR_SIZE), fill);
} }
else { else
{
std::uninitialized_fill_n(out + CHAR_SIZE, std::uninitialized_fill_n(out + CHAR_SIZE,
spec_.width_ - CHAR_SIZE, fill); spec_.width_ - CHAR_SIZE, fill);
} }
} }
else { else
{
out = writer_.grow_buffer(CHAR_SIZE); out = writer_.grow_buffer(CHAR_SIZE);
} }
*out = internal::CharTraits<Char>::cast(value); *out = internal::CharTraits<Char>::cast(value);
@ -2458,7 +2487,8 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
bool check_no_auto_index(const char *&error) bool check_no_auto_index(const char *&error)
{ {
if (next_arg_index_ > 0) { if (next_arg_index_ > 0)
{
error = "cannot switch from automatic to manual argument indexing"; error = "cannot switch from automatic to manual argument indexing";
return false; return false;
} }
@ -2603,7 +2633,8 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
# define FMT_GEN14(f) FMT_GEN13(f), f(13) # define FMT_GEN14(f) FMT_GEN13(f), f(13)
# define FMT_GEN15(f) FMT_GEN14(f), f(14) # define FMT_GEN15(f) FMT_GEN14(f), f(14)
namespace internal { namespace internal
{
inline uint64_t make_type() inline uint64_t make_type()
{ {
return 0; return 0;
@ -2922,11 +2953,13 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
{ {
typedef typename internal::IntTraits<Int>::MainType MainType; typedef typename internal::IntTraits<Int>::MainType MainType;
MainType abs_value = static_cast<MainType>(value); MainType abs_value = static_cast<MainType>(value);
if (internal::is_negative(value)) { if (internal::is_negative(value))
{
abs_value = 0 - abs_value; abs_value = 0 - abs_value;
*write_unsigned_decimal(abs_value, 1) = '-'; *write_unsigned_decimal(abs_value, 1) = '-';
} }
else { else
{
write_unsigned_decimal(abs_value, 0); write_unsigned_decimal(abs_value, 0);
} }
} }
@ -3193,21 +3226,26 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
const StrChar *s, std::size_t size, const AlignSpec &spec) const StrChar *s, std::size_t size, const AlignSpec &spec)
{ {
CharPtr out = CharPtr(); CharPtr out = CharPtr();
if (spec.width() > size) { if (spec.width() > size)
{
out = grow_buffer(spec.width()); out = grow_buffer(spec.width());
Char fill = internal::CharTraits<Char>::cast(spec.fill()); Char fill = internal::CharTraits<Char>::cast(spec.fill());
if (spec.align() == ALIGN_RIGHT) { if (spec.align() == ALIGN_RIGHT)
{
std::uninitialized_fill_n(out, spec.width() - size, fill); std::uninitialized_fill_n(out, spec.width() - size, fill);
out += spec.width() - size; out += spec.width() - size;
} }
else if (spec.align() == ALIGN_CENTER) { else if (spec.align() == ALIGN_CENTER)
{
out = fill_padding(out, spec.width(), size, fill); out = fill_padding(out, spec.width(), size, fill);
} }
else { else
{
std::uninitialized_fill_n(out + size, spec.width() - size, fill); std::uninitialized_fill_n(out + size, spec.width() - size, fill);
} }
} }
else { else
{
out = grow_buffer(size); out = grow_buffer(size);
} }
std::uninitialized_copy(s, s + size, out); std::uninitialized_copy(s, s + size, out);
@ -3225,8 +3263,10 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
internal::report_unknown_type(spec.type_, "string"); internal::report_unknown_type(spec.type_, "string");
const StrChar *str_value = s.value; const StrChar *str_value = s.value;
std::size_t str_size = s.size; std::size_t str_size = s.size;
if (str_size == 0) { if (str_size == 0)
if (!str_value) { {
if (!str_value)
{
FMT_THROW(FormatError("string pointer is null")); FMT_THROW(FormatError("string pointer is null"));
} }
} }
@ -3263,7 +3303,8 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
unsigned width = spec.width(); unsigned width = spec.width();
Alignment align = spec.align(); Alignment align = spec.align();
Char fill = internal::CharTraits<Char>::cast(spec.fill()); Char fill = internal::CharTraits<Char>::cast(spec.fill());
if (spec.precision() > static_cast<int>(num_digits)) { if (spec.precision() > static_cast<int>(num_digits))
{
// Octal prefix '0' is counted as a digit, so ignore it if precision // Octal prefix '0' is counted as a digit, so ignore it if precision
// is specified. // is specified.
if (prefix_size > 0 && prefix[prefix_size - 1] == '0') if (prefix_size > 0 && prefix[prefix_size - 1] == '0')
@ -3275,44 +3316,53 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
return prepare_int_buffer(num_digits, subspec, prefix, prefix_size); return prepare_int_buffer(num_digits, subspec, prefix, prefix_size);
buffer_.reserve(width); buffer_.reserve(width);
unsigned fill_size = width - number_size; unsigned fill_size = width - number_size;
if (align != ALIGN_LEFT) { if (align != ALIGN_LEFT)
{
CharPtr p = grow_buffer(fill_size); CharPtr p = grow_buffer(fill_size);
std::uninitialized_fill(p, p + fill_size, fill); std::uninitialized_fill(p, p + fill_size, fill);
} }
CharPtr result = prepare_int_buffer( CharPtr result = prepare_int_buffer(
num_digits, subspec, prefix, prefix_size); num_digits, subspec, prefix, prefix_size);
if (align == ALIGN_LEFT) { if (align == ALIGN_LEFT)
{
CharPtr p = grow_buffer(fill_size); CharPtr p = grow_buffer(fill_size);
std::uninitialized_fill(p, p + fill_size, fill); std::uninitialized_fill(p, p + fill_size, fill);
} }
return result; return result;
} }
unsigned size = prefix_size + num_digits; unsigned size = prefix_size + num_digits;
if (width <= size) { if (width <= size)
{
CharPtr p = grow_buffer(size); CharPtr p = grow_buffer(size);
std::uninitialized_copy(prefix, prefix + prefix_size, p); std::uninitialized_copy(prefix, prefix + prefix_size, p);
return p + size - 1; return p + size - 1;
} }
CharPtr p = grow_buffer(width); CharPtr p = grow_buffer(width);
CharPtr end = p + width; CharPtr end = p + width;
if (align == ALIGN_LEFT) { if (align == ALIGN_LEFT)
{
std::uninitialized_copy(prefix, prefix + prefix_size, p); std::uninitialized_copy(prefix, prefix + prefix_size, p);
p += size; p += size;
std::uninitialized_fill(p, end, fill); std::uninitialized_fill(p, end, fill);
} }
else if (align == ALIGN_CENTER) { else if (align == ALIGN_CENTER)
{
p = fill_padding(p, width, size, fill); p = fill_padding(p, width, size, fill);
std::uninitialized_copy(prefix, prefix + prefix_size, p); std::uninitialized_copy(prefix, prefix + prefix_size, p);
p += size; p += size;
} }
else { else
if (align == ALIGN_NUMERIC) { {
if (prefix_size != 0) { if (align == ALIGN_NUMERIC)
{
if (prefix_size != 0)
{
p = std::uninitialized_copy(prefix, prefix + prefix_size, p); p = std::uninitialized_copy(prefix, prefix + prefix_size, p);
size -= prefix_size; size -= prefix_size;
} }
} }
else { else
{
std::uninitialized_copy(prefix, prefix + prefix_size, end - size); std::uninitialized_copy(prefix, prefix + prefix_size, end - size);
} }
std::uninitialized_fill(p, end - size, fill); std::uninitialized_fill(p, end - size, fill);
@ -3329,75 +3379,100 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
typedef typename internal::IntTraits<T>::MainType UnsignedType; typedef typename internal::IntTraits<T>::MainType UnsignedType;
UnsignedType abs_value = static_cast<UnsignedType>(value); UnsignedType abs_value = static_cast<UnsignedType>(value);
char prefix[4] = ""; char prefix[4] = "";
if (internal::is_negative(value)) { if (internal::is_negative(value))
{
prefix[0] = '-'; prefix[0] = '-';
++prefix_size; ++prefix_size;
abs_value = 0 - abs_value; abs_value = 0 - abs_value;
} }
else if (spec.flag(SIGN_FLAG)) { else if (spec.flag(SIGN_FLAG))
{
prefix[0] = spec.flag(PLUS_FLAG) ? '+' : ' '; prefix[0] = spec.flag(PLUS_FLAG) ? '+' : ' ';
++prefix_size; ++prefix_size;
} }
switch (spec.type()) { switch (spec.type())
case 0: case 'd': { {
case 0:
case 'd':
{
unsigned num_digits = internal::count_digits(abs_value); unsigned num_digits = internal::count_digits(abs_value);
CharPtr p = prepare_int_buffer(num_digits, spec, prefix, prefix_size) + 1; CharPtr p = prepare_int_buffer(num_digits, spec, prefix, prefix_size) + 1;
internal::format_decimal(get(p), abs_value, 0); internal::format_decimal(get(p), abs_value, 0);
break; break;
} }
case 'x': case 'X': { case 'x':
case 'X':
{
UnsignedType n = abs_value; UnsignedType n = abs_value;
if (spec.flag(HASH_FLAG)) { if (spec.flag(HASH_FLAG))
{
prefix[prefix_size++] = '0'; prefix[prefix_size++] = '0';
prefix[prefix_size++] = spec.type(); prefix[prefix_size++] = spec.type();
} }
unsigned num_digits = 0; unsigned num_digits = 0;
do { do
{
++num_digits; ++num_digits;
} while ((n >>= 4) != 0); }
while ((n >>= 4) != 0);
Char *p = get(prepare_int_buffer( Char *p = get(prepare_int_buffer(
num_digits, spec, prefix, prefix_size)); num_digits, spec, prefix, prefix_size));
n = abs_value; n = abs_value;
const char *digits = spec.type() == 'x' ? const char *digits = spec.type() == 'x' ?
"0123456789abcdef" : "0123456789ABCDEF"; "0123456789abcdef" : "0123456789ABCDEF";
do { do
{
*p-- = digits[n & 0xf]; *p-- = digits[n & 0xf];
} while ((n >>= 4) != 0); }
while ((n >>= 4) != 0);
break; break;
} }
case 'b': case 'B': { case 'b':
case 'B':
{
UnsignedType n = abs_value; UnsignedType n = abs_value;
if (spec.flag(HASH_FLAG)) { if (spec.flag(HASH_FLAG))
{
prefix[prefix_size++] = '0'; prefix[prefix_size++] = '0';
prefix[prefix_size++] = spec.type(); prefix[prefix_size++] = spec.type();
} }
unsigned num_digits = 0; unsigned num_digits = 0;
do { do
{
++num_digits; ++num_digits;
} while ((n >>= 1) != 0); }
while ((n >>= 1) != 0);
Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size)); Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
n = abs_value; n = abs_value;
do { do
{
*p-- = static_cast<Char>('0' + (n & 1)); *p-- = static_cast<Char>('0' + (n & 1));
} while ((n >>= 1) != 0); }
while ((n >>= 1) != 0);
break; break;
} }
case 'o': { case 'o':
{
UnsignedType n = abs_value; UnsignedType n = abs_value;
if (spec.flag(HASH_FLAG)) if (spec.flag(HASH_FLAG))
prefix[prefix_size++] = '0'; prefix[prefix_size++] = '0';
unsigned num_digits = 0; unsigned num_digits = 0;
do { do
{
++num_digits; ++num_digits;
} while ((n >>= 3) != 0); }
while ((n >>= 3) != 0);
Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size)); Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
n = abs_value; n = abs_value;
do { do
{
*p-- = static_cast<Char>('0' + (n & 7)); *p-- = static_cast<Char>('0' + (n & 7));
} while ((n >>= 3) != 0); }
while ((n >>= 3) != 0);
break; break;
} }
case 'n': { case 'n':
{
unsigned num_digits = internal::count_digits(abs_value); unsigned num_digits = internal::count_digits(abs_value);
fmt::StringRef sep = ""; fmt::StringRef sep = "";
#ifndef ANDROID #ifndef ANDROID
@ -3423,11 +3498,15 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
// Check type. // Check type.
char type = spec.type(); char type = spec.type();
bool upper = false; bool upper = false;
switch (type) { switch (type)
{
case 0: case 0:
type = 'g'; type = 'g';
break; break;
case 'e': case 'f': case 'g': case 'a': case 'e':
case 'f':
case 'g':
case 'a':
break; break;
case 'F': case 'F':
#if FMT_MSC_VER #if FMT_MSC_VER
@ -3435,7 +3514,9 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
type = 'f'; type = 'f';
#endif #endif
// Fall through. // Fall through.
case 'E': case 'G': case 'A': case 'E':
case 'G':
case 'A':
upper = true; upper = true;
break; break;
default: default:
@ -3446,20 +3527,24 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
char sign = 0; char sign = 0;
// Use isnegative instead of value < 0 because the latter is always // Use isnegative instead of value < 0 because the latter is always
// false for NaN. // false for NaN.
if (internal::FPUtil::isnegative(static_cast<double>(value))) { if (internal::FPUtil::isnegative(static_cast<double>(value)))
{
sign = '-'; sign = '-';
value = -value; value = -value;
} }
else if (spec.flag(SIGN_FLAG)) { else if (spec.flag(SIGN_FLAG))
{
sign = spec.flag(PLUS_FLAG) ? '+' : ' '; sign = spec.flag(PLUS_FLAG) ? '+' : ' ';
} }
if (internal::FPUtil::isnotanumber(value)) { if (internal::FPUtil::isnotanumber(value))
{
// Format NaN ourselves because sprintf's output is not consistent // Format NaN ourselves because sprintf's output is not consistent
// across platforms. // across platforms.
std::size_t nan_size = 4; std::size_t nan_size = 4;
const char *nan = upper ? " NAN" : " nan"; const char *nan = upper ? " NAN" : " nan";
if (!sign) { if (!sign)
{
--nan_size; --nan_size;
++nan; ++nan;
} }
@ -3469,12 +3554,14 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
return; return;
} }
if (internal::FPUtil::isinfinity(value)) { if (internal::FPUtil::isinfinity(value))
{
// Format infinity ourselves because sprintf's output is not consistent // Format infinity ourselves because sprintf's output is not consistent
// across platforms. // across platforms.
std::size_t inf_size = 4; std::size_t inf_size = 4;
const char *inf = upper ? " INF" : " inf"; const char *inf = upper ? " INF" : " inf";
if (!sign) { if (!sign)
{
--inf_size; --inf_size;
++inf; ++inf;
} }
@ -3486,7 +3573,8 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
std::size_t offset = buffer_.size(); std::size_t offset = buffer_.size();
unsigned width = spec.width(); unsigned width = spec.width();
if (sign) { if (sign)
{
buffer_.reserve(buffer_.size() + (width > 1u ? width : 1u)); buffer_.reserve(buffer_.size() + (width > 1u ? width : 1u));
if (width > 0) if (width > 0)
--width; --width;
@ -3504,16 +3592,19 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
unsigned width_for_sprintf = width; unsigned width_for_sprintf = width;
if (spec.flag(HASH_FLAG)) if (spec.flag(HASH_FLAG))
*format_ptr++ = '#'; *format_ptr++ = '#';
if (spec.align() == ALIGN_CENTER) { if (spec.align() == ALIGN_CENTER)
{
width_for_sprintf = 0; width_for_sprintf = 0;
} }
else { else
{
if (spec.align() == ALIGN_LEFT) if (spec.align() == ALIGN_LEFT)
*format_ptr++ = '-'; *format_ptr++ = '-';
if (width != 0) if (width != 0)
*format_ptr++ = '*'; *format_ptr++ = '*';
} }
if (spec.precision() >= 0) { if (spec.precision() >= 0)
{
*format_ptr++ = '.'; *format_ptr++ = '.';
*format_ptr++ = '*'; *format_ptr++ = '*';
} }
@ -3526,13 +3617,15 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
Char fill = internal::CharTraits<Char>::cast(spec.fill()); Char fill = internal::CharTraits<Char>::cast(spec.fill());
unsigned n = 0; unsigned n = 0;
Char *start = FMT_NULL; Char *start = FMT_NULL;
for (;;) { for (;;)
{
std::size_t buffer_size = buffer_.capacity() - offset; std::size_t buffer_size = buffer_.capacity() - offset;
#if FMT_MSC_VER #if FMT_MSC_VER
// MSVC's vsnprintf_s doesn't work with zero size, so reserve // MSVC's vsnprintf_s doesn't work with zero size, so reserve
// space for at least one extra character to make the size non-zero. // space for at least one extra character to make the size non-zero.
// Note that the buffer's capacity will increase by more than 1. // Note that the buffer's capacity will increase by more than 1.
if (buffer_size == 0) { if (buffer_size == 0)
{
buffer_.reserve(offset + 1); buffer_.reserve(offset + 1);
buffer_size = buffer_.capacity() - offset; buffer_size = buffer_.capacity() - offset;
} }
@ -3540,37 +3633,44 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
start = &buffer_[offset]; start = &buffer_[offset];
int result = internal::CharTraits<Char>::format_float( int result = internal::CharTraits<Char>::format_float(
start, buffer_size, format, width_for_sprintf, spec.precision(), value); start, buffer_size, format, width_for_sprintf, spec.precision(), value);
if (result >= 0) { if (result >= 0)
{
n = internal::to_unsigned(result); n = internal::to_unsigned(result);
if (offset + n < buffer_.capacity()) if (offset + n < buffer_.capacity())
break; // The buffer is large enough - continue with formatting. break; // The buffer is large enough - continue with formatting.
buffer_.reserve(offset + n + 1); buffer_.reserve(offset + n + 1);
} }
else { else
{
// If result is negative we ask to increase the capacity by at least 1, // If result is negative we ask to increase the capacity by at least 1,
// but as std::vector, the buffer grows exponentially. // but as std::vector, the buffer grows exponentially.
buffer_.reserve(buffer_.capacity() + 1); buffer_.reserve(buffer_.capacity() + 1);
} }
} }
if (sign) { if (sign)
{
if ((spec.align() != ALIGN_RIGHT && spec.align() != ALIGN_DEFAULT) || if ((spec.align() != ALIGN_RIGHT && spec.align() != ALIGN_DEFAULT) ||
*start != ' ') { *start != ' ')
{
*(start - 1) = sign; *(start - 1) = sign;
sign = 0; sign = 0;
} }
else { else
{
*(start - 1) = fill; *(start - 1) = fill;
} }
++n; ++n;
} }
if (spec.align() == ALIGN_CENTER && spec.width() > n) { if (spec.align() == ALIGN_CENTER && spec.width() > n)
{
width = spec.width(); width = spec.width();
CharPtr p = grow_buffer(width); CharPtr p = grow_buffer(width);
std::memmove(get(p) + (width - n) / 2, get(p), n * sizeof(Char)); std::memmove(get(p) + (width - n) / 2, get(p), n * sizeof(Char));
fill_padding(p, spec.width(), n, fill); fill_padding(p, spec.width(), n, fill);
return; return;
} }
if (spec.fill() != ' ' || sign) { if (spec.fill() != ' ' || sign)
{
while (*start == ' ') while (*start == ' ')
*start++ = fill; *start++ = fill;
if (sign) if (sign)
@ -3836,7 +3936,8 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
char *format_decimal(ULongLong value) char *format_decimal(ULongLong value)
{ {
char *buffer_end = buffer_ + BUFFER_SIZE - 1; char *buffer_end = buffer_ + BUFFER_SIZE - 1;
while (value >= 100) { while (value >= 100)
{
// Integer division is slow so do it for a group of two digits instead // Integer division is slow so do it for a group of two digits instead
// of for every digit. The idea comes from the talk by Alexandrescu // of for every digit. The idea comes from the talk by Alexandrescu
// "Three Optimization Tips for C++". See speed-test for a comparison. // "Three Optimization Tips for C++". See speed-test for a comparison.
@ -3845,7 +3946,8 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
*--buffer_end = internal::Data::DIGITS[index + 1]; *--buffer_end = internal::Data::DIGITS[index + 1];
*--buffer_end = internal::Data::DIGITS[index]; *--buffer_end = internal::Data::DIGITS[index];
} }
if (value < 10) { if (value < 10)
{
*--buffer_end = static_cast<char>('0' + value); *--buffer_end = static_cast<char>('0' + value);
return buffer_end; return buffer_end;
} }
@ -3930,12 +4032,15 @@ inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
{ {
typedef typename internal::IntTraits<T>::MainType MainType; typedef typename internal::IntTraits<T>::MainType MainType;
MainType abs_value = static_cast<MainType>(value); MainType abs_value = static_cast<MainType>(value);
if (internal::is_negative(value)) { if (internal::is_negative(value))
{
*buffer++ = '-'; *buffer++ = '-';
abs_value = 0 - abs_value; abs_value = 0 - abs_value;
} }
if (abs_value < 100) { if (abs_value < 100)
if (abs_value < 10) { {
if (abs_value < 10)
{
*buffer++ = static_cast<char>('0' + abs_value); *buffer++ = static_cast<char>('0' + abs_value);
return; return;
} }
@ -4105,14 +4210,16 @@ print("point: ({x}, {y})", FMT_CAPTURE(x, y));
#define FMT_CAPTURE_W(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_W_, __VA_ARGS__) #define FMT_CAPTURE_W(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_W_, __VA_ARGS__)
namespace fmt { namespace fmt
{
FMT_VARIADIC(std::string, format, CStringRef) FMT_VARIADIC(std::string, format, CStringRef)
FMT_VARIADIC_W(std::wstring, format, WCStringRef) FMT_VARIADIC_W(std::wstring, format, WCStringRef)
FMT_VARIADIC(void, print, CStringRef) FMT_VARIADIC(void, print, CStringRef)
FMT_VARIADIC(void, print, std::FILE *, CStringRef) FMT_VARIADIC(void, print, std::FILE *, CStringRef)
FMT_VARIADIC(void, print_colored, Color, CStringRef) FMT_VARIADIC(void, print_colored, Color, CStringRef)
namespace internal { namespace internal
{
template <typename Char> template <typename Char>
inline bool is_name_start(Char c) inline bool is_name_start(Char c)
{ {
@ -4126,15 +4233,18 @@ namespace fmt {
{ {
assert('0' <= *s && *s <= '9'); assert('0' <= *s && *s <= '9');
unsigned value = 0; unsigned value = 0;
do { do
{
unsigned new_value = value * 10 + (*s++ - '0'); unsigned new_value = value * 10 + (*s++ - '0');
// Check if value wrapped around. // Check if value wrapped around.
if (new_value < value) { if (new_value < value)
{
value = (std::numeric_limits<unsigned>::max)(); value = (std::numeric_limits<unsigned>::max)();
break; break;
} }
value = new_value; value = new_value;
} while ('0' <= *s && *s <= '9'); }
while ('0' <= *s && *s <= '9');
// Convert to unsigned to prevent a warning. // Convert to unsigned to prevent a warning.
unsigned max_int = (std::numeric_limits<int>::max)(); unsigned max_int = (std::numeric_limits<int>::max)();
if (value > max_int) if (value > max_int)
@ -4144,7 +4254,8 @@ namespace fmt {
inline void require_numeric_argument(const Arg &arg, char spec) inline void require_numeric_argument(const Arg &arg, char spec)
{ {
if (arg.type > Arg::LAST_NUMERIC_TYPE) { if (arg.type > Arg::LAST_NUMERIC_TYPE)
{
std::string message = std::string message =
fmt::format("format specifier '{}' requires numeric argument", spec); fmt::format("format specifier '{}' requires numeric argument", spec);
FMT_THROW(fmt::FormatError(message)); FMT_THROW(fmt::FormatError(message));
@ -4156,7 +4267,8 @@ namespace fmt {
{ {
char sign = static_cast<char>(*s); char sign = static_cast<char>(*s);
require_numeric_argument(arg, sign); require_numeric_argument(arg, sign);
if (arg.type == Arg::UINT || arg.type == Arg::ULONG_LONG) { if (arg.type == Arg::UINT || arg.type == Arg::ULONG_LONG)
{
FMT_THROW(FormatError(fmt::format( FMT_THROW(FormatError(fmt::format(
"format specifier '{}' requires signed argument", sign))); "format specifier '{}' requires signed argument", sign)));
} }
@ -4168,7 +4280,8 @@ namespace fmt {
inline internal::Arg BasicFormatter<Char, AF>::get_arg( inline internal::Arg BasicFormatter<Char, AF>::get_arg(
BasicStringRef<Char> arg_name, const char *&error) BasicStringRef<Char> arg_name, const char *&error)
{ {
if (check_no_auto_index(error)) { if (check_no_auto_index(error))
{
map_.init(args()); map_.init(args());
const internal::Arg *arg = map_.find(arg_name); const internal::Arg *arg = map_.find(arg_name);
if (arg) if (arg)
@ -4184,7 +4297,8 @@ namespace fmt {
const char *error = FMT_NULL; const char *error = FMT_NULL;
internal::Arg arg = *s < '0' || *s > '9' ? internal::Arg arg = *s < '0' || *s > '9' ?
next_arg(error) : get_arg(internal::parse_nonnegative_int(s), error); next_arg(error) : get_arg(internal::parse_nonnegative_int(s), error);
if (error) { if (error)
{
FMT_THROW(FormatError( FMT_THROW(FormatError(
*s != '}' && *s != ':' ? "invalid format string" : error)); *s != '}' && *s != ':' ? "invalid format string" : error));
} }
@ -4197,9 +4311,11 @@ namespace fmt {
assert(internal::is_name_start(*s)); assert(internal::is_name_start(*s));
const Char *start = s; const Char *start = s;
Char c; Char c;
do { do
{
c = *++s; c = *++s;
} while (internal::is_name_start(c) || ('0' <= c && c <= '9')); }
while (internal::is_name_start(c) || ('0' <= c && c <= '9'));
const char *error = FMT_NULL; const char *error = FMT_NULL;
internal::Arg arg = get_arg(BasicStringRef<Char>(start, s - start), error); internal::Arg arg = get_arg(BasicStringRef<Char>(start, s - start), error);
if (error) if (error)
@ -4214,18 +4330,23 @@ namespace fmt {
using internal::Arg; using internal::Arg;
const Char *s = format_str; const Char *s = format_str;
FormatSpec spec; FormatSpec spec;
if (*s == ':') { if (*s == ':')
if (arg.type == Arg::CUSTOM) { {
if (arg.type == Arg::CUSTOM)
{
arg.custom.format(this, arg.custom.value, &s); arg.custom.format(this, arg.custom.value, &s);
return s; return s;
} }
++s; ++s;
// Parse fill and alignment. // Parse fill and alignment.
if (Char c = *s) { if (Char c = *s)
{
const Char *p = s + 1; const Char *p = s + 1;
spec.align_ = ALIGN_DEFAULT; spec.align_ = ALIGN_DEFAULT;
do { do
switch (*p) { {
switch (*p)
{
case '<': case '<':
spec.align_ = ALIGN_LEFT; spec.align_ = ALIGN_LEFT;
break; break;
@ -4239,8 +4360,10 @@ namespace fmt {
spec.align_ = ALIGN_CENTER; spec.align_ = ALIGN_CENTER;
break; break;
} }
if (spec.align_ != ALIGN_DEFAULT) { if (spec.align_ != ALIGN_DEFAULT)
if (p != s) { {
if (p != s)
{
if (c == '}') break; if (c == '}') break;
if (c == '{') if (c == '{')
FMT_THROW(FormatError("invalid fill character '{'")); FMT_THROW(FormatError("invalid fill character '{'"));
@ -4252,11 +4375,13 @@ namespace fmt {
require_numeric_argument(arg, '='); require_numeric_argument(arg, '=');
break; break;
} }
} while (--p >= s); }
while (--p >= s);
} }
// Parse sign. // Parse sign.
switch (*s) { switch (*s)
{
case '+': case '+':
check_sign(s, arg); check_sign(s, arg);
spec.flags_ |= SIGN_FLAG | PLUS_FLAG; spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
@ -4271,14 +4396,16 @@ namespace fmt {
break; break;
} }
if (*s == '#') { if (*s == '#')
{
require_numeric_argument(arg, '#'); require_numeric_argument(arg, '#');
spec.flags_ |= HASH_FLAG; spec.flags_ |= HASH_FLAG;
++s; ++s;
} }
// Parse zero flag. // Parse zero flag.
if (*s == '0') { if (*s == '0')
{
require_numeric_argument(arg, '0'); require_numeric_argument(arg, '0');
spec.align_ = ALIGN_NUMERIC; spec.align_ = ALIGN_NUMERIC;
spec.fill_ = '0'; spec.fill_ = '0';
@ -4286,17 +4413,20 @@ namespace fmt {
} }
// Parse width. // Parse width.
if ('0' <= *s && *s <= '9') { if ('0' <= *s && *s <= '9')
{
spec.width_ = internal::parse_nonnegative_int(s); spec.width_ = internal::parse_nonnegative_int(s);
} }
else if (*s == '{') { else if (*s == '{')
{
++s; ++s;
Arg width_arg = internal::is_name_start(*s) ? Arg width_arg = internal::is_name_start(*s) ?
parse_arg_name(s) : parse_arg_index(s); parse_arg_name(s) : parse_arg_index(s);
if (*s++ != '}') if (*s++ != '}')
FMT_THROW(FormatError("invalid format string")); FMT_THROW(FormatError("invalid format string"));
ULongLong value = 0; ULongLong value = 0;
switch (width_arg.type) { switch (width_arg.type)
{
case Arg::INT: case Arg::INT:
if (width_arg.int_value < 0) if (width_arg.int_value < 0)
FMT_THROW(FormatError("negative width")); FMT_THROW(FormatError("negative width"));
@ -4322,20 +4452,24 @@ namespace fmt {
} }
// Parse precision. // Parse precision.
if (*s == '.') { if (*s == '.')
{
++s; ++s;
spec.precision_ = 0; spec.precision_ = 0;
if ('0' <= *s && *s <= '9') { if ('0' <= *s && *s <= '9')
{
spec.precision_ = internal::parse_nonnegative_int(s); spec.precision_ = internal::parse_nonnegative_int(s);
} }
else if (*s == '{') { else if (*s == '{')
{
++s; ++s;
Arg precision_arg = internal::is_name_start(*s) ? Arg precision_arg = internal::is_name_start(*s) ?
parse_arg_name(s) : parse_arg_index(s); parse_arg_name(s) : parse_arg_index(s);
if (*s++ != '}') if (*s++ != '}')
FMT_THROW(FormatError("invalid format string")); FMT_THROW(FormatError("invalid format string"));
ULongLong value = 0; ULongLong value = 0;
switch (precision_arg.type) { switch (precision_arg.type)
{
case Arg::INT: case Arg::INT:
if (precision_arg.int_value < 0) if (precision_arg.int_value < 0)
FMT_THROW(FormatError("negative precision")); FMT_THROW(FormatError("negative precision"));
@ -4359,10 +4493,12 @@ namespace fmt {
FMT_THROW(FormatError("number is too big")); FMT_THROW(FormatError("number is too big"));
spec.precision_ = static_cast<int>(value); spec.precision_ = static_cast<int>(value);
} }
else { else
{
FMT_THROW(FormatError("missing precision specifier")); FMT_THROW(FormatError("missing precision specifier"));
} }
if (arg.type <= Arg::LAST_INTEGER_TYPE || arg.type == Arg::POINTER) { if (arg.type <= Arg::LAST_INTEGER_TYPE || arg.type == Arg::POINTER)
{
FMT_THROW(FormatError( FMT_THROW(FormatError(
fmt::format("precision not allowed in {} format specifier", fmt::format("precision not allowed in {} format specifier",
arg.type == Arg::POINTER ? "pointer" : "integer"))); arg.type == Arg::POINTER ? "pointer" : "integer")));
@ -4387,10 +4523,12 @@ namespace fmt {
{ {
const Char *s = format_str.c_str(); const Char *s = format_str.c_str();
const Char *start = s; const Char *start = s;
while (*s) { while (*s)
{
Char c = *s++; Char c = *s++;
if (c != '{' && c != '}') continue; if (c != '{' && c != '}') continue;
if (*s == c) { if (*s == c)
{
write(writer_, start, s); write(writer_, start, s);
start = ++s; start = ++s;
continue; continue;
@ -4407,8 +4545,10 @@ namespace fmt {
} // namespace fmt } // namespace fmt
#if FMT_USE_USER_DEFINED_LITERALS #if FMT_USE_USER_DEFINED_LITERALS
namespace fmt { namespace fmt
namespace internal { {
namespace internal
{
template <typename Char> template <typename Char>
struct UdlFormat struct UdlFormat
@ -4437,7 +4577,8 @@ namespace fmt {
} // namespace internal } // namespace internal
inline namespace literals { inline namespace literals
{
/** /**
\rst \rst

View File

@ -14,9 +14,11 @@ For the license information refer to format.h.
// #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>
@ -36,7 +38,8 @@ namespace fmt {
int_type overflow(int_type ch = traits_type::eof()) int_type overflow(int_type ch = traits_type::eof())
{ {
if (!traits_type::eq_int_type(ch, traits_type::eof())) { if (!traits_type::eq_int_type(ch, traits_type::eof()))
{
size_t buf_size = size(); size_t buf_size = size();
buffer_.resize(buf_size); buffer_.resize(buf_size);
buffer_.reserve(buf_size * 2); buffer_.reserve(buf_size * 2);

View File

@ -15,8 +15,10 @@ For the license information refer to format.h.
#include "ostream.h" #include "ostream.h"
namespace fmt { namespace fmt
namespace internal { {
namespace internal
{
// 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.
@ -127,27 +129,33 @@ namespace fmt {
using internal::Arg; using internal::Arg;
typedef typename internal::Conditional< typedef typename internal::Conditional<
is_same<T, void>::value, U, T>::type TargetType; is_same<T, void>::value, U, T>::type TargetType;
if (sizeof(TargetType) <= sizeof(int)) { if (sizeof(TargetType) <= sizeof(int))
{
// Extra casts are used to silence warnings. // Extra casts are used to silence warnings.
if (is_signed) { if (is_signed)
{
arg_.type = Arg::INT; arg_.type = Arg::INT;
arg_.int_value = static_cast<int>(static_cast<TargetType>(value)); arg_.int_value = static_cast<int>(static_cast<TargetType>(value));
} }
else { else
{
arg_.type = Arg::UINT; arg_.type = Arg::UINT;
typedef typename internal::MakeUnsigned<TargetType>::Type Unsigned; typedef typename internal::MakeUnsigned<TargetType>::Type Unsigned;
arg_.uint_value = static_cast<unsigned>(static_cast<Unsigned>(value)); arg_.uint_value = static_cast<unsigned>(static_cast<Unsigned>(value));
} }
} }
else { else
if (is_signed) { {
if (is_signed)
{
arg_.type = Arg::LONG_LONG; arg_.type = Arg::LONG_LONG;
// glibc's printf doesn't sign extend arguments of smaller types: // glibc's printf doesn't sign extend arguments of smaller types:
// std::printf("%lld", -42); // prints "4294967254" // std::printf("%lld", -42); // prints "4294967254"
// but we don't have to do the same because it's a UB. // but we don't have to do the same because it's a UB.
arg_.long_long_value = static_cast<LongLong>(value); arg_.long_long_value = static_cast<LongLong>(value);
} }
else { else
{
arg_.type = Arg::ULONG_LONG; arg_.type = Arg::ULONG_LONG;
arg_.ulong_long_value = arg_.ulong_long_value =
static_cast<typename internal::MakeUnsigned<U>::Type>(value); static_cast<typename internal::MakeUnsigned<U>::Type>(value);
@ -199,7 +207,8 @@ namespace fmt {
{ {
typedef typename internal::IntTraits<T>::MainType UnsignedType; typedef typename internal::IntTraits<T>::MainType UnsignedType;
UnsignedType width = static_cast<UnsignedType>(value); UnsignedType width = static_cast<UnsignedType>(value);
if (internal::is_negative(value)) { if (internal::is_negative(value))
{
spec_.align_ = ALIGN_LEFT; spec_.align_ = ALIGN_LEFT;
width = 0 - width; width = 0 - width;
} }
@ -271,18 +280,22 @@ namespace fmt {
w.write_int(value, fmt_spec); w.write_int(value, fmt_spec);
typedef typename BasicWriter<Char>::CharPtr CharPtr; typedef typename BasicWriter<Char>::CharPtr CharPtr;
CharPtr out = CharPtr(); CharPtr out = CharPtr();
if (fmt_spec.width_ > 1) { if (fmt_spec.width_ > 1)
{
Char fill = ' '; Char fill = ' ';
out = w.grow_buffer(fmt_spec.width_); out = w.grow_buffer(fmt_spec.width_);
if (fmt_spec.align_ != ALIGN_LEFT) { if (fmt_spec.align_ != ALIGN_LEFT)
{
std::fill_n(out, fmt_spec.width_ - 1, fill); std::fill_n(out, fmt_spec.width_ - 1, fill);
out += fmt_spec.width_ - 1; out += fmt_spec.width_ - 1;
} }
else { else
{
std::fill_n(out + 1, fmt_spec.width_ - 1, fill); std::fill_n(out + 1, fmt_spec.width_ - 1, fill);
} }
} }
else { else
{
out = w.grow_buffer(1); out = w.grow_buffer(1);
} }
*out = static_cast<Char>(value); *out = static_cast<Char>(value);
@ -367,8 +380,10 @@ namespace fmt {
template <typename Char, typename AF> template <typename Char, typename AF>
void PrintfFormatter<Char, AF>::parse_flags(FormatSpec &spec, const Char *&s) void PrintfFormatter<Char, AF>::parse_flags(FormatSpec &spec, const Char *&s)
{ {
for (;;) { for (;;)
switch (*s++) { {
switch (*s++)
{
case '-': case '-':
spec.align_ = ALIGN_LEFT; spec.align_ = ALIGN_LEFT;
break; break;
@ -410,18 +425,22 @@ namespace fmt {
{ {
unsigned arg_index = std::numeric_limits<unsigned>::max(); unsigned arg_index = std::numeric_limits<unsigned>::max();
Char c = *s; Char c = *s;
if (c >= '0' && c <= '9') { if (c >= '0' && c <= '9')
{
// Parse an argument index (if followed by '$') or a width possibly // Parse an argument index (if followed by '$') or a width possibly
// preceded with '0' flag(s). // preceded with '0' flag(s).
unsigned value = internal::parse_nonnegative_int(s); unsigned value = internal::parse_nonnegative_int(s);
if (*s == '$') { // value is an argument index if (*s == '$') // value is an argument index
{
++s; ++s;
arg_index = value; arg_index = value;
} }
else { else
{
if (c == '0') if (c == '0')
spec.fill_ = '0'; spec.fill_ = '0';
if (value != 0) { if (value != 0)
{
// Nonzero value means that we parsed width and don't need to // Nonzero value means that we parsed width and don't need to
// parse it or flags again, so return now. // parse it or flags again, so return now.
spec.width_ = value; spec.width_ = value;
@ -431,10 +450,12 @@ namespace fmt {
} }
parse_flags(spec, s); parse_flags(spec, s);
// Parse width. // Parse width.
if (*s >= '0' && *s <= '9') { if (*s >= '0' && *s <= '9')
{
spec.width_ = internal::parse_nonnegative_int(s); spec.width_ = internal::parse_nonnegative_int(s);
} }
else if (*s == '*') { else if (*s == '*')
{
++s; ++s;
spec.width_ = internal::WidthHandler(spec).visit(get_arg(s)); spec.width_ = internal::WidthHandler(spec).visit(get_arg(s));
} }
@ -446,10 +467,12 @@ namespace fmt {
{ {
const Char *start = format_str.c_str(); const Char *start = format_str.c_str();
const Char *s = start; const Char *s = start;
while (*s) { while (*s)
{
Char c = *s++; Char c = *s++;
if (c != '%') continue; if (c != '%') continue;
if (*s == c) { if (*s == c)
{
write(writer_, start, s); write(writer_, start, s);
start = ++s; start = ++s;
continue; continue;
@ -463,12 +486,15 @@ namespace fmt {
unsigned arg_index = parse_header(s, spec); unsigned arg_index = parse_header(s, spec);
// Parse precision. // Parse precision.
if (*s == '.') { if (*s == '.')
{
++s; ++s;
if ('0' <= *s && *s <= '9') { if ('0' <= *s && *s <= '9')
{
spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(s)); spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(s));
} }
else if (*s == '*') { else if (*s == '*')
{
++s; ++s;
spec.precision_ = internal::PrecisionHandler().visit(get_arg(s)); spec.precision_ = internal::PrecisionHandler().visit(get_arg(s));
} }
@ -478,7 +504,8 @@ namespace fmt {
Arg arg = get_arg(s, arg_index); Arg arg = get_arg(s, arg_index);
if (spec.flag(HASH_FLAG) && internal::IsZeroInt().visit(arg)) if (spec.flag(HASH_FLAG) && internal::IsZeroInt().visit(arg))
spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG); spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
if (spec.fill_ == '0') { if (spec.fill_ == '0')
{
if (arg.type <= Arg::LAST_NUMERIC_TYPE) if (arg.type <= Arg::LAST_NUMERIC_TYPE)
spec.align_ = ALIGN_NUMERIC; spec.align_ = ALIGN_NUMERIC;
else else
@ -487,7 +514,8 @@ namespace fmt {
// Parse length and convert the argument to the required type. // Parse length and convert the argument to the required type.
using internal::ArgConverter; using internal::ArgConverter;
switch (*s++) { switch (*s++)
{
case 'h': case 'h':
if (*s == 'h') if (*s == 'h')
ArgConverter<signed char>(arg, *++s).visit(arg); ArgConverter<signed char>(arg, *++s).visit(arg);
@ -522,10 +550,13 @@ namespace fmt {
if (!*s) if (!*s)
FMT_THROW(FormatError("invalid format string")); FMT_THROW(FormatError("invalid format string"));
spec.type_ = static_cast<char>(*s++); spec.type_ = static_cast<char>(*s++);
if (arg.type <= Arg::LAST_INTEGER_TYPE) { if (arg.type <= Arg::LAST_INTEGER_TYPE)
{
// Normalize type. // Normalize type.
switch (spec.type_) { switch (spec.type_)
case 'i': case 'u': {
case 'i':
case 'u':
spec.type_ = 'd'; spec.type_ = 'd';
break; break;
case 'c': case 'c':