From: Alan Cox Date: Wed, 20 May 2015 22:37:18 +0000 (+0100) Subject: xitoa: use our own int helper X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=d5f31fbe0b88d83756f36059ada2250e78d03acd;p=FUZIX.git xitoa: use our own int helper For stdio and perror using apps it costs us a small amount of memory. For stdio using apps in general it's free, and for non stdio apps using itoa and not sucking in the 32bit maths helpers its a big saving. --- diff --git a/Library/libs/xitoa.c b/Library/libs/xitoa.c index a066c10d..2c076f1d 100644 --- a/Library/libs/xitoa.c +++ b/Library/libs/xitoa.c @@ -1,10 +1,34 @@ /* - * Internal wrapper for ltostr. Ought to go away + * Internal wrapper (in theory) but used in various places it's helpful */ #include /*********************** xitoa.c ***************************/ -char *_itoa(int i) + +/* Don't go via long - the long version is expensive on an 8bit processor and + can often be avoided */ + +static char buf[7]; + +char *_uitoa(int i) { - return _ltoa((long)i); + char *p = buf + sizeof(buf); + int c; + + *--p = '\0'; + do { + c = i % 10; + i /= 10; + *--p = '0' + c; + } while(i); + return p; +} + +char *_itoa(int i) { + char *p; + if (i >= 0) + return _uitoa(i); + p = _uitoa(-i); + *--p = '-'; + return p; }