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