-
Notifications
You must be signed in to change notification settings - Fork 1
/
fourmat.c
199 lines (170 loc) · 3.68 KB
/
fourmat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <langinfo.h>
#include "fourmat.h"
#define array_len(a) (sizeof a / sizeof *a)
static void
dec_sep(char *c)
{
char const *const item = nl_langinfo(RADIXCHAR);
*c = '\0' == item[1] ? item[0] : '.';
}
int
fmt_time(char *str, uint32_t time)
{
static char const
TIME_UNITS[] = {
's',
'm',
'h',
'd',
'w',
'y'
};
static uint32_t const
TIME_BASE[] = {
1,
60,
60 * 60,
60 * 60 * 24,
60 * 60 * 24 * 7,
60 * 60 * 24 * 7 * 52,
UINT32_MAX
};
if (time >= 100 * TIME_BASE[array_len(TIME_BASE) - 2]) {
str[0] = 'n';
str[1] = 'e';
str[2] = 'v';
str[3] = 'e';
str[4] = 'r';
} else {
uint8_t i, j;
for (i = 0; (TIME_BASE + 1)[i] < time; ++i)
;
/* t1 UNIT[i] [ t2 UNIT[j] ] */
uint8_t t1, t2;
t1 = time / TIME_BASE[i];
time %= TIME_BASE[i];
for (j = i; 0 < j && 0 == (t2 = time / TIME_BASE[--j]);)
time %= TIME_BASE[j];
if ((t1 < 10 || t2 < 10) && 0 < t2) {
str[3] = '0' + (t2 % 10);
str[4] = TIME_UNITS[j];
if (t2 < 10) {
str[0] = t1 >= 10 ? '0' + (t1 / 10) : ' ';
str -= 2;
} else {
str[0] = ' ';
str[2] = '0' + (t2 / 10);
str -= 3;
}
} else {
str[0] = ' ';
str[1] = ' ';
str[2] = t1 >= 10 ? '0' + (t1 / 10) : ' ';
}
str[3] = '0' + (t1 % 10);
str[4] = TIME_UNITS[i];
}
return 5;
}
static int
fmt_decimal(char *str, uint64_t n, char const *UNITS, uint64_t const *BASE)
{
int i = 0;
while (n >= 1000 * BASE[i++])
;
n *= BASE[2];
n /= (BASE - 1)[i];
if (' ' == (UNITS - 1)[i])
*str++ = ' ';
if (n >= 10 * BASE[2] || 1 == (BASE - 1)[i]) {
n /= BASE[2];
str[0] = n >= 100 ? '0' + (n / 100) : ' ';
str[1] = n >= 10 ? '0' + (n % 100 / 10) : ' ';
str[2] = '0' + (n % 10);
} else {
n *= 10;
n /= BASE[2];
str[0] = '0' + (n / 10);
dec_sep(&str[1]);
str[2] = '0' + (n % 10);
}
if (' ' != (UNITS - 1)[i])
str[3] = (UNITS - 1)[i];
return 4;
}
int
fmt_speed(char *str, uint64_t num)
{
static char const
BYTES_METRIC_UNITS[] = { ' ', 'B', 'k', 'M', 'G', 'T', 'Y' };
static uint64_t const
BYTES_METRIC_BASE[] = { 1, 1e0, 1e3, 1e6, 1e9, 1e12, 1e15, 1e18 };
return fmt_decimal(str, num, BYTES_METRIC_UNITS, BYTES_METRIC_BASE);
}
int
fmt_space(char *str, uint64_t num)
{
#define E(n) (UINT64_C(1) << (10U * n))
static char const
BYTES_IEC_UNITS[] = { ' ', 'B', 'K', 'M', 'G', 'T', 'Y' };
static uint64_t const
BYTES_IEC_BASE[] = { 1, E(0), E(1), E(2), E(3), E(4), E(5), E(6) };
#undef E
return fmt_decimal(str, num, BYTES_IEC_UNITS, BYTES_IEC_BASE);
}
int
fmt_number(char *str, uint64_t num)
{
static char *const
NUMBER_UNITS = "kMGT";
if (num < 10000) {
str[0] = num >= 1000 ? '0' + (num / 1000) : ' ';
} else {
char *unit = NUMBER_UNITS;
while ((num /= 1000) >= 1000)
++unit;
str[3] = *unit;
--str;
}
str[1] = num >= 100 ? '0' + (num % 1000 / 100) : ' ';
str[2] = num >= 10 ? '0' + (num % 100 / 10) : ' ';
str[3] = '0' + (num % 10);
return 4;
}
int
fmt_percent(char *str, uint64_t num, uint64_t total)
{
#define PERCENT * 100
uint32_t p = total > 0 ? num * (100 PERCENT) / total : 0;
if (p < 10 PERCENT) {
/* 9.99% */
str[0] = '0' + (p / 100);
dec_sep(&str[1]);
str[2] = '0' + (p % 100 / 10);
str[3] = '0' + (p % 10);
str[4] = '%';
} else if (p < 100 PERCENT) {
/* 99.9% */
p /= 10;
str[0] = '0' + (p / 100);
str[1] = '0' + (p % 100 / 10);
dec_sep(&str[2]);
str[3] = '0' + (p % 10);
str[4] = '%';
} else if (p < 10000 PERCENT) {
/* 999% */
p /= 100;
str[0] = p >= 1000 ? '0' + (p / 1000) : ' ';
str[1] = '0' + (p % 1000 / 100);
str[2] = '0' + (p % 100 / 10);
str[3] = '0' + (p % 10);
str[4] = '%';
} else {
/* x 9 */
p /= 100 PERCENT;
str[0] = 'x';
return 1 + fmt_number(str + 1, p);
}
#undef PERCENT
return 5;
}