]> git.ipfire.org Git - thirdparty/git.git/commitdiff
strbuf: split out logic to humanise byte values
authorJustin Tobler <jltobler@gmail.com>
Wed, 17 Dec 2025 17:53:59 +0000 (11:53 -0600)
committerJunio C Hamano <gitster@pobox.com>
Thu, 18 Dec 2025 00:02:31 +0000 (09:02 +0900)
In a subsequent commit, byte size values displayed in table output for
the git-repo(1) "structure" subcommand will be shown in a more
human-readable format with the appropriate unit prefixes. For this
usecase, the downscaled values and unit strings must be handled
separately to ensure proper column alignment.

Split out logic from strbuf_humanise() to downscale byte values and
determine the corresponding unit prefix into a separate humanise_bytes()
function that provides seperate value and unit strings.

Note that the "byte" string in "t/helper/test-simple-ipc.c" is unmarked
for translation here so that it doesn't conflict with the newly defined
plural "byte/bytes" translation and instead uses it.

Signed-off-by: Justin Tobler <jltobler@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
strbuf.c
strbuf.h
t/helper/test-simple-ipc.c

index 6c3851a7f84d72ebf5719723dc7f0ac9cf60edf3..349ee9727a19209b0d1e1cfac490d4b07d11990a 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -836,47 +836,53 @@ void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
        strbuf_add_urlencode(sb, s, strlen(s), allow_unencoded_fn);
 }
 
-static void strbuf_humanise(struct strbuf *buf, off_t bytes,
-                                int humanise_rate)
+void humanise_bytes(off_t bytes, char **value, const char **unit,
+                   unsigned flags)
 {
+       int humanise_rate = flags & HUMANISE_RATE;
+
        if (bytes > 1 << 30) {
-               strbuf_addf(buf,
-                               humanise_rate == 0 ?
-                                       /* TRANSLATORS: IEC 80000-13:2008 gibibyte */
-                                       _("%u.%2.2u GiB") :
-                                       /* TRANSLATORS: IEC 80000-13:2008 gibibyte/second */
-                                       _("%u.%2.2u GiB/s"),
-                           (unsigned)(bytes >> 30),
-                           (unsigned)(bytes & ((1 << 30) - 1)) / 10737419);
+               *value = xstrfmt(_("%u.%2.2u"), (unsigned)(bytes >> 30),
+                                (unsigned)(bytes & ((1 << 30) - 1)) / 10737419);
+               /* TRANSLATORS: IEC 80000-13:2008 gibibyte/second and gibibyte */
+               *unit = humanise_rate ? _("GiB/s") : _("GiB");
        } else if (bytes > 1 << 20) {
-               unsigned x = bytes + 5243;  /* for rounding */
-               strbuf_addf(buf,
-                               humanise_rate == 0 ?
-                                       /* TRANSLATORS: IEC 80000-13:2008 mebibyte */
-                                       _("%u.%2.2u MiB") :
-                                       /* TRANSLATORS: IEC 80000-13:2008 mebibyte/second */
-                                       _("%u.%2.2u MiB/s"),
-                           x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20);
+               unsigned x = bytes + 5243; /* for rounding */
+               *value = xstrfmt(_("%u.%2.2u"), x >> 20,
+                                ((x & ((1 << 20) - 1)) * 100) >> 20);
+               /* TRANSLATORS: IEC 80000-13:2008 mebibyte/second and mebibyte */
+               *unit = humanise_rate ? _("MiB/s") : _("MiB");
        } else if (bytes > 1 << 10) {
-               unsigned x = bytes + 5;  /* for rounding */
-               strbuf_addf(buf,
-                               humanise_rate == 0 ?
-                                       /* TRANSLATORS: IEC 80000-13:2008 kibibyte */
-                                       _("%u.%2.2u KiB") :
-                                       /* TRANSLATORS: IEC 80000-13:2008 kibibyte/second */
-                                       _("%u.%2.2u KiB/s"),
-                           x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10);
+               unsigned x = bytes + 5; /* for rounding */
+               *value = xstrfmt(_("%u.%2.2u"), x >> 10,
+                                ((x & ((1 << 10) - 1)) * 100) >> 10);
+               /* TRANSLATORS: IEC 80000-13:2008 kibibyte/second and kibibyte */
+               *unit = humanise_rate ? _("KiB/s") : _("KiB");
        } else {
-               strbuf_addf(buf,
-                               humanise_rate == 0 ?
-                                       /* TRANSLATORS: IEC 80000-13:2008 byte */
-                                       Q_("%u byte", "%u bytes", bytes) :
-                                       /* TRANSLATORS: IEC 80000-13:2008 byte/second */
-                                       Q_("%u byte/s", "%u bytes/s", bytes),
-                               (unsigned)bytes);
+               *value = xstrfmt("%u", (unsigned)bytes);
+               *unit = humanise_rate ?
+                              /* TRANSLATORS: IEC 80000-13:2008 byte/second */
+                              Q_("byte/s", "bytes/s", bytes) :
+                              /* TRANSLATORS: IEC 80000-13:2008 byte */
+                              Q_("byte", "bytes", bytes);
        }
 }
 
+static void strbuf_humanise(struct strbuf *buf, off_t bytes, unsigned flags)
+{
+       char *value;
+       const char *unit;
+
+       humanise_bytes(bytes, &value, &unit, flags);
+
+       /*
+        * TRANSLATORS: The first argument is the number string. The second
+        * argument is the unit string (i.e. "12.34 MiB/s").
+        */
+       strbuf_addf(buf, _("%s %s"), value, unit);
+       free(value);
+}
+
 void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)
 {
        strbuf_humanise(buf, bytes, 0);
@@ -884,7 +890,7 @@ void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)
 
 void strbuf_humanise_rate(struct strbuf *buf, off_t bytes)
 {
-       strbuf_humanise(buf, bytes, 1);
+       strbuf_humanise(buf, bytes, HUMANISE_RATE);
 }
 
 int printf_ln(const char *fmt, ...)
index a580ac6084b7f105614815c790ba23dd3a8ff49a..698b3cc4a5136727b6d17c6b28b9094f4eb7a637 100644 (file)
--- a/strbuf.h
+++ b/strbuf.h
@@ -367,6 +367,20 @@ void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src);
  */
 void strbuf_add_percentencode(struct strbuf *dst, const char *src, int flags);
 
+enum humanise_flags {
+       /*
+        * Use rate based units for humanised values.
+        */
+       HUMANISE_RATE = (1 << 0),
+};
+
+/**
+ * Converts the given byte size into a downscaled human-readable value and
+ * corresponding unit as two separate strings.
+ */
+void humanise_bytes(off_t bytes, char **value, const char **unit,
+                   unsigned flags);
+
 /**
  * Append the given byte size as a human-readable string (i.e. 12.23 KiB,
  * 3.50 MiB).
index 03cc5eea2c2944677548151446786f30db782332..442ad6b16f18d89012fdcd0e6f320747a91fe3cc 100644 (file)
@@ -603,7 +603,12 @@ int cmd__simple_ipc(int argc, const char **argv)
                OPT_INTEGER(0, "bytecount", &cl_args.bytecount, N_("number of bytes")),
                OPT_INTEGER(0, "batchsize", &cl_args.batchsize, N_("number of requests per thread")),
 
-               OPT_STRING(0, "byte", &bytevalue, N_("byte"), N_("ballast character")),
+               /*
+                * The "byte" string here is not marked for translation and
+                * instead relies on translation in strbuf.c:humanise_bytes() to
+                * avoid conflict with the plural form.
+                */
+               OPT_STRING(0, "byte", &bytevalue, "byte", N_("ballast character")),
                OPT_STRING(0, "token", &cl_args.token, N_("token"), N_("command token to send to the server")),
 
                OPT_END()