From eb670e26a58285b1884ff390fa8e7bf73b70991f Mon Sep 17 00:00:00 2001 From: Marco Cetica Date: Thu, 26 Feb 2026 09:36:46 +0100 Subject: [PATCH] Improved `bigint_printf` method --- src/bigint.c | 56 +++++++++++++++++++++++++--------------------------- src/map.c | 2 -- src/vector.c | 1 - 3 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/bigint.c b/src/bigint.c index 2e4b7d3..b37f0cd 100644 --- a/src/bigint.c +++ b/src/bigint.c @@ -23,7 +23,6 @@ #include "bigint.h" #include "vector.h" -// Internal methods /** * bigint_trim_zeros * @number: a non-null big integer @@ -1673,8 +1672,6 @@ bigint_result_t bigint_prod(const bigint_t *x, const bigint_t *y) { return result; } - - /** * bigint_divmod * @x: a valid non-null big integer @@ -1886,40 +1883,37 @@ bigint_result_t bigint_printf(const char *format, ...) { // Process string char by char for (const char *p = format; *p != '\0'; p++) { - if (*p == '%' && *(p + 1) == 'B') { - // Process a big number - bigint_t *num = va_arg(args, bigint_t*); - if (num == NULL) { - printf(""); - } else { - bigint_result_t num_str_res = bigint_to_string(num); - if (num_str_res.status != BIGINT_OK) { - va_end(args); - return num_str_res; - } - - char* const number_str = num_str_res.value.string_num; - printf("%s", number_str); - free(number_str); - } + if (*p == '%' && *(p + 1) != '%') { p++; - } else if (*p == '%' && *(p + 1) != '%') { - // Handle common printf placeholders - p++; - char placeholder = *p; + const char placeholder = *p; switch (placeholder) { + case 'B': { + bigint_t *num = va_arg(args, bigint_t*); + if (num == NULL) { + for (const char *s = ""; *s != '\0'; s++) { putchar(*s); } + } else { + bigint_result_t num_str_res = bigint_to_string(num); + if (num_str_res.status != BIGINT_OK) { + va_end(args); + return num_str_res; + } + + char *number_str = num_str_res.value.string_num; + for (const char *s = number_str; *s != '\0'; s++) { putchar(*s); } + free(number_str); + } + break; + } case 'd': case 'i': { int val = va_arg(args, int); printf("%d", val); - break; } case 'u': { unsigned int val = va_arg(args, unsigned int); printf("%u", val); - break; } case 'l': { @@ -1939,13 +1933,17 @@ bigint_result_t bigint_printf(const char *format, ...) { break; } case 's': { - char *val = va_arg(args, char*); - printf("%s", val ? val : ""); + char* val = va_arg(args, char*); + if (val) { + for (const char *s = val; *s != '\0'; s++) { putchar(*s); } + } else { + for (const char *s = ""; *s != '\0'; s++) { putchar(*s); } + } break; } case 'c': { int val = va_arg(args, int); - printf("%c", val); + putchar(val); break; } case 'f': { @@ -1954,7 +1952,7 @@ bigint_result_t bigint_printf(const char *format, ...) { break; } case 'p': { - void *val = va_arg(args, void*); + void* const val = va_arg(args, void*); printf("%p", val); break; } diff --git a/src/map.c b/src/map.c index 7576a5b..7c20b5c 100644 --- a/src/map.c +++ b/src/map.c @@ -10,8 +10,6 @@ #include "map.h" -// Internal methods - /** * hash_key * @key: The input string for the hash function diff --git a/src/vector.c b/src/vector.c index acb2807..3effebe 100644 --- a/src/vector.c +++ b/src/vector.c @@ -9,7 +9,6 @@ #include "vector.h" -// Internal methods /** * vector_resize * @vector: a non-null vector