util/string.h: ease custom Number_of_bytes types

This patch exposes the formatted K/M/G byte output of 'Number_of_bytes'
as a class template function that accepts different basic types. This
enables the easy creation of a formatted output type for a type larger
than size_t.

Introduced in the context of issue #5489
This commit is contained in:
Norman Feske 2025-03-28 11:58:07 +01:00
parent 3cd5a69b65
commit b601ad5272

View File

@ -55,18 +55,20 @@ class Genode::Number_of_bytes
*/
operator size_t() const { return _n; }
void print(Output &output) const
static void print(Output &output, auto const n)
{
using Genode::print;
enum { KB = 1024UL, MB = KB*1024UL, GB = MB*1024UL };
enum { KB = 1024U, MB = KB*1024U, GB = MB*1024U };
if (_n == 0) print(output, 0);
else if (_n % GB == 0) print(output, _n/GB, "G");
else if (_n % MB == 0) print(output, _n/MB, "M");
else if (_n % KB == 0) print(output, _n/KB, "K");
else print(output, _n);
if (n == 0) print(output, 0);
else if (n % GB == 0) print(output, n/GB, "G");
else if (n % MB == 0) print(output, n/MB, "M");
else if (n % KB == 0) print(output, n/KB, "K");
else print(output, n);
}
void print(Output &output) const { print(output, _n); }
};