@@ -38,6 +38,12 @@ String::String(const String &value)
38
38
*this = value;
39
39
}
40
40
41
+ String::String (const __FlashStringHelper *pstr)
42
+ {
43
+ init ();
44
+ *this = pstr;
45
+ }
46
+
41
47
#ifdef __GXX_EXPERIMENTAL_CXX0X__
42
48
String::String (String &&rval)
43
49
{
@@ -100,6 +106,20 @@ String::String(unsigned long value, unsigned char base)
100
106
*this = buf;
101
107
}
102
108
109
+ String::String (float value, int decimalPlaces)
110
+ {
111
+ init ();
112
+ char buf[33 ];
113
+ *this = dtostrf (value, (decimalPlaces + 2 ), decimalPlaces, buf);
114
+ }
115
+
116
+ String::String (double value, int decimalPlaces)
117
+ {
118
+ init ();
119
+ char buf[33 ];
120
+ *this = dtostrf (value, (decimalPlaces + 2 ), decimalPlaces, buf);
121
+ }
122
+
103
123
String::~String ()
104
124
{
105
125
free (buffer);
@@ -160,6 +180,17 @@ String & String::copy(const char *cstr, unsigned int length)
160
180
return *this ;
161
181
}
162
182
183
+ String & String::copy (const __FlashStringHelper *pstr, unsigned int length)
184
+ {
185
+ if (!reserve (length)) {
186
+ invalidate ();
187
+ return *this ;
188
+ }
189
+ len = length;
190
+ strcpy_P (buffer, (const prog_char *)pstr);
191
+ return *this ;
192
+ }
193
+
163
194
#ifdef __GXX_EXPERIMENTAL_CXX0X__
164
195
void String::move (String &rhs)
165
196
{
@@ -214,6 +245,14 @@ String & String::operator = (const char *cstr)
214
245
return *this ;
215
246
}
216
247
248
+ String & String::operator = (const __FlashStringHelper *pstr)
249
+ {
250
+ if (pstr) copy (pstr, strlen_P ((const prog_char *)pstr));
251
+ else invalidate ();
252
+
253
+ return *this ;
254
+ }
255
+
217
256
/* ********************************************/
218
257
/* concat */
219
258
/* ********************************************/
@@ -257,14 +296,14 @@ unsigned char String::concat(unsigned char num)
257
296
258
297
unsigned char String::concat (int num)
259
298
{
260
- char buf[7 ];
299
+ char buf[12 ];
261
300
itoa (num, buf, 10 );
262
301
return concat (buf, strlen (buf));
263
302
}
264
303
265
304
unsigned char String::concat (unsigned int num)
266
305
{
267
- char buf[6 ];
306
+ char buf[11 ];
268
307
utoa (num, buf, 10 );
269
308
return concat (buf, strlen (buf));
270
309
}
@@ -283,6 +322,32 @@ unsigned char String::concat(unsigned long num)
283
322
return concat (buf, strlen (buf));
284
323
}
285
324
325
+ unsigned char String::concat (float num)
326
+ {
327
+ char buf[20 ];
328
+ char * string = dtostrf (num, 8 , 6 , buf);
329
+ return concat (string, strlen (string));
330
+ }
331
+
332
+ unsigned char String::concat (double num)
333
+ {
334
+ char buf[20 ];
335
+ char * string = dtostrf (num, 8 , 6 , buf);
336
+ return concat (string, strlen (string));
337
+ }
338
+
339
+ unsigned char String::concat (const __FlashStringHelper * str)
340
+ {
341
+ if (!str) return 0 ;
342
+ int length = strlen_P ((const char *) str);
343
+ if (length == 0 ) return 1 ;
344
+ unsigned int newlen = len + length;
345
+ if (!reserve (newlen)) return 0 ;
346
+ strcpy_P (buffer + len, (const char *) str);
347
+ len = newlen;
348
+ return 1 ;
349
+ }
350
+
286
351
/* ********************************************/
287
352
/* Concatenate */
288
353
/* ********************************************/
@@ -343,6 +408,27 @@ StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
343
408
return a;
344
409
}
345
410
411
+ StringSumHelper & operator + (const StringSumHelper &lhs, float num)
412
+ {
413
+ StringSumHelper &a = const_cast <StringSumHelper&>(lhs);
414
+ if (!a.concat (num)) a.invalidate ();
415
+ return a;
416
+ }
417
+
418
+ StringSumHelper & operator + (const StringSumHelper &lhs, double num)
419
+ {
420
+ StringSumHelper &a = const_cast <StringSumHelper&>(lhs);
421
+ if (!a.concat (num)) a.invalidate ();
422
+ return a;
423
+ }
424
+
425
+ StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
426
+ {
427
+ StringSumHelper &a = const_cast <StringSumHelper&>(lhs);
428
+ if (!a.concat (rhs)) a.invalidate ();
429
+ return a;
430
+ }
431
+
346
432
/* ********************************************/
347
433
/* Comparison */
348
434
/* ********************************************/
@@ -527,11 +613,6 @@ int String::lastIndexOf(const String &s2, unsigned int fromIndex) const
527
613
return found;
528
614
}
529
615
530
- String String::substring ( unsigned int left ) const
531
- {
532
- return substring (left, len);
533
- }
534
-
535
616
String String::substring (unsigned int left, unsigned int right) const
536
617
{
537
618
if (left > right) {
@@ -604,6 +685,22 @@ void String::replace(const String& find, const String& replace)
604
685
}
605
686
}
606
687
688
+ void String::remove (unsigned int index){
689
+ if (index >= len) { return ; }
690
+ int count = len - index;
691
+ remove (index, count);
692
+ }
693
+
694
+ void String::remove (unsigned int index, unsigned int count){
695
+ if (index >= len) { return ; }
696
+ if (count <= 0 ) { return ; }
697
+ if (index + count > len) { count = len - index; }
698
+ char *writeTo = buffer + index;
699
+ len = len - count;
700
+ strncpy (writeTo, buffer + index + count,len - index);
701
+ buffer[len] = 0 ;
702
+ }
703
+
607
704
void String::toLowerCase (void )
608
705
{
609
706
if (!buffer) return ;
@@ -642,4 +739,8 @@ long String::toInt(void) const
642
739
return 0 ;
643
740
}
644
741
645
-
742
+ float String::toFloat (void ) const
743
+ {
744
+ if (buffer) return float (atof (buffer));
745
+ return 0 ;
746
+ }
0 commit comments