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

@ -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;

View file

@ -31,8 +31,9 @@
#include "ProcessFileDescriptorMapWidget.h"
#include "ProcessMemoryMapWidget.h"
#include "ProcessModel.h"
#include "ThreadStackWidget.h"
#include "ProcessUnveiledPathsWidget.h"
#include "ThreadStackWidget.h"
#include <AK/NumberFormat.h>
#include <LibCore/Timer.h>
#include <LibGUI/AboutDialog.h>
#include <LibGUI/Action.h>
@ -61,17 +62,6 @@
#include <stdio.h>
#include <unistd.h>
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<GUI::Widget> build_file_systems_tab();
static NonnullRefPtr<GUI::Widget> build_pci_devices_tab();
static NonnullRefPtr<GUI::Widget> build_devices_tab();

View file

@ -24,16 +24,17 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/String.h>
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/NumberFormat.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
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;

View file

@ -26,6 +26,7 @@
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/NumberFormat.h>
#include <AK/String.h>
#include <AK/Types.h>
#include <LibCore/ArgsParser.h>
@ -38,17 +39,6 @@
#include <sys/ioctl.h>
#include <sys/socket.h>
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");
});

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)