mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-08-17 09:20:28 +03:00
AK: Allow human_readable_size_long to use a thousands separator
This commit is contained in:
parent
72ea046b68
commit
54539c42c2
Notes:
sideshowbarker
2024-07-16 21:37:56 +09:00
Author: https://github.com/tcl3 Commit: https://github.com/SerenityOS/serenity/commit/54539c42c2 Pull-request: https://github.com/SerenityOS/serenity/pull/17739 Reviewed-by: https://github.com/gmta
@ -13,17 +13,20 @@
|
||||
namespace AK {
|
||||
|
||||
// FIXME: Remove this hackery once printf() supports floats.
|
||||
static DeprecatedString number_string_with_one_decimal(u64 number, u64 unit, StringView suffix)
|
||||
static DeprecatedString number_string_with_one_decimal(u64 number, u64 unit, StringView suffix, UseThousandsSeparator use_thousands_separator)
|
||||
{
|
||||
constexpr auto max_unit_size = NumericLimits<u64>::max() / 10;
|
||||
VERIFY(unit < max_unit_size);
|
||||
|
||||
auto integer_part = number / unit;
|
||||
auto decimal_part = (number % unit) * 10 / unit;
|
||||
if (use_thousands_separator == UseThousandsSeparator::Yes)
|
||||
return DeprecatedString::formatted("{:'d}.{} {}", integer_part, decimal_part, suffix);
|
||||
|
||||
return DeprecatedString::formatted("{}.{} {}", integer_part, decimal_part, suffix);
|
||||
}
|
||||
|
||||
DeprecatedString human_readable_quantity(u64 quantity, HumanReadableBasedOn based_on, StringView unit)
|
||||
DeprecatedString human_readable_quantity(u64 quantity, HumanReadableBasedOn based_on, StringView unit, UseThousandsSeparator use_thousands_separator)
|
||||
{
|
||||
u64 size_of_unit = based_on == HumanReadableBasedOn::Base2 ? 1024 : 1000;
|
||||
constexpr auto unit_prefixes = AK::Array { "", "K", "M", "G", "T", "P", "E" };
|
||||
@ -41,27 +44,35 @@ DeprecatedString human_readable_quantity(u64 quantity, HumanReadableBasedOn base
|
||||
for (size_t i = 1; i < unit_prefixes.size() - 1; i++) {
|
||||
auto suffix = full_unit_suffix(i);
|
||||
if (quantity < size_of_unit * size_of_current_unit) {
|
||||
return number_string_with_one_decimal(quantity, size_of_current_unit, suffix);
|
||||
return number_string_with_one_decimal(quantity, size_of_current_unit, suffix, use_thousands_separator);
|
||||
}
|
||||
|
||||
size_of_current_unit *= size_of_unit;
|
||||
}
|
||||
|
||||
return number_string_with_one_decimal(quantity,
|
||||
size_of_current_unit, full_unit_suffix(unit_prefixes.size() - 1));
|
||||
size_of_current_unit, full_unit_suffix(unit_prefixes.size() - 1), use_thousands_separator);
|
||||
}
|
||||
|
||||
DeprecatedString human_readable_size(u64 size, HumanReadableBasedOn based_on)
|
||||
DeprecatedString human_readable_size(u64 size, HumanReadableBasedOn based_on, UseThousandsSeparator use_thousands_separator)
|
||||
{
|
||||
return human_readable_quantity(size, based_on, "B"sv);
|
||||
return human_readable_quantity(size, based_on, "B"sv, use_thousands_separator);
|
||||
}
|
||||
|
||||
DeprecatedString human_readable_size_long(u64 size)
|
||||
DeprecatedString human_readable_size_long(u64 size, UseThousandsSeparator use_thousands_separator)
|
||||
{
|
||||
if (size < 1 * KiB)
|
||||
if (size < 1 * KiB) {
|
||||
if (use_thousands_separator == UseThousandsSeparator::Yes)
|
||||
return DeprecatedString::formatted("{:'d} bytes", size);
|
||||
|
||||
return DeprecatedString::formatted("{} bytes", size);
|
||||
else
|
||||
return DeprecatedString::formatted("{} ({} bytes)", human_readable_size(size), size);
|
||||
}
|
||||
|
||||
auto human_readable_size_string = human_readable_size(size, HumanReadableBasedOn::Base2, use_thousands_separator);
|
||||
if (use_thousands_separator == UseThousandsSeparator::Yes)
|
||||
return DeprecatedString::formatted("{} ({:'d} bytes)", human_readable_size_string, size);
|
||||
|
||||
return DeprecatedString::formatted("{} ({} bytes)", human_readable_size_string, size);
|
||||
}
|
||||
|
||||
DeprecatedString human_readable_time(i64 time_in_seconds)
|
||||
|
@ -15,10 +15,15 @@ enum class HumanReadableBasedOn {
|
||||
Base10
|
||||
};
|
||||
|
||||
DeprecatedString human_readable_size(u64 size, HumanReadableBasedOn based_on = HumanReadableBasedOn::Base2);
|
||||
DeprecatedString human_readable_quantity(u64 quantity, HumanReadableBasedOn based_on = HumanReadableBasedOn::Base2, StringView unit = "B"sv);
|
||||
enum class UseThousandsSeparator {
|
||||
Yes,
|
||||
No
|
||||
};
|
||||
|
||||
DeprecatedString human_readable_size_long(u64 size);
|
||||
DeprecatedString human_readable_size(u64 size, HumanReadableBasedOn based_on = HumanReadableBasedOn::Base2, UseThousandsSeparator use_thousands_separator = UseThousandsSeparator::No);
|
||||
DeprecatedString human_readable_quantity(u64 quantity, HumanReadableBasedOn based_on = HumanReadableBasedOn::Base2, StringView unit = "B"sv, UseThousandsSeparator use_thousands_separator = UseThousandsSeparator::No);
|
||||
|
||||
DeprecatedString human_readable_size_long(u64 size, UseThousandsSeparator use_thousands_separator = UseThousandsSeparator::No);
|
||||
DeprecatedString human_readable_time(i64 time_in_seconds);
|
||||
DeprecatedString human_readable_digital_time(i64 time_in_seconds);
|
||||
|
||||
@ -30,4 +35,5 @@ using AK::human_readable_quantity;
|
||||
using AK::human_readable_size;
|
||||
using AK::human_readable_size_long;
|
||||
using AK::human_readable_time;
|
||||
using AK::UseThousandsSeparator;
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user