Skip to content

Commit 4b875e1

Browse files
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. Fixes bug esp8266#2813
1 parent 8a64a12 commit 4b875e1

File tree

2 files changed

+13
-52
lines changed

2 files changed

+13
-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

+11
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) {

0 commit comments

Comments
 (0)