Skip to content

Commit 2d075a3

Browse files
committed
Use itoa from samd core to replace use of deprecated sprintf call
1 parent 66aa7db commit 2d075a3

File tree

1 file changed

+106
-43
lines changed

1 file changed

+106
-43
lines changed

test/src/itoa.cpp

+106-43
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,125 @@
11
/*
2-
* Copyright (c) 2020 Arduino. All rights reserved.
3-
*/
2+
Copyright (c) 2014 Arduino LLC. All right reserved.
43
5-
/**************************************************************************************
6-
* INCLUDE
7-
**************************************************************************************/
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
88
9-
#include <api/itoa.h>
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
1013
11-
#include <string>
12-
#include <stdexcept>
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
1318

14-
#include <stdio.h>
19+
#include <string.h>
1520

16-
/**************************************************************************************
17-
* FUNCTION IMPLEMENTATION
18-
**************************************************************************************/
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
1924

20-
std::string radixToFmtString(int const radix)
25+
char* ltoa( long value, char *string, int radix )
2126
{
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.");
26-
}
27+
char tmp[33];
28+
char *tp = tmp;
29+
long i;
30+
unsigned long v;
31+
int sign;
32+
char *sp;
2733

28-
char * itoa(int value, char * str, int radix)
29-
{
30-
#pragma GCC diagnostic push
31-
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
32-
sprintf(str, radixToFmtString(radix).c_str(), value);
33-
#pragma GCC diagnostic pop
34-
return str;
34+
if ( string == NULL )
35+
{
36+
return 0 ;
37+
}
38+
39+
if (radix > 36 || radix <= 1)
40+
{
41+
return 0 ;
42+
}
43+
44+
sign = (radix == 10 && value < 0);
45+
if (sign)
46+
{
47+
v = -value;
48+
}
49+
else
50+
{
51+
v = (unsigned long)value;
52+
}
53+
54+
while (v || tp == tmp)
55+
{
56+
i = v % radix;
57+
v = v / radix;
58+
if (i < 10)
59+
*tp++ = i+'0';
60+
else
61+
*tp++ = i + 'a' - 10;
62+
}
63+
64+
sp = string;
65+
66+
if (sign)
67+
*sp++ = '-';
68+
while (tp > tmp)
69+
*sp++ = *--tp;
70+
*sp = 0;
71+
72+
return string;
3573
}
3674

37-
char * ltoa(long value, char * str, int radix)
75+
char* ultoa( unsigned long value, char *string, int radix )
3876
{
39-
#pragma GCC diagnostic push
40-
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
41-
sprintf(str, radixToFmtString(radix).c_str(), value);
42-
#pragma GCC diagnostic pop
43-
return str;
77+
char tmp[33];
78+
char *tp = tmp;
79+
long i;
80+
unsigned long v = value;
81+
char *sp;
82+
83+
if ( string == NULL )
84+
{
85+
return 0;
86+
}
87+
88+
if (radix > 36 || radix <= 1)
89+
{
90+
return 0;
91+
}
92+
93+
while (v || tp == tmp)
94+
{
95+
i = v % radix;
96+
v = v / radix;
97+
if (i < 10)
98+
*tp++ = i+'0';
99+
else
100+
*tp++ = i + 'a' - 10;
101+
}
102+
103+
sp = string;
104+
105+
106+
while (tp > tmp)
107+
*sp++ = *--tp;
108+
*sp = 0;
109+
110+
return string;
44111
}
45112

46-
char * utoa(unsigned value, char *str, int radix)
113+
char* itoa( int value, char *string, int radix )
47114
{
48-
#pragma GCC diagnostic push
49-
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
50-
sprintf(str, radixToFmtString(radix).c_str(), value);
51-
#pragma GCC diagnostic pop
52-
return str;
115+
return ltoa( value, string, radix ) ;
53116
}
54117

55-
char * ultoa(unsigned long value, char * str, int radix)
118+
char* utoa( unsigned int value, char *string, int radix )
56119
{
57-
#pragma GCC diagnostic push
58-
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
59-
sprintf(str, radixToFmtString(radix).c_str(), value);
60-
#pragma GCC diagnostic pop
61-
return str;
120+
return ultoa( value, string, radix ) ;
62121
}
122+
123+
#ifdef __cplusplus
124+
} // extern "C"
125+
#endif

0 commit comments

Comments
 (0)