Skip to content

Commit

Permalink
Fix precision handling in snprintf_float
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Nov 26, 2019
1 parent 0d07db1 commit 7ffa62d
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ int snprintf_float(T value, int precision, float_spec spec, buffer<char>& buf) {

// Subtract 1 to account for the difference in precision since we use %e for
// both general and exponent format.
if (spec.format == float_format::general)
if (spec.format == float_format::general || spec.format == float_format::exp)
precision = (precision >= 0 ? precision : 6) - 1;

// Build the format string.
Expand All @@ -1127,7 +1127,7 @@ int snprintf_float(T value, int precision, float_spec spec, buffer<char>& buf) {
char* format_ptr = format;
*format_ptr++ = '%';
if (spec.alt) *format_ptr++ = '#';
if (precision > 0) {
if (precision >= 0) {
*format_ptr++ = '.';
*format_ptr++ = '*';
}
Expand All @@ -1149,7 +1149,7 @@ int snprintf_float(T value, int precision, float_spec spec, buffer<char>& buf) {
#endif
// Suppress the warning about a nonliteral format string.
auto snprintf_ptr = FMT_SNPRINTF;
int result = precision > 0
int result = precision >= 0
? snprintf_ptr(begin, capacity, format, precision, value)
: snprintf_ptr(begin, capacity, format, value);
if (result < 0) {
Expand All @@ -1164,6 +1164,10 @@ int snprintf_float(T value, int precision, float_spec spec, buffer<char>& buf) {
}
auto is_digit = [](char c) { return c >= '0' && c <= '9'; };
if (spec.format == float_format::fixed) {
if (precision == 0) {
buf.resize(size);
return 0;
}
// Find and remove the decimal point.
auto end = begin + size, p = end;
do {
Expand Down

0 comments on commit 7ffa62d

Please sign in to comment.