]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/format-table: do not print '.0'
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 29 May 2024 16:07:23 +0000 (18:07 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 30 May 2024 09:38:30 +0000 (11:38 +0200)
This makes output a bit shorter and nicer. For us, shorter output is generally
better.

Also, drop unnecessary UINT64_C macros. The left operand is always uint64_t,
and C upcasting rules mean that it doesn't matter if the right operand is
narrower or signed, the operation is always done on the wider unsigned type.

src/basic/format-util.c
src/test/test-format-table.c
src/test/test-format-util.c
test/units/TEST-58-REPART.sh

index 056c990cc77c966cc4b2fcd33355dffc64997f46..445fecc8cad736fc74b73dbc9311abf071a5bb9f 100644 (file)
@@ -70,15 +70,17 @@ char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) {
 
         for (size_t i = 0; i < n; i++)
                 if (t >= table[i].factor) {
-                        if (flag & FORMAT_BYTES_BELOW_POINT) {
+                        uint64_t remainder = i != n - 1 ?
+                                (t / table[i + 1].factor * 10 / table[n - 1].factor) % 10 :
+                                (t * 10 / table[i].factor) % 10;
+
+                        if (FLAGS_SET(flag, FORMAT_BYTES_BELOW_POINT) && remainder > 0)
                                 (void) snprintf(buf, l,
                                                 "%" PRIu64 ".%" PRIu64 "%s",
                                                 t / table[i].factor,
-                                                i != n - 1 ?
-                                                (t / table[i + 1].factor * UINT64_C(10) / table[n - 1].factor) % UINT64_C(10):
-                                                (t * UINT64_C(10) / table[i].factor) % UINT64_C(10),
+                                                remainder,
                                                 table[i].suffix);
-                        else
+                        else
                                 (void) snprintf(buf, l,
                                                 "%" PRIu64 "%s",
                                                 t / table[i].factor,
index 4425f4097a2eca7fc52f72164cf6384ae0a812b9..3a0efdacc16ada12e444887cada79a161ebae652 100644 (file)
@@ -551,7 +551,7 @@ TEST(vertical) {
 
         assert_se(streq(formatted,
                         "     pfft aa: foo\n"
-                        "       uuu o: 1.0K\n"
+                        "       uuu o: 1K\n"
                         "lllllllllllo: jjjjjjjjjjjjjjjjj\n"));
 
         _cleanup_(json_variant_unrefp) JsonVariant *a = NULL, *b = NULL;
@@ -653,7 +653,7 @@ TEST(table_bps) {
                         "UINT64             SIZE            BPS\n"
                         "2500               2.4K            2Kbps\n"
                         "10000000           9.5M            10Mbps\n"
-                        "20000000           19.0M           20Mbps\n"
+                        "20000000           19M             20Mbps\n"
                         "25000000           23.8M           25Mbps\n"
                         "1000000000         953.6M          1Gbps\n"
                         "2000000000         1.8G            2Gbps\n"
index 94feb6cafc0b5a31d8b51ee30fc1a6d819f6fe15..8afba4e585fee8b372253126b94e4a0380f0d6d6 100644 (file)
@@ -43,17 +43,17 @@ static void test_format_bytes_one(uint64_t val, bool trailing_B, const char *iec
 TEST(format_bytes) {
         test_format_bytes_one(900, true, "900B", "900B", "900B", "900B");
         test_format_bytes_one(900, false, "900", "900", "900", "900");
-        test_format_bytes_one(1023, true, "1023B", "1023B", "1.0K", "1K");
-        test_format_bytes_one(1023, false, "1023", "1023", "1.0K", "1K");
-        test_format_bytes_one(1024, true, "1.0K", "1K", "1.0K", "1K");
-        test_format_bytes_one(1024, false, "1.0K", "1K", "1.0K", "1K");
-        test_format_bytes_one(1100, true, "1.0K", "1K", "1.1K", "1K");
+        test_format_bytes_one(1023, true, "1023B", "1023B", "1K", "1K");
+        test_format_bytes_one(1023, false, "1023", "1023", "1K", "1K");
+        test_format_bytes_one(1024, true, "1K", "1K", "1K", "1K");
+        test_format_bytes_one(1024, false, "1K", "1K", "1K", "1K");
+        test_format_bytes_one(1100, true, "1K", "1K", "1.1K", "1K");
         test_format_bytes_one(1500, true, "1.4K", "1K", "1.5K", "1K");
-        test_format_bytes_one(UINT64_C(3)*1024*1024, true, "3.0M", "3M", "3.1M", "3M");
-        test_format_bytes_one(UINT64_C(3)*1024*1024*1024, true, "3.0G", "3G", "3.2G", "3G");
-        test_format_bytes_one(UINT64_C(3)*1024*1024*1024*1024, true, "3.0T", "3T", "3.2T", "3T");
-        test_format_bytes_one(UINT64_C(3)*1024*1024*1024*1024*1024, true, "3.0P", "3P", "3.3P", "3P");
-        test_format_bytes_one(UINT64_C(3)*1024*1024*1024*1024*1024*1024, true, "3.0E", "3E", "3.4E", "3E");
+        test_format_bytes_one(UINT64_C(3)*1024*1024, true, "3M", "3M", "3.1M", "3M");
+        test_format_bytes_one(UINT64_C(3)*1024*1024*1024, true, "3G", "3G", "3.2G", "3G");
+        test_format_bytes_one(UINT64_C(3)*1024*1024*1024*1024, true, "3T", "3T", "3.2T", "3T");
+        test_format_bytes_one(UINT64_C(3)*1024*1024*1024*1024*1024, true, "3P", "3P", "3.3P", "3P");
+        test_format_bytes_one(UINT64_C(3)*1024*1024*1024*1024*1024*1024, true, "3E", "3E", "3.4E", "3E");
         test_format_bytes_one(UINT64_MAX, true, NULL, NULL, NULL, NULL);
         test_format_bytes_one(UINT64_MAX, false, NULL, NULL, NULL, NULL);
 }
index 7280352fb9b4ebc7fb490f03565c7bf03efa6e0d..8a014ac9fb43fab771e702699fdc58f636ece70c 100755 (executable)
@@ -435,7 +435,7 @@ EOF
                "offset" : 1048576,
                "old_size" : 0,
                "raw_size" : 33554432,
-               "size" : "-> 32.0M",
+               "size" : "-> 32M",
                "old_padding" : 0,
                "raw_padding" : 0,
                "padding" : "-> 0B",
@@ -496,7 +496,7 @@ EOF
                "offset" : 1048576,
                "old_size" : 0,
                "raw_size" : 33554432,
-               "size" : "-> 32.0M",
+               "size" : "-> 32M",
                "old_padding" : 0,
                "raw_padding" : 0,
                "padding" : "-> 0B",
@@ -512,7 +512,7 @@ EOF
                "offset" : 34603008,
                "old_size" : 0,
                "raw_size" : 33554432,
-               "size" : "-> 32.0M",
+               "size" : "-> 32M",
                "old_padding" : 0,
                "raw_padding" : 0,
                "padding" : "-> 0B",