Skip to content

Commit 6883bee

Browse files
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
1 parent 7ee503d commit 6883bee

File tree

3 files changed

+25
-52
lines changed

3 files changed

+25
-52
lines changed

cores/esp8266/core_esp8266_noniso.c

+2-52
Original file line numberDiff line numberDiff line change
@@ -29,62 +29,12 @@
2929
#include <math.h>
3030
#include "stdlib_noniso.h"
3131

32-
void reverse(char* begin, char* end) {
33-
char *is = begin;
34-
char *ie = end - 1;
35-
while(is < ie) {
36-
char tmp = *ie;
37-
*ie = *is;
38-
*is = tmp;
39-
++is;
40-
--ie;
41-
}
42-
}
43-
4432
char* ltoa(long value, char* result, int base) {
45-
if(base < 2 || base > 16) {
46-
*result = 0;
47-
return result;
48-
}
49-
50-
char* out = result;
51-
long quotient = abs(value);
52-
53-
do {
54-
const long tmp = quotient / base;
55-
*out = "0123456789abcdef"[quotient - (tmp * base)];
56-
++out;
57-
quotient = tmp;
58-
} while(quotient);
59-
60-
// Apply negative sign
61-
if(value < 0)
62-
*out++ = '-';
63-
64-
reverse(result, out);
65-
*out = 0;
66-
return result;
33+
return itoa((int)value, result, base);
6734
}
6835

6936
char* ultoa(unsigned long value, char* result, int base) {
70-
if(base < 2 || base > 16) {
71-
*result = 0;
72-
return result;
73-
}
74-
75-
char* out = result;
76-
unsigned long quotient = value;
77-
78-
do {
79-
const unsigned long tmp = quotient / base;
80-
*out = "0123456789abcdef"[quotient - (tmp * base)];
81-
++out;
82-
quotient = tmp;
83-
} while(quotient);
84-
85-
reverse(result, out);
86-
*out = 0;
87-
return result;
37+
return utoa((unsigned int)value, result, base);
8838
}
8939

9040
char * dtostrf(double number, signed char width, unsigned char prec, char *s) {

tests/host/common/noniso.c

+14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@
2222
#include "stdlib_noniso.h"
2323

2424

25+
void reverse(char* begin, char* end) {
26+
char *is = begin;
27+
char *ie = end - 1;
28+
while(is < ie) {
29+
char tmp = *ie;
30+
*ie = *is;
31+
*is = tmp;
32+
++is;
33+
--ie;
34+
}
35+
}
2536

2637
char* utoa(unsigned value, char* result, int base) {
2738
if(base < 2 || base > 16) {
@@ -49,6 +60,9 @@ char* itoa(int value, char* result, int base) {
4960
*result = 0;
5061
return result;
5162
}
63+
if (base != 10) {
64+
return utoa((unsigned)value, result, base);
65+
}
5266

5367
char* out = result;
5468
int quotient = abs(value);

tests/host/core/test_string.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ TEST_CASE("String concantenation", "[core][String]")
115115
str = "clean";
116116
REQUIRE(str.concat(str) == true);
117117
REQUIRE(str == "cleanclean");
118+
// non-decimal negative #s should be as if they were unsigned
119+
str = String((int)-100, 16);
120+
REQUIRE(str == "ffffff9c");
121+
str = String((long)-101, 16);
122+
REQUIRE(str == "ffffff9b");
123+
str = String((int)-100, 10);
124+
REQUIRE(str == "-100");
125+
str = String((long)-100, 10);
126+
REQUIRE(str == "-100");
118127
}
119128

120129
TEST_CASE("String comparison", "[core][String]")

0 commit comments

Comments
 (0)