[PATCH v2 2/7] strbuf: split out logic to humanise byte values
- From
Justin Tobler <jltobler@gmail.com>
- Date
- Dec 12, 2025, 22:36 UTC
- Message-ID
- <20251212223644.3090879-3-jltobler@gmail.com>
- In-Reply-To
- <20251212223644.3090879-1-jltobler@gmail.com>
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 prefixes must be handled separately to ensure proper column alignment. Refactor strbuf_humanise() to instead append the downscaled byte value to the buffer only and return the appropriate unit prefix string.
Signed-off-by: Justin Tobler <jltobler@gmail.com> --- strbuf.c | 62 +++++++++++++++++++++++++------------------------------- strbuf.h | 9 ++++++++ 2 files changed, 37 insertions(+), 34 deletions(-)
diff --git a/strbuf.c b/strbuf.c index 6c3851a7f8..1fb47bf21b 100644 --- a/strbuf.c +++ b/strbuf.c @@ -836,55 +836,49 @@ 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) +char *strbuf_humanise_bytes_value(struct strbuf *buf, off_t bytes, unsigned flags) { + int humanise_rate = flags & STRBUF_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), + strbuf_addf(buf, "%u.%2.2u", (unsigned)(bytes >> 30), (unsigned)(bytes & ((1 << 30) - 1)) / 10737419); + /* TRANSLATORS: IEC 80000-13:2008 gibibyte/second and gibibyte */ + return humanise_rate ? xstrfmt(_("GiB/s")) : xstrfmt(_("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 */ + strbuf_addf(buf, "%u.%2.2u", x >> 20, + ((x & ((1 << 20) - 1)) * 100) >> 20); + /* TRANSLATORS: IEC 80000-13:2008 mebibyte/second and mebibyte */ + return humanise_rate ? xstrfmt(_("MiB/s")) : xstrfmt(_("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 */ + strbuf_addf(buf, "%u.%2.2u", x >> 10, + ((x & ((1 << 10) - 1)) * 100) >> 10); + /* TRANSLATORS: IEC 80000-13:2008 kibibyte/second and kibibyte */ + return humanise_rate ? xstrfmt(_("KiB/s")) : xstrfmt(_("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); + strbuf_addf(buf, "%u", (unsigned)bytes); + return humanise_rate ? + /* TRANSLATORS: IEC 80000-13:2008 byte/second */ + xstrfmt(Q_("byte/s", "bytes/s", bytes)) : + /* TRANSLATORS: IEC 80000-13:2008 byte */ + xstrfmt(Q_("byte", "bytes", bytes)); } } void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes) { - strbuf_humanise(buf, bytes, 0); + char *unit = strbuf_humanise_bytes_value(buf, bytes, 0); + strbuf_addf(buf, " %s", unit); + free(unit); } void strbuf_humanise_rate(struct strbuf *buf, off_t bytes) { - strbuf_humanise(buf, bytes, 1); + char *unit = strbuf_humanise_bytes_value(buf, bytes, STRBUF_HUMANISE_RATE); + strbuf_addf(buf, " %s", unit); + free(unit); } int printf_ln(const char *fmt, ...) diff --git a/strbuf.h b/strbuf.h index a580ac6084..a5e3ab0cb4 100644 --- a/strbuf.h +++ b/strbuf.h @@ -367,6 +367,15 @@ void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src); */ void strbuf_add_percentencode(struct strbuf *dst, const char *src, int flags); +#define STRBUF_HUMANISE_RATE 1 << 0 + +/** + * Append the given byte size as a human-readable string that is downscaled by + * some factor. A string with the corresponding unit prefix is returned + * separately. + */ +char *strbuf_humanise_bytes_value(struct strbuf *buf, off_t bytes, unsigned flags); + /** * Append the given byte size as a human-readable string (i.e. 12.23 KiB, * 3.50 MiB).
-- 2.52.0.209.ge85ae279b0