diff options
author | Aaron Marcher <me@drkhsh.at> | 2018-05-21 14:44:21 +0200 |
---|---|---|
committer | Aaron Marcher <me@drkhsh.at> | 2018-05-21 14:44:21 +0200 |
commit | 10dbc9543edd5b5a7929dd8fa87ed33dd545375c (patch) | |
tree | 6f95a31f3a4c3d95b317a3280870dffb5ba5d4e1 /util.c | |
parent | fc0dde5a60a066b8506834f508aebf0ea5055990 (diff) |
Merge fmt_human_2 and fmt_human_10 to one function
Now only one function, fmt_human, takes an additional argument "base".
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 32 |
1 files changed, 12 insertions, 20 deletions
@@ -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]); |