Optimizing printing of numbers (writing a single buffer).

Fix from Bill Greiman via Limor.
This commit is contained in:
David A. Mellis 2011-05-07 18:17:32 -04:00
parent ac5defcea6
commit 5e5cce8124
1 changed files with 13 additions and 15 deletions

View File

@ -179,25 +179,23 @@ void Print::println(double n, int digits)
// Private Methods ///////////////////////////////////////////////////////////// // Private Methods /////////////////////////////////////////////////////////////
void Print::printNumber(unsigned long n, uint8_t base) void Print::printNumber(unsigned long n, uint8_t base) {
{ char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars. char *str = &buf[sizeof(buf) - 1];
unsigned long i = 0;
if (n == 0) { *str = '\0';
print('0');
return;
}
while (n > 0) { // prevent crash if called with base == 1
buf[i++] = n % base; if (base < 2) base = 10;
do {
unsigned long m = n;
n /= base; n /= base;
} char c = m - base * n;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
for (; i > 0; i--) write(str);
print((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
} }
void Print::printFloat(double number, uint8_t digits) void Print::printFloat(double number, uint8_t digits)