From d90f0e1cbee25a1167fb4b691bc851b8622924cf Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 2 Apr 2024 12:55:42 +0200 Subject: [PATCH] Add formatting binary ``%*_b`` --- lib/default/Ext-printf/src/ext_printf.cpp | 35 ++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/default/Ext-printf/src/ext_printf.cpp b/lib/default/Ext-printf/src/ext_printf.cpp index df9204e3e..10aee5e61 100644 --- a/lib/default/Ext-printf/src/ext_printf.cpp +++ b/lib/default/Ext-printf/src/ext_printf.cpp @@ -157,6 +157,26 @@ char * U64toHex(uint64_t value, char *str) { } */ +char * ToBinary(uint32_t value, char *str, uint32_t zeroleads) { + str[32] = 0; // end of string + for (uint32_t i=0; i<32; i++) { // 32 digits in uint32_t + str[31 - i] = (char)(value & 1)+'0'; + value = value >> 1; + } + if (zeroleads < 32) { + uint32_t max_zeroes = 32 - zeroleads; + while (max_zeroes) { + if (str[0] == '0') { + memmove(str, str +1, strlen(str)); + } else { + break; + } + max_zeroes--; + } + } + return str; +} + char * U64toHex(uint64_t value, char *str, uint32_t zeroleads) { // str must be at least 17 bytes long str[16] = 0; // end of string @@ -235,7 +255,7 @@ int32_t ext_vsnprintf_P(char * out_buf, size_t buf_len, const char * fmt_P, va_l const uint32_t ALLOC_SIZE = 12; static const char * allocs[ALLOC_SIZE] = {}; // initialized to zeroes uint32_t alloc_idx = 0; - static char hex[20]; // buffer used for 64 bits, favor RAM instead of stack to remove pressure + static char hex[34]; // buffer used for 64 bits, favor RAM instead of stack to remove pressure for (; *fmt != 0; ++fmt) { int32_t decimals = -2; // default to 2 decimals and remove trailing zeros @@ -306,6 +326,19 @@ int32_t ext_vsnprintf_P(char * out_buf, size_t buf_len, const char * fmt_P, va_l } } break; + // '%_b' outputs an unsigned int to binary + case 'b': // Binary, decimals indicates the zero prefill + { + if (cur_val < min_valid_ptr) { new_val_str = ext_invalid_mem; } + else { + if ((decimals < 0) || (decimals > 32)) { decimals = 1; } + ToBinary(*(uint32_t*)cur_val, hex, decimals); + new_val_str = copyStr(hex); + if (new_val_str == nullptr) { goto free_allocs; } + allocs[alloc_idx++] = new_val_str; + } + } + break; /* case 'V': // 2-byte values, decimals indicates the length, default 2 {