CE better human_size ()

This commit is contained in:
2026-03-09 18:02:19 +01:00
parent 29e377aea3
commit 1ef028f919

View File

@@ -26,23 +26,16 @@ bool interp_is_running (void) { return run; }
void interp_shutdown (void) { run = false; } void interp_shutdown (void) { run = false; }
static void human_size (double size, double* out, char** str) { static void human_size (double size, double* out, char** str) {
if (size >= 1024.0f && size < 1024.0f * 1024.0f) { static char* units[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"};
*out = (double)size / 1024.0f;
*str = "KiB"; size_t i = 0;
} else if (size >= 1024.0f * 1024.0f && size < 1024.0f * 1024.0f * 1024.0f) { while (size >= 1024.0 && i < (sizeof (units) / sizeof (units[0])) - 1) {
*out = (double)size / (1024.0f * 1024.0f); size /= 1024.0;
*str = "MiB"; i++;
} 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 = "???";
} }
*out = size;
*str = units[i];
} }
static void echo (struct context* context, char** strings, size_t strings_count) { static void echo (struct context* context, char** strings, size_t strings_count) {