[MINOR] provide easier-to-use ultoa_* functions

Current ultoa() function is limited to one use per expression or
function call. Sometimes this is limitating. Change this in favor
of an array of 10 return values and shorter macros U2A0..U2A9
which respectively call the function with the 10 different buffers.
This commit is contained in:
Willy Tarreau 2007-10-25 12:14:10 +02:00
parent fe94460d53
commit 72d759c9c1
2 changed files with 30 additions and 8 deletions

View File

@ -64,10 +64,29 @@
extern int strlcpy2(char *dst, const char *src, int size);
/*
* This function simply returns a statically allocated string containing
* This function simply returns a locally allocated string containing
* the ascii representation for number 'n' in decimal.
*/
extern char *ultoa(unsigned long n);
extern char itoa_str[][21];
extern const char *ultoa_r(unsigned long n, char *buffer, int size);
static inline const char *ultoa(unsigned long n)
{
return ultoa_r(n, itoa_str[0], sizeof(itoa_str[0]));
}
/* Fast macros to convert up to 10 different parameters inside a same call of
* expression.
*/
#define U2A0(n) ({ ultoa_r((n), itoa_str[0], sizeof(itoa_str[0])); })
#define U2A1(n) ({ ultoa_r((n), itoa_str[1], sizeof(itoa_str[1])); })
#define U2A2(n) ({ ultoa_r((n), itoa_str[2], sizeof(itoa_str[2])); })
#define U2A3(n) ({ ultoa_r((n), itoa_str[3], sizeof(itoa_str[3])); })
#define U2A4(n) ({ ultoa_r((n), itoa_str[4], sizeof(itoa_str[4])); })
#define U2A5(n) ({ ultoa_r((n), itoa_str[5], sizeof(itoa_str[5])); })
#define U2A6(n) ({ ultoa_r((n), itoa_str[6], sizeof(itoa_str[6])); })
#define U2A7(n) ({ ultoa_r((n), itoa_str[7], sizeof(itoa_str[7])); })
#define U2A8(n) ({ ultoa_r((n), itoa_str[8], sizeof(itoa_str[8])); })
#define U2A9(n) ({ ultoa_r((n), itoa_str[9], sizeof(itoa_str[9])); })
/*
* Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.

View File

@ -20,8 +20,11 @@
#include <common/standard.h>
#include <proto/log.h>
/* enough to store 2^64-1 = 18446744073709551615 */
static char itoa_str[21];
/* enough to store 10 integers of :
* 2^64-1 = 18446744073709551615 or
* -2^63 = -9223372036854775808
*/
char itoa_str[10][21];
/*
* copies at most <size-1> chars from <src> to <dst>. Last char is always
@ -43,20 +46,20 @@ int strlcpy2(char *dst, const char *src, int size)
}
/*
* This function simply returns a statically allocated string containing
* This function simply returns a locally allocated string containing
* the ascii representation for number 'n' in decimal.
*/
char *ultoa(unsigned long n)
const char *ultoa_r(unsigned long n, char *buffer, int size)
{
char *pos;
pos = itoa_str + sizeof(itoa_str) - 1;
pos = buffer + size - 1;
*pos-- = '\0';
do {
*pos-- = '0' + n % 10;
n /= 10;
} while (n && pos >= itoa_str);
} while (n && pos >= buffer);
return pos + 1;
}