1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Remove broken ltoa/ultoa, call itoa/utoa (#5625)

* Remove broken ltoa/ultoa, call itoa/utoa

Use the newlib integer-to-ASCII non-POSIX calls instead of rolling
our own.  Should be safe as sizeof(long) == sizeof(int).
The custom functions behaved differently from itoa when passed in
negative values in non-base-10.

Add host tests for negative non-base-10 int/longs
This commit is contained in:
Earle F. Philhower, III 2019-01-17 02:08:19 +00:00 committed by GitHub
parent 7ee503d353
commit 6883beedec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 52 deletions

View File

@ -29,62 +29,12 @@
#include <math.h>
#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) {

View File

@ -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);

View File

@ -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]")