From 10dbc9543edd5b5a7929dd8fa87ed33dd545375c Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Mon, 21 May 2018 14:44:21 +0200 Subject: Merge fmt_human_2 and fmt_human_10 to one function Now only one function, fmt_human, takes an additional argument "base". --- util.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) (limited to 'util.c') diff --git a/util.c b/util.c index 6dd08d7..fc8b72a 100644 --- a/util.c +++ b/util.c @@ -87,32 +87,24 @@ bprintf(const char *fmt, ...) } const char * -fmt_human_2(size_t num) +fmt_human(size_t num, int base) { size_t i; double scaled; - const char *prefix[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", - "Zi", "Yi" }; - - scaled = num; - for (i = 0; i < LEN(prefix) && scaled >= 1024; i++) { - scaled /= 1024.0; + const char *siprefix[] = { "", "k", "M", "G", "T", "P", "E", "Z", "Y" }; + const char *iecprefix[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", + "Zi", "Yi" }; + char *prefix[9]; + + if (base == 1000) { + memcpy(prefix, siprefix, sizeof(prefix)); + } else if (base == 1024) { + memcpy(prefix, iecprefix, sizeof(prefix)); } - return bprintf("%.1f%s", scaled, prefix[i]); -} - -const char * -fmt_human_10(size_t num) -{ - size_t i; - double scaled; - const char *prefix[] = { "", "K", "M", "G", "T", "P", "E", - "Z", "Y" }; - scaled = num; - for (i = 0; i < LEN(prefix) && scaled >= 1000; i++) { - scaled /= 1000.0; + for (i = 0; i < LEN(prefix) && scaled >= 1024; i++) { + scaled /= base; } return bprintf("%.1f%s", scaled, prefix[i]); -- cgit v1.2.3