Skip to content

Commit 908c526

Browse files
committed
Import WString from 1.5.6
1 parent 3fb1c59 commit 908c526

File tree

2 files changed

+71
-24
lines changed

2 files changed

+71
-24
lines changed

cores/arduino/WString.cpp

+58-20
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
#include "WString.h"
2323

24-
2524
/*********************************************/
2625
/* Constructors */
2726
/*********************************************/
@@ -38,6 +37,12 @@ String::String(const String &value)
3837
*this = value;
3938
}
4039

40+
String::String(const __FlashStringHelper *pstr)
41+
{
42+
init();
43+
*this = pstr;
44+
}
45+
4146
#ifdef __GXX_EXPERIMENTAL_CXX0X__
4247
String::String(String &&rval)
4348
{
@@ -63,39 +68,39 @@ String::String(char c)
6368
String::String(unsigned char value, unsigned char base)
6469
{
6570
init();
66-
char buf[9];
71+
char buf[1 + 8 * sizeof(unsigned char)];
6772
utoa(value, buf, base);
6873
*this = buf;
6974
}
7075

7176
String::String(int value, unsigned char base)
7277
{
7378
init();
74-
char buf[18];
79+
char buf[2 + 8 * sizeof(int)];
7580
itoa(value, buf, base);
7681
*this = buf;
7782
}
7883

7984
String::String(unsigned int value, unsigned char base)
8085
{
8186
init();
82-
char buf[17];
87+
char buf[1 + 8 * sizeof(unsigned int)];
8388
utoa(value, buf, base);
8489
*this = buf;
8590
}
8691

8792
String::String(long value, unsigned char base)
8893
{
8994
init();
90-
char buf[34];
95+
char buf[2 + 8 * sizeof(long)];
9196
ltoa(value, buf, base);
9297
*this = buf;
9398
}
9499

95100
String::String(unsigned long value, unsigned char base)
96101
{
97102
init();
98-
char buf[33];
103+
char buf[1 + 8 * sizeof(unsigned long)];
99104
ultoa(value, buf, base);
100105
*this = buf;
101106
}
@@ -113,6 +118,7 @@ String::String(double value, unsigned char decimalPlaces)
113118
char buf[33];
114119
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
115120
}
121+
116122
String::~String()
117123
{
118124
free(buffer);
@@ -127,7 +133,6 @@ inline void String::init(void)
127133
buffer = NULL;
128134
capacity = 0;
129135
len = 0;
130-
flags = 0;
131136
}
132137

133138
void String::invalidate(void)
@@ -173,6 +178,17 @@ String & String::copy(const char *cstr, unsigned int length)
173178
return *this;
174179
}
175180

181+
String & String::copy(const __FlashStringHelper *pstr, unsigned int length)
182+
{
183+
if (!reserve(length)) {
184+
invalidate();
185+
return *this;
186+
}
187+
len = length;
188+
strcpy_P(buffer, (PGM_P)pstr);
189+
return *this;
190+
}
191+
176192
#ifdef __GXX_EXPERIMENTAL_CXX0X__
177193
void String::move(String &rhs)
178194
{
@@ -227,6 +243,14 @@ String & String::operator = (const char *cstr)
227243
return *this;
228244
}
229245

246+
String & String::operator = (const __FlashStringHelper *pstr)
247+
{
248+
if (pstr) copy(pstr, strlen_P((PGM_P)pstr));
249+
else invalidate();
250+
251+
return *this;
252+
}
253+
230254
/*********************************************/
231255
/* concat */
232256
/*********************************************/
@@ -263,53 +287,65 @@ unsigned char String::concat(char c)
263287

264288
unsigned char String::concat(unsigned char num)
265289
{
266-
char buf[4];
290+
char buf[1 + 3 * sizeof(unsigned char)];
267291
itoa(num, buf, 10);
268292
return concat(buf, strlen(buf));
269293
}
270294

271295
unsigned char String::concat(int num)
272296
{
273-
char buf[7];
297+
char buf[2 + 3 * sizeof(int)];
274298
itoa(num, buf, 10);
275299
return concat(buf, strlen(buf));
276300
}
277301

278302
unsigned char String::concat(unsigned int num)
279303
{
280-
char buf[6];
304+
char buf[1 + 3 * sizeof(unsigned int)];
281305
utoa(num, buf, 10);
282306
return concat(buf, strlen(buf));
283307
}
284308

285309
unsigned char String::concat(long num)
286310
{
287-
char buf[12];
311+
char buf[2 + 3 * sizeof(long)];
288312
ltoa(num, buf, 10);
289313
return concat(buf, strlen(buf));
290314
}
291315

292316
unsigned char String::concat(unsigned long num)
293317
{
294-
char buf[11];
318+
char buf[1 + 3 * sizeof(unsigned long)];
295319
ultoa(num, buf, 10);
296320
return concat(buf, strlen(buf));
297321
}
298322

299323
unsigned char String::concat(float num)
300324
{
301325
char buf[20];
302-
char* string = dtostrf(num, 8, 6, buf);
326+
char* string = dtostrf(num, 4, 2, buf);
303327
return concat(string, strlen(string));
304328
}
305329

306330
unsigned char String::concat(double num)
307331
{
308332
char buf[20];
309-
char* string = dtostrf(num, 8, 6, buf);
333+
char* string = dtostrf(num, 4, 2, buf);
310334
return concat(string, strlen(string));
311335
}
312336

337+
unsigned char String::concat(const __FlashStringHelper * str)
338+
{
339+
if (!str) return 0;
340+
int length = strlen_P((const char *) str);
341+
if (length == 0) return 1;
342+
unsigned int newlen = len + length;
343+
if (!reserve(newlen)) return 0;
344+
strcpy_P(buffer + len, (const char *) str);
345+
len = newlen;
346+
return 1;
347+
}
348+
313349
/*********************************************/
314350
/* Concatenate */
315351
/*********************************************/
@@ -383,6 +419,14 @@ StringSumHelper & operator + (const StringSumHelper &lhs, double num)
383419
if (!a.concat(num)) a.invalidate();
384420
return a;
385421
}
422+
423+
StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
424+
{
425+
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
426+
if (!a.concat(rhs)) a.invalidate();
427+
return a;
428+
}
429+
386430
/*********************************************/
387431
/* Comparison */
388432
/*********************************************/
@@ -567,11 +611,6 @@ int String::lastIndexOf(const String &s2, unsigned int fromIndex) const
567611
return found;
568612
}
569613

570-
String String::substring( unsigned int left ) const
571-
{
572-
return substring(left, len);
573-
}
574-
575614
String String::substring(unsigned int left, unsigned int right) const
576615
{
577616
if (left > right) {
@@ -698,7 +737,6 @@ long String::toInt(void) const
698737
return 0;
699738
}
700739

701-
702740
float String::toFloat(void) const
703741
{
704742
if (buffer) return float(atof(buffer));

cores/arduino/WString.h

+13-4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class String
5858
// be false).
5959
String(const char *cstr = "");
6060
String(const String &str);
61+
String(const __FlashStringHelper *str);
6162
#ifdef __GXX_EXPERIMENTAL_CXX0X__
6263
String(String &&rval);
6364
String(StringSumHelper &&rval);
@@ -68,8 +69,8 @@ class String
6869
explicit String(unsigned int, unsigned char base=10);
6970
explicit String(long, unsigned char base=10);
7071
explicit String(unsigned long, unsigned char base=10);
71-
explicit String(float, unsigned char decimalPlaces=6);
72-
explicit String(double, unsigned char decimalPlaces=6);
72+
explicit String(float, unsigned char decimalPlaces=2);
73+
explicit String(double, unsigned char decimalPlaces=2);
7374
~String(void);
7475

7576
// memory management
@@ -84,6 +85,7 @@ class String
8485
// marked as invalid ("if (s)" will be false).
8586
String & operator = (const String &rhs);
8687
String & operator = (const char *cstr);
88+
String & operator = (const __FlashStringHelper *str);
8789
#ifdef __GXX_EXPERIMENTAL_CXX0X__
8890
String & operator = (String &&rval);
8991
String & operator = (StringSumHelper &&rval);
@@ -104,6 +106,7 @@ class String
104106
unsigned char concat(unsigned long num);
105107
unsigned char concat(float num);
106108
unsigned char concat(double num);
109+
unsigned char concat(const __FlashStringHelper * str);
107110

108111
// if there's not enough memory for the concatenated value, the string
109112
// will be left unchanged (but this isn't signalled in any way)
@@ -115,6 +118,9 @@ class String
115118
String & operator += (unsigned int num) {concat(num); return (*this);}
116119
String & operator += (long num) {concat(num); return (*this);}
117120
String & operator += (unsigned long num) {concat(num); return (*this);}
121+
String & operator += (float num) {concat(num); return (*this);}
122+
String & operator += (double num) {concat(num); return (*this);}
123+
String & operator += (const __FlashStringHelper *str){concat(str); return (*this);}
118124

119125
friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
120126
friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
@@ -126,6 +132,7 @@ class String
126132
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
127133
friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
128134
friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
135+
friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs);
129136

130137
// comparison (only works w/ Strings and "strings")
131138
operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
@@ -164,7 +171,7 @@ class String
164171
int lastIndexOf( char ch, unsigned int fromIndex ) const;
165172
int lastIndexOf( const String &str ) const;
166173
int lastIndexOf( const String &str, unsigned int fromIndex ) const;
167-
String substring( unsigned int beginIndex ) const;
174+
String substring( unsigned int beginIndex ) const { return substring(beginIndex, len); };
168175
String substring( unsigned int beginIndex, unsigned int endIndex ) const;
169176

170177
// modification
@@ -184,7 +191,6 @@ class String
184191
char *buffer; // the actual char array
185192
unsigned int capacity; // the array length minus one (for the '\0')
186193
unsigned int len; // the String length (not counting the '\0')
187-
unsigned char flags; // unused, for future features
188194
protected:
189195
void init(void);
190196
void invalidate(void);
@@ -193,6 +199,7 @@ class String
193199

194200
// copy and move
195201
String & copy(const char *cstr, unsigned int length);
202+
String & copy(const __FlashStringHelper *pstr, unsigned int length);
196203
#ifdef __GXX_EXPERIMENTAL_CXX0X__
197204
void move(String &rhs);
198205
#endif
@@ -209,6 +216,8 @@ class StringSumHelper : public String
209216
StringSumHelper(unsigned int num) : String(num) {}
210217
StringSumHelper(long num) : String(num) {}
211218
StringSumHelper(unsigned long num) : String(num) {}
219+
StringSumHelper(float num) : String(num) {}
220+
StringSumHelper(double num) : String(num) {}
212221
};
213222

214223
#endif // __cplusplus

0 commit comments

Comments
 (0)