From: Willy Tarreau Date: Sun, 29 Mar 2009 11:41:58 +0000 (+0200) Subject: [MINOR] implement ulltoh() to write HTML-formatted numbers X-Git-Tag: v1.3.17~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e7239b51520552bd3746146602bc81170195c049;p=thirdparty%2Fhaproxy.git [MINOR] implement ulltoh() to write HTML-formatted numbers This function sets CSS letter spacing after each 3rd digit. The page must create a class "rls" (right letter spacing) with style "letter-spacing: 0.3em" in order to use it. --- diff --git a/include/common/standard.h b/include/common/standard.h index 84f768cbbe..6e7e9634a9 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -78,8 +78,9 @@ extern int strlcpy2(char *dst, const char *src, int size); * This function simply returns a locally allocated string containing * the ascii representation for number 'n' in decimal. */ -extern char itoa_str[][21]; +extern char itoa_str[][171]; extern const char *ultoa_r(unsigned long n, char *buffer, int size); +extern const char *ulltoh_r(unsigned long 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])); @@ -99,6 +100,18 @@ static inline const char *ultoa(unsigned long n) #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])); }) +/* The same macros provide HTML encoding of numbers */ +#define U2H0(n) ({ ulltoh_r((n), itoa_str[0], sizeof(itoa_str[0])); }) +#define U2H1(n) ({ ulltoh_r((n), itoa_str[1], sizeof(itoa_str[1])); }) +#define U2H2(n) ({ ulltoh_r((n), itoa_str[2], sizeof(itoa_str[2])); }) +#define U2H3(n) ({ ulltoh_r((n), itoa_str[3], sizeof(itoa_str[3])); }) +#define U2H4(n) ({ ulltoh_r((n), itoa_str[4], sizeof(itoa_str[4])); }) +#define U2H5(n) ({ ulltoh_r((n), itoa_str[5], sizeof(itoa_str[5])); }) +#define U2H6(n) ({ ulltoh_r((n), itoa_str[6], sizeof(itoa_str[6])); }) +#define U2H7(n) ({ ulltoh_r((n), itoa_str[7], sizeof(itoa_str[7])); }) +#define U2H8(n) ({ ulltoh_r((n), itoa_str[8], sizeof(itoa_str[8])); }) +#define U2H9(n) ({ ulltoh_r((n), itoa_str[9], sizeof(itoa_str[9])); }) + /* * 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 diff --git a/src/standard.c b/src/standard.c index a85552866c..246561977b 100644 --- a/src/standard.c +++ b/src/standard.c @@ -26,8 +26,12 @@ /* enough to store 10 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 */ -char itoa_str[10][21]; +char itoa_str[10][171]; /* * copies at most chars from to . Last char is always @@ -66,6 +70,38 @@ const char *ultoa_r(unsigned long n, char *buffer, int size) return pos + 1; } +/* + * 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