mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-01 20:10:28 +00:00
AK: Implement terabytes, petabytes, exabytes
This commit is contained in:
parent
7ffc6c371a
commit
607fac662d
Notes:
sideshowbarker
2024-07-18 21:15:36 +09:00
Author: https://github.com/boricj Commit: https://github.com/SerenityOS/serenity/commit/607fac662da Pull-request: https://github.com/SerenityOS/serenity/pull/5766 Reviewed-by: https://github.com/supercomputer7
3 changed files with 20 additions and 12 deletions
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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)");
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue