123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- #include "libbb.h"
- const char* FAST_FUNC make_human_readable_str(unsigned long long val,
- unsigned long block_size, unsigned long display_unit)
- {
- static const char unit_chars[] ALIGN1 = {
- '\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'
- };
- unsigned frac;
- const char *u;
- const char *fmt;
- if (val == 0)
- return "0";
- fmt = "%llu";
- if (block_size > 1)
- val *= block_size;
- frac = 0;
- u = unit_chars;
- if (display_unit) {
- val += display_unit/2;
- val /= display_unit;
-
- } else {
- while ((val >= 1024)
-
- ) {
- fmt = "%llu.%u%c";
- u++;
- frac = (((unsigned)val % 1024) * 10 + 1024/2) / 1024;
- val /= 1024;
- }
- if (frac >= 10) {
- ++val;
- frac = 0;
- }
- #if 1
-
- if (block_size == 0) {
- if (frac >= 5) {
- ++val;
- }
- fmt = "%llu%*c";
- frac = 1;
- }
- #endif
- }
- return auto_string(xasprintf(fmt, val, frac, *u));
- }
- char* FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale)
- {
- const char *fmt;
- char c;
- unsigned v, u, idx = 0;
- if (ul > 99999) {
- ul *= 10;
- do {
- ul /= 1024;
- idx++;
- } while (ul >= 100000);
- }
- v = ul;
- fmt = " 123456789";
- u = v / 10;
- v = v % 10;
- if (!idx) {
-
-
- c = buf[0] = " 123456789"[u/1000];
- if (c != ' ') fmt = "0123456789";
- c = buf[1] = fmt[u/100%10];
- if (c != ' ') fmt = "0123456789";
- c = buf[2] = fmt[u/10%10];
- if (c != ' ') fmt = "0123456789";
- buf[3] = fmt[u%10];
- buf[4] = "0123456789"[v];
- } else {
-
-
- if (u >= 100) {
-
- c = buf[0] = " 123456789"[u/1000];
- if (c != ' ') fmt = "0123456789";
- c = buf[1] = fmt[u/100%10];
- if (c != ' ') fmt = "0123456789";
- v = u % 10;
- u = u / 10;
- buf[2] = fmt[u%10];
- } else {
-
- c = buf[0] = " 123456789"[u/10];
- if (c != ' ') fmt = "0123456789";
- buf[1] = fmt[u%10];
- buf[2] = '.';
- }
- buf[3] = "0123456789"[v];
- buf[4] = scale[idx];
- }
- return buf + 5;
- }
- char* FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[4], const char *scale)
- {
- const char *fmt;
- char c;
- unsigned v, u, idx = 0;
- if (ul > 9999) {
- ul *= 10;
- do {
- ul /= 1024;
- idx++;
- } while (ul >= 10000);
- }
- v = ul;
- fmt = " 123456789";
- u = v / 10;
- v = v % 10;
- if (!idx) {
-
-
- c = buf[0] = " 123456789"[u/100];
- if (c != ' ') fmt = "0123456789";
- c = buf[1] = fmt[u/10%10];
- if (c != ' ') fmt = "0123456789";
- buf[2] = fmt[u%10];
- buf[3] = "0123456789"[v];
- } else {
-
- if (u >= 10) {
-
- c = buf[0] = " 123456789"[u/100];
- if (c != ' ') fmt = "0123456789";
- v = u % 10;
- u = u / 10;
- buf[1] = fmt[u%10];
- } else {
-
- buf[0] = "0123456789"[u];
- buf[1] = '.';
- }
- buf[2] = "0123456789"[v];
- buf[3] = scale[idx];
- }
- return buf + 4;
- }
|