trailing_zeros -> showpoint

This commit is contained in:
Victor Zverovich 2019-12-18 12:12:09 -08:00
parent d7e72a09e0
commit 1b1c70108a
3 changed files with 11 additions and 10 deletions

View File

@ -1106,7 +1106,7 @@ int snprintf_float(T value, int precision, float_specs specs,
char format[max_format_size];
char* format_ptr = format;
*format_ptr++ = '%';
if (specs.trailing_zeros) *format_ptr++ = '#';
if (specs.showpoint) *format_ptr++ = '#';
if (precision >= 0) {
*format_ptr++ = '.';
*format_ptr++ = '*';

View File

@ -1035,7 +1035,7 @@ struct float_specs {
bool percent : 1;
bool binary32 : 1;
bool use_grisu : 1;
bool trailing_zeros : 1;
bool showpoint : 1;
};
// Writes the exponent exp in the form "[+-]d{2,3}" to buffer.
@ -1076,7 +1076,7 @@ template <typename Char> class float_writer {
// Insert a decimal point after the first digit and add an exponent.
*it++ = static_cast<Char>(*digits_);
int num_zeros = specs_.precision - num_digits_;
bool trailing_zeros = num_zeros > 0 && specs_.trailing_zeros;
bool trailing_zeros = num_zeros > 0 && specs_.showpoint;
if (num_digits_ > 1 || trailing_zeros) *it++ = decimal_point_;
it = copy_str<Char>(digits_ + 1, digits_ + num_digits_, it);
if (trailing_zeros)
@ -1088,7 +1088,7 @@ template <typename Char> class float_writer {
// 1234e7 -> 12340000000[.0+]
it = copy_str<Char>(digits_, digits_ + num_digits_, it);
it = std::fill_n(it, full_exp - num_digits_, static_cast<Char>('0'));
if (specs_.trailing_zeros) {
if (specs_.showpoint) {
*it++ = decimal_point_;
int num_zeros = specs_.precision - full_exp;
if (num_zeros <= 0) {
@ -1105,7 +1105,7 @@ template <typename Char> class float_writer {
} else if (full_exp > 0) {
// 1234e-2 -> 12.34[0+]
it = copy_str<Char>(digits_, digits_ + full_exp, it);
if (!specs_.trailing_zeros) {
if (!specs_.showpoint) {
// Remove trailing zeros.
int num_digits = num_digits_;
while (num_digits > full_exp && digits_[num_digits - 1] == '0')
@ -1127,7 +1127,7 @@ template <typename Char> class float_writer {
if (specs_.precision >= 0 && specs_.precision < num_zeros)
num_zeros = specs_.precision;
int num_digits = num_digits_;
if (!specs_.trailing_zeros)
if (!specs_.showpoint)
while (num_digits > 0 && digits_[num_digits - 1] == '0') --num_digits;
if (num_zeros != 0 || num_digits != 0) {
*it++ = decimal_point_;
@ -1206,11 +1206,11 @@ template <typename ErrorHandler = error_handler, typename Char>
FMT_CONSTEXPR float_specs parse_float_type_spec(
const basic_format_specs<Char>& specs, ErrorHandler&& eh = {}) {
auto result = float_specs();
result.trailing_zeros = specs.alt;
result.showpoint = specs.alt;
switch (specs.type) {
case 0:
result.format = float_format::general;
result.trailing_zeros |= specs.precision != 0;
result.showpoint |= specs.precision != 0;
break;
case 'G':
result.upper = true;
@ -1223,14 +1223,14 @@ FMT_CONSTEXPR float_specs parse_float_type_spec(
FMT_FALLTHROUGH;
case 'e':
result.format = float_format::exp;
result.trailing_zeros |= specs.precision != 0;
result.showpoint |= specs.precision != 0;
break;
case 'F':
result.upper = true;
FMT_FALLTHROUGH;
case 'f':
result.format = float_format::fixed;
result.trailing_zeros |= specs.precision != 0;
result.showpoint |= specs.precision != 0;
break;
#if FMT_DEPRECATED_PERCENT
case '%':

View File

@ -924,6 +924,7 @@ TEST(FormatterTest, HashFlag) {
EXPECT_EQ("-42.0", format("{0:#}", -42.0));
EXPECT_EQ("-42.0", format("{0:#}", -42.0l));
EXPECT_EQ("4.e+01", format("{:#.0e}", 42.0));
EXPECT_THROW_MSG(format("{0:#", 'c'), format_error,
"missing '}' in format string");
EXPECT_THROW_MSG(format("{0:#}", 'c'), format_error,