2929
3030#include " WString.h"
3131#include < stdio.h>
32+ #include " stdlib_noniso.h"
3233
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- }
4334/* ********************************************/
4435/* Constructors */
4536/* ********************************************/
@@ -56,7 +47,13 @@ String::String(const String &value)
5647 *this = value;
5748}
5849
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__)
6057String::String (String &&rval)
6158{
6259 init ();
@@ -162,7 +159,6 @@ inline void String::init(void)
162159 buffer = NULL ;
163160 capacity = 0 ;
164161 len = 0 ;
165- flags = 0 ;
166162}
167163
168164void String::invalidate (void )
@@ -197,18 +193,29 @@ unsigned char String::changeBuffer(unsigned int maxStrLen)
197193/* Copy and Move */
198194/* ********************************************/
199195
200- String & String::copy (const char *cstr, unsigned int _length )
196+ String & String::copy (const char *cstr, unsigned int length )
201197{
202- if (!reserve (_length )) {
198+ if (!reserve (length )) {
203199 invalidate ();
204200 return *this ;
205201 }
206- len = _length ;
202+ len = length ;
207203 strcpy (buffer, cstr);
208204 return *this ;
209205}
210206
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__)
212219void String::move (String &rhs)
213220{
214221 if (buffer) {
@@ -240,7 +247,7 @@ String & String::operator = (const String &rhs)
240247 return *this ;
241248}
242249
243- #ifdef __GXX_EXPERIMENTAL_CXX0X__
250+ #if __cplusplus >= 201103L || defined( __GXX_EXPERIMENTAL_CXX0X__)
244251String & String::operator = (String &&rval)
245252{
246253 if (this != &rval) move (rval);
@@ -262,6 +269,14 @@ String & String::operator = (const char *cstr)
262269 return *this ;
263270}
264271
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+
265280/* ********************************************/
266281/* concat */
267282/* ********************************************/
@@ -359,6 +374,18 @@ unsigned char String::concat(double num)
359374 return concat (string, strlen (string));
360375}
361376
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+
362389/* ********************************************/
363390/* Concatenate */
364391/* ********************************************/
@@ -447,6 +474,13 @@ StringSumHelper & operator + (const StringSumHelper &lhs, double num)
447474 return a;
448475}
449476
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+
450484/* ********************************************/
451485/* Comparison */
452486/* ********************************************/
@@ -639,7 +673,7 @@ String String::substring(unsigned int left, unsigned int right) const
639673 left = temp;
640674 }
641675 String out;
642- if (left > len) return out;
676+ if (left >= len) return out;
643677 if (right > len) right = len;
644678 char temp = buffer[right]; // save the replaced character
645679 buffer[right] = ' \0 ' ;
@@ -652,33 +686,33 @@ String String::substring(unsigned int left, unsigned int right) const
652686/* Modification */
653687/* ********************************************/
654688
655- void String::replace (char find, char _replace )
689+ void String::replace (char find, char replace )
656690{
657691 if (!buffer) return ;
658692 for (char *p = buffer; *p; p++) {
659- if (*p == find) *p = _replace ;
693+ if (*p == find) *p = replace ;
660694 }
661695}
662696
663- void String::replace (const String& find, const String& _replace )
697+ void String::replace (const String& find, const String& replace )
664698{
665699 if (len == 0 || find.len == 0 ) return ;
666- int diff = _replace .len - find.len ;
700+ int diff = replace .len - find.len ;
667701 char *readFrom = buffer;
668702 char *foundAt;
669703 if (diff == 0 ) {
670704 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 ;
673707 }
674708 } else if (diff < 0 ) {
675709 char *writeTo = buffer;
676710 while ((foundAt = strstr (readFrom, find.buffer )) != NULL ) {
677711 unsigned int n = foundAt - readFrom;
678712 memcpy (writeTo, readFrom, n);
679713 writeTo += n;
680- memcpy (writeTo, _replace .buffer , _replace .len );
681- writeTo += _replace .len ;
714+ memcpy (writeTo, replace .buffer , replace .len );
715+ writeTo += replace .len ;
682716 readFrom = foundAt + find.len ;
683717 len += diff;
684718 }
@@ -697,7 +731,7 @@ void String::replace(const String& find, const String& _replace)
697731 memmove (readFrom + diff, readFrom, len - (readFrom - buffer));
698732 len += diff;
699733 buffer[len] = 0 ;
700- memcpy (buffer + index, _replace .buffer , _replace .len );
734+ memcpy (buffer + index, replace .buffer , replace .len );
701735 index--;
702736 }
703737 }
0 commit comments