From ba3c63160dede86d280eff1050e9b7b91a293140 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 2 Dec 2018 20:32:04 +0100 Subject: [PATCH] fixing itoa on unix --- src/util/strings.c | 20 ++++++++------------ src/util/strings.h | 1 - src/util/strings.test.c | 9 +++++++++ 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/util/strings.c b/src/util/strings.c index 23947330b..7f6cad26c 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -18,7 +18,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #ifdef _MSC_VER #include -#define HAVE__ITOA +#undef HAVE__ITOA #endif #include "strings.h" @@ -35,27 +35,23 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #endif -const char* str_itoab(int val, int base) +const char* str_itoa_r(int val, char *buf) { - static char buf[32] = { 0 }; -#ifdef HAVE__ITOAB - return _itoa(val, buf, base); +#ifdef HAVE__ITOA + return _itoa(val, buf, 10); #else - int i = 30; - for (; val && i; --i, val /= base) { - buf[i] = "0123456789abcdefghijklmnopqrstuvwxyz"[val % base]; - } - return &buf[i + 1]; + snprintf(buf, 12, "%d", val); + return buf; #endif } const char *str_itoa(int n) { + static char buf[12]; #ifdef HAVE__ITOA - static char buf[32] = { 0 }; return _itoa(n, buf, 10); #else - return str_itoab(n, 10); + return str_itoa_r(n, buf); #endif } diff --git a/src/util/strings.h b/src/util/strings.h index 5308886dd..a381b56b9 100644 --- a/src/util/strings.h +++ b/src/util/strings.h @@ -29,7 +29,6 @@ extern "C" { void str_replace(char *buffer, size_t size, const char *tmpl, const char *var, const char *value); int str_hash(const char *s); const char *str_itoa(int i); - const char *str_itoab(int i, int base); size_t str_slprintf(char * dst, size_t size, const char * format, ...); size_t str_strlcpy(char *dst, const char *src, size_t len); size_t str_strlcat(char *dst, const char *src, size_t len); diff --git a/src/util/strings.test.c b/src/util/strings.test.c index 506ce8c12..ac05b91e9 100644 --- a/src/util/strings.test.c +++ b/src/util/strings.test.c @@ -133,6 +133,14 @@ static void test_str_strlcpy(CuTest * tc) errno = 0; } +static void test_str_itoa(CuTest * tc) +{ + CuAssertStrEquals(tc, "1234", str_itoa(1234)); + CuAssertStrEquals(tc, "0", str_itoa(0)); + CuAssertStrEquals(tc, "1234567890", str_itoa(1234567890)); + CuAssertStrEquals(tc, "-1234567890", str_itoa(-1234567890)); +} + static void test_sbstring(CuTest * tc) { char buffer[16]; @@ -274,6 +282,7 @@ CuSuite *get_strings_suite(void) SUITE_ADD_TEST(suite, test_str_slprintf); SUITE_ADD_TEST(suite, test_str_strlcat); SUITE_ADD_TEST(suite, test_str_strlcpy); + SUITE_ADD_TEST(suite, test_str_itoa); SUITE_ADD_TEST(suite, test_sbstring); SUITE_ADD_TEST(suite, test_sbs_strcat); SUITE_ADD_TEST(suite, test_sbs_substr);