Skip to content

Commit 4c41f0b

Browse files
committed
Repairing itoa module as we'd have doubly defined functions since itoa/... are also defined within stdlib and the Arduino header redefines them with the same namespace
1 parent dd000bb commit 4c41f0b

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

test/src/itoa.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,43 @@
88

99
#include <itoa.h>
1010

11-
#include <stdlib.h>
11+
#include <string>
12+
#include <stdexcept>
13+
14+
#include <stdio.h>
1215

1316
/**************************************************************************************
1417
* FUNCTION IMPLEMENTATION
1518
**************************************************************************************/
1619

17-
#ifdef __cplusplus
18-
extern "C" {
19-
#endif
20-
21-
char * itoa(int value, char *string, int radix)
20+
std::string radixToFmtString(int const radix)
2221
{
23-
return itoa(value, string, radix);
22+
if (radix == 8) return std::string("%o");
23+
else if (radix == 10) return std::string("%d");
24+
else if (radix == 16) return std::string("%X");
25+
else throw std::runtime_error("Invalid radix.");
2426
}
2527

26-
char * ltoa(long value, char *string, int radix)
28+
char * itoa(int value, char * str, int radix)
2729
{
28-
return ltoa(value, string, radix);
30+
sprintf(str, radixToFmtString(radix).c_str(), value);
31+
return str;
2932
}
3033

31-
char * utoa(unsigned value, char *string, int radix)
34+
char * ltoa(long value, char * str, int radix)
3235
{
33-
return utoa(value, string, radix);
36+
sprintf(str, radixToFmtString(radix).c_str(), value);
37+
return str;
3438
}
3539

36-
char * ultoa(unsigned long value, char *string, int radix)
40+
char * utoa(unsigned value, char *str, int radix)
3741
{
38-
return ultoa(value, string, radix);
42+
sprintf(str, radixToFmtString(radix).c_str(), value);
43+
return str;
3944
}
4045

41-
#ifdef __cplusplus
42-
} // extern "C"
43-
#endif
46+
char * ultoa(unsigned long value, char * str, int radix)
47+
{
48+
sprintf(str, radixToFmtString(radix).c_str(), value);
49+
return str;
50+
}

0 commit comments

Comments
 (0)