--- /dev/null
+/* 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 <ret> with only the highest bit set. <ret> 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 <ret>, with all bits set */
+#define MAX_RANGE(ret) (~(typeof(ret))0)
+
+
+#endif /* _INTOPS_T_H_ */
#define _HAPROXY_INTOPS_H
#include <haproxy/api.h>
+#include <haproxy/intops-t.h>
/* if a > max, then bound <a> to <max>. The macro returns the new <a> */
#define UBOUND(a, max) ({ typeof(a) b = (max); if ((a) > b) (a) = b; (a); })
#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 <i> down to the closest value having max 2 digits */
unsigned int round_2dig(unsigned int i);
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.
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 */
/*
#include <netinet/in.h>
#include <import/cebtree.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
-
/* 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 <ret> with only the highest bit set. <ret> 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 <ret>, 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
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);
*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
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
* SPDX-License-Identifier: GPL-2.0-or-later.
*/
+#include <string.h>
#include <haproxy/intops.h>
+/* 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
+ * '<span class="rls"></span>' 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 <i> down to the closest value having max 2 digits */
unsigned int round_2dig(unsigned int i)
{
*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, "</span>", 7);
+
+ if (start >= buffer + 1) {
+ *--start = '0' + n % 10;
+ n /= 10;
+ }
+
+ if (digit == 3 && start >= buffer + 18)
+ memcpy(start -= 18, "<span class=\"rls\">", 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 : "");
+}
/* 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
- * '<span class="rls"></span>' 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.
*/
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 <size-1> chars from <src> to <dst>. Last char is always
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, "</span>", 7);
-
- if (start >= buffer + 1) {
- *--start = '0' + n % 10;
- n /= 10;
- }
-
- if (digit == 3 && start >= buffer + 18)
- memcpy(start -= 18, "<span class=\"rls\">", 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 <buffer> of length <len>, and the