AK: Implement terabytes, petabytes, exabytes

This commit is contained in:
Jean-Baptiste Boric 2021-03-17 18:34:13 +01:00 committed by Andreas Kling
parent 7ffc6c371a
commit 607fac662d
Notes: sideshowbarker 2024-07-18 21:15:36 +09:00
3 changed files with 20 additions and 12 deletions

View file

@ -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");
}
}

View file

@ -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)");
}

View file

@ -74,9 +74,12 @@ using __ptrdiff_t = __PTRDIFF_TYPE__;
using FlatPtr = Conditional<sizeof(void*) == 8, u64, u32>::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);