aboutsummaryrefslogtreecommitdiffstats
path: root/src/libutil/printf.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@rspamd.com>2022-05-15 13:08:33 +0100
committerVsevolod Stakhov <vsevolod@rspamd.com>2022-05-15 13:08:33 +0100
commit66d6a41bd3992550dc5f7f2701988db418bab40b (patch)
treef0bf403ce9134161985b3a37fbe2a30be7f150ba /src/libutil/printf.c
parent8eeeed0da6862294f02992bad4769c7ef628029f (diff)
downloadrspamd-66d6a41bd3992550dc5f7f2701988db418bab40b.tar.gz
rspamd-66d6a41bd3992550dc5f7f2701988db418bab40b.zip
[Minor] Implement bits printing for numbers
Diffstat (limited to 'src/libutil/printf.c')
-rw-r--r--src/libutil/printf.c22
1 files changed, 16 insertions, 6 deletions
diff --git a/src/libutil/printf.c b/src/libutil/printf.c
index ed15d1389..8eaab0c40 100644
--- a/src/libutil/printf.c
+++ b/src/libutil/printf.c
@@ -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);