21
21
22
22
#include " WString.h"
23
23
24
-
25
24
/* ********************************************/
26
25
/* Constructors */
27
26
/* ********************************************/
@@ -38,6 +37,12 @@ String::String(const String &value)
38
37
*this = value;
39
38
}
40
39
40
+ String::String (const __FlashStringHelper *pstr)
41
+ {
42
+ init ();
43
+ *this = pstr;
44
+ }
45
+
41
46
#ifdef __GXX_EXPERIMENTAL_CXX0X__
42
47
String::String (String &&rval)
43
48
{
@@ -63,39 +68,39 @@ String::String(char c)
63
68
String::String (unsigned char value, unsigned char base)
64
69
{
65
70
init ();
66
- char buf[9 ];
71
+ char buf[1 + 8 * sizeof ( unsigned char ) ];
67
72
utoa (value, buf, base);
68
73
*this = buf;
69
74
}
70
75
71
76
String::String (int value, unsigned char base)
72
77
{
73
78
init ();
74
- char buf[18 ];
79
+ char buf[2 + 8 * sizeof ( int ) ];
75
80
itoa (value, buf, base);
76
81
*this = buf;
77
82
}
78
83
79
84
String::String (unsigned int value, unsigned char base)
80
85
{
81
86
init ();
82
- char buf[17 ];
87
+ char buf[1 + 8 * sizeof ( unsigned int ) ];
83
88
utoa (value, buf, base);
84
89
*this = buf;
85
90
}
86
91
87
92
String::String (long value, unsigned char base)
88
93
{
89
94
init ();
90
- char buf[34 ];
95
+ char buf[2 + 8 * sizeof ( long ) ];
91
96
ltoa (value, buf, base);
92
97
*this = buf;
93
98
}
94
99
95
100
String::String (unsigned long value, unsigned char base)
96
101
{
97
102
init ();
98
- char buf[33 ];
103
+ char buf[1 + 8 * sizeof ( unsigned long ) ];
99
104
ultoa (value, buf, base);
100
105
*this = buf;
101
106
}
@@ -113,6 +118,7 @@ String::String(double value, unsigned char decimalPlaces)
113
118
char buf[33 ];
114
119
*this = dtostrf (value, (decimalPlaces + 2 ), decimalPlaces, buf);
115
120
}
121
+
116
122
String::~String ()
117
123
{
118
124
free (buffer);
@@ -127,7 +133,6 @@ inline void String::init(void)
127
133
buffer = NULL ;
128
134
capacity = 0 ;
129
135
len = 0 ;
130
- flags = 0 ;
131
136
}
132
137
133
138
void String::invalidate (void )
@@ -173,6 +178,17 @@ String & String::copy(const char *cstr, unsigned int length)
173
178
return *this ;
174
179
}
175
180
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
+
176
192
#ifdef __GXX_EXPERIMENTAL_CXX0X__
177
193
void String::move (String &rhs)
178
194
{
@@ -227,6 +243,14 @@ String & String::operator = (const char *cstr)
227
243
return *this ;
228
244
}
229
245
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
+
230
254
/* ********************************************/
231
255
/* concat */
232
256
/* ********************************************/
@@ -263,53 +287,65 @@ unsigned char String::concat(char c)
263
287
264
288
unsigned char String::concat (unsigned char num)
265
289
{
266
- char buf[4 ];
290
+ char buf[1 + 3 * sizeof ( unsigned char ) ];
267
291
itoa (num, buf, 10 );
268
292
return concat (buf, strlen (buf));
269
293
}
270
294
271
295
unsigned char String::concat (int num)
272
296
{
273
- char buf[7 ];
297
+ char buf[2 + 3 * sizeof ( int ) ];
274
298
itoa (num, buf, 10 );
275
299
return concat (buf, strlen (buf));
276
300
}
277
301
278
302
unsigned char String::concat (unsigned int num)
279
303
{
280
- char buf[6 ];
304
+ char buf[1 + 3 * sizeof ( unsigned int ) ];
281
305
utoa (num, buf, 10 );
282
306
return concat (buf, strlen (buf));
283
307
}
284
308
285
309
unsigned char String::concat (long num)
286
310
{
287
- char buf[12 ];
311
+ char buf[2 + 3 * sizeof ( long ) ];
288
312
ltoa (num, buf, 10 );
289
313
return concat (buf, strlen (buf));
290
314
}
291
315
292
316
unsigned char String::concat (unsigned long num)
293
317
{
294
- char buf[11 ];
318
+ char buf[1 + 3 * sizeof ( unsigned long ) ];
295
319
ultoa (num, buf, 10 );
296
320
return concat (buf, strlen (buf));
297
321
}
298
322
299
323
unsigned char String::concat (float num)
300
324
{
301
325
char buf[20 ];
302
- char * string = dtostrf (num, 8 , 6 , buf);
326
+ char * string = dtostrf (num, 4 , 2 , buf);
303
327
return concat (string, strlen (string));
304
328
}
305
329
306
330
unsigned char String::concat (double num)
307
331
{
308
332
char buf[20 ];
309
- char * string = dtostrf (num, 8 , 6 , buf);
333
+ char * string = dtostrf (num, 4 , 2 , buf);
310
334
return concat (string, strlen (string));
311
335
}
312
336
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
+
313
349
/* ********************************************/
314
350
/* Concatenate */
315
351
/* ********************************************/
@@ -383,6 +419,14 @@ StringSumHelper & operator + (const StringSumHelper &lhs, double num)
383
419
if (!a.concat (num)) a.invalidate ();
384
420
return a;
385
421
}
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
+
386
430
/* ********************************************/
387
431
/* Comparison */
388
432
/* ********************************************/
@@ -567,11 +611,6 @@ int String::lastIndexOf(const String &s2, unsigned int fromIndex) const
567
611
return found;
568
612
}
569
613
570
- String String::substring ( unsigned int left ) const
571
- {
572
- return substring (left, len);
573
- }
574
-
575
614
String String::substring (unsigned int left, unsigned int right) const
576
615
{
577
616
if (left > right) {
@@ -698,7 +737,6 @@ long String::toInt(void) const
698
737
return 0 ;
699
738
}
700
739
701
-
702
740
float String::toFloat (void ) const
703
741
{
704
742
if (buffer) return float (atof (buffer));
0 commit comments