]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] provide easier-to-use ultoa_* functions
authorWilly Tarreau <w@1wt.eu>
Thu, 25 Oct 2007 10:14:10 +0000 (12:14 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 25 Oct 2007 14:59:40 +0000 (16:59 +0200)
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.

include/common/standard.h
src/standard.c

index d8b56b68da73cf459d43f5852fca37adedaec965..4808a228590420181729d55679a9180f85d409c8 100644 (file)
 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.
index 80e0d1b4a384f3d27b40ea329259c129b4bf2348..fff7168ca66777cb0fb5b55c17d5c7788da7991f 100644 (file)
 #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;
 }