diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-06-10 10:54:44 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-06-10 10:54:44 +0200 |
commit | ab60cd13828896febb6c5c44046610f2c75a11f5 (patch) | |
tree | 87ee10c35ebe37781b35ddd688ec31af180d2ae4 | |
parent | 7fdf5a88b4aaabb88624e66cd31d589a5b7280f6 (diff) | |
download | busybox-w32-ab60cd13828896febb6c5c44046610f2c75a11f5.tar.gz busybox-w32-ab60cd13828896febb6c5c44046610f2c75a11f5.tar.bz2 busybox-w32-ab60cd13828896febb6c5c44046610f2c75a11f5.zip |
utoa: shrink
function old new delta
itoa_to_buf 20 23 +3
utoa_to_buf 117 101 -16
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | libbb/xfuncs.c | 44 |
1 files changed, 29 insertions, 15 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 1cd8d7c01..65437211d 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -49,23 +49,35 @@ char* FAST_FUNC strncpy_IFNAMSIZ(char *dst, const char *src) | |||
49 | } | 49 | } |
50 | 50 | ||
51 | 51 | ||
52 | // Convert unsigned integer to ascii, writing into supplied buffer. | 52 | /* Convert unsigned integer to ascii, writing into supplied buffer. |
53 | // A truncated result contains the first few digits of the result ala strncpy. | 53 | * A truncated result contains the first few digits of the result ala strncpy. |
54 | // Returns a pointer past last generated digit, does _not_ store NUL. | 54 | * Returns a pointer past last generated digit, does _not_ store NUL. |
55 | void BUG_sizeof_unsigned_not_4(void); | 55 | */ |
56 | void BUG_sizeof(void); | ||
56 | char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen) | 57 | char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen) |
57 | { | 58 | { |
58 | unsigned i, out, res; | 59 | unsigned i, out, res; |
59 | if (sizeof(unsigned) != 4) | 60 | |
60 | BUG_sizeof_unsigned_not_4(); | ||
61 | if (buflen) { | 61 | if (buflen) { |
62 | out = 0; | 62 | out = 0; |
63 | for (i = 1000000000; i; i /= 10) { | 63 | if (sizeof(n) == 4) |
64 | // 2^32-1 = 4294967295 | ||
65 | i = 1000000000; | ||
66 | #if UINT_MAX > 4294967295 /* prevents warning about "const too large" */ | ||
67 | else | ||
68 | if (sizeof(n) == 8) | ||
69 | // 2^64-1 = 18446744073709551615 | ||
70 | i = 10000000000000000000; | ||
71 | #endif | ||
72 | else | ||
73 | BUG_sizeof(); | ||
74 | for (; i; i /= 10) { | ||
64 | res = n / i; | 75 | res = n / i; |
76 | n = n % i; | ||
65 | if (res || out || i == 1) { | 77 | if (res || out || i == 1) { |
66 | if (!--buflen) break; | 78 | if (--buflen == 0) |
79 | break; | ||
67 | out++; | 80 | out++; |
68 | n -= res*i; | ||
69 | *buf++ = '0' + res; | 81 | *buf++ = '0' + res; |
70 | } | 82 | } |
71 | } | 83 | } |
@@ -76,7 +88,9 @@ char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen) | |||
76 | /* Convert signed integer to ascii, like utoa_to_buf() */ | 88 | /* Convert signed integer to ascii, like utoa_to_buf() */ |
77 | char* FAST_FUNC itoa_to_buf(int n, char *buf, unsigned buflen) | 89 | char* FAST_FUNC itoa_to_buf(int n, char *buf, unsigned buflen) |
78 | { | 90 | { |
79 | if (buflen && n < 0) { | 91 | if (!buflen) |
92 | return buf; | ||
93 | if (n < 0) { | ||
80 | n = -n; | 94 | n = -n; |
81 | *buf++ = '-'; | 95 | *buf++ = '-'; |
82 | buflen--; | 96 | buflen--; |
@@ -87,16 +101,16 @@ char* FAST_FUNC itoa_to_buf(int n, char *buf, unsigned buflen) | |||
87 | // The following two functions use a static buffer, so calling either one a | 101 | // The following two functions use a static buffer, so calling either one a |
88 | // second time will overwrite previous results. | 102 | // second time will overwrite previous results. |
89 | // | 103 | // |
90 | // The largest 32 bit integer is -2 billion plus null terminator, or 12 bytes. | 104 | // The largest 32 bit integer is -2 billion plus NUL, or 1+10+1=12 bytes. |
91 | // It so happens that sizeof(int) * 3 is enough for 32+ bits. | 105 | // It so happens that sizeof(int) * 3 is enough for 32+ bit ints. |
92 | // (sizeof(int) * 3 + 2 is correct for any width, even 8-bit) | 106 | // (sizeof(int) * 3 + 2 is correct for any width, even 8-bit) |
93 | 107 | ||
94 | static char local_buf[sizeof(int) * 3]; | 108 | static char local_buf[sizeof(int) * 3]; |
95 | 109 | ||
96 | // Convert unsigned integer to ascii using a static buffer (returned). | 110 | /* Convert unsigned integer to ascii using a static buffer (returned). */ |
97 | char* FAST_FUNC utoa(unsigned n) | 111 | char* FAST_FUNC utoa(unsigned n) |
98 | { | 112 | { |
99 | *(utoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0'; | 113 | *(utoa_to_buf(n, local_buf, sizeof(local_buf) - 1)) = '\0'; |
100 | 114 | ||
101 | return local_buf; | 115 | return local_buf; |
102 | } | 116 | } |
@@ -104,7 +118,7 @@ char* FAST_FUNC utoa(unsigned n) | |||
104 | /* Convert signed integer to ascii using a static buffer (returned). */ | 118 | /* Convert signed integer to ascii using a static buffer (returned). */ |
105 | char* FAST_FUNC itoa(int n) | 119 | char* FAST_FUNC itoa(int n) |
106 | { | 120 | { |
107 | *(itoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0'; | 121 | *(itoa_to_buf(n, local_buf, sizeof(local_buf) - 1)) = '\0'; |
108 | 122 | ||
109 | return local_buf; | 123 | return local_buf; |
110 | } | 124 | } |