Skip to content

Commit 720b56c

Browse files
mcsprhasenradball
authored andcommitted
WString: unify numeric conversion and fix assignments (esp8266#8526)
Restore the pre-3.0.0 behaviour when we could assign numeric values to the string object. After introducing operator =(char), everything was converted to char instead of the expected 'stringification' of the number (built-in int, long, unsigned int, unsigned long, long long, unsigned long long, float and double) Add toString() that handles conversions, re-use it through out the class Fix esp8266#8430
1 parent 7e5824e commit 720b56c

File tree

3 files changed

+321
-103
lines changed

3 files changed

+321
-103
lines changed

cores/esp8266/WString.cpp

+129-85
Original file line numberDiff line numberDiff line change
@@ -32,111 +32,169 @@
3232
#define STR(x) __STRHELPER(x) // stringifier
3333

3434
/*********************************************/
35-
/* Constructors */
35+
/* Conversion helpers */
3636
/*********************************************/
3737

38-
String::String(const char *cstr) {
39-
init();
40-
if (cstr)
41-
copy(cstr, strlen_P(cstr));
42-
}
43-
44-
String::String(const String &value) {
45-
init();
46-
*this = value;
47-
}
38+
static String toString(unsigned char value, unsigned char base) {
39+
String out;
4840

49-
String::String(const __FlashStringHelper *pstr) {
50-
init();
51-
*this = pstr; // see operator =
52-
}
41+
char buf[1 + 8 * sizeof(unsigned char)];
42+
out = utoa(value, buf, base);
5343

54-
String::String(String &&rval) noexcept {
55-
init();
56-
move(rval);
44+
return out;
5745
}
5846

59-
String::String(unsigned char value, unsigned char base) {
60-
init();
61-
char buf[1 + 8 * sizeof(unsigned char)];
62-
utoa(value, buf, base);
63-
*this = buf;
64-
}
47+
static String toString(int value, unsigned char base) {
48+
String out;
6549

66-
String::String(int value, unsigned char base) {
67-
init();
6850
char buf[2 + 8 * sizeof(int)];
6951
if (base == 10) {
70-
sprintf(buf, "%d", value);
52+
out.concat(buf, sprintf(buf, "%d", value));
7153
} else {
72-
itoa(value, buf, base);
54+
out = itoa(value, buf, base);
7355
}
74-
*this = buf;
56+
57+
return out;
7558
}
7659

77-
String::String(unsigned int value, unsigned char base) {
78-
init();
60+
static String toString(unsigned int value, unsigned char base) {
61+
String out;
62+
7963
char buf[1 + 8 * sizeof(unsigned int)];
80-
utoa(value, buf, base);
81-
*this = buf;
64+
out = utoa(value, buf, base);
65+
66+
return out;
8267
}
8368

84-
String::String(long value, unsigned char base) {
85-
init();
69+
static String toString(long value, unsigned char base) {
70+
String out;
71+
8672
char buf[2 + 8 * sizeof(long)];
8773
if (base == 10) {
88-
sprintf(buf, "%ld", value);
74+
out.concat(buf, sprintf(buf, "%ld", value));
8975
} else {
90-
ltoa(value, buf, base);
76+
out = ltoa(value, buf, base);
9177
}
92-
*this = buf;
78+
79+
return out;
9380
}
9481

95-
String::String(unsigned long value, unsigned char base) {
96-
init();
82+
static String toString(unsigned long value, unsigned char base) {
83+
String out;
84+
9785
char buf[1 + 8 * sizeof(unsigned long)];
98-
ultoa(value, buf, base);
99-
*this = buf;
86+
out = ultoa(value, buf, base);
87+
88+
return out;
10089
}
10190

102-
String::String(long long value) {
103-
init();
91+
// TODO: {u,}lltoa don't guarantee that the buffer is usable directly, one should always use the returned pointer
92+
93+
static String toString(long long value, unsigned char base) {
94+
String out;
95+
10496
char buf[2 + 8 * sizeof(long long)];
105-
sprintf(buf, "%lld", value);
106-
*this = buf;
97+
if (base == 10) {
98+
out.concat(buf, sprintf(buf, "%lld", value));
99+
} else {
100+
out = lltoa(value, buf, sizeof(buf), base);
101+
}
102+
103+
return out;
107104
}
108105

109-
String::String(unsigned long long value) {
110-
init();
106+
static String toString(unsigned long long value, unsigned char base) {
107+
String out;
108+
111109
char buf[1 + 8 * sizeof(unsigned long long)];
112-
sprintf(buf, "%llu", value);
113-
*this = buf;
110+
if (base == 10) {
111+
out.concat(buf, sprintf(buf, "%llu", value));
112+
} else {
113+
out = ulltoa(value, buf, sizeof(buf), base);
114+
}
115+
116+
return out;
114117
}
115118

116-
String::String(long long value, unsigned char base) {
119+
static String toString(float value, unsigned char decimalPlaces) {
120+
String out;
121+
122+
char buf[33];
123+
out = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
124+
125+
return out;
126+
}
127+
128+
static String toString(double value, unsigned char decimalPlaces) {
129+
String out;
130+
131+
char buf[33];
132+
out = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
133+
134+
return out;
135+
}
136+
137+
/*********************************************/
138+
/* Constructors */
139+
/*********************************************/
140+
141+
String::String(const char *cstr) {
117142
init();
118-
char buf[2 + 8 * sizeof(long long)];
119-
*this = lltoa(value, buf, sizeof(buf), base);
143+
if (cstr)
144+
copy(cstr, strlen_P(cstr));
120145
}
121146

122-
String::String(unsigned long long value, unsigned char base) {
147+
String::String(const String &value) {
123148
init();
124-
char buf[1 + 8 * sizeof(unsigned long long)];
125-
*this = ulltoa(value, buf, sizeof(buf), base);
149+
*this = value;
126150
}
127151

128-
String::String(float value, unsigned char decimalPlaces) {
152+
String::String(const __FlashStringHelper *pstr) {
129153
init();
130-
char buf[33];
131-
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
154+
*this = pstr; // see operator =
132155
}
133156

134-
String::String(double value, unsigned char decimalPlaces) {
157+
String::String(String &&rval) noexcept {
135158
init();
136-
char buf[33];
137-
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
159+
move(rval);
138160
}
139161

162+
String::String(unsigned char value, unsigned char base) :
163+
String(toString(value, base))
164+
{}
165+
166+
String::String(int value, unsigned char base) :
167+
String(toString(value, base))
168+
{}
169+
170+
String::String(unsigned int value, unsigned char base) :
171+
String(toString(value, base))
172+
{}
173+
174+
String::String(long value, unsigned char base) :
175+
String(toString(value, base))
176+
{}
177+
178+
String::String(unsigned long value, unsigned char base) :
179+
String(toString(value, base))
180+
{}
181+
182+
String::String(long long value, unsigned char base) :
183+
String(toString(value, base))
184+
{}
185+
186+
String::String(unsigned long long value, unsigned char base) :
187+
String(toString(value, base))
188+
{}
189+
190+
String::String(float value, unsigned char decimalPlaces) :
191+
String(toString(value, decimalPlaces))
192+
{}
193+
194+
String::String(double value, unsigned char decimalPlaces) :
195+
String(toString(value, decimalPlaces))
196+
{}
197+
140198
/*********************************************/
141199
/* Memory Management */
142200
/*********************************************/
@@ -279,7 +337,6 @@ String &String::operator =(char c) {
279337
return *this;
280338
}
281339

282-
283340
/*********************************************/
284341
/* concat */
285342
/*********************************************/
@@ -329,52 +386,39 @@ bool String::concat(char c) {
329386
}
330387

331388
bool String::concat(unsigned char num) {
332-
char buf[1 + 3 * sizeof(unsigned char)];
333-
return concat(buf, sprintf(buf, "%d", num));
389+
return concat(String(num));
334390
}
335391

336392
bool String::concat(int num) {
337-
char buf[2 + 3 * sizeof(int)];
338-
return concat(buf, sprintf(buf, "%d", num));
393+
return concat(String(num));
339394
}
340395

341396
bool String::concat(unsigned int num) {
342-
char buf[1 + 3 * sizeof(unsigned int)];
343-
utoa(num, buf, 10);
344-
return concat(buf, strlen(buf));
397+
return concat(String(num));
345398
}
346399

347400
bool String::concat(long num) {
348-
char buf[2 + 3 * sizeof(long)];
349-
return concat(buf, sprintf(buf, "%ld", num));
401+
return concat(String(num));
350402
}
351403

352404
bool String::concat(unsigned long num) {
353-
char buf[1 + 3 * sizeof(unsigned long)];
354-
ultoa(num, buf, 10);
355-
return concat(buf, strlen(buf));
405+
return concat(String(num));
356406
}
357407

358408
bool String::concat(long long num) {
359-
char buf[2 + 3 * sizeof(long long)];
360-
return concat(buf, sprintf(buf, "%lld", num));
409+
return concat(String(num));
361410
}
362411

363412
bool String::concat(unsigned long long num) {
364-
char buf[1 + 3 * sizeof(unsigned long long)];
365-
return concat(buf, sprintf(buf, "%llu", num));
413+
return concat(String(num));
366414
}
367415

368416
bool String::concat(float num) {
369-
char buf[20];
370-
char *string = dtostrf(num, 4, 2, buf);
371-
return concat(string, strlen(string));
417+
return concat(String(num));
372418
}
373419

374420
bool String::concat(double num) {
375-
char buf[20];
376-
char *string = dtostrf(num, 4, 2, buf);
377-
return concat(string, strlen(string));
421+
return concat(String(num));
378422
}
379423

380424
bool String::concat(const __FlashStringHelper *str) {

0 commit comments

Comments
 (0)