Skip to content

Commit e487817

Browse files
committed
import stdlib noniso functions
try not to reinvent the wheel solves print(*, BIN) returning "unsupported base"
1 parent a03d93d commit e487817

File tree

5 files changed

+277
-93
lines changed

5 files changed

+277
-93
lines changed

cores/arduino/Arduino.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include <math.h>
2828

2929
#include "binary.h"
30-
#include "itoa.h"
30+
//#include "itoa.h"
3131

3232
#ifdef __cplusplus
3333
extern "C"{

cores/arduino/WString.cpp

Lines changed: 28 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -78,131 +78,74 @@ String::String(char c)
7878
*this = buf;
7979
}
8080

81-
static char * c_spec_signed[] = {
82-
(char *) "%d",
83-
(char *) "%ld",
84-
(char *) "%o",
85-
(char *) "%x",
86-
(char *) "unsupported base",
87-
};
88-
89-
static char * c_spec_unsigned[] = {
90-
(char *) "%u",
91-
(char *) "%lu",
92-
(char *) "%o",
93-
(char *) "%x",
94-
(char *) "unsupported base",
95-
};
96-
97-
98-
char * String::getCSpec(int base, bool issigned, bool islong){
99-
int int_idx = 0;
100-
101-
if(islong == true)
102-
int_idx = 1;
103-
104-
switch(base){
105-
case 8:
106-
if(issigned == true)
107-
return c_spec_signed[2];
108-
else
109-
return c_spec_unsigned[2];
110-
case 10:
111-
if(issigned == true)
112-
return c_spec_signed[int_idx];
113-
else
114-
return c_spec_unsigned[int_idx];
115-
case 16:
116-
if(issigned == true)
117-
return c_spec_signed[3];
118-
else
119-
return c_spec_unsigned[3];
120-
default:
121-
return c_spec_unsigned[4];
122-
}
123-
};
124-
12581
String::String(unsigned char value, unsigned char base)
12682
{
12783
init();
12884
char buf[1 + 8 * sizeof(unsigned char)];
129-
//utoa(value, buf, base);
130-
snprintf(buf, sizeof(buf), getCSpec(base, false, false), value);
85+
utoa(value, buf, base);
13186
*this = buf;
13287
}
13388

13489
String::String(int value, unsigned char base)
13590
{
13691
init();
13792
char buf[2 + 8 * sizeof(int)];
138-
//itoa(value, buf, base);
139-
snprintf(buf, sizeof(buf), getCSpec(base, true, false), value);
93+
itoa(value, buf, base);
14094
*this = buf;
14195
}
14296

14397
String::String(unsigned int value, unsigned char base)
14498
{
14599
init();
146100
char buf[1 + 8 * sizeof(unsigned int)];
147-
//utoa(value, buf, base);
148-
snprintf(buf, sizeof(buf), getCSpec(base, false, false), value);
101+
utoa(value, buf, base);
149102
*this = buf;
150103
}
151104

152105
String::String(long value, unsigned char base)
153106
{
154107
init();
155108
char buf[2 + 8 * sizeof(long)];
156-
//ltoa(value, buf, base);
157-
snprintf(buf, sizeof(buf), getCSpec(base, true, true), value);
109+
ltoa(value, buf, base);
158110
*this = buf;
159111
}
160112

161113
String::String(unsigned long value, unsigned char base)
162114
{
163115
init();
164116
char buf[1 + 8 * sizeof(unsigned long)];
165-
//ultoa(value, buf, base);
166-
snprintf(buf, sizeof(buf), getCSpec(base, false, true), value);
117+
ultoa(value, buf, base);
167118
*this = buf;
168119
}
169120

170121
String::String(long long value, unsigned char base)
171122
{
172123
init();
173124
char buf[2 + 8 * sizeof(long long)];
174-
//ltoa(value, buf, base);
175-
snprintf(buf, sizeof(buf), getCSpec(base, true, true), value);
125+
ltoa(value, buf, base);
176126
*this = buf;
177127
}
178128

179129
String::String(unsigned long long value, unsigned char base)
180130
{
181131
init();
182132
char buf[1 + 8 * sizeof(unsigned long long)];
183-
//ultoa(value, buf, base);
184-
snprintf(buf, sizeof(buf), getCSpec(base, false, true), value);
133+
ultoa(value, buf, base);
185134
*this = buf;
186135
}
187136

188137
String::String(float value, unsigned char decimalPlaces)
189138
{
190139
init();
191140
char buf[33];
192-
String dec(decimalPlaces);
193-
String tmp = "%." + dec + "f";
194-
snprintf(buf, sizeof(buf), tmp.c_str(), value);
195-
*this = buf;
141+
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
196142
}
197143

198144
String::String(double value, unsigned char decimalPlaces)
199145
{
200-
init();
201-
char buf[33];
202-
String dec(decimalPlaces);
203-
String tmp = "%." + dec + "f";
204-
snprintf(buf, sizeof(buf), tmp.c_str(), value);
205-
*this = buf;
146+
init();
147+
char buf[33];
148+
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
206149
}
207150

208151
String::~String()
@@ -328,11 +271,11 @@ unsigned char String::concat(const String &s)
328271
return concat(s.buffer, s.len);
329272
}
330273

331-
unsigned char String::concat(const char *cstr, unsigned int _length)
274+
unsigned char String::concat(const char *cstr, unsigned int length)
332275
{
333-
unsigned int newlen = len + _length;
276+
unsigned int newlen = len + length;
334277
if (!cstr) return 0;
335-
if (_length == 0) return 1;
278+
if (length == 0) return 1;
336279
if (!reserve(newlen)) return 0;
337280
strcpy(buffer + len, cstr);
338281
len = newlen;
@@ -356,71 +299,64 @@ unsigned char String::concat(char c)
356299
unsigned char String::concat(unsigned char num)
357300
{
358301
char buf[1 + 3 * sizeof(unsigned char)];
359-
//itoa(num, buf, 10);
360-
snprintf(buf, sizeof(buf), getCSpec(10, true, false), num);
302+
itoa(num, buf, 10);
361303
return concat(buf, strlen(buf));
362304
}
363305

364306
unsigned char String::concat(int num)
365307
{
366308
char buf[2 + 3 * sizeof(int)];
367-
//itoa(num, buf, 10);
368-
snprintf(buf, sizeof(buf), getCSpec(10, true, false), num);
309+
itoa(num, buf, 10);
369310
return concat(buf, strlen(buf));
370311
}
371312

372313
unsigned char String::concat(unsigned int num)
373314
{
374315
char buf[1 + 3 * sizeof(unsigned int)];
375-
//utoa(num, buf, 10);
376-
snprintf(buf, sizeof(buf), getCSpec(10, false, false), num);
316+
utoa(num, buf, 10);
377317
return concat(buf, strlen(buf));
378318
}
379319

380320
unsigned char String::concat(long num)
381321
{
382322
char buf[2 + 3 * sizeof(long)];
383-
//ltoa(num, buf, 10);
384-
snprintf(buf, sizeof(buf), getCSpec(10, true, true), num);
323+
ltoa(num, buf, 10);
385324
return concat(buf, strlen(buf));
386325
}
387326

388327
unsigned char String::concat(unsigned long num)
389328
{
390329
char buf[1 + 3 * sizeof(unsigned long)];
391-
//ultoa(num, buf, 10);
392-
snprintf(buf, sizeof(buf), getCSpec(10, false, true), num);
330+
ultoa(num, buf, 10);
393331
return concat(buf, strlen(buf));
394332
}
395333

396334
unsigned char String::concat(long long num)
397335
{
398-
char buf[12];
399-
//ltoa(num, buf, 10);
400-
snprintf(buf, sizeof(buf), getCSpec(10, true, true), num);
336+
char buf[2 + 3 * sizeof(long long)];
337+
ltoa(num, buf, 10);
401338
return concat(buf, strlen(buf));
402339
}
403340

404341
unsigned char String::concat(unsigned long long num)
405342
{
406-
char buf[11];
407-
//ultoa(num, buf, 10);
408-
snprintf(buf, sizeof(buf), getCSpec(10, false, true), num);
343+
char buf[1 + 3 * sizeof(unsigned long long)];
344+
ultoa(num, buf, 10);
409345
return concat(buf, strlen(buf));
410346
}
411347

412348
unsigned char String::concat(float num)
413349
{
414350
char buf[20];
415-
snprintf(buf, sizeof(buf), "%f", num);
416-
return concat(buf, strlen(buf));
351+
char* string = dtostrf(num, 4, 2, buf);
352+
return concat(string, strlen(string));
417353
}
418354

419355
unsigned char String::concat(double num)
420356
{
421-
char buf[20];
422-
snprintf(buf, sizeof(buf), "%f", num);
423-
return concat(buf, strlen(buf));
357+
char buf[20];
358+
char* string = dtostrf(num, 4, 2, buf);
359+
return concat(string, strlen(string));
424360
}
425361

426362
/*********************************************/

cores/arduino/WString.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <stdlib.h>
3131
#include <string.h>
3232
#include <ctype.h>
33+
#include "stdlib_noniso.h"
3334
//#include <avr/pgmspace.h>
3435

3536
// When compiling programs with this class, the following gcc parameters

0 commit comments

Comments
 (0)