]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Avoid sometimes too wide percent figure in --show-stats
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 30 Dec 2022 20:49:23 +0000 (21:49 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 11 Jan 2023 18:42:31 +0000 (19:42 +0100)
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.

src/core/Statistics.cpp

index 98a7623103cb628b6eefda4a95076adfb18ad81d..13302fe9252d8d544e002cd6ff678391bdbab2ad 100644 (file)
@@ -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);
   }
 }