From: Willy Tarreau Date: Fri, 20 Nov 2026 10:25:29 +0000 (+0100) Subject: REORG: tools/intops: move the int-to-str conversion functions to intops as well X-Git-Url: https://git.kaiwu.me/web/%22data:,//%22%22?a=commitdiff_plain;h=e2b5b31fd4f5846f5e02da33eecf2e7bb240d01d;p=haproxy.git REORG: tools/intops: move the int-to-str conversion functions to intops as well These functions come with statically allocated thread-local arrays that are used to store the output values during conversions. But they're used just like the str-to-int ones, so let's place them here. This comes with the creation of intops-t.h for a few macro definitions used to define the array sizes. --- diff --git a/include/haproxy/intops-t.h b/include/haproxy/intops-t.h new file mode 100644 index 000000000..e66d1cbd5 --- /dev/null +++ b/include/haproxy/intops-t.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#ifndef _INTOPS_T_H_ +#define _INTOPS_T_H_ + +/* size used for max length of decimal representation of long long int. */ +#define NB_LLMAX_STR (sizeof("-9223372036854775807")-1) + +/* number of itoa_str entries */ +#define NB_ITOA_STR 16 + +/* returns 1 only if only zero or one bit is set in X, which means that X is a + * power of 2, and 0 otherwise */ +#define POWEROF2(x) (((x) & ((x)-1)) == 0) + +/* return an integer of type with only the highest bit set. may be + * both a variable or a type. + */ +#define MID_RANGE(ret) ((typeof(ret))1 << (8*sizeof(ret) - 1)) + +/* return the largest possible integer of type , with all bits set */ +#define MAX_RANGE(ret) (~(typeof(ret))0) + + +#endif /* _INTOPS_T_H_ */ diff --git a/include/haproxy/intops.h b/include/haproxy/intops.h index fb5dcfd08..269ee104b 100644 --- a/include/haproxy/intops.h +++ b/include/haproxy/intops.h @@ -23,6 +23,7 @@ #define _HAPROXY_INTOPS_H #include +#include /* if a > max, then bound to . The macro returns the new */ #define UBOUND(a, max) ({ typeof(a) b = (max); if ((a) > b) (a) = b; (a); }) @@ -32,6 +33,9 @@ #define SWAP(a, b) do { typeof(a) t; t = a; a = b; b = t; } while(0) +extern THREAD_LOCAL int itoa_idx; /* index of next itoa_str to use */ +extern THREAD_LOCAL char itoa_str[][171]; + /* exported functions, mostly integer parsing */ /* rounds down to the closest value having max 2 digits */ unsigned int round_2dig(unsigned int i); @@ -62,6 +66,15 @@ void mask_prep_rank_map(unsigned long m, unsigned long *a, unsigned long *b, unsigned long *c, unsigned long *d); int one_among_mask(unsigned long v, int bit); +char *ultoa_r(unsigned long n, char *buffer, int size); +char *lltoa_r(long long int n, char *buffer, int size); +char *sltoa_r(long n, char *buffer, int size); +const char *ulltoh_r(unsigned long long n, char *buffer, int size); +char *ulltoa(unsigned long long n, char *dst, size_t size); +char *ultoa_o(unsigned long n, char *dst, size_t size); +char *ltoa_o(long int n, char *dst, size_t size); +char *lltoa(long long n, char *dst, size_t size); +char *utoa_pad(unsigned int n, char *dst, size_t size); /* Multiply the two 32-bit operands and shift the 64-bit result right 32 bits. @@ -695,6 +708,67 @@ static inline int decode_varint(char **buf, char *end, uint64_t *i) return r; } + +/***************************************************************************** + * Here are a number of integer-to-string converters, with different types, + * ranges, signedness, paddings etc. + *****************************************************************************/ + + +static inline const char *ultoa(unsigned long n) +{ + return ultoa_r(n, itoa_str[0], sizeof(itoa_str[0])); +} + +/* + * This function simply returns a locally allocated string containing the ascii + * representation for number 'n' in decimal, unless n is 0 in which case it + * returns the alternate string (or an empty string if the alternate string is + * NULL). It use is intended for limits reported in reports, where it's + * desirable not to display anything if there is no limit. Warning! it shares + * the same vector as ultoa_r(). + */ +extern const char *limit_r(unsigned long n, char *buffer, int size, const char *alt); + +/* returns a locally allocated string containing the ASCII representation of + * the number 'n' in decimal. Up to NB_ITOA_STR calls may be used in the same + * function call (eg: printf), shared with the other similar functions making + * use of itoa_str[]. + */ +static inline const char *U2A(unsigned long n) +{ + const char *ret = ultoa_r(n, itoa_str[itoa_idx], sizeof(itoa_str[0])); + if (++itoa_idx >= NB_ITOA_STR) + itoa_idx = 0; + return ret; +} + +/* returns a locally allocated string containing the HTML representation of + * the number 'n' in decimal. Up to NB_ITOA_STR calls may be used in the same + * function call (eg: printf), shared with the other similar functions making + * use of itoa_str[]. + */ +static inline const char *U2H(unsigned long long n) +{ + const char *ret = ulltoh_r(n, itoa_str[itoa_idx], sizeof(itoa_str[0])); + if (++itoa_idx >= NB_ITOA_STR) + itoa_idx = 0; + return ret; +} + +/* returns a locally allocated string containing the ASCII representation of + * the number 'n' in decimal. Up to NB_ITOA_STR calls may be used in the same + * function call (eg: printf), shared with the other similar functions making + * use of itoa_str[]. + */ +static inline const char *LIM2A(unsigned long n, const char *alt) +{ + const char *ret = limit_r(n, itoa_str[itoa_idx], sizeof(itoa_str[0]), alt); + if (++itoa_idx >= NB_ITOA_STR) + itoa_idx = 0; + return ret; +} + #endif /* _HAPROXY_INTOPS_H */ /* diff --git a/include/haproxy/tools-t.h b/include/haproxy/tools-t.h index 00f4598c8..0dfd63536 100644 --- a/include/haproxy/tools-t.h +++ b/include/haproxy/tools-t.h @@ -25,28 +25,10 @@ #include #include -/* size used for max length of decimal representation of long long int. */ -#define NB_LLMAX_STR (sizeof("-9223372036854775807")-1) - -/* number of itoa_str entries */ -#define NB_ITOA_STR 16 - /* maximum quoted string length (truncated above) */ #define QSTR_SIZE 200 #define NB_QSTR 10 -/* returns 1 only if only zero or one bit is set in X, which means that X is a - * power of 2, and 0 otherwise */ -#define POWEROF2(x) (((x) & ((x)-1)) == 0) - -/* return an integer of type with only the highest bit set. may be - * both a variable or a type. - */ -#define MID_RANGE(ret) ((typeof(ret))1 << (8*sizeof(ret) - 1)) - -/* return the largest possible integer of type , with all bits set */ -#define MAX_RANGE(ret) (~(typeof(ret))0) - /* options flags for parse_line() */ #define PARSE_OPT_SHARP 0x00000001 // '#' ends the line #define PARSE_OPT_BKSLASH 0x00000002 // '\' escapes chars diff --git a/include/haproxy/tools.h b/include/haproxy/tools.h index b0c53c60c..888bc8be9 100644 --- a/include/haproxy/tools.h +++ b/include/haproxy/tools.h @@ -81,22 +81,8 @@ static inline size_t strnlen2(const char *s, size_t maxlen) return len; } -/* - * This function simply returns a locally allocated string containing - * the ascii representation for number 'n' in decimal. - */ -extern THREAD_LOCAL int itoa_idx; /* index of next itoa_str to use */ -extern THREAD_LOCAL char itoa_str[][171]; -extern char *ultoa_r(unsigned long n, char *buffer, int size); -extern char *lltoa_r(long long int n, char *buffer, int size); -extern char *sltoa_r(long n, char *buffer, int size); -extern const char *ulltoh_r(unsigned long long n, char *buffer, int size); size_t flt_trim(char *buffer, size_t num_start, size_t len); char *ftoa_r(double n, char *buffer, int size); -static inline const char *ultoa(unsigned long n) -{ - return ultoa_r(n, itoa_str[0], sizeof(itoa_str[0])); -} /* file names management */ const char *copy_file_name(const char *name); @@ -110,82 +96,6 @@ static inline void drop_file_name(const char **name) *name = NULL; } -/* - * unsigned long long ASCII representation - * - * return the last char '\0' or NULL if no enough - * space in dst - */ -char *ulltoa(unsigned long long n, char *dst, size_t size); - - -/* - * unsigned long ASCII representation - * - * return the last char '\0' or NULL if no enough - * space in dst - */ -char *ultoa_o(unsigned long n, char *dst, size_t size); - -/* - * signed long ASCII representation - * - * return the last char '\0' or NULL if no enough - * space in dst - */ -char *ltoa_o(long int n, char *dst, size_t size); - -/* - * signed long long ASCII representation - * - * return the last char '\0' or NULL if no enough - * space in dst - */ -char *lltoa(long long n, char *dst, size_t size); - -/* - * write a ascii representation of a unsigned into dst, - * return a pointer to the last character - * Pad the ascii representation with '0', using size. - */ -char *utoa_pad(unsigned int n, char *dst, size_t size); - -/* - * This function simply returns a locally allocated string containing the ascii - * representation for number 'n' in decimal, unless n is 0 in which case it - * returns the alternate string (or an empty string if the alternate string is - * NULL). It use is intended for limits reported in reports, where it's - * desirable not to display anything if there is no limit. Warning! it shares - * the same vector as ultoa_r(). - */ -extern const char *limit_r(unsigned long n, char *buffer, int size, const char *alt); - -/* returns a locally allocated string containing the ASCII representation of - * the number 'n' in decimal. Up to NB_ITOA_STR calls may be used in the same - * function call (eg: printf), shared with the other similar functions making - * use of itoa_str[]. - */ -static inline const char *U2A(unsigned long n) -{ - const char *ret = ultoa_r(n, itoa_str[itoa_idx], sizeof(itoa_str[0])); - if (++itoa_idx >= NB_ITOA_STR) - itoa_idx = 0; - return ret; -} - -/* returns a locally allocated string containing the HTML representation of - * the number 'n' in decimal. Up to NB_ITOA_STR calls may be used in the same - * function call (eg: printf), shared with the other similar functions making - * use of itoa_str[]. - */ -static inline const char *U2H(unsigned long long n) -{ - const char *ret = ulltoh_r(n, itoa_str[itoa_idx], sizeof(itoa_str[0])); - if (++itoa_idx >= NB_ITOA_STR) - itoa_idx = 0; - return ret; -} - /* returns a locally allocated string containing the ASCII representation of * the number 'n' in decimal. Up to NB_ITOA_STR calls may be used in the same * function call (eg: printf), shared with the other similar functions making @@ -212,19 +122,6 @@ static inline const char *F2H(double n) return ret; } -/* returns a locally allocated string containing the ASCII representation of - * the number 'n' in decimal. Up to NB_ITOA_STR calls may be used in the same - * function call (eg: printf), shared with the other similar functions making - * use of itoa_str[]. - */ -static inline const char *LIM2A(unsigned long n, const char *alt) -{ - const char *ret = limit_r(n, itoa_str[itoa_idx], sizeof(itoa_str[0]), alt); - if (++itoa_idx >= NB_ITOA_STR) - itoa_idx = 0; - return ret; -} - /* returns a locally allocated string containing the quoted encoding of the * input string. The output may be truncated to QSTR_SIZE chars, but it is * guaranteed that the string will always be properly terminated. Quotes are diff --git a/src/intops.c b/src/intops.c index 65658ac71..8ae5e3c14 100644 --- a/src/intops.c +++ b/src/intops.c @@ -4,8 +4,24 @@ * SPDX-License-Identifier: GPL-2.0-or-later. */ +#include #include +/* enough to store NB_ITOA_STR integers of : + * 2^64-1 = 18446744073709551615 or + * -2^63 = -9223372036854775808 + * + * The HTML version needs room for adding the 25 characters + * '' around digits at positions 3N+1 in order + * to add spacing at up to 6 positions : 18 446 744 073 709 551 615 + */ +THREAD_LOCAL char itoa_str[NB_ITOA_STR][171]; +THREAD_LOCAL int itoa_idx = 0; /* index of next itoa_str to use */ + +/***************************************************************************** + * misc general purpose integer processing functions + *****************************************************************************/ + /* rounds down to the closest value having max 2 digits */ unsigned int round_2dig(unsigned int i) { @@ -444,3 +460,440 @@ int strl2llrc(const char *s, int len, long long *ret) *ret = i; return 0; } + + +/***************************************************************************** + * Here are a number of integer-to-string converters, with different types, + * ranges, signedness, paddings etc. + *****************************************************************************/ + +/* + * unsigned long long ASCII representation + * + * return the last char '\0' or NULL if no enough + * space in dst + */ +char *ulltoa(unsigned long long n, char *dst, size_t size) +{ + int i = 0; + char *res; + + switch(n) { + case 1ULL ... 9ULL: + i = 0; + break; + + case 10ULL ... 99ULL: + i = 1; + break; + + case 100ULL ... 999ULL: + i = 2; + break; + + case 1000ULL ... 9999ULL: + i = 3; + break; + + case 10000ULL ... 99999ULL: + i = 4; + break; + + case 100000ULL ... 999999ULL: + i = 5; + break; + + case 1000000ULL ... 9999999ULL: + i = 6; + break; + + case 10000000ULL ... 99999999ULL: + i = 7; + break; + + case 100000000ULL ... 999999999ULL: + i = 8; + break; + + case 1000000000ULL ... 9999999999ULL: + i = 9; + break; + + case 10000000000ULL ... 99999999999ULL: + i = 10; + break; + + case 100000000000ULL ... 999999999999ULL: + i = 11; + break; + + case 1000000000000ULL ... 9999999999999ULL: + i = 12; + break; + + case 10000000000000ULL ... 99999999999999ULL: + i = 13; + break; + + case 100000000000000ULL ... 999999999999999ULL: + i = 14; + break; + + case 1000000000000000ULL ... 9999999999999999ULL: + i = 15; + break; + + case 10000000000000000ULL ... 99999999999999999ULL: + i = 16; + break; + + case 100000000000000000ULL ... 999999999999999999ULL: + i = 17; + break; + + case 1000000000000000000ULL ... 9999999999999999999ULL: + i = 18; + break; + + case 10000000000000000000ULL ... ULLONG_MAX: + i = 19; + break; + } + if (i + 2 > size) // (i + 1) + '\0' + return NULL; // too long + res = dst + i + 1; + *res = '\0'; + for (; i >= 0; i--) { + dst[i] = n % 10ULL + '0'; + n /= 10ULL; + } + return res; +} + +/* + * unsigned long ASCII representation + * + * return the last char '\0' or NULL if no enough + * space in dst + */ +char *ultoa_o(unsigned long n, char *dst, size_t size) +{ + int i = 0; + char *res; + + switch (n) { + case 0U ... 9UL: + i = 0; + break; + + case 10U ... 99UL: + i = 1; + break; + + case 100U ... 999UL: + i = 2; + break; + + case 1000U ... 9999UL: + i = 3; + break; + + case 10000U ... 99999UL: + i = 4; + break; + + case 100000U ... 999999UL: + i = 5; + break; + + case 1000000U ... 9999999UL: + i = 6; + break; + + case 10000000U ... 99999999UL: + i = 7; + break; + + case 100000000U ... 999999999UL: + i = 8; + break; +#if __WORDSIZE == 32 + + case 1000000000ULL ... ULONG_MAX: + i = 9; + break; + +#elif __WORDSIZE == 64 + + case 1000000000ULL ... 9999999999UL: + i = 9; + break; + + case 10000000000ULL ... 99999999999UL: + i = 10; + break; + + case 100000000000ULL ... 999999999999UL: + i = 11; + break; + + case 1000000000000ULL ... 9999999999999UL: + i = 12; + break; + + case 10000000000000ULL ... 99999999999999UL: + i = 13; + break; + + case 100000000000000ULL ... 999999999999999UL: + i = 14; + break; + + case 1000000000000000ULL ... 9999999999999999UL: + i = 15; + break; + + case 10000000000000000ULL ... 99999999999999999UL: + i = 16; + break; + + case 100000000000000000ULL ... 999999999999999999UL: + i = 17; + break; + + case 1000000000000000000ULL ... 9999999999999999999UL: + i = 18; + break; + + case 10000000000000000000ULL ... ULONG_MAX: + i = 19; + break; + +#endif + } + if (i + 2 > size) // (i + 1) + '\0' + return NULL; // too long + res = dst + i + 1; + *res = '\0'; + for (; i >= 0; i--) { + dst[i] = n % 10U + '0'; + n /= 10U; + } + return res; +} + +/* + * signed long ASCII representation + * + * return the last char '\0' or NULL if no enough + * space in dst + */ +char *ltoa_o(long int n, char *dst, size_t size) +{ + char *pos = dst; + + if (n < 0) { + if (size < 3) + return NULL; // min size is '-' + digit + '\0' but another test in ultoa + *pos = '-'; + pos++; + dst = ultoa_o(-n, pos, size - 1); + } else { + dst = ultoa_o(n, dst, size); + } + return dst; +} + +/* + * signed long long ASCII representation + * + * return the last char '\0' or NULL if no enough + * space in dst + */ +char *lltoa(long long n, char *dst, size_t size) +{ + char *pos = dst; + + if (n < 0) { + if (size < 3) + return NULL; // min size is '-' + digit + '\0' but another test in ulltoa + *pos = '-'; + pos++; + dst = ulltoa(-n, pos, size - 1); + } else { + dst = ulltoa(n, dst, size); + } + return dst; +} + +/* + * write a ascii representation of a unsigned into dst, + * return a pointer to the last character + * Pad the ascii representation with '0', using size. + */ +char *utoa_pad(unsigned int n, char *dst, size_t size) +{ + int i = 0; + char *ret; + + switch(n) { + case 0U ... 9U: + i = 0; + break; + + case 10U ... 99U: + i = 1; + break; + + case 100U ... 999U: + i = 2; + break; + + case 1000U ... 9999U: + i = 3; + break; + + case 10000U ... 99999U: + i = 4; + break; + + case 100000U ... 999999U: + i = 5; + break; + + case 1000000U ... 9999999U: + i = 6; + break; + + case 10000000U ... 99999999U: + i = 7; + break; + + case 100000000U ... 999999999U: + i = 8; + break; + + case 1000000000U ... 4294967295U: + i = 9; + break; + } + if (i + 2 > size) // (i + 1) + '\0' + return NULL; // too long + i = size - 2; // padding - '\0' + + ret = dst + i + 1; + *ret = '\0'; + for (; i >= 0; i--) { + dst[i] = n % 10U + '0'; + n /= 10U; + } + return ret; +} + +/* + * This function simply returns a locally allocated string containing + * the ascii representation for number 'n' in decimal. + */ +char *ultoa_r(unsigned long n, char *buffer, int size) +{ + char *pos; + + pos = buffer + size - 1; + *pos-- = '\0'; + + do { + *pos-- = '0' + n % 10; + n /= 10; + } while (n && pos >= buffer); + return pos + 1; +} + +/* + * This function simply returns a locally allocated string containing + * the ascii representation for number 'n' in decimal. + */ +char *lltoa_r(long long int in, char *buffer, int size) +{ + char *pos; + int neg = 0; + unsigned long long int n; + + pos = buffer + size - 1; + *pos-- = '\0'; + + if (in < 0) { + neg = 1; + n = -in; + } + else + n = in; + + do { + *pos-- = '0' + n % 10; + n /= 10; + } while (n && pos >= buffer); + if (neg && pos > buffer) + *pos-- = '-'; + return pos + 1; +} + +/* + * This function simply returns a locally allocated string containing + * the ascii representation for signed number 'n' in decimal. + */ +char *sltoa_r(long n, char *buffer, int size) +{ + char *pos; + + if (n >= 0) + return ultoa_r(n, buffer, size); + + pos = ultoa_r(-n, buffer + 1, size - 1) - 1; + *pos = '-'; + return pos; +} + +/* + * This function simply returns a locally allocated string containing + * the ascii representation for number 'n' in decimal, formatted for + * HTML output with tags to create visual grouping by 3 digits. The + * output needs to support at least 171 characters. + */ +const char *ulltoh_r(unsigned long long n, char *buffer, int size) +{ + char *start; + int digit = 0; + + start = buffer + size; + *--start = '\0'; + + do { + if (digit == 3 && start >= buffer + 7) + memcpy(start -= 7, "", 7); + + if (start >= buffer + 1) { + *--start = '0' + n % 10; + n /= 10; + } + + if (digit == 3 && start >= buffer + 18) + memcpy(start -= 18, "", 18); + + if (digit++ == 3) + digit = 1; + } while (n && start > buffer); + return start; +} + +/* + * This function simply returns a locally allocated string containing the ascii + * representation for number 'n' in decimal, unless n is 0 in which case it + * returns the alternate string (or an empty string if the alternate string is + * NULL). It use is intended for limits reported in reports, where it's + * desirable not to display anything if there is no limit. Warning! it shares + * the same vector as ultoa_r(). + */ +const char *limit_r(unsigned long n, char *buffer, int size, const char *alt) +{ + return (n) ? ultoa_r(n, buffer, size) : (alt ? alt : ""); +} diff --git a/src/tools.c b/src/tools.c index 8f9c0eb1c..2747c4eea 100644 --- a/src/tools.c +++ b/src/tools.c @@ -85,17 +85,6 @@ extern char **environ; /* return the hash of a string and length for a given key. All keys are valid. */ #define HA_ANON(key, str, len) (XXH32(str, len, key) & 0xFFFFFF) -/* enough to store NB_ITOA_STR integers of : - * 2^64-1 = 18446744073709551615 or - * -2^63 = -9223372036854775808 - * - * The HTML version needs room for adding the 25 characters - * '' around digits at positions 3N+1 in order - * to add spacing at up to 6 positions : 18 446 744 073 709 551 615 - */ -THREAD_LOCAL char itoa_str[NB_ITOA_STR][171]; -THREAD_LOCAL int itoa_idx = 0; /* index of next itoa_str to use */ - /* sometimes we'll need to quote strings (eg: in stats), and we don't expect * to quote strings larger than a max configuration line. */ @@ -118,328 +107,6 @@ struct { static THREAD_LOCAL char hash_word[NB_L_HASH_WORD][20]; static THREAD_LOCAL int index_hash = 0; -/* - * unsigned long long ASCII representation - * - * return the last char '\0' or NULL if no enough - * space in dst - */ -char *ulltoa(unsigned long long n, char *dst, size_t size) -{ - int i = 0; - char *res; - - switch(n) { - case 1ULL ... 9ULL: - i = 0; - break; - - case 10ULL ... 99ULL: - i = 1; - break; - - case 100ULL ... 999ULL: - i = 2; - break; - - case 1000ULL ... 9999ULL: - i = 3; - break; - - case 10000ULL ... 99999ULL: - i = 4; - break; - - case 100000ULL ... 999999ULL: - i = 5; - break; - - case 1000000ULL ... 9999999ULL: - i = 6; - break; - - case 10000000ULL ... 99999999ULL: - i = 7; - break; - - case 100000000ULL ... 999999999ULL: - i = 8; - break; - - case 1000000000ULL ... 9999999999ULL: - i = 9; - break; - - case 10000000000ULL ... 99999999999ULL: - i = 10; - break; - - case 100000000000ULL ... 999999999999ULL: - i = 11; - break; - - case 1000000000000ULL ... 9999999999999ULL: - i = 12; - break; - - case 10000000000000ULL ... 99999999999999ULL: - i = 13; - break; - - case 100000000000000ULL ... 999999999999999ULL: - i = 14; - break; - - case 1000000000000000ULL ... 9999999999999999ULL: - i = 15; - break; - - case 10000000000000000ULL ... 99999999999999999ULL: - i = 16; - break; - - case 100000000000000000ULL ... 999999999999999999ULL: - i = 17; - break; - - case 1000000000000000000ULL ... 9999999999999999999ULL: - i = 18; - break; - - case 10000000000000000000ULL ... ULLONG_MAX: - i = 19; - break; - } - if (i + 2 > size) // (i + 1) + '\0' - return NULL; // too long - res = dst + i + 1; - *res = '\0'; - for (; i >= 0; i--) { - dst[i] = n % 10ULL + '0'; - n /= 10ULL; - } - return res; -} - -/* - * unsigned long ASCII representation - * - * return the last char '\0' or NULL if no enough - * space in dst - */ -char *ultoa_o(unsigned long n, char *dst, size_t size) -{ - int i = 0; - char *res; - - switch (n) { - case 0U ... 9UL: - i = 0; - break; - - case 10U ... 99UL: - i = 1; - break; - - case 100U ... 999UL: - i = 2; - break; - - case 1000U ... 9999UL: - i = 3; - break; - - case 10000U ... 99999UL: - i = 4; - break; - - case 100000U ... 999999UL: - i = 5; - break; - - case 1000000U ... 9999999UL: - i = 6; - break; - - case 10000000U ... 99999999UL: - i = 7; - break; - - case 100000000U ... 999999999UL: - i = 8; - break; -#if __WORDSIZE == 32 - - case 1000000000ULL ... ULONG_MAX: - i = 9; - break; - -#elif __WORDSIZE == 64 - - case 1000000000ULL ... 9999999999UL: - i = 9; - break; - - case 10000000000ULL ... 99999999999UL: - i = 10; - break; - - case 100000000000ULL ... 999999999999UL: - i = 11; - break; - - case 1000000000000ULL ... 9999999999999UL: - i = 12; - break; - - case 10000000000000ULL ... 99999999999999UL: - i = 13; - break; - - case 100000000000000ULL ... 999999999999999UL: - i = 14; - break; - - case 1000000000000000ULL ... 9999999999999999UL: - i = 15; - break; - - case 10000000000000000ULL ... 99999999999999999UL: - i = 16; - break; - - case 100000000000000000ULL ... 999999999999999999UL: - i = 17; - break; - - case 1000000000000000000ULL ... 9999999999999999999UL: - i = 18; - break; - - case 10000000000000000000ULL ... ULONG_MAX: - i = 19; - break; - -#endif - } - if (i + 2 > size) // (i + 1) + '\0' - return NULL; // too long - res = dst + i + 1; - *res = '\0'; - for (; i >= 0; i--) { - dst[i] = n % 10U + '0'; - n /= 10U; - } - return res; -} - -/* - * signed long ASCII representation - * - * return the last char '\0' or NULL if no enough - * space in dst - */ -char *ltoa_o(long int n, char *dst, size_t size) -{ - char *pos = dst; - - if (n < 0) { - if (size < 3) - return NULL; // min size is '-' + digit + '\0' but another test in ultoa - *pos = '-'; - pos++; - dst = ultoa_o(-n, pos, size - 1); - } else { - dst = ultoa_o(n, dst, size); - } - return dst; -} - -/* - * signed long long ASCII representation - * - * return the last char '\0' or NULL if no enough - * space in dst - */ -char *lltoa(long long n, char *dst, size_t size) -{ - char *pos = dst; - - if (n < 0) { - if (size < 3) - return NULL; // min size is '-' + digit + '\0' but another test in ulltoa - *pos = '-'; - pos++; - dst = ulltoa(-n, pos, size - 1); - } else { - dst = ulltoa(n, dst, size); - } - return dst; -} - -/* - * write a ascii representation of a unsigned into dst, - * return a pointer to the last character - * Pad the ascii representation with '0', using size. - */ -char *utoa_pad(unsigned int n, char *dst, size_t size) -{ - int i = 0; - char *ret; - - switch(n) { - case 0U ... 9U: - i = 0; - break; - - case 10U ... 99U: - i = 1; - break; - - case 100U ... 999U: - i = 2; - break; - - case 1000U ... 9999U: - i = 3; - break; - - case 10000U ... 99999U: - i = 4; - break; - - case 100000U ... 999999U: - i = 5; - break; - - case 1000000U ... 9999999U: - i = 6; - break; - - case 10000000U ... 99999999U: - i = 7; - break; - - case 100000000U ... 999999999U: - i = 8; - break; - - case 1000000000U ... 4294967295U: - i = 9; - break; - } - if (i + 2 > size) // (i + 1) + '\0' - return NULL; // too long - i = size - 2; // padding - '\0' - - ret = dst + i + 1; - *ret = '\0'; - for (; i >= 0; i--) { - dst[i] = n % 10U + '0'; - n /= 10U; - } - return ret; -} /* * copies at most chars from to . Last char is always @@ -460,114 +127,6 @@ int strlcpy2(char *dst, const char *src, int size) return dst - orig; } -/* - * This function simply returns a locally allocated string containing - * the ascii representation for number 'n' in decimal. - */ -char *ultoa_r(unsigned long n, char *buffer, int size) -{ - char *pos; - - pos = buffer + size - 1; - *pos-- = '\0'; - - do { - *pos-- = '0' + n % 10; - n /= 10; - } while (n && pos >= buffer); - return pos + 1; -} - -/* - * This function simply returns a locally allocated string containing - * the ascii representation for number 'n' in decimal. - */ -char *lltoa_r(long long int in, char *buffer, int size) -{ - char *pos; - int neg = 0; - unsigned long long int n; - - pos = buffer + size - 1; - *pos-- = '\0'; - - if (in < 0) { - neg = 1; - n = -in; - } - else - n = in; - - do { - *pos-- = '0' + n % 10; - n /= 10; - } while (n && pos >= buffer); - if (neg && pos > buffer) - *pos-- = '-'; - return pos + 1; -} - -/* - * This function simply returns a locally allocated string containing - * the ascii representation for signed number 'n' in decimal. - */ -char *sltoa_r(long n, char *buffer, int size) -{ - char *pos; - - if (n >= 0) - return ultoa_r(n, buffer, size); - - pos = ultoa_r(-n, buffer + 1, size - 1) - 1; - *pos = '-'; - return pos; -} - -/* - * This function simply returns a locally allocated string containing - * the ascii representation for number 'n' in decimal, formatted for - * HTML output with tags to create visual grouping by 3 digits. The - * output needs to support at least 171 characters. - */ -const char *ulltoh_r(unsigned long long n, char *buffer, int size) -{ - char *start; - int digit = 0; - - start = buffer + size; - *--start = '\0'; - - do { - if (digit == 3 && start >= buffer + 7) - memcpy(start -= 7, "", 7); - - if (start >= buffer + 1) { - *--start = '0' + n % 10; - n /= 10; - } - - if (digit == 3 && start >= buffer + 18) - memcpy(start -= 18, "", 18); - - if (digit++ == 3) - digit = 1; - } while (n && start > buffer); - return start; -} - -/* - * This function simply returns a locally allocated string containing the ascii - * representation for number 'n' in decimal, unless n is 0 in which case it - * returns the alternate string (or an empty string if the alternate string is - * NULL). It use is intended for limits reported in reports, where it's - * desirable not to display anything if there is no limit. Warning! it shares - * the same vector as ultoa_r(). - */ -const char *limit_r(unsigned long n, char *buffer, int size, const char *alt) -{ - return (n) ? ultoa_r(n, buffer, size) : (alt ? alt : ""); -} - /* Trims the first "%f" float in a string to its minimum number of digits after * the decimal point by trimming trailing zeroes, even dropping the decimal * point if not needed. The string is in of length , and the