diff --git a/cores/esp8266/core_esp8266_noniso.c b/cores/esp8266/core_esp8266_noniso.c index e792048903..c499c1bcf3 100644 --- a/cores/esp8266/core_esp8266_noniso.c +++ b/cores/esp8266/core_esp8266_noniso.c @@ -29,62 +29,12 @@ #include #include "stdlib_noniso.h" -void reverse(char* begin, char* end) { - char *is = begin; - char *ie = end - 1; - while(is < ie) { - char tmp = *ie; - *ie = *is; - *is = tmp; - ++is; - --ie; - } -} - char* ltoa(long value, char* result, int base) { - if(base < 2 || base > 16) { - *result = 0; - return result; - } - - char* out = result; - long quotient = abs(value); - - do { - const long tmp = quotient / base; - *out = "0123456789abcdef"[quotient - (tmp * base)]; - ++out; - quotient = tmp; - } while(quotient); - - // Apply negative sign - if(value < 0) - *out++ = '-'; - - reverse(result, out); - *out = 0; - return result; + return itoa((int)value, result, base); } char* ultoa(unsigned long value, char* result, int base) { - if(base < 2 || base > 16) { - *result = 0; - return result; - } - - char* out = result; - unsigned long quotient = value; - - do { - const unsigned long tmp = quotient / base; - *out = "0123456789abcdef"[quotient - (tmp * base)]; - ++out; - quotient = tmp; - } while(quotient); - - reverse(result, out); - *out = 0; - return result; + return utoa((unsigned int)value, result, base); } char * dtostrf(double number, signed char width, unsigned char prec, char *s) { diff --git a/tests/host/common/noniso.c b/tests/host/common/noniso.c index 21a26f02eb..869a4b7f8e 100644 --- a/tests/host/common/noniso.c +++ b/tests/host/common/noniso.c @@ -22,6 +22,17 @@ #include "stdlib_noniso.h" +void reverse(char* begin, char* end) { + char *is = begin; + char *ie = end - 1; + while(is < ie) { + char tmp = *ie; + *ie = *is; + *is = tmp; + ++is; + --ie; + } +} char* utoa(unsigned value, char* result, int base) { if(base < 2 || base > 16) { @@ -49,6 +60,9 @@ char* itoa(int value, char* result, int base) { *result = 0; return result; } + if (base != 10) { + return utoa((unsigned)value, result, base); + } char* out = result; int quotient = abs(value); diff --git a/tests/host/core/test_string.cpp b/tests/host/core/test_string.cpp index 39140b049b..ff13de8c79 100644 --- a/tests/host/core/test_string.cpp +++ b/tests/host/core/test_string.cpp @@ -115,6 +115,15 @@ TEST_CASE("String concantenation", "[core][String]") str = "clean"; REQUIRE(str.concat(str) == true); REQUIRE(str == "cleanclean"); + // non-decimal negative #s should be as if they were unsigned + str = String((int)-100, 16); + REQUIRE(str == "ffffff9c"); + str = String((long)-101, 16); + REQUIRE(str == "ffffff9b"); + str = String((int)-100, 10); + REQUIRE(str == "-100"); + str = String((long)-100, 10); + REQUIRE(str == "-100"); } TEST_CASE("String comparison", "[core][String]")