]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Implement bits printing for numbers
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sun, 15 May 2022 12:08:33 +0000 (13:08 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sun, 15 May 2022 12:08:33 +0000 (13:08 +0100)
src/libutil/printf.c
src/libutil/printf.h

index ed15d1389b03de1ae8f43d593d7276fffb65e453..8eaab0c4021fc874cbe4ac86decc27451b20b98b 100644 (file)
@@ -351,12 +351,12 @@ rspamd_uint64_print (guint64 in, gchar *out)
 
 static gchar *
 rspamd_sprintf_num (gchar *buf, gchar *last, guint64 ui64, gchar zero,
-                                         guint hexadecimal, guint width)
+                                         guint hexadecimal, guint binary, guint width)
 {
-       gchar *p, temp[sizeof ("18446744073709551615")];
-       size_t len;
+       gchar *p, temp[64];
+       size_t len = 0;
 
-       if (hexadecimal == 0) {
+       if (G_LIKELY(hexadecimal == 0 && binary == 0)) {
                p = temp;
 
                if (ui64 < G_MAXUINT32) {
@@ -374,7 +374,7 @@ rspamd_sprintf_num (gchar *buf, gchar *last, guint64 ui64, gchar zero,
 
                len = (temp + sizeof (temp)) - p;
        }
-       else { /* hexadecimal == 2 */
+       else if (hexadecimal == 2) { /* hexadecimal == 2 */
                p = temp + sizeof(temp);
                do {
                        *--p = _HEX[(guint32) (ui64 & 0xf)];
@@ -382,6 +382,16 @@ rspamd_sprintf_num (gchar *buf, gchar *last, guint64 ui64, gchar zero,
 
                len = (temp + sizeof (temp)) - p;
        }
+       else if (binary > 0) {
+               int first_bit = MIN(sizeof(temp), ffsll(ui64));
+
+               p = temp + sizeof(temp);
+               for (int i = 0; i <= first_bit; i ++, ui64 >>= 1) {
+                       *--p = '0' + (ui64 & 0x1);
+               }
+
+               len = (temp + sizeof (temp)) - p;
+       }
 
        /* zero or space padding */
 
@@ -1040,7 +1050,7 @@ rspamd_vprintf_common (rspamd_printf_append_func func,
                        }
 
                        if (!humanize) {
-                               p = rspamd_sprintf_num (p, last, ui64, zero, hex, width);
+                               p = rspamd_sprintf_num (p, last, ui64, zero, hex, b64 + b32, width);
                        }
                        else {
                                p = rspamd_humanize_number (p, last, ui64, bytes);
index 008e082451eedfac42c11392d7c3f7deeab46bbf..ee0dd52155083970cf72aea3db0fab1dfb5ee4de 100644 (file)
@@ -26,11 +26,11 @@ extern "C" {
  * supported formats:
  *     %[0][width][x][X]O                  off_t
  *     %[0][width]T                        time_t
- *     %[0][width][u][x|X|h|H]z            ssize_t/size_t
- *     %[0][width][u][x|X|h|H]d            gint/guint
- *     %[0][width][u][x|X|h|H]l            long
- *     %[0][width][u][x|X|h|H]D            gint32/guint32
- *     %[0][width][u][x|X|h|H]L            gint64/guint64
+ *     %[0][width][u][x|X|h|H|b|B]z        ssize_t/size_t
+ *     %[0][width][u][x|X|h|H|b|B]d        gint/guint
+ *     %[0][width][u][x|X|h|H|b|B]l        long
+ *     %[0][width][u][x|X|h|H|b|B]D        gint32/guint32
+ *     %[0][width][u][x|X|h|H|b|B]L        gint64/guint64
  *     %[0][width][.width]f        double
  *     %[0][width][.width]F        long double
  *     %[0][width][.width]g        double