From 1ef028f919cdcfdaf6d80daf6993336b441e9b5d Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Mon, 9 Mar 2026 18:02:19 +0100 Subject: [PATCH] CE better human_size () --- ce/interp.c | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/ce/interp.c b/ce/interp.c index d9bbd18..798f54f 100644 --- a/ce/interp.c +++ b/ce/interp.c @@ -26,23 +26,16 @@ bool interp_is_running (void) { return run; } void interp_shutdown (void) { run = false; } static void human_size (double size, double* out, char** str) { - if (size >= 1024.0f && size < 1024.0f * 1024.0f) { - *out = (double)size / 1024.0f; - *str = "KiB"; - } else if (size >= 1024.0f * 1024.0f && size < 1024.0f * 1024.0f * 1024.0f) { - *out = (double)size / (1024.0f * 1024.0f); - *str = "MiB"; - } else if (size >= 1024.0f * 1024.0f * 1024.0f && size < 1024.0f * 1024.0f * 1024.0f * 1024.0f) { - *out = (double)size / (1024.0f * 1024.0f * 1024.0f); - *str = "GiB"; - } else if (size >= 1024.0f * 1024.0f * 1024.0f * 1024.0f && - size < 1024.0f * 1024.0f * 1024.0f * 1024.0f * 1024.0f) { - *out = (double)size / (1024.0f * 1024.0f * 1024.0f * 1024.0f); - *str = "TiB"; - } else { - *out = 0.0f; - *str = "???"; + static char* units[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"}; + + size_t i = 0; + while (size >= 1024.0 && i < (sizeof (units) / sizeof (units[0])) - 1) { + size /= 1024.0; + i++; } + + *out = size; + *str = units[i]; } static void echo (struct context* context, char** strings, size_t strings_count) {