MCPcopy Index your code
hub / github.com/F-Stack/f-stack / humanize_number

Function humanize_number

tools/libutil/humanize_number.c:50–182  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

48static const int maxscale = 6;
49
50int
51humanize_number(char *buf, size_t len, int64_t quotient,
52 const char *suffix, int scale, int flags)
53{
54 const char *prefixes, *sep;
55 int i, r, remainder, s1, s2, sign;
56 int divisordeccut;
57 int64_t divisor, max;
58 size_t baselen;
59
60 /* Since so many callers don't check -1, NUL terminate the buffer */
61 if (len > 0)
62 buf[0] = '\0';
63
64 /* validate args */
65 if (buf == NULL || suffix == NULL)
66 return (-1);
67 if (scale < 0)
68 return (-1);
69 else if (scale > maxscale &&
70 ((scale & ~(HN_AUTOSCALE|HN_GETSCALE)) != 0))
71 return (-1);
72 if ((flags & HN_DIVISOR_1000) && (flags & HN_IEC_PREFIXES))
73 return (-1);
74
75 /* setup parameters */
76 remainder = 0;
77
78 if (flags & HN_IEC_PREFIXES) {
79 baselen = 2;
80 /*
81 * Use the prefixes for power of two recommended by
82 * the International Electrotechnical Commission
83 * (IEC) in IEC 80000-3 (i.e. Ki, Mi, Gi...).
84 *
85 * HN_IEC_PREFIXES implies a divisor of 1024 here
86 * (use of HN_DIVISOR_1000 would have triggered
87 * an assertion earlier).
88 */
89 divisor = 1024;
90 divisordeccut = 973; /* ceil(.95 * 1024) */
91 if (flags & HN_B)
92 prefixes = "B\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
93 else
94 prefixes = "\0\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
95 } else {
96 baselen = 1;
97 if (flags & HN_DIVISOR_1000) {
98 divisor = 1000;
99 divisordeccut = 950;
100 if (flags & HN_B)
101 prefixes = "B\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
102 else
103 prefixes = "\0\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
104 } else {
105 divisor = 1024;
106 divisordeccut = 973; /* ceil(.95 * 1024) */
107 if (flags & HN_B)

Callers 3

mainFunction · 0.85
show_statFunction · 0.85
list_pipesFunction · 0.85

Calls 1

snprintfFunction · 0.85

Tested by 1

mainFunction · 0.68