1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 20:57:35 +00:00

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.
This commit is contained in:
Nico Weber 2020-08-15 14:05:46 -04:00 committed by Andreas Kling
parent 430b265cd4
commit aa97166739
5 changed files with 10 additions and 65 deletions

View file

@ -25,6 +25,7 @@
*/
#include <AK/HashMap.h>
#include <AK/NumberFormat.h>
#include <AK/QuickSort.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
@ -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)