From aa9716673988597d270ee6223b42d5207fddc1ad Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Sat, 15 Aug 2020 14:05:46 -0400 Subject: [PATCH] Everywhere: Consolidate human_readable_size() implementations Let's use the one in AK/NumberFormat.h everywhere. It has slightly different behavior than some of the copies this removes, but it's probably nice to have uniform human readable size outputs across the system. --- AK/NumberFormat.h | 1 + Applications/SystemMonitor/main.cpp | 14 ++------------ Userland/df.cpp | 24 +++--------------------- Userland/ifconfig.cpp | 16 +++------------- Userland/ls.cpp | 20 +------------------- 5 files changed, 10 insertions(+), 65 deletions(-) diff --git a/AK/NumberFormat.h b/AK/NumberFormat.h index e3dd86a7fd6..4fbb8d009ab 100644 --- a/AK/NumberFormat.h +++ b/AK/NumberFormat.h @@ -31,6 +31,7 @@ namespace AK { +// FIXME: Remove this hackery once printf() supports floats. static String number_string_with_one_decimal(float number, const char* suffix) { float decimals = number - (int)number; diff --git a/Applications/SystemMonitor/main.cpp b/Applications/SystemMonitor/main.cpp index eb58978a90a..c8b69a80bce 100644 --- a/Applications/SystemMonitor/main.cpp +++ b/Applications/SystemMonitor/main.cpp @@ -31,8 +31,9 @@ #include "ProcessFileDescriptorMapWidget.h" #include "ProcessMemoryMapWidget.h" #include "ProcessModel.h" -#include "ThreadStackWidget.h" #include "ProcessUnveiledPathsWidget.h" +#include "ThreadStackWidget.h" +#include #include #include #include @@ -61,17 +62,6 @@ #include #include -static String human_readable_size(u32 size) -{ - if (size < (64 * KiB)) - return String::format("%u", size); - if (size < MiB) - return String::format("%u KB", size / KiB); - if (size < GiB) - return String::format("%u MB", size / MiB); - return String::format("%u GB", size / GiB); -} - static NonnullRefPtr build_file_systems_tab(); static NonnullRefPtr build_pci_devices_tab(); static NonnullRefPtr build_devices_tab(); diff --git a/Userland/df.cpp b/Userland/df.cpp index 84681ee443b..a28fe8cb938 100644 --- a/Userland/df.cpp +++ b/Userland/df.cpp @@ -24,16 +24,17 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include #include #include +#include +#include #include #include #include -#include #include #include #include +#include #include static bool flag_human_readable = false; @@ -48,25 +49,6 @@ struct FileSystem { String mount_point; }; -// FIXME: Remove this hackery once printf() supports floats. -// FIXME: Also, we should probably round the sizes in df -h output. -static String number_string_with_one_decimal(float number, const char* suffix) -{ - float decimals = number - (int)number; - return String::format("%d.%d%s", (int)number, (int)(decimals * 10), suffix); -} - -static String human_readable_size(size_t size) -{ - if (size < 1 * KiB) - return String::number(size); - if (size < 1 * MiB) - return number_string_with_one_decimal((float)size / (float)KiB, "K"); - if (size < 1 * GiB) - return number_string_with_one_decimal((float)size / (float)MiB, "M"); - return number_string_with_one_decimal((float)size / (float)GiB, "G"); -} - int main(int argc, char** argv) { Core::ArgsParser args_parser; diff --git a/Userland/ifconfig.cpp b/Userland/ifconfig.cpp index bd32b8ee681..eb5bd75cec6 100644 --- a/Userland/ifconfig.cpp +++ b/Userland/ifconfig.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -38,17 +39,6 @@ #include #include -static String si_bytes(unsigned bytes) -{ - if (bytes >= GiB) - return String::format("%fGiB", (double)bytes / (double)GiB); - if (bytes >= MiB) - return String::format("%fMiB", (double)bytes / (double)MiB); - if (bytes >= KiB) - return String::format("%fkiB", (double)bytes / (double)KiB); - return String::format("%dB", bytes); -} - int main(int argc, char** argv) { const char* value_ipv4 = nullptr; @@ -95,8 +85,8 @@ int main(int argc, char** argv) printf("\tnetmask: %s\n", netmask.characters()); printf("\tgateway: %s\n", gateway.characters()); printf("\tclass: %s\n", class_name.characters()); - printf("\tRX: %u packets %u bytes (%s)\n", packets_in, bytes_in, si_bytes(bytes_in).characters()); - printf("\tTX: %u packets %u bytes (%s)\n", packets_out, bytes_out, si_bytes(bytes_out).characters()); + printf("\tRX: %u packets %u bytes (%s)\n", packets_in, bytes_in, human_readable_size(bytes_in).characters()); + printf("\tTX: %u packets %u bytes (%s)\n", packets_out, bytes_out, human_readable_size(bytes_out).characters()); printf("\tMTU: %u\n", mtu); printf("\n"); }); diff --git a/Userland/ls.cpp b/Userland/ls.cpp index 7fed159f1e1..7d97aa50e4e 100644 --- a/Userland/ls.cpp +++ b/Userland/ls.cpp @@ -25,6 +25,7 @@ */ #include +#include #include #include #include @@ -231,25 +232,6 @@ static size_t print_name(const struct stat& st, const String& name, const char* return nprinted; } -// FIXME: Remove this hackery once printf() supports floats. -// FIXME: Also, we should probably round the sizes in ls -lh output. -static String number_string_with_one_decimal(float number, const char* suffix) -{ - float decimals = number - (int)number; - return String::format("%d.%d%s", (int)number, (int)(decimals * 10), suffix); -} - -static String human_readable_size(size_t size) -{ - if (size < 1 * KiB) - return String::number(size); - if (size < 1 * MiB) - return number_string_with_one_decimal((float)size / (float)KiB, "K"); - if (size < 1 * GiB) - return number_string_with_one_decimal((float)size / (float)MiB, "M"); - return number_string_with_one_decimal((float)size / (float)GiB, "G"); -} - static bool print_filesystem_object(const String& path, const String& name, const struct stat& st) { if (flag_show_inode)