31
31
#include < stdio.h>
32
32
33
33
34
- // following the C++ standard operators with attributes in right
35
- // side must be defined globally
36
- String operator + ( const char *cstr, const String &str_arg)
37
- {
38
- String &str_arg_o = const_cast <String&>(str_arg);
39
- String aux = String (cstr);
40
- aux.concat (str_arg_o);
41
- return aux;
42
- }
34
+
43
35
/* ********************************************/
44
36
/* Constructors */
45
37
/* ********************************************/
@@ -56,7 +48,13 @@ String::String(const String &value)
56
48
*this = value;
57
49
}
58
50
59
- #ifdef __GXX_EXPERIMENTAL_CXX0X__
51
+ String::String (const __FlashStringHelper *pstr)
52
+ {
53
+ init ();
54
+ *this = pstr;
55
+ }
56
+
57
+ #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
60
58
String::String (String &&rval)
61
59
{
62
60
init ();
@@ -162,7 +160,6 @@ inline void String::init(void)
162
160
buffer = NULL ;
163
161
capacity = 0 ;
164
162
len = 0 ;
165
- flags = 0 ;
166
163
}
167
164
168
165
void String::invalidate (void )
@@ -197,18 +194,29 @@ unsigned char String::changeBuffer(unsigned int maxStrLen)
197
194
/* Copy and Move */
198
195
/* ********************************************/
199
196
200
- String & String::copy (const char *cstr, unsigned int _length )
197
+ String & String::copy (const char *cstr, unsigned int length )
201
198
{
202
- if (!reserve (_length )) {
199
+ if (!reserve (length )) {
203
200
invalidate ();
204
201
return *this ;
205
202
}
206
- len = _length ;
203
+ len = length ;
207
204
strcpy (buffer, cstr);
208
205
return *this ;
209
206
}
210
207
211
- #ifdef __GXX_EXPERIMENTAL_CXX0X__
208
+ String & String::copy (const __FlashStringHelper *pstr, unsigned int length)
209
+ {
210
+ if (!reserve (length)) {
211
+ invalidate ();
212
+ return *this ;
213
+ }
214
+ len = length;
215
+ strcpy_P (buffer, (PGM_P)pstr);
216
+ return *this ;
217
+ }
218
+
219
+ #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
212
220
void String::move (String &rhs)
213
221
{
214
222
if (buffer) {
@@ -240,7 +248,7 @@ String & String::operator = (const String &rhs)
240
248
return *this ;
241
249
}
242
250
243
- #ifdef __GXX_EXPERIMENTAL_CXX0X__
251
+ #if __cplusplus >= 201103L || defined( __GXX_EXPERIMENTAL_CXX0X__)
244
252
String & String::operator = (String &&rval)
245
253
{
246
254
if (this != &rval) move (rval);
@@ -262,6 +270,14 @@ String & String::operator = (const char *cstr)
262
270
return *this ;
263
271
}
264
272
273
+ String & String::operator = (const __FlashStringHelper *pstr)
274
+ {
275
+ if (pstr) copy (pstr, strlen_P ((PGM_P)pstr));
276
+ else invalidate ();
277
+
278
+ return *this ;
279
+ }
280
+
265
281
/* ********************************************/
266
282
/* concat */
267
283
/* ********************************************/
@@ -359,6 +375,18 @@ unsigned char String::concat(double num)
359
375
return concat (string, strlen (string));
360
376
}
361
377
378
+ unsigned char String::concat (const __FlashStringHelper * str)
379
+ {
380
+ if (!str) return 0 ;
381
+ int length = strlen_P ((const char *) str);
382
+ if (length == 0 ) return 1 ;
383
+ unsigned int newlen = len + length;
384
+ if (!reserve (newlen)) return 0 ;
385
+ strcpy_P (buffer + len, (const char *) str);
386
+ len = newlen;
387
+ return 1 ;
388
+ }
389
+
362
390
/* ********************************************/
363
391
/* Concatenate */
364
392
/* ********************************************/
@@ -447,6 +475,13 @@ StringSumHelper & operator + (const StringSumHelper &lhs, double num)
447
475
return a;
448
476
}
449
477
478
+ StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
479
+ {
480
+ StringSumHelper &a = const_cast <StringSumHelper&>(lhs);
481
+ if (!a.concat (rhs)) a.invalidate ();
482
+ return a;
483
+ }
484
+
450
485
/* ********************************************/
451
486
/* Comparison */
452
487
/* ********************************************/
@@ -639,7 +674,7 @@ String String::substring(unsigned int left, unsigned int right) const
639
674
left = temp;
640
675
}
641
676
String out;
642
- if (left > len) return out;
677
+ if (left >= len) return out;
643
678
if (right > len) right = len;
644
679
char temp = buffer[right]; // save the replaced character
645
680
buffer[right] = ' \0 ' ;
@@ -652,33 +687,33 @@ String String::substring(unsigned int left, unsigned int right) const
652
687
/* Modification */
653
688
/* ********************************************/
654
689
655
- void String::replace (char find, char _replace )
690
+ void String::replace (char find, char replace )
656
691
{
657
692
if (!buffer) return ;
658
693
for (char *p = buffer; *p; p++) {
659
- if (*p == find) *p = _replace ;
694
+ if (*p == find) *p = replace ;
660
695
}
661
696
}
662
697
663
- void String::replace (const String& find, const String& _replace )
698
+ void String::replace (const String& find, const String& replace )
664
699
{
665
700
if (len == 0 || find.len == 0 ) return ;
666
- int diff = _replace .len - find.len ;
701
+ int diff = replace .len - find.len ;
667
702
char *readFrom = buffer;
668
703
char *foundAt;
669
704
if (diff == 0 ) {
670
705
while ((foundAt = strstr (readFrom, find.buffer )) != NULL ) {
671
- memcpy (foundAt, _replace .buffer , _replace .len );
672
- readFrom = foundAt + _replace .len ;
706
+ memcpy (foundAt, replace .buffer , replace .len );
707
+ readFrom = foundAt + replace .len ;
673
708
}
674
709
} else if (diff < 0 ) {
675
710
char *writeTo = buffer;
676
711
while ((foundAt = strstr (readFrom, find.buffer )) != NULL ) {
677
712
unsigned int n = foundAt - readFrom;
678
713
memcpy (writeTo, readFrom, n);
679
714
writeTo += n;
680
- memcpy (writeTo, _replace .buffer , _replace .len );
681
- writeTo += _replace .len ;
715
+ memcpy (writeTo, replace .buffer , replace .len );
716
+ writeTo += replace .len ;
682
717
readFrom = foundAt + find.len ;
683
718
len += diff;
684
719
}
@@ -697,7 +732,7 @@ void String::replace(const String& find, const String& _replace)
697
732
memmove (readFrom + diff, readFrom, len - (readFrom - buffer));
698
733
len += diff;
699
734
buffer[len] = 0 ;
700
- memcpy (buffer + index , _replace .buffer , _replace .len );
735
+ memcpy (buffer + index , replace .buffer , replace .len );
701
736
index --;
702
737
}
703
738
}
0 commit comments