summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Rosdahl <joel@rosdahl.net>2022-12-30 21:49:23 +0100
committerJoel Rosdahl <joel@rosdahl.net>2023-01-11 19:42:31 +0100
commitdfb749a6c871babefff358c3b1a460a33dea67a0 (patch)
treeddd40e0404599e57aca986f66a99b1681e273fe7
parent93e793b8f64e61ad8f27a065c6565a951cfcad1d (diff)
downloadccache-dfb749a6c871babefff358c3b1a460a33dea67a0.tar.gz
fix: Avoid sometimes too wide percent figure in --show-stats
If the nominator is 99999 and the denominator is 100000, the percent function in Statistics.cpp would return "(100.00%)" instead of the wanted "(100.0%)". Fix this by using the alternate format string if the result string overflows its target size.
-rw-r--r--src/core/Statistics.cpp9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/core/Statistics.cpp b/src/core/Statistics.cpp
index 98a76231..13302fe9 100644
--- a/src/core/Statistics.cpp
+++ b/src/core/Statistics.cpp
@@ -152,10 +152,13 @@ percent(const uint64_t nominator, const uint64_t denominator)
{
if (denominator == 0) {
return "";
- } else if (nominator >= denominator) {
- return FMT("({:5.1f}%)", (100.0 * nominator) / denominator);
+ }
+
+ std::string result = FMT("({:5.2f}%)", (100.0 * nominator) / denominator);
+ if (result.length() <= 8) {
+ return result;
} else {
- return FMT("({:5.2f}%)", (100.0 * nominator) / denominator);
+ return FMT("({:5.1f}%)", (100.0 * nominator) / denominator);
}
}