Commit 29246221 authored by Victor Zverovich's avatar Victor Zverovich

Fix naming of basic_format_specs members

parent bda5f9a5
...@@ -569,17 +569,17 @@ struct gen_digits_params { ...@@ -569,17 +569,17 @@ struct gen_digits_params {
// Creates digit generation parameters from format specifiers for a number in // Creates digit generation parameters from format specifiers for a number in
// the range [pow(10, exp - 1), pow(10, exp) or 0 if exp == 1. // the range [pow(10, exp - 1), pow(10, exp) or 0 if exp == 1.
gen_digits_params(const core_format_specs &specs, int exp) gen_digits_params(const core_format_specs &specs, int exp)
: min_digits(specs.precision_ >= 0 ? to_unsigned(specs.precision_) : 6), : min_digits(specs.precision >= 0 ? to_unsigned(specs.precision) : 6),
fixed(false), upper(false), trailing_zeros(false) { fixed(false), upper(false), trailing_zeros(false) {
switch (specs.type_) { switch (specs.type) {
case 'G': case 'G':
upper = true; upper = true;
FMT_FALLTHROUGH FMT_FALLTHROUGH
case '\0': case 'g': case '\0': case 'g':
trailing_zeros = (specs.flags_ & HASH_FLAG) != 0; trailing_zeros = (specs.flags & HASH_FLAG) != 0;
if (-4 <= exp && exp < static_cast<int>(min_digits) + 1) { if (-4 <= exp && exp < static_cast<int>(min_digits) + 1) {
fixed = true; fixed = true;
if (!specs.type_ && trailing_zeros && exp >= 0) if (!specs.type && trailing_zeros && exp >= 0)
min_digits = to_unsigned(exp) + 1; min_digits = to_unsigned(exp) + 1;
} }
break; break;
...@@ -718,6 +718,50 @@ FMT_FUNC typename std::enable_if<sizeof(Double) == sizeof(uint64_t), bool>::type ...@@ -718,6 +718,50 @@ FMT_FUNC typename std::enable_if<sizeof(Double) == sizeof(uint64_t), bool>::type
buf.resize(size); buf.resize(size);
return true; return true;
} }
template <typename Double>
void sprintf_format(
Double value, internal::buffer &buffer, core_format_specs spec) {
// Buffer capacity must be non-zero, otherwise MSVC's vsnprintf_s will fail.
FMT_ASSERT(buffer.capacity() != 0, "empty buffer");
// Build format string.
enum { MAX_FORMAT_SIZE = 10}; // longest format: %#-*.*Lg
char format[MAX_FORMAT_SIZE];
char *format_ptr = format;
*format_ptr++ = '%';
if (spec.has(HASH_FLAG))
*format_ptr++ = '#';
if (spec.precision >= 0) {
*format_ptr++ = '.';
*format_ptr++ = '*';
}
if (std::is_same<Double, long double>::value)
*format_ptr++ = 'L';
*format_ptr++ = spec.type;
*format_ptr = '\0';
// Format using snprintf.
char *start = FMT_NULL;
for (;;) {
std::size_t buffer_size = buffer.capacity();
start = &buffer[0];
int result = internal::char_traits<char>::format_float(
start, buffer_size, format, spec.precision, value);
if (result >= 0) {
unsigned n = internal::to_unsigned(result);
if (n < buffer.capacity()) {
buffer.resize(n);
break; // The buffer is large enough - continue with formatting.
}
buffer.reserve(n + 1);
} else {
// If result is negative we ask to increase the capacity by at least 1,
// but as std::vector, the buffer grows exponentially.
buffer.reserve(buffer.capacity() + 1);
}
}
}
} // namespace internal } // namespace internal
#if FMT_USE_WINDOWS_H #if FMT_USE_WINDOWS_H
......
This diff is collapsed.
...@@ -222,12 +222,12 @@ class printf_arg_formatter: ...@@ -222,12 +222,12 @@ class printf_arg_formatter:
context_type &context_; context_type &context_;
void write_null_pointer(char) { void write_null_pointer(char) {
this->spec()->type_ = 0; this->spec()->type = 0;
this->write("(nil)"); this->write("(nil)");
} }
void write_null_pointer(wchar_t) { void write_null_pointer(wchar_t) {
this->spec()->type_ = 0; this->spec()->type = 0;
this->write(L"(nil)"); this->write(L"(nil)");
} }
...@@ -253,15 +253,15 @@ class printf_arg_formatter: ...@@ -253,15 +253,15 @@ class printf_arg_formatter:
// use std::is_same instead. // use std::is_same instead.
if (std::is_same<T, bool>::value) { if (std::is_same<T, bool>::value) {
format_specs &fmt_spec = *this->spec(); format_specs &fmt_spec = *this->spec();
if (fmt_spec.type_ != 's') if (fmt_spec.type != 's')
return base::operator()(value ? 1 : 0); return base::operator()(value ? 1 : 0);
fmt_spec.type_ = 0; fmt_spec.type = 0;
this->write(value != 0); this->write(value != 0);
} else if (std::is_same<T, char_type>::value) { } else if (std::is_same<T, char_type>::value) {
format_specs &fmt_spec = *this->spec(); format_specs &fmt_spec = *this->spec();
if (fmt_spec.type_ && fmt_spec.type_ != 'c') if (fmt_spec.type && fmt_spec.type != 'c')
return (*this)(static_cast<int>(value)); return (*this)(static_cast<int>(value));
fmt_spec.flags_ = 0; fmt_spec.flags = 0;
fmt_spec.align_ = ALIGN_RIGHT; fmt_spec.align_ = ALIGN_RIGHT;
return base::operator()(value); return base::operator()(value);
} else { } else {
...@@ -280,7 +280,7 @@ class printf_arg_formatter: ...@@ -280,7 +280,7 @@ class printf_arg_formatter:
iterator operator()(const char *value) { iterator operator()(const char *value) {
if (value) if (value)
base::operator()(value); base::operator()(value);
else if (this->spec()->type_ == 'p') else if (this->spec()->type == 'p')
write_null_pointer(char_type()); write_null_pointer(char_type());
else else
this->write("(null)"); this->write("(null)");
...@@ -291,7 +291,7 @@ class printf_arg_formatter: ...@@ -291,7 +291,7 @@ class printf_arg_formatter:
iterator operator()(const wchar_t *value) { iterator operator()(const wchar_t *value) {
if (value) if (value)
base::operator()(value); base::operator()(value);
else if (this->spec()->type_ == 'p') else if (this->spec()->type == 'p')
write_null_pointer(char_type()); write_null_pointer(char_type());
else else
this->write(L"(null)"); this->write(L"(null)");
...@@ -310,7 +310,7 @@ class printf_arg_formatter: ...@@ -310,7 +310,7 @@ class printf_arg_formatter:
iterator operator()(const void *value) { iterator operator()(const void *value) {
if (value) if (value)
return base::operator()(value); return base::operator()(value);
this->spec()->type_ = 0; this->spec()->type = 0;
write_null_pointer(char_type()); write_null_pointer(char_type());
return this->out(); return this->out();
} }
...@@ -394,16 +394,16 @@ void basic_printf_context<OutputIt, Char, AF>::parse_flags( ...@@ -394,16 +394,16 @@ void basic_printf_context<OutputIt, Char, AF>::parse_flags(
spec.align_ = ALIGN_LEFT; spec.align_ = ALIGN_LEFT;
break; break;
case '+': case '+':
spec.flags_ |= SIGN_FLAG | PLUS_FLAG; spec.flags |= SIGN_FLAG | PLUS_FLAG;
break; break;
case '0': case '0':
spec.fill_ = '0'; spec.fill_ = '0';
break; break;
case ' ': case ' ':
spec.flags_ |= SIGN_FLAG; spec.flags |= SIGN_FLAG;
break; break;
case '#': case '#':
spec.flags_ |= HASH_FLAG; spec.flags |= HASH_FLAG;
break; break;
default: default:
--it; --it;
...@@ -486,19 +486,19 @@ void basic_printf_context<OutputIt, Char, AF>::format() { ...@@ -486,19 +486,19 @@ void basic_printf_context<OutputIt, Char, AF>::format() {
++it; ++it;
if ('0' <= *it && *it <= '9') { if ('0' <= *it && *it <= '9') {
internal::error_handler eh; internal::error_handler eh;
spec.precision_ = static_cast<int>(parse_nonnegative_int(it, eh)); spec.precision = static_cast<int>(parse_nonnegative_int(it, eh));
} else if (*it == '*') { } else if (*it == '*') {
++it; ++it;
spec.precision_ = spec.precision =
visit_format_arg(internal::printf_precision_handler(), get_arg(it)); visit_format_arg(internal::printf_precision_handler(), get_arg(it));
} else { } else {
spec.precision_ = 0; spec.precision = 0;
} }
} }
format_arg arg = get_arg(it, arg_index); format_arg arg = get_arg(it, arg_index);
if (spec.flag(HASH_FLAG) && visit_format_arg(internal::is_zero_int(), arg)) if (spec.has(HASH_FLAG) && visit_format_arg(internal::is_zero_int(), 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.is_arithmetic()) if (arg.is_arithmetic())
spec.align_ = ALIGN_NUMERIC; spec.align_ = ALIGN_NUMERIC;
...@@ -542,12 +542,12 @@ void basic_printf_context<OutputIt, Char, AF>::format() { ...@@ -542,12 +542,12 @@ void basic_printf_context<OutputIt, Char, AF>::format() {
// Parse type. // Parse type.
if (!*it) if (!*it)
FMT_THROW(format_error("invalid format string")); FMT_THROW(format_error("invalid format string"));
spec.type_ = static_cast<char>(*it++); spec.type = static_cast<char>(*it++);
if (arg.is_integral()) { if (arg.is_integral()) {
// 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':
// TODO: handle wchar_t better? // TODO: handle wchar_t better?
......
...@@ -26,8 +26,8 @@ class custom_arg_formatter : ...@@ -26,8 +26,8 @@ class custom_arg_formatter :
using base::operator(); using base::operator();
iterator operator()(double value) { iterator operator()(double value) {
// Comparing a float to 0.0 is safe // Comparing a float to 0.0 is safe.
if (round(value * pow(10, spec()->precision())) == 0.0) if (round(value * pow(10, spec()->precision)) == 0.0)
value = 0; value = 0;
return base::operator()(value); return base::operator()(value);
} }
......
...@@ -103,7 +103,6 @@ TEST(FPTest, GetCachedPower) { ...@@ -103,7 +103,6 @@ TEST(FPTest, GetCachedPower) {
} }
TEST(FPTest, Grisu2FormatCompilesWithNonIEEEDouble) { TEST(FPTest, Grisu2FormatCompilesWithNonIEEEDouble) {
size_t size = 0;
fmt::memory_buffer buf; fmt::memory_buffer buf;
grisu2_format(4.2f, buf, fmt::core_format_specs()); grisu2_format(4.2f, buf, fmt::core_format_specs());
} }
......
...@@ -2182,19 +2182,19 @@ FMT_CONSTEXPR fmt::format_specs parse_specs(const char *s) { ...@@ -2182,19 +2182,19 @@ FMT_CONSTEXPR fmt::format_specs parse_specs(const char *s) {
} }
TEST(FormatTest, ConstexprSpecsHandler) { TEST(FormatTest, ConstexprSpecsHandler) {
static_assert(parse_specs("<").align() == fmt::ALIGN_LEFT, ""); static_assert(parse_specs("<").align == fmt::ALIGN_LEFT, "");
static_assert(parse_specs("*^").fill() == '*', ""); static_assert(parse_specs("*^").fill() == '*', "");
static_assert(parse_specs("+").flag(fmt::PLUS_FLAG), ""); static_assert(parse_specs("+").has(fmt::PLUS_FLAG), "");
static_assert(parse_specs("-").flag(fmt::MINUS_FLAG), ""); static_assert(parse_specs("-").has(fmt::MINUS_FLAG), "");
static_assert(parse_specs(" ").flag(fmt::SIGN_FLAG), ""); static_assert(parse_specs(" ").has(fmt::SIGN_FLAG), "");
static_assert(parse_specs("#").flag(fmt::HASH_FLAG), ""); static_assert(parse_specs("#").has(fmt::HASH_FLAG), "");
static_assert(parse_specs("0").align() == fmt::ALIGN_NUMERIC, ""); static_assert(parse_specs("0").align == fmt::ALIGN_NUMERIC, "");
static_assert(parse_specs("42").width() == 42, ""); static_assert(parse_specs("42").width() == 42, "");
static_assert(parse_specs("{}").width() == 11, ""); static_assert(parse_specs("{}").width() == 11, "");
static_assert(parse_specs("{0}").width() == 22, ""); static_assert(parse_specs("{0}").width() == 22, "");
static_assert(parse_specs(".42").precision() == 42, ""); static_assert(parse_specs(".42").precision == 42, "");
static_assert(parse_specs(".{}").precision() == 11, ""); static_assert(parse_specs(".{}").precision == 11, "");
static_assert(parse_specs(".{0}").precision() == 22, ""); static_assert(parse_specs(".{0}").precision == 22, "");
static_assert(parse_specs("d").type() == 'd', ""); static_assert(parse_specs("d").type() == 'd', "");
} }
...@@ -2208,17 +2208,17 @@ FMT_CONSTEXPR fmt::internal::dynamic_format_specs<char> ...@@ -2208,17 +2208,17 @@ FMT_CONSTEXPR fmt::internal::dynamic_format_specs<char>
} }
TEST(FormatTest, ConstexprDynamicSpecsHandler) { TEST(FormatTest, ConstexprDynamicSpecsHandler) {
static_assert(parse_dynamic_specs("<").align() == fmt::ALIGN_LEFT, ""); static_assert(parse_dynamic_specs("<").align == fmt::ALIGN_LEFT, "");
static_assert(parse_dynamic_specs("*^").fill() == '*', ""); static_assert(parse_dynamic_specs("*^").fill() == '*', "");
static_assert(parse_dynamic_specs("+").flag(fmt::PLUS_FLAG), ""); static_assert(parse_dynamic_specs("+").has(fmt::PLUS_FLAG), "");
static_assert(parse_dynamic_specs("-").flag(fmt::MINUS_FLAG), ""); static_assert(parse_dynamic_specs("-").has(fmt::MINUS_FLAG), "");
static_assert(parse_dynamic_specs(" ").flag(fmt::SIGN_FLAG), ""); static_assert(parse_dynamic_specs(" ").has(fmt::SIGN_FLAG), "");
static_assert(parse_dynamic_specs("#").flag(fmt::HASH_FLAG), ""); static_assert(parse_dynamic_specs("#").has(fmt::HASH_FLAG), "");
static_assert(parse_dynamic_specs("0").align() == fmt::ALIGN_NUMERIC, ""); static_assert(parse_dynamic_specs("0").align == fmt::ALIGN_NUMERIC, "");
static_assert(parse_dynamic_specs("42").width() == 42, ""); static_assert(parse_dynamic_specs("42").width() == 42, "");
static_assert(parse_dynamic_specs("{}").width_ref.index == 33, ""); static_assert(parse_dynamic_specs("{}").width_ref.index == 33, "");
static_assert(parse_dynamic_specs("{42}").width_ref.index == 42, ""); static_assert(parse_dynamic_specs("{42}").width_ref.index == 42, "");
static_assert(parse_dynamic_specs(".42").precision() == 42, ""); static_assert(parse_dynamic_specs(".42").precision == 42, "");
static_assert(parse_dynamic_specs(".{}").precision_ref.index == 33, ""); static_assert(parse_dynamic_specs(".{}").precision_ref.index == 33, "");
static_assert(parse_dynamic_specs(".{42}").precision_ref.index == 42, ""); static_assert(parse_dynamic_specs(".{42}").precision_ref.index == 42, "");
static_assert(parse_dynamic_specs("d").type() == 'd', ""); static_assert(parse_dynamic_specs("d").type() == 'd', "");
......
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