commit 66d6a41: [Minor] Implement bits printing for numbers
Vsevolod Stakhov
vsevolod at rspamd.com
Sun May 15 12:14:04 UTC 2022
Author: Vsevolod Stakhov
Date: 2022-05-15 13:08:33 +0100
URL: https://github.com/rspamd/rspamd/commit/66d6a41bd3992550dc5f7f2701988db418bab40b
[Minor] Implement bits printing for numbers
---
src/libutil/printf.c | 22 ++++++++++++++++------
src/libutil/printf.h | 10 +++++-----
2 files changed, 21 insertions(+), 11 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);
diff --git a/src/libutil/printf.h b/src/libutil/printf.h
index 008e08245..ee0dd5215 100644
--- a/src/libutil/printf.h
+++ b/src/libutil/printf.h
@@ -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
More information about the Commits
mailing list