Add size.hpp utility for human-readable size formatting

This commit is contained in:
2025-03-22 21:02:07 -04:00
parent 2d719023c0
commit 2c9a03aa46

23
include/fgl/size.hpp Normal file
View File

@@ -0,0 +1,23 @@
//
// Created by kj16609 on 3/21/25.
//
#pragma once
#include <cmath>
#include <cstdint>
#include <format>
#include <string>
namespace fgl::size
{
std::string toHuman( std::size_t size )
{
constexpr std::size_t mod { 1024 };
if ( size < mod ) return std::format( "{}B", size );
if ( size < std::pow( mod, 2 ) ) return std::format( "{}KiB", static_cast< int >( size / mod ) );
if ( size < std::pow( mod, 3 ) ) return std::format( "{}MiB", static_cast< int >( size / std::pow( mod, 2 ) ) );
if ( size < std::pow( mod, 4 ) ) return std::format( "{}GiB", static_cast< int >( size / std::pow( mod, 3 ) ) );
return std::format( "{}TiB", static_cast< int >( size / std::pow( mod, 4 ) ) );
}
} // namespace fgl::size