diff --git a/CMakeLists.txt b/CMakeLists.txt index aebc264..095987a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -207,7 +207,6 @@ set(unit_test_files tests/unit_ftoa_rev_16.cc tests/unit_ftoa_rev_32.cc tests/unit_ftoa_rev_64.cc - tests/unit_itoa_rev.cc tests/unit_utoa_rev.cc tests/unit_snprintf.cc tests/unit_snprintf_safe_empty.cc diff --git a/nanoprintf.h b/nanoprintf.h index e9fc971..bb7dd94 100644 --- a/nanoprintf.h +++ b/nanoprintf.h @@ -273,7 +273,6 @@ typedef struct npf_bufputc_ctx { static int npf_parse_format_spec(char const *format, npf_format_spec_t *out_spec); static void npf_bufputc(int c, void *ctx); static void npf_bufputc_nop(int c, void *ctx); -static int npf_itoa_rev(char *buf, npf_int_t i); static int npf_utoa_rev(char *buf, npf_uint_t i, unsigned base, unsigned case_adjust); #if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1 @@ -479,13 +478,6 @@ int npf_parse_format_spec(char const *format, npf_format_spec_t *out_spec) { return (int)(cur - format); } -int npf_itoa_rev(char *buf, npf_int_t i) { - int n = 0; - int const sign = (i >= 0) ? 1 : -1; - do { *buf++ = (char)('0' + (sign * (i % 10))); i /= 10; ++n; } while (i); - return n; -} - int npf_utoa_rev(char *buf, npf_uint_t i, unsigned base, unsigned case_adj) { int n = 0; do { @@ -540,7 +532,7 @@ enum { which are mathematically exact and fast, but require large lookup tables. This implementation was inspired by Wojciech Muła's (zdjęcia@garnek.pl) - algorithm (http://0x80.pl/notesen/2015-12-29-float-to-string.html) and + algorithm (http://0x80.pl/notesen/2015-12-29-float-to-string.html) and extended further by adding dynamic scaling and configurable integer width by Oskars Rubenis (https://github.com/Okarss). */ @@ -851,7 +843,11 @@ int npf_vpprintf(npf_putc pc, void *pc_ctx, char const *format, va_list args) { cbuf_len = 0; } else #endif - { cbuf_len = npf_itoa_rev(cbuf, val); } + { + npf_uint_t uval = (npf_uint_t)val; + if (val < 0) { uval = 0 - uval; } + cbuf_len = npf_utoa_rev(cbuf, uval, 10, (unsigned)fs.case_adjust); + } } break; #if NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS == 1 diff --git a/tests/unit_itoa_rev.cc b/tests/unit_itoa_rev.cc deleted file mode 100644 index 76e0974..0000000 --- a/tests/unit_itoa_rev.cc +++ /dev/null @@ -1,69 +0,0 @@ -#include "unit_nanoprintf.h" - -#include -#include -#include - -#if NANOPRINTF_HAVE_GCC_WARNING_PRAGMAS - #pragma GCC diagnostic push - #if NANOPRINTF_CLANG - #pragma GCC diagnostic ignored "-Wformat-pedantic" - #pragma GCC diagnostic ignored "-Wmissing-prototypes" - #pragma GCC diagnostic ignored "-Wold-style-cast" - #endif -#endif - -void require_npf_itoa(char const *expected, npf_int_t val) { - char buf[64]; - int const n = npf_itoa_rev(buf, val); - buf[n] = '\0'; - REQUIRE(n == (int)strlen(expected)); - REQUIRE(std::string{buf} == std::string{expected}); -} - -TEST_CASE("npf_itoa_rev") { - require_npf_itoa("0", 0); - require_npf_itoa("1", 1); - require_npf_itoa("9", 9); - require_npf_itoa("01", 10); - require_npf_itoa("24", 42); - require_npf_itoa("99", 99); - require_npf_itoa("001", 100); - require_npf_itoa("321", 123); - require_npf_itoa("999", 999); - require_npf_itoa("0123456", 6543210); - - SUBCASE("max values") { -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 -#if INTMAX_MAX == 9223372036854775807ll - require_npf_itoa("7085774586302733229", INTMAX_MAX); -#else -#error Unknown INTMAX_MAX here, please add another branch. -#endif -#else -#if INT_MAX == 2147483647 - require_npf_itoa("7463847412", INT_MAX); -#else -#error Unknown INT_MAX here, please add another branch. -#endif -#endif - } - - SUBCASE("min values") { -#if NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS == 1 - require_npf_itoa("8085774586302733229", INTMAX_MIN); -#else -#if INT_MIN == -2147483648 - require_npf_itoa("8463847412", INT_MIN); -#else -#error Unknown INT_MIN here, please add another branch. -#endif -#endif - } - - SUBCASE("negative values have minus sign stripped") { - require_npf_itoa("1", -1); - require_npf_itoa("5987987", -7897895); - require_npf_itoa("12345", -54321); - } -}