]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] implement ulltoh() to write HTML-formatted numbers
authorWilly Tarreau <w@1wt.eu>
Sun, 29 Mar 2009 11:41:58 +0000 (13:41 +0200)
committerWilly Tarreau <w@1wt.eu>
Sun, 29 Mar 2009 11:41:58 +0000 (13:41 +0200)
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.

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

index 84f768cbbe7f613ac5d807da7dfaa6d92cef9580..6e7e9634a9bdcc9110311b53f03361d9ab553ad7 100644 (file)
@@ -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
index a85552866c8035c5ad5dbf78a025627caaa2f8b8..246561977bff630b40e6bcc8d9ef790494674fc7 100644 (file)
 /* enough to store 10 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
  */
-char itoa_str[10][21];
+char itoa_str[10][171];
 
 /*
  * copies at most <size-1> chars from <src> to <dst>. 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, "</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