From 388856dc7e0bfd9dfe6cf8a267cee63b2f88dd36 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 24 Jan 2024 12:16:32 +0000 Subject: [PATCH] AK+Userland: Return String from human_readable_size() functions --- AK/NumberFormat.cpp | 26 +++++++++---------- AK/NumberFormat.h | 6 ++--- .../FileManager/PropertiesWindow.cpp | 4 +-- .../LibWeb/Loader/GeneratedPagesLoader.cpp | 2 +- Userland/Services/WebServer/Client.cpp | 2 +- Userland/Utilities/dd.cpp | 2 +- Userland/Utilities/ls.cpp | 4 +-- 7 files changed, 23 insertions(+), 23 deletions(-) diff --git a/AK/NumberFormat.cpp b/AK/NumberFormat.cpp index b1dcb459e1..2027b22bf3 100644 --- a/AK/NumberFormat.cpp +++ b/AK/NumberFormat.cpp @@ -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::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) diff --git a/AK/NumberFormat.h b/AK/NumberFormat.h index 49caf11a45..ba99d0caf7 100644 --- a/AK/NumberFormat.h +++ b/AK/NumberFormat.h @@ -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); diff --git a/Userland/Applications/FileManager/PropertiesWindow.cpp b/Userland/Applications/FileManager/PropertiesWindow.cpp index 8dea2d90d2..8811e9938c 100644 --- a/Userland/Applications/FileManager/PropertiesWindow.cpp +++ b/Userland/Applications/FileManager/PropertiesWindow.cpp @@ -186,7 +186,7 @@ ErrorOr PropertiesWindow::create_general_tab(GUI::TabWidget& tab_widget, b m_size_label = general_tab.find_descendant_of_type_named("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("owner"); owner->set_text(String::formatted("{} ({})", owner_name, st.st_uid).release_value_but_fixme_should_propagate_errors()); @@ -266,7 +266,7 @@ ErrorOr PropertiesWindow::create_archive_tab(GUI::TabWidget& tab_widget, N tab.find_descendant_of_type_named("archive_file_count")->set_text(TRY(String::number(statistics.file_count()))); tab.find_descendant_of_type_named("archive_format")->set_text("ZIP"_string); tab.find_descendant_of_type_named("archive_directory_count")->set_text(TRY(String::number(statistics.directory_count()))); - tab.find_descendant_of_type_named("archive_uncompressed_size")->set_text(TRY(String::from_byte_string(AK::human_readable_size(statistics.total_uncompressed_bytes())))); + tab.find_descendant_of_type_named("archive_uncompressed_size")->set_text(human_readable_size(statistics.total_uncompressed_bytes())); return {}; } diff --git a/Userland/Libraries/LibWeb/Loader/GeneratedPagesLoader.cpp b/Userland/Libraries/LibWeb/Loader/GeneratedPagesLoader.cpp index 20975b9ea5..22a383bc7b 100644 --- a/Userland/Libraries/LibWeb/Loader/GeneratedPagesLoader.cpp +++ b/Userland/Libraries/LibWeb/Loader/GeneratedPagesLoader.cpp @@ -60,7 +60,7 @@ ErrorOr load_file_directory_page(AK::URL const& url) contents.append(""sv); contents.appendff("", is_directory ? "folder" : "file"); contents.appendff("{} "sv, path, name); - contents.appendff("{:10} ", is_directory ? "-" : human_readable_size(st.st_size)); + contents.appendff("{:10} ", is_directory ? "-"_string : human_readable_size(st.st_size)); contents.appendff("{}"sv, Core::DateTime::from_timestamp(st.st_mtime).to_byte_string()); contents.append("\n"sv); } diff --git a/Userland/Services/WebServer/Client.cpp b/Userland/Services/WebServer/Client.cpp index 961d167213..27d6250d64 100644 --- a/Userland/Services/WebServer/Client.cpp +++ b/Userland/Services/WebServer/Client.cpp @@ -324,7 +324,7 @@ ErrorOr Client::handle_directory_listing(String const& requested_path, Str TRY(builder.try_append(escape_html_entities(name))); TRY(builder.try_append(" "sv)); - TRY(builder.try_appendff("{:10} ", is_directory ? "-" : human_readable_size(st.st_size))); + TRY(builder.try_appendff("{:10} ", is_directory ? "-"_string : human_readable_size(st.st_size))); TRY(builder.try_append(""sv)); TRY(builder.try_append(TRY(Core::DateTime::from_timestamp(st.st_mtime).to_string()))); TRY(builder.try_append(""sv)); diff --git a/Userland/Utilities/dd.cpp b/Userland/Utilities/dd.cpp index c7c30e7bea..cb5d289ff5 100644 --- a/Userland/Utilities/dd.cpp +++ b/Userland/Utilities/dd.cpp @@ -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); diff --git a/Userland/Utilities/ls.cpp b/Userland/Utilities/ls.cpp index ac3d136827..e9e1718a3d 100644 --- a/Userland/Utilities/ls.cpp +++ b/Userland/Utilities/ls.cpp @@ -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); }