1
0
mirror of https://github.com/SerenityOS/serenity synced 2024-07-01 11:39:22 +00:00

AK+Userland: Return String from human_readable_size() functions

This commit is contained in:
Sam Atkins 2024-01-24 12:16:32 +00:00 committed by Andreas Kling
parent 7e8cfb60eb
commit 388856dc7e
7 changed files with 23 additions and 23 deletions

View File

@ -11,7 +11,7 @@
namespace AK {
// FIXME: Remove this hackery once printf() supports floats.
static ByteString number_string_with_one_decimal(u64 number, u64 unit, StringView suffix, UseThousandsSeparator use_thousands_separator)
static String 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);
@ -19,25 +19,25 @@ static ByteString number_string_with_one_decimal(u64 number, u64 unit, StringVie
auto integer_part = number / unit;
auto decimal_part = (number % unit) * 10 / unit;
if (use_thousands_separator == UseThousandsSeparator::Yes)
return ByteString::formatted("{:'d}.{} {}", integer_part, decimal_part, suffix);
return MUST(String::formatted("{:'d}.{} {}", integer_part, decimal_part, suffix));
return ByteString::formatted("{}.{} {}", integer_part, decimal_part, suffix);
return MUST(String::formatted("{}.{} {}", integer_part, decimal_part, suffix));
}
ByteString human_readable_quantity(u64 quantity, HumanReadableBasedOn based_on, StringView unit, UseThousandsSeparator use_thousands_separator)
String 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" };
auto full_unit_suffix = [&](int index) {
auto binary_infix = (based_on == HumanReadableBasedOn::Base2 && index != 0) ? "i"sv : ""sv;
return ByteString::formatted("{}{}{}",
unit_prefixes[index], binary_infix, unit);
return MUST(String::formatted("{}{}{}",
unit_prefixes[index], binary_infix, unit));
};
auto size_of_current_unit = size_of_unit;
if (quantity < size_of_unit)
return ByteString::formatted("{} {}", quantity, full_unit_suffix(0));
return MUST(String::formatted("{} {}", quantity, full_unit_suffix(0)));
for (size_t i = 1; i < unit_prefixes.size() - 1; i++) {
auto suffix = full_unit_suffix(i);
@ -52,25 +52,25 @@ ByteString human_readable_quantity(u64 quantity, HumanReadableBasedOn based_on,
size_of_current_unit, full_unit_suffix(unit_prefixes.size() - 1), use_thousands_separator);
}
ByteString human_readable_size(u64 size, HumanReadableBasedOn based_on, UseThousandsSeparator use_thousands_separator)
String human_readable_size(u64 size, HumanReadableBasedOn based_on, UseThousandsSeparator use_thousands_separator)
{
return human_readable_quantity(size, based_on, "B"sv, use_thousands_separator);
}
ByteString human_readable_size_long(u64 size, UseThousandsSeparator use_thousands_separator)
String human_readable_size_long(u64 size, UseThousandsSeparator use_thousands_separator)
{
if (size < 1 * KiB) {
if (use_thousands_separator == UseThousandsSeparator::Yes)
return ByteString::formatted("{:'d} bytes", size);
return MUST(String::formatted("{:'d} bytes", size));
return ByteString::formatted("{} bytes", size);
return MUST(String::formatted("{} bytes", size));
}
auto human_readable_size_string = human_readable_size(size, HumanReadableBasedOn::Base2, use_thousands_separator);
if (use_thousands_separator == UseThousandsSeparator::Yes)
return ByteString::formatted("{} ({:'d} bytes)", human_readable_size_string, size);
return MUST(String::formatted("{} ({:'d} bytes)", human_readable_size_string, size));
return ByteString::formatted("{} ({} bytes)", human_readable_size_string, size);
return MUST(String::formatted("{} ({} bytes)", human_readable_size_string, size));
}
String human_readable_time(i64 time_in_seconds)

View File

@ -20,10 +20,10 @@ enum class UseThousandsSeparator {
No
};
ByteString human_readable_size(u64 size, HumanReadableBasedOn based_on = HumanReadableBasedOn::Base2, UseThousandsSeparator use_thousands_separator = UseThousandsSeparator::No);
ByteString human_readable_quantity(u64 quantity, HumanReadableBasedOn based_on = HumanReadableBasedOn::Base2, StringView unit = "B"sv, UseThousandsSeparator use_thousands_separator = UseThousandsSeparator::No);
String human_readable_size(u64 size, HumanReadableBasedOn based_on = HumanReadableBasedOn::Base2, UseThousandsSeparator use_thousands_separator = UseThousandsSeparator::No);
String human_readable_quantity(u64 quantity, HumanReadableBasedOn based_on = HumanReadableBasedOn::Base2, StringView unit = "B"sv, UseThousandsSeparator use_thousands_separator = UseThousandsSeparator::No);
ByteString human_readable_size_long(u64 size, UseThousandsSeparator use_thousands_separator = UseThousandsSeparator::No);
String human_readable_size_long(u64 size, UseThousandsSeparator use_thousands_separator = UseThousandsSeparator::No);
String human_readable_time(i64 time_in_seconds);
String human_readable_digital_time(i64 time_in_seconds);

View File

@ -186,7 +186,7 @@ ErrorOr<void> PropertiesWindow::create_general_tab(GUI::TabWidget& tab_widget, b
m_size_label = general_tab.find_descendant_of_type_named<GUI::Label>("size");
m_size_label->set_text(S_ISDIR(st.st_mode)
? "Calculating..."_string
: TRY(String::from_byte_string(human_readable_size_long(st.st_size, UseThousandsSeparator::Yes))));
: human_readable_size_long(st.st_size, UseThousandsSeparator::Yes));
auto* owner = general_tab.find_descendant_of_type_named<GUI::Label>("owner");
owner->set_text(String::formatted("{} ({})", owner_name, st.st_uid).release_value_but_fixme_should_propagate_errors());
@ -266,7 +266,7 @@ ErrorOr<void> PropertiesWindow::create_archive_tab(GUI::TabWidget& tab_widget, N
tab.find_descendant_of_type_named<GUI::Label>("archive_file_count")->set_text(TRY(String::number(statistics.file_count())));
tab.find_descendant_of_type_named<GUI::Label>("archive_format")->set_text("ZIP"_string);
tab.find_descendant_of_type_named<GUI::Label>("archive_directory_count")->set_text(TRY(String::number(statistics.directory_count())));
tab.find_descendant_of_type_named<GUI::Label>("archive_uncompressed_size")->set_text(TRY(String::from_byte_string(AK::human_readable_size(statistics.total_uncompressed_bytes()))));
tab.find_descendant_of_type_named<GUI::Label>("archive_uncompressed_size")->set_text(human_readable_size(statistics.total_uncompressed_bytes()));
return {};
}

View File

@ -60,7 +60,7 @@ ErrorOr<String> load_file_directory_page(AK::URL const& url)
contents.append("<tr>"sv);
contents.appendff("<td><span class=\"{}\"></span></td>", is_directory ? "folder" : "file");
contents.appendff("<td><a href=\"file://{}\">{}</a></td><td>&nbsp;</td>"sv, path, name);
contents.appendff("<td>{:10}</td><td>&nbsp;</td>", is_directory ? "-" : human_readable_size(st.st_size));
contents.appendff("<td>{:10}</td><td>&nbsp;</td>", is_directory ? "-"_string : human_readable_size(st.st_size));
contents.appendff("<td>{}</td>"sv, Core::DateTime::from_timestamp(st.st_mtime).to_byte_string());
contents.append("</tr>\n"sv);
}

View File

@ -324,7 +324,7 @@ ErrorOr<void> Client::handle_directory_listing(String const& requested_path, Str
TRY(builder.try_append(escape_html_entities(name)));
TRY(builder.try_append("</a></td><td>&nbsp;</td>"sv));
TRY(builder.try_appendff("<td>{:10}</td><td>&nbsp;</td>", is_directory ? "-" : human_readable_size(st.st_size)));
TRY(builder.try_appendff("<td>{:10}</td><td>&nbsp;</td>", is_directory ? "-"_string : human_readable_size(st.st_size)));
TRY(builder.try_append("<td>"sv));
TRY(builder.try_append(TRY(Core::DateTime::from_timestamp(st.st_mtime).to_string())));
TRY(builder.try_append("</td>"sv));

View File

@ -56,7 +56,7 @@ static void closing_statistics()
warnln("{}+{} blocks out", statistics.total_blocks_out, statistics.partial_blocks_out);
if (statistics.status != Noxfer) {
auto elapsed_time = statistics.timer.elapsed_time();
ByteString copy_speed = "INF B/s";
String copy_speed = "INF B/s"_string;
if (!elapsed_time.is_zero()) {
auto speed = statistics.total_bytes_copied * 1000 / elapsed_time.to_milliseconds();
copy_speed = human_readable_quantity(speed, AK::HumanReadableBasedOn::Base2, "B/s"sv);

View File

@ -405,9 +405,9 @@ static bool print_filesystem_object(ByteString const& path, ByteString const& na
printf(" %4u,%4u ", major(st.st_rdev), minor(st.st_rdev));
} else {
if (flag_human_readable) {
printf(" %10s ", human_readable_size(st.st_size).characters());
printf(" %10s ", human_readable_size(st.st_size).to_byte_string().characters());
} else if (flag_human_readable_si) {
printf(" %10s ", human_readable_size(st.st_size, AK::HumanReadableBasedOn::Base10).characters());
printf(" %10s ", human_readable_size(st.st_size, AK::HumanReadableBasedOn::Base10).to_byte_string().characters());
} else {
printf(" %10" PRIu64 " ", (uint64_t)st.st_size);
}