commit 1280098658c6bed84e3772cbb8bf4bf2108a0392
parent 3c2e38ff8234afc2fcbc0ffa496107e58ee9504e
Author: Quentin Rameau <quinq@fifth.space>
Date: Sat, 30 Jul 2016 11:30:53 +0200
Replace the if-then-else check with a for loop in scale()
Diffstat:
M | sbm.c | | | 16 | +++------------- |
1 file changed, 3 insertions(+), 13 deletions(-)
diff --git a/sbm.c b/sbm.c
@@ -148,23 +148,13 @@ scale(char **suffix, uint64_t bits)
static char *suffixes[][5] = { { "b", "kb", "Mb", "Gb", "Tb" },
{ "B", "kB", "MB", "GB", "TB" } };
double rounded = bits;
+ int unit = 0;
if (Bflag)
rounded /= 8;
- *suffix = suffixes[Bflag][0];
- if (rounded >= 1000 && rounded < 1000 * 1000) {
- *suffix = suffixes[Bflag][1];
+ for (; rounded >= 1000 && unit < 5; ++unit)
rounded /= 1000;
- } else if (rounded >= 1000 * 1000) {
- *suffix = suffixes[Bflag][2];
- rounded /= 1000 * 1000;
- } else if (rounded >= 1000 * 1000 * 1000) {
- *suffix = suffixes[Bflag][3];
- rounded /= 1000 * 1000 * 1000;
- } else if (rounded >= (long)1000 * 1000 * 1000 * 1000) {
- *suffix = suffixes[Bflag][4];
- rounded /= (long)1000 * 1000 * 1000 * 1000;
- }
+ *suffix = suffixes[Bflag][unit];
return rounded;
}