From 607fac662da628afdcfafa2393a72bc908db4f20 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Boric Date: Wed, 17 Mar 2021 18:34:13 +0100 Subject: [PATCH] AK: Implement terabytes, petabytes, exabytes --- AK/NumberFormat.h | 12 +++++++++--- AK/Tests/TestNumberFormat.cpp | 11 +++++------ AK/Types.h | 9 ++++++--- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/AK/NumberFormat.h b/AK/NumberFormat.h index 22ff2559f1a..480c731975e 100644 --- a/AK/NumberFormat.h +++ b/AK/NumberFormat.h @@ -31,13 +31,13 @@ namespace AK { // FIXME: Remove this hackery once printf() supports floats. -static String number_string_with_one_decimal(u64 number, u32 unit, const char* suffix) +static String number_string_with_one_decimal(u64 number, u64 unit, const char* suffix) { int decimal = (number % unit) * 10 / unit; return String::formatted("{}.{} {}", number / unit, decimal, suffix); } -static inline String human_readable_size(size_t size) +static inline String human_readable_size(u64 size) { if (size < 1 * KiB) return String::formatted("{} B", size); @@ -45,7 +45,13 @@ static inline String human_readable_size(size_t size) return number_string_with_one_decimal(size, KiB, "KiB"); if (size < 1 * GiB) return number_string_with_one_decimal(size, MiB, "MiB"); - return number_string_with_one_decimal(size, GiB, "GiB"); + if (size < 1 * TiB) + return number_string_with_one_decimal(size, GiB, "GiB"); + if (size < 1 * PiB) + return number_string_with_one_decimal(size, TiB, "TiB"); + if (size < 1 * EiB) + return number_string_with_one_decimal(size, PiB, "PiB"); + return number_string_with_one_decimal(size, EiB, "EiB"); } } diff --git a/AK/Tests/TestNumberFormat.cpp b/AK/Tests/TestNumberFormat.cpp index 6fa554e2228..90a2cb63b5a 100644 --- a/AK/Tests/TestNumberFormat.cpp +++ b/AK/Tests/TestNumberFormat.cpp @@ -136,12 +136,11 @@ TEST_CASE(extremes_8byte) EXPECT_EQ(human_readable_size(0x100000000ULL), "4.0 GiB"); EXPECT_EQ(human_readable_size(0x100000001ULL), "4.0 GiB"); EXPECT_EQ(human_readable_size(0x800000000ULL), "32.0 GiB"); - EXPECT_EQ(human_readable_size(0x10000000000ULL), "1024.0 GiB"); - - // Oh yeah! These are *correct*! - EXPECT_EQ(human_readable_size(0x7fffffffffffffffULL), "8589934591.9 GiB"); - EXPECT_EQ(human_readable_size(0x8000000000000000ULL), "8589934592.0 GiB"); - EXPECT_EQ(human_readable_size(0xffffffffffffffffULL), "17179869183.9 GiB"); + EXPECT_EQ(human_readable_size(0x10000000000ULL), "1.0 TiB"); + EXPECT_EQ(human_readable_size(0x4000000000000ULL), "1.0 PiB"); + EXPECT_EQ(human_readable_size(0x7fffffffffffffffULL), "7.9 EiB"); + EXPECT_EQ(human_readable_size(0x8000000000000000ULL), "8.0 EiB"); + EXPECT_EQ(human_readable_size(0xffffffffffffffffULL), "15.9 EiB"); } else { warnln("(Skipping 8-byte-size_t test on 32-bit platform)"); } diff --git a/AK/Types.h b/AK/Types.h index 7e5316924c4..c4b09c9f754 100644 --- a/AK/Types.h +++ b/AK/Types.h @@ -74,9 +74,12 @@ using __ptrdiff_t = __PTRDIFF_TYPE__; using FlatPtr = Conditional::Type; -constexpr unsigned KiB = 1024; -constexpr unsigned MiB = KiB * KiB; -constexpr unsigned GiB = KiB * KiB * KiB; +constexpr u64 KiB = 1024; +constexpr u64 MiB = KiB * KiB; +constexpr u64 GiB = KiB * KiB * KiB; +constexpr u64 TiB = KiB * KiB * KiB * KiB; +constexpr u64 PiB = KiB * KiB * KiB * KiB * KiB; +constexpr u64 EiB = KiB * KiB * KiB * KiB * KiB * KiB; namespace std { using nullptr_t = decltype(nullptr);