From ebec133018a247c4c42121ca74889e443eef8d8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Mon, 21 Nov 2022 00:36:16 +0200 Subject: [PATCH] Add baseconv_digits to the API --- baseconv.c | 25 +++++++++++++++++++++---- baseconv.h | 1 + 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/baseconv.c b/baseconv.c index 5ec5e3d..62cd02e 100644 --- a/baseconv.c +++ b/baseconv.c @@ -3,13 +3,16 @@ #include #include #include +#include -static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; - -bool baseconv(char outbuf[], size_t bufsize, unsigned base, uintmax_t num) { +static bool baseconv_internal( + char outbuf[], size_t bufsize, + const char *digits, unsigned base, + uintmax_t num +) { // Supported bases are 2 to 36 inclusive (though it could be higher // if our digits string was longer - if (base < 2 || base > sizeof(digits)) + if (base < 2 || base > strlen(digits)) return false; // Longest possible converted string is base-2, where we produce one @@ -45,3 +48,17 @@ bool baseconv(char outbuf[], size_t bufsize, unsigned base, uintmax_t num) { return true; } + +bool baseconv(char outbuf[], size_t bufsize, unsigned base, uintmax_t num) { + const char *digits = "0123456789abcdefghijklmnopqrstuvwxyz"; + return baseconv_internal(outbuf, bufsize, digits, base, num); +} + +bool baseconv_digits( + char outbuf[], size_t bufsize, + const char *digits, + uintmax_t num +) { + size_t base = strlen(digits); + return baseconv_internal(outbuf, bufsize, digits, base, num); +} diff --git a/baseconv.h b/baseconv.h index 72f5df1..4c4bafa 100644 --- a/baseconv.h +++ b/baseconv.h @@ -1 +1,2 @@ bool baseconv(char outbuf[], size_t bufsize, unsigned base, uintmax_t num); +bool baseconv_digits(char outbuf[], size_t bufsize, const char *digits, uintmax_t num);