diff options
author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-11-05 00:44:39 +0000 |
---|---|---|
committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-11-05 00:44:39 +0000 |
commit | 04353387ec2a1ee4a28e44be9062c2a6967f397d (patch) | |
tree | 95ef467494c3178ffb308b95f8743886d9c9a5ae /libbb | |
parent | 48b1ebd12ec4ef182b4ea423f2309705ae141e8e (diff) | |
download | busybox-w32-04353387ec2a1ee4a28e44be9062c2a6967f397d.tar.gz busybox-w32-04353387ec2a1ee4a28e44be9062c2a6967f397d.tar.bz2 busybox-w32-04353387ec2a1ee4a28e44be9062c2a6967f397d.zip |
smart_ulltoa5: make available in libbb
git-svn-id: svn://busybox.net/trunk/busybox@16510 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/xfuncs.c | 60 |
1 files changed, 54 insertions, 6 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index c72265003..352515af4 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -201,23 +201,71 @@ void xsetenv(const char *key, const char *value) | |||
201 | bb_error_msg_and_die(bb_msg_memory_exhausted); | 201 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
202 | } | 202 | } |
203 | 203 | ||
204 | |||
205 | // Converts unsigned long long value into compact 4-char | ||
206 | // representation. Examples: "1234", "1.2k", " 27M", "123T" | ||
207 | // Fifth char is always '\0' | ||
208 | void smart_ulltoa5(unsigned long long ul, char buf[5]) | ||
209 | { | ||
210 | char *fmt; | ||
211 | char c; | ||
212 | unsigned v,idx = 0; | ||
213 | ul *= 10; | ||
214 | if (ul > 9999*10) { // do not scale if 9999 or less | ||
215 | while (ul >= 10000) { | ||
216 | ul /= 1024; | ||
217 | idx++; | ||
218 | } | ||
219 | } | ||
220 | v = ul; // ullong divisions are expensive, avoid them | ||
221 | |||
222 | fmt = " 123456789"; | ||
223 | if (!idx) { // 9999 or less: use 1234 format | ||
224 | c = buf[0] = " 123456789"[v/10000]; | ||
225 | if (c!=' ') fmt = "0123456789"; | ||
226 | c = buf[1] = fmt[v/1000%10]; | ||
227 | if (c!=' ') fmt = "0123456789"; | ||
228 | buf[2] = fmt[v/100%10]; | ||
229 | buf[3] = "0123456789"[v/10%10]; | ||
230 | } else { | ||
231 | if (v>=10*10) { // scaled value is >=10: use 123M format | ||
232 | c = buf[0] = " 123456789"[v/1000]; | ||
233 | if (c!=' ') fmt = "0123456789"; | ||
234 | buf[1] = fmt[v/100%10]; | ||
235 | buf[2] = "0123456789"[v/10%10]; | ||
236 | } else { // scaled value is <10: use 1.2M format | ||
237 | buf[0] = "0123456789"[v/10]; | ||
238 | buf[1] = '.'; | ||
239 | buf[2] = "0123456789"[v%10]; | ||
240 | } | ||
241 | // see http://en.wikipedia.org/wiki/Tera | ||
242 | buf[3] = " kMGTPEZY"[idx]; | ||
243 | } | ||
244 | buf[4] = '\0'; | ||
245 | } | ||
246 | |||
247 | |||
204 | // Convert unsigned integer to ascii, writing into supplied buffer. A | 248 | // Convert unsigned integer to ascii, writing into supplied buffer. A |
205 | // truncated result is always null terminated (unless buflen is 0), and | 249 | // truncated result is always null terminated (unless buflen is 0), and |
206 | // contains the first few digits of the result ala strncpy. | 250 | // contains the first few digits of the result ala strncpy. |
251 | void BUG_sizeof_unsigned_not_4(void); | ||
207 | void utoa_to_buf(unsigned n, char *buf, unsigned buflen) | 252 | void utoa_to_buf(unsigned n, char *buf, unsigned buflen) |
208 | { | 253 | { |
209 | int i, out = 0; | 254 | unsigned i, out, res; |
255 | if (sizeof(unsigned) != 4) | ||
256 | BUG_sizeof_unsigned_not_4(); | ||
210 | if (buflen) { | 257 | if (buflen) { |
211 | for (i=1000000000; i; i/=10) { | 258 | out = 0; |
212 | int res = n/i; | 259 | for (i = 1000000000; i; i /= 10) { |
213 | 260 | res = n / i; | |
214 | if ((res || out || i == 1) && --buflen>0) { | 261 | if (res || out || i == 1) { |
262 | if (!--buflen) break; | ||
215 | out++; | 263 | out++; |
216 | n -= res*i; | 264 | n -= res*i; |
217 | *buf++ = '0' + res; | 265 | *buf++ = '0' + res; |
218 | } | 266 | } |
219 | } | 267 | } |
220 | *buf = 0; | 268 | *buf = '\0'; |
221 | } | 269 | } |
222 | } | 270 | } |
223 | 271 | ||