-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for '%' type to output floating point values as a percentage. #1060
Closed
+45
−8
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
29db6bd
Add support for '%' type to output floating point values as a
gawain-bolton 73f31a8
Updates for formatting output as a percentage.
gawain-bolton d6d33a9
Changes to fix CI as g++-4.4 BUILD was broken.
gawain-bolton 141d1cf
Revert unnecessary changes.
gawain-bolton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1274,6 +1274,9 @@ FMT_CONSTEXPR void handle_float_type_spec(char spec, Handler&& handler) { | |
case 'F': | ||
handler.on_fixed(); | ||
break; | ||
case '%': | ||
handler.on_percent(); | ||
break; | ||
case 'a': | ||
case 'A': | ||
handler.on_hex(); | ||
|
@@ -1338,6 +1341,7 @@ class float_type_checker : private ErrorHandler { | |
FMT_CONSTEXPR void on_general() {} | ||
FMT_CONSTEXPR void on_exp() {} | ||
FMT_CONSTEXPR void on_fixed() {} | ||
FMT_CONSTEXPR void on_percent() {} | ||
FMT_CONSTEXPR void on_hex() {} | ||
|
||
FMT_CONSTEXPR void on_error() { | ||
|
@@ -2642,17 +2646,20 @@ template <typename Range> class basic_writer { | |
|
||
struct inf_or_nan_writer { | ||
char sign; | ||
bool as_percentage; | ||
const char* str; | ||
|
||
size_t size() const { | ||
return static_cast<std::size_t>(INF_SIZE + (sign ? 1 : 0)); | ||
return static_cast<std::size_t>(INF_SIZE + (sign ? 1 : 0) + | ||
(as_percentage ? 1 : 0)); | ||
} | ||
size_t width() const { return size(); } | ||
|
||
template <typename It> void operator()(It&& it) const { | ||
if (sign) *it++ = static_cast<char_type>(sign); | ||
it = internal::copy_str<char_type>( | ||
str, str + static_cast<std::size_t>(INF_SIZE), it); | ||
if (as_percentage) *it++ = static_cast<char_type>('%'); | ||
} | ||
}; | ||
|
||
|
@@ -2810,8 +2817,9 @@ struct float_spec_handler { | |
char type; | ||
bool upper; | ||
bool fixed; | ||
bool as_percentage; | ||
|
||
explicit float_spec_handler(char t) : type(t), upper(false), fixed(false) {} | ||
explicit float_spec_handler(char t) : type(t), upper(false), fixed(false), as_percentage(false) {} | ||
|
||
void on_general() { | ||
if (type == 'G') upper = true; | ||
|
@@ -2826,6 +2834,11 @@ struct float_spec_handler { | |
if (type == 'F') upper = true; | ||
} | ||
|
||
void on_percent() { | ||
fixed = true; | ||
as_percentage = true; | ||
} | ||
|
||
void on_hex() { | ||
if (type == 'A') upper = true; | ||
} | ||
|
@@ -2854,26 +2867,39 @@ void basic_writer<Range>::write_double(T value, const format_specs& spec) { | |
basic_writer& writer; | ||
format_specs spec; | ||
char sign; | ||
bool as_percentage; | ||
void operator()(const char* str) const { | ||
writer.write_padded(spec, inf_or_nan_writer{sign, str}); | ||
writer.write_padded(spec, inf_or_nan_writer{sign, as_percentage, str}); | ||
} | ||
} write_inf_or_nan = {*this, spec, sign}; | ||
} write_inf_or_nan = {*this, spec, sign, handler.as_percentage}; | ||
|
||
// Format NaN and ininity ourselves because sprintf's output is not consistent | ||
// across platforms. | ||
if (internal::fputil::isnotanumber(value)) | ||
return write_inf_or_nan(handler.upper ? "NAN" : "nan"); | ||
if (internal::fputil::isinfinity(value)) | ||
return write_inf_or_nan(handler.upper ? "INF" : "inf"); | ||
if (internal::fputil::isnotanumber(value)) { | ||
write_inf_or_nan(handler.upper ? "NAN" : "nan"); | ||
return; | ||
} | ||
if (internal::fputil::isinfinity(value)) { | ||
write_inf_or_nan(handler.upper ? "INF" : "inf"); | ||
return; | ||
} | ||
|
||
memory_buffer buffer; | ||
int exp = 0; | ||
int precision = spec.has_precision() || !spec.type ? spec.precision : 6; | ||
|
||
if (handler.as_percentage) value *= 100.; | ||
|
||
bool use_grisu = fmt::internal::use_grisu<T>() && | ||
(!spec.type || handler.fixed) && | ||
internal::grisu2_format(static_cast<double>(value), buffer, | ||
precision, handler.fixed, exp); | ||
if (!use_grisu) internal::sprintf_format(value, buffer, spec); | ||
|
||
if (handler.as_percentage) { | ||
buffer.push_back('%'); | ||
--exp; // Adjust decimal place position. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be -= 2? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The decimal place position is shifted by one place because only one character, the percent sign, is added. |
||
} | ||
align_spec as = spec; | ||
if (spec.align() == ALIGN_NUMERIC) { | ||
if (sign) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change looks unrelated. Please revert.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, I've reverted this changes.