Skip to content

Commit db286ac

Browse files
committed
Added support for Flash string on String class.
1 parent 2719777 commit db286ac

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

cores/arduino/WString.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ String::String(const String &value)
3838
*this = value;
3939
}
4040

41+
String::String(const __FlashStringHelper *pstr)
42+
{
43+
init();
44+
*this = pstr;
45+
}
46+
4147
#ifdef __GXX_EXPERIMENTAL_CXX0X__
4248
String::String(String &&rval)
4349
{
@@ -160,6 +166,17 @@ String & String::copy(const char *cstr, unsigned int length)
160166
return *this;
161167
}
162168

169+
String & String::copy(const __FlashStringHelper *pstr, unsigned int length)
170+
{
171+
if (!reserve(length)) {
172+
invalidate();
173+
return *this;
174+
}
175+
len = length;
176+
strcpy_P(buffer, (const prog_char *)pstr);
177+
return *this;
178+
}
179+
163180
#ifdef __GXX_EXPERIMENTAL_CXX0X__
164181
void String::move(String &rhs)
165182
{
@@ -214,6 +231,14 @@ String & String::operator = (const char *cstr)
214231
return *this;
215232
}
216233

234+
String & String::operator = (const __FlashStringHelper *pstr)
235+
{
236+
if (pstr) copy(pstr, strlen_P((const prog_char *)pstr));
237+
else invalidate();
238+
239+
return *this;
240+
}
241+
217242
/*********************************************/
218243
/* concat */
219244
/*********************************************/
@@ -283,6 +308,18 @@ unsigned char String::concat(unsigned long num)
283308
return concat(buf, strlen(buf));
284309
}
285310

311+
unsigned char String::concat(const __FlashStringHelper * str)
312+
{
313+
if (!str) return 0;
314+
int length = strlen_P((const char *) str);
315+
if (length == 0) return 1;
316+
unsigned int newlen = len + length;
317+
if (!reserve(newlen)) return 0;
318+
strcpy_P(buffer + len, (const char *) str);
319+
len = newlen;
320+
return 1;
321+
}
322+
286323
/*********************************************/
287324
/* Concatenate */
288325
/*********************************************/
@@ -343,6 +380,13 @@ StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
343380
return a;
344381
}
345382

383+
StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
384+
{
385+
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
386+
if (!a.concat(rhs)) a.invalidate();
387+
return a;
388+
}
389+
346390
/*********************************************/
347391
/* Comparison */
348392
/*********************************************/

cores/arduino/WString.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class String
5858
// be false).
5959
String(const char *cstr = "");
6060
String(const String &str);
61+
String(const __FlashStringHelper *str);
6162
#ifdef __GXX_EXPERIMENTAL_CXX0X__
6263
String(String &&rval);
6364
String(StringSumHelper &&rval);
@@ -82,6 +83,7 @@ class String
8283
// marked as invalid ("if (s)" will be false).
8384
String & operator = (const String &rhs);
8485
String & operator = (const char *cstr);
86+
String & operator = (const __FlashStringHelper *str);
8587
#ifdef __GXX_EXPERIMENTAL_CXX0X__
8688
String & operator = (String &&rval);
8789
String & operator = (StringSumHelper &&rval);
@@ -100,17 +102,19 @@ class String
100102
unsigned char concat(unsigned int num);
101103
unsigned char concat(long num);
102104
unsigned char concat(unsigned long num);
105+
unsigned char concat(const __FlashStringHelper * str);
103106

104107
// if there's not enough memory for the concatenated value, the string
105108
// will be left unchanged (but this isn't signalled in any way)
106109
String & operator += (const String &rhs) {concat(rhs); return (*this);}
107110
String & operator += (const char *cstr) {concat(cstr); return (*this);}
108111
String & operator += (char c) {concat(c); return (*this);}
109-
String & operator += (unsigned char num) {concat(num); return (*this);}
112+
String & operator += (unsigned char num) {concat(num); return (*this);}
110113
String & operator += (int num) {concat(num); return (*this);}
111114
String & operator += (unsigned int num) {concat(num); return (*this);}
112115
String & operator += (long num) {concat(num); return (*this);}
113116
String & operator += (unsigned long num) {concat(num); return (*this);}
117+
String & operator += (const __FlashStringHelper *str){concat(str); return (*this);}
114118

115119
friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
116120
friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
@@ -120,6 +124,7 @@ class String
120124
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
121125
friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
122126
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
127+
friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs);
123128

124129
// comparison (only works w/ Strings and "strings")
125130
operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
@@ -184,6 +189,7 @@ class String
184189

185190
// copy and move
186191
String & copy(const char *cstr, unsigned int length);
192+
String & copy(const __FlashStringHelper *pstr, unsigned int length);
187193
#ifdef __GXX_EXPERIMENTAL_CXX0X__
188194
void move(String &rhs);
189195
#endif

0 commit comments

Comments
 (0)