Skip to content

Commit ce849b1

Browse files
jltoblergitster
authored andcommitted
strbuf: split out logic to humanise byte values
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>
1 parent 9faaf25 commit ce849b1

3 files changed

Lines changed: 60 additions & 35 deletions

File tree

strbuf.c

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -836,55 +836,61 @@ void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
836836
strbuf_add_urlencode(sb, s, strlen(s), allow_unencoded_fn);
837837
}
838838

839-
static void strbuf_humanise(struct strbuf *buf, off_t bytes,
840-
int humanise_rate)
839+
void humanise_bytes(off_t bytes, char **value, const char **unit,
840+
unsigned flags)
841841
{
842+
int humanise_rate = flags & HUMANISE_RATE;
843+
842844
if (bytes > 1 << 30) {
843-
strbuf_addf(buf,
844-
humanise_rate == 0 ?
845-
/* TRANSLATORS: IEC 80000-13:2008 gibibyte */
846-
_("%u.%2.2u GiB") :
847-
/* TRANSLATORS: IEC 80000-13:2008 gibibyte/second */
848-
_("%u.%2.2u GiB/s"),
849-
(unsigned)(bytes >> 30),
850-
(unsigned)(bytes & ((1 << 30) - 1)) / 10737419);
845+
*value = xstrfmt(_("%u.%2.2u"), (unsigned)(bytes >> 30),
846+
(unsigned)(bytes & ((1 << 30) - 1)) / 10737419);
847+
/* TRANSLATORS: IEC 80000-13:2008 gibibyte/second and gibibyte */
848+
*unit = humanise_rate ? _("GiB/s") : _("GiB");
851849
} else if (bytes > 1 << 20) {
852-
unsigned x = bytes + 5243; /* for rounding */
853-
strbuf_addf(buf,
854-
humanise_rate == 0 ?
855-
/* TRANSLATORS: IEC 80000-13:2008 mebibyte */
856-
_("%u.%2.2u MiB") :
857-
/* TRANSLATORS: IEC 80000-13:2008 mebibyte/second */
858-
_("%u.%2.2u MiB/s"),
859-
x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20);
850+
unsigned x = bytes + 5243; /* for rounding */
851+
*value = xstrfmt(_("%u.%2.2u"), x >> 20,
852+
((x & ((1 << 20) - 1)) * 100) >> 20);
853+
/* TRANSLATORS: IEC 80000-13:2008 mebibyte/second and mebibyte */
854+
*unit = humanise_rate ? _("MiB/s") : _("MiB");
860855
} else if (bytes > 1 << 10) {
861-
unsigned x = bytes + 5; /* for rounding */
862-
strbuf_addf(buf,
863-
humanise_rate == 0 ?
864-
/* TRANSLATORS: IEC 80000-13:2008 kibibyte */
865-
_("%u.%2.2u KiB") :
866-
/* TRANSLATORS: IEC 80000-13:2008 kibibyte/second */
867-
_("%u.%2.2u KiB/s"),
868-
x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10);
856+
unsigned x = bytes + 5; /* for rounding */
857+
*value = xstrfmt(_("%u.%2.2u"), x >> 10,
858+
((x & ((1 << 10) - 1)) * 100) >> 10);
859+
/* TRANSLATORS: IEC 80000-13:2008 kibibyte/second and kibibyte */
860+
*unit = humanise_rate ? _("KiB/s") : _("KiB");
869861
} else {
870-
strbuf_addf(buf,
871-
humanise_rate == 0 ?
872-
/* TRANSLATORS: IEC 80000-13:2008 byte */
873-
Q_("%u byte", "%u bytes", bytes) :
874-
/* TRANSLATORS: IEC 80000-13:2008 byte/second */
875-
Q_("%u byte/s", "%u bytes/s", bytes),
876-
(unsigned)bytes);
862+
*value = xstrfmt("%u", (unsigned)bytes);
863+
*unit = humanise_rate ?
864+
/* TRANSLATORS: IEC 80000-13:2008 byte/second */
865+
Q_("byte/s", "bytes/s", bytes) :
866+
/* TRANSLATORS: IEC 80000-13:2008 byte */
867+
Q_("byte", "bytes", bytes);
877868
}
878869
}
879870

871+
static void strbuf_humanise(struct strbuf *buf, off_t bytes, unsigned flags)
872+
{
873+
char *value;
874+
const char *unit;
875+
876+
humanise_bytes(bytes, &value, &unit, flags);
877+
878+
/*
879+
* TRANSLATORS: The first argument is the number string. The second
880+
* argument is the unit string (i.e. "12.34 MiB/s").
881+
*/
882+
strbuf_addf(buf, _("%s %s"), value, unit);
883+
free(value);
884+
}
885+
880886
void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)
881887
{
882888
strbuf_humanise(buf, bytes, 0);
883889
}
884890

885891
void strbuf_humanise_rate(struct strbuf *buf, off_t bytes)
886892
{
887-
strbuf_humanise(buf, bytes, 1);
893+
strbuf_humanise(buf, bytes, HUMANISE_RATE);
888894
}
889895

890896
int printf_ln(const char *fmt, ...)

strbuf.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,20 @@ void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src);
367367
*/
368368
void strbuf_add_percentencode(struct strbuf *dst, const char *src, int flags);
369369

370+
enum humanise_flags {
371+
/*
372+
* Use rate based units for humanised values.
373+
*/
374+
HUMANISE_RATE = (1 << 0),
375+
};
376+
377+
/**
378+
* Converts the given byte size into a downscaled human-readable value and
379+
* corresponding unit as two separate strings.
380+
*/
381+
void humanise_bytes(off_t bytes, char **value, const char **unit,
382+
unsigned flags);
383+
370384
/**
371385
* Append the given byte size as a human-readable string (i.e. 12.23 KiB,
372386
* 3.50 MiB).

t/helper/test-simple-ipc.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,12 @@ int cmd__simple_ipc(int argc, const char **argv)
603603
OPT_INTEGER(0, "bytecount", &cl_args.bytecount, N_("number of bytes")),
604604
OPT_INTEGER(0, "batchsize", &cl_args.batchsize, N_("number of requests per thread")),
605605

606-
OPT_STRING(0, "byte", &bytevalue, N_("byte"), N_("ballast character")),
606+
/*
607+
* The "byte" string here is not marked for translation and
608+
* instead relies on translation in strbuf.c:humanise_bytes() to
609+
* avoid conflict with the plural form.
610+
*/
611+
OPT_STRING(0, "byte", &bytevalue, "byte", N_("ballast character")),
607612
OPT_STRING(0, "token", &cl_args.token, N_("token"), N_("command token to send to the server")),
608613

609614
OPT_END()

0 commit comments

Comments
 (0)