40#include "xbps_api_impl.h"
44humanize_number(
char *buf,
size_t len, int64_t bytes,
45 const char *suffix,
int scale,
int flags)
47 const char *prefixes, *sep;
48 int b, i, r, maxscale, s1, s2, sign;
53 assert(suffix != NULL);
56 if (flags & HN_DIVISOR_1000) {
60 prefixes =
"B\0k\0M\0G\0T\0P\0E";
62 prefixes =
"\0\0k\0M\0G\0T\0P\0E";
70 prefixes =
"B\0K\0M\0G\0T\0P\0E";
72 prefixes =
"\0\0K\0M\0G\0T\0P\0E";
75#define SCALE2PREFIX(scale) (&prefixes[(scale) << 1])
78 if (scale >= maxscale &&
79 (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0)
82 if (buf == NULL || suffix == NULL)
96 if (flags & HN_NOSPACE)
102 baselen += strlen(suffix);
105 if (len < baselen + 1)
108 if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
110 for (max = 100, i = (
int)(len - baselen); i-- > 0;)
118 for (i = 0; bytes >= max - 50 && i < maxscale; i++)
121 if (scale & HN_GETSCALE)
124 for (i = 0; i < scale && i < maxscale; i++)
128 if (bytes < 995 && i > 0 && flags & HN_DECIMAL) {
130 if (len < baselen + 1 + 2)
132 b = ((int)bytes + 5) / 10;
135 r = snprintf(buf, len,
"%d%s%d%s%s%s",
136 sign * s1, localeconv()->decimal_point, s2,
137 sep, SCALE2PREFIX(i), suffix);
139 r = snprintf(buf, len,
"%" PRId64
"%s%s%s",
140 sign * ((bytes + 50) / 100),
141 sep, SCALE2PREFIX(i), suffix);