bytes to human readalbe string more gracefully

use_iouring V1.0.79
YuQing 2025-08-29 13:25:58 +08:00
parent 3f19715e45
commit 84a1f90a9a
1 changed files with 25 additions and 6 deletions

View File

@ -3251,6 +3251,24 @@ const char *long2str(const int64_t n, char *buff, const bool thousands_separator
return buff; return buff;
} }
static int format_bytes_string(const int64_t input_bytes,
const int64_t unit_bytes, char *buff)
{
int n;
int fragment;
char *p;
n = input_bytes / unit_bytes;
p = buff + fc_itoa(n, buff);
if (n < 10)
{
fragment = ((input_bytes - (n * unit_bytes)) * 10LL) / unit_bytes;
*p++ = '.';
p += fc_itoa(fragment, p);
}
return p - buff;
}
const char *bytes_to_human_str(const int64_t bytes, char *buff) const char *bytes_to_human_str(const int64_t bytes, char *buff)
{ {
char *p; char *p;
@ -3263,10 +3281,11 @@ const char *bytes_to_human_str(const int64_t bytes, char *buff)
{ {
p = buff + fc_itoa(bytes, buff); p = buff + fc_itoa(bytes, buff);
*p++ = ' '; *p++ = ' ';
*p++ = ' '; //for alignment
} }
else else
{ {
p = buff + fc_itoa(bytes / FC_BYTES_ONE_KB, buff); p = buff + format_bytes_string(bytes, FC_BYTES_ONE_KB, buff);
*p++ = ' '; *p++ = ' ';
*p++ = 'K'; *p++ = 'K';
} }
@ -3275,13 +3294,13 @@ const char *bytes_to_human_str(const int64_t bytes, char *buff)
{ {
if (bytes < FC_BYTES_ONE_GB) if (bytes < FC_BYTES_ONE_GB)
{ {
p = buff + fc_itoa(bytes / FC_BYTES_ONE_MB, buff); p = buff + format_bytes_string(bytes, FC_BYTES_ONE_MB, buff);
*p++ = ' '; *p++ = ' ';
*p++ = 'M'; *p++ = 'M';
} }
else else
{ {
p = buff + fc_itoa(bytes / FC_BYTES_ONE_GB, buff); p = buff + format_bytes_string(bytes, FC_BYTES_ONE_GB, buff);
*p++ = ' '; *p++ = ' ';
*p++ = 'G'; *p++ = 'G';
} }
@ -3293,20 +3312,20 @@ const char *bytes_to_human_str(const int64_t bytes, char *buff)
{ {
if (bytes < FC_BYTES_ONE_PB) if (bytes < FC_BYTES_ONE_PB)
{ {
p = buff + fc_itoa(bytes / FC_BYTES_ONE_TB, buff); p = buff + format_bytes_string(bytes, FC_BYTES_ONE_TB, buff);
*p++ = ' '; *p++ = ' ';
*p++ = 'T'; *p++ = 'T';
} }
else else
{ {
p = buff + fc_itoa(bytes / FC_BYTES_ONE_PB, buff); p = buff + format_bytes_string(bytes, FC_BYTES_ONE_PB, buff);
*p++ = ' '; *p++ = ' ';
*p++ = 'P'; *p++ = 'P';
} }
} }
else else
{ {
p = buff + fc_itoa(bytes / FC_BYTES_ONE_EB, buff); p = buff + format_bytes_string(bytes, FC_BYTES_ONE_EB, buff);
*p++ = ' '; *p++ = ' ';
*p++ = 'E'; *p++ = 'E';
} }