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__
4247String::String (String &&rval)
4348{
@@ -63,56 +68,57 @@ String::String(char c)
6368String::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
7176String::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
7984String::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
8792String::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
95100String::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}
102107
103- String::String (float value, int decimalPlaces)
108+ String::String (float value, unsigned char decimalPlaces)
104109{
105110 init ();
106111 char buf[33 ];
107112 *this = dtostrf (value, (decimalPlaces + 2 ), decimalPlaces, buf);
108113}
109114
110- String::String (double value, int decimalPlaces)
115+ String::String (double value, unsigned char decimalPlaces)
111116{
112117 init ();
113118 char buf[33 ];
114119 *this = dtostrf (value, (decimalPlaces + 2 ), decimalPlaces, buf);
115120}
121+
116122String::~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
133138void 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__
177193void 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
264288unsigned 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
271295unsigned 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
278302unsigned 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
285309unsigned 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
292316unsigned 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
299323unsigned 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
306330unsigned 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-
575614String 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-
702740float String::toFloat (void ) const
703741{
704742 if (buffer) return float (atof (buffer));
0 commit comments