From 3e829e28801f5ff492453410687926d56967d121 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 29 Sep 2014 18:32:12 +0200 Subject: [PATCH] support for itoab with smaller bases and larger numbers. also a missing English message (museum-related) --- res/core/en/strings.xml | 2 +- src/kernel/messages.c | 2 +- src/util/base36.c | 35 ++++++++++++++++++++++++----------- src/util/base36.test.c | 2 ++ 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/res/core/en/strings.xml b/res/core/en/strings.xml index e21954ebe..a85bb5a4f 100644 --- a/res/core/en/strings.xml +++ b/res/core/en/strings.xml @@ -177,7 +177,7 @@ Wall - a solid wall + a solid wall the %s diff --git a/src/kernel/messages.c b/src/kernel/messages.c index 9adb32abe..086ec2134 100644 --- a/src/kernel/messages.c +++ b/src/kernel/messages.c @@ -69,7 +69,7 @@ arg_set(variant args[], const message_type * mtype, const char *buffer, if (i != mtype->nparameters) { args[i] = v; } else { - fprintf(stderr, "invalid parameter %s for message type %s\n", buffer, + log_error("invalid parameter %s for message type %s\n", buffer, mtype->name); assert(!"program aborted."); } diff --git a/src/util/base36.c b/src/util/base36.c index 8407c3fc4..58e521a78 100644 --- a/src/util/base36.c +++ b/src/util/base36.c @@ -18,6 +18,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include "base36.h" +#include "log.h" #include #include @@ -56,6 +57,7 @@ int atoi36(const char *str) const char *itoab(int i, int base) { + const int maxlen = 20; static char **as = NULL; // FIXME: static return value char *s, *dst; static int index = 0; /* STATIC_XCALL: used across calls */ @@ -63,34 +65,45 @@ const char *itoab(int i, int base) if (!as) { int j; - char *x = (char *)calloc(sizeof(char), 8 * 4); /* STATIC_LEAK: malloc in static variable */ + char *x = (char *)calloc(sizeof(char), maxlen * 4); /* STATIC_LEAK: malloc in static variable */ as = (char **)calloc(sizeof(char *), 4); for (j = 0; j != 4; ++j) - as[j] = x + j * 8; + as[j] = x + j * maxlen; } s = as[index]; index = (index + 1) & 3; /* quick for % 4 */ - dst = s + 7; + dst = s + maxlen - 1; (*dst--) = 0; if (i != 0) { if (i < 0) { i = -i; neg = 1; } - while (i) { + while (i && dst>=s) { int x = i % base; i = i / base; - if (x < 10) + if (x < 10) { *(dst--) = (char)('0' + x); - else if ('a' + x - 10 == 'l') + } + else if ('a' + x - 10 == 'l') { *(dst--) = 'L'; - else + } + else { *(dst--) = (char)('a' + (x - 10)); + } + } + if (dst > s) { + if (neg) { + *(dst) = '-'; + } + else { + ++dst; + } + } + else { + log_error("static buffer exhauset, itoab(%d, %d)", i, base); + assert(i == 0 || !"itoab: static buffer exhausted"); } - if (neg) - *(dst) = '-'; - else - ++dst; } else *dst = '0'; diff --git a/src/util/base36.test.c b/src/util/base36.test.c index 3d19f94df..b2e6fcbac 100644 --- a/src/util/base36.test.c +++ b/src/util/base36.test.c @@ -17,6 +17,8 @@ static void test_itoa36(CuTest * tc) { CuAssertStrEquals(tc, itoa36(0), "0"); CuAssertStrEquals(tc, itoa10(INT_MAX), "2147483647"); + CuAssertStrEquals(tc, itoab(INT_MAX, 8), "17777777777"); + CuAssertStrEquals(tc, itoab(INT_MAX, 4), "1333333333333333"); CuAssertStrEquals(tc, itoab(-1, 5), "-1"); CuAssertStrEquals(tc, itoa36(-1), "-1"); CuAssertStrEquals(tc, itoa36(-10), "-a");