3
0

human_readable.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * June 30, 2001 Manuel Novoa III
  4. *
  5. * All-integer version (hey, not everyone has floating point) of
  6. * make_human_readable_str, modified from similar code I had written
  7. * for busybox several months ago.
  8. *
  9. * Notes:
  10. * 1) I'm using an unsigned long long to hold the product size * block_size,
  11. * as df (which calls this routine) could request a representation of a
  12. * partition size in bytes > max of unsigned long. If long longs aren't
  13. * available, it would be possible to do what's needed using polynomial
  14. * representations (say, powers of 1024) and manipulating coefficients.
  15. * The base ten "bytes" output could be handled similarly.
  16. *
  17. * 2) This routine outputs a decimal point and a tenths digit when
  18. * display_unit == 0. Hence, it isn't uncommon for the returned string
  19. * to have a length of 5 or 6.
  20. *
  21. * If block_size is also 0, no decimal digits are printed.
  22. *
  23. * Licensed under GPLv2, see file LICENSE in this source tree.
  24. */
  25. #include "libbb.h"
  26. const char* FAST_FUNC make_human_readable_str(unsigned long long val,
  27. unsigned long block_size, unsigned long display_unit)
  28. {
  29. static const char unit_chars[] ALIGN1 = {
  30. '\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'
  31. };
  32. unsigned frac; /* 0..9 - the fractional digit */
  33. const char *u;
  34. const char *fmt;
  35. if (val == 0)
  36. return "0";
  37. fmt = "%llu";
  38. if (block_size > 1)
  39. val *= block_size;
  40. frac = 0;
  41. u = unit_chars;
  42. if (display_unit) {
  43. val += display_unit/2; /* Deal with rounding */
  44. val /= display_unit; /* Don't combine with the line above! */
  45. /* will just print it as ulonglong (below) */
  46. } else {
  47. while ((val >= 1024)
  48. /* && (u < unit_chars + sizeof(unit_chars) - 1) - always true */
  49. ) {
  50. fmt = "%llu.%u%c";
  51. u++;
  52. frac = (((unsigned)val % 1024) * 10 + 1024/2) / 1024;
  53. val /= 1024;
  54. }
  55. if (frac >= 10) { /* we need to round up here */
  56. ++val;
  57. frac = 0;
  58. }
  59. #if 1
  60. /* If block_size is 0, dont print fractional part */
  61. if (block_size == 0) {
  62. if (frac >= 5) {
  63. ++val;
  64. }
  65. fmt = "%llu%*c";
  66. frac = 1;
  67. }
  68. #endif
  69. }
  70. return auto_string(xasprintf(fmt, val, frac, *u));
  71. }
  72. /* vda's implementations of the similar idea */
  73. /* Convert unsigned long long value into compact 5-char representation.
  74. * String is not terminated (buf[5] is untouched) */
  75. char* FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale)
  76. {
  77. const char *fmt;
  78. char c;
  79. unsigned v, u, idx = 0;
  80. if (ul > 99999) { // do not scale if 99999 or less
  81. ul *= 10;
  82. do {
  83. ul /= 1024;
  84. idx++;
  85. } while (ul >= 100000);
  86. }
  87. v = ul; // ullong divisions are expensive, avoid them
  88. fmt = " 123456789";
  89. u = v / 10;
  90. v = v % 10;
  91. if (!idx) {
  92. // 99999 or less: use "12345" format
  93. // u is value/10, v is last digit
  94. c = buf[0] = " 123456789"[u/1000];
  95. if (c != ' ') fmt = "0123456789";
  96. c = buf[1] = fmt[u/100%10];
  97. if (c != ' ') fmt = "0123456789";
  98. c = buf[2] = fmt[u/10%10];
  99. if (c != ' ') fmt = "0123456789";
  100. buf[3] = fmt[u%10];
  101. buf[4] = "0123456789"[v];
  102. } else {
  103. // value has been scaled into 0..9999.9 range
  104. // u is value, v is 1/10ths (allows for 92.1M format)
  105. if (u >= 100) {
  106. // value is >= 100: use "1234M', " 123M" formats
  107. c = buf[0] = " 123456789"[u/1000];
  108. if (c != ' ') fmt = "0123456789";
  109. c = buf[1] = fmt[u/100%10];
  110. if (c != ' ') fmt = "0123456789";
  111. v = u % 10;
  112. u = u / 10;
  113. buf[2] = fmt[u%10];
  114. } else {
  115. // value is < 100: use "92.1M" format
  116. c = buf[0] = " 123456789"[u/10];
  117. if (c != ' ') fmt = "0123456789";
  118. buf[1] = fmt[u%10];
  119. buf[2] = '.';
  120. }
  121. buf[3] = "0123456789"[v];
  122. buf[4] = scale[idx]; /* typically scale = " kmgt..." */
  123. }
  124. return buf + 5;
  125. }
  126. /* Convert unsigned long long value into compact 4-char
  127. * representation. Examples: "1234", "1.2k", " 27M", "123T"
  128. * String is not terminated (buf[4] is untouched) */
  129. char* FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[4], const char *scale)
  130. {
  131. const char *fmt;
  132. char c;
  133. unsigned v, u, idx = 0;
  134. if (ul > 9999) { // do not scale if 9999 or less
  135. ul *= 10;
  136. do {
  137. ul /= 1024;
  138. idx++;
  139. } while (ul >= 10000);
  140. }
  141. v = ul; // ullong divisions are expensive, avoid them
  142. fmt = " 123456789";
  143. u = v / 10;
  144. v = v % 10;
  145. if (!idx) {
  146. // 9999 or less: use "1234" format
  147. // u is value/10, v is last digit
  148. c = buf[0] = " 123456789"[u/100];
  149. if (c != ' ') fmt = "0123456789";
  150. c = buf[1] = fmt[u/10%10];
  151. if (c != ' ') fmt = "0123456789";
  152. buf[2] = fmt[u%10];
  153. buf[3] = "0123456789"[v];
  154. } else {
  155. // u is value, v is 1/10ths (allows for 9.2M format)
  156. if (u >= 10) {
  157. // value is >= 10: use "123M', " 12M" formats
  158. c = buf[0] = " 123456789"[u/100];
  159. if (c != ' ') fmt = "0123456789";
  160. v = u % 10;
  161. u = u / 10;
  162. buf[1] = fmt[u%10];
  163. } else {
  164. // value is < 10: use "9.2M" format
  165. buf[0] = "0123456789"[u];
  166. buf[1] = '.';
  167. }
  168. buf[2] = "0123456789"[v];
  169. buf[3] = scale[idx]; /* typically scale = " kmgt..." */
  170. }
  171. return buf + 4;
  172. }