Skip to content

Commit dc9bccf

Browse files
committed
Merge branch 'ide-1.5.x' into ide-1.5.x-discovery
2 parents 1286ade + b4b50b1 commit dc9bccf

File tree

2 files changed

+130
-10
lines changed

2 files changed

+130
-10
lines changed

hardware/arduino/avr/cores/arduino/WString.cpp

Lines changed: 109 additions & 8 deletions
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
{
@@ -100,6 +106,20 @@ String::String(unsigned long value, unsigned char base)
100106
*this = buf;
101107
}
102108

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+
103123
String::~String()
104124
{
105125
free(buffer);
@@ -160,6 +180,17 @@ String & String::copy(const char *cstr, unsigned int length)
160180
return *this;
161181
}
162182

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+
163194
#ifdef __GXX_EXPERIMENTAL_CXX0X__
164195
void String::move(String &rhs)
165196
{
@@ -214,6 +245,14 @@ String & String::operator = (const char *cstr)
214245
return *this;
215246
}
216247

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+
217256
/*********************************************/
218257
/* concat */
219258
/*********************************************/
@@ -257,14 +296,14 @@ unsigned char String::concat(unsigned char num)
257296

258297
unsigned char String::concat(int num)
259298
{
260-
char buf[7];
299+
char buf[12];
261300
itoa(num, buf, 10);
262301
return concat(buf, strlen(buf));
263302
}
264303

265304
unsigned char String::concat(unsigned int num)
266305
{
267-
char buf[6];
306+
char buf[11];
268307
utoa(num, buf, 10);
269308
return concat(buf, strlen(buf));
270309
}
@@ -283,6 +322,32 @@ unsigned char String::concat(unsigned long num)
283322
return concat(buf, strlen(buf));
284323
}
285324

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+
286351
/*********************************************/
287352
/* Concatenate */
288353
/*********************************************/
@@ -343,6 +408,27 @@ StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
343408
return a;
344409
}
345410

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+
346432
/*********************************************/
347433
/* Comparison */
348434
/*********************************************/
@@ -527,11 +613,6 @@ int String::lastIndexOf(const String &s2, unsigned int fromIndex) const
527613
return found;
528614
}
529615

530-
String String::substring( unsigned int left ) const
531-
{
532-
return substring(left, len);
533-
}
534-
535616
String String::substring(unsigned int left, unsigned int right) const
536617
{
537618
if (left > right) {
@@ -604,6 +685,22 @@ void String::replace(const String& find, const String& replace)
604685
}
605686
}
606687

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+
607704
void String::toLowerCase(void)
608705
{
609706
if (!buffer) return;
@@ -642,4 +739,8 @@ long String::toInt(void) const
642739
return 0;
643740
}
644741

645-
742+
float String::toFloat(void) const
743+
{
744+
if (buffer) return float(atof(buffer));
745+
return 0;
746+
}

hardware/arduino/avr/cores/arduino/WString.h

Lines changed: 21 additions & 2 deletions
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);
@@ -68,6 +69,8 @@ class String
6869
explicit String(unsigned int, unsigned char base=10);
6970
explicit String(long, unsigned char base=10);
7071
explicit String(unsigned long, unsigned char base=10);
72+
explicit String(float, int decimalPlaces=6);
73+
explicit String(double, int decimalPlaces=6);
7174
~String(void);
7275

7376
// memory management
@@ -82,6 +85,7 @@ class String
8285
// marked as invalid ("if (s)" will be false).
8386
String & operator = (const String &rhs);
8487
String & operator = (const char *cstr);
88+
String & operator = (const __FlashStringHelper *str);
8589
#ifdef __GXX_EXPERIMENTAL_CXX0X__
8690
String & operator = (String &&rval);
8791
String & operator = (StringSumHelper &&rval);
@@ -100,17 +104,23 @@ class String
100104
unsigned char concat(unsigned int num);
101105
unsigned char concat(long num);
102106
unsigned char concat(unsigned long num);
107+
unsigned char concat(float num);
108+
unsigned char concat(double num);
109+
unsigned char concat(const __FlashStringHelper * str);
103110

104111
// if there's not enough memory for the concatenated value, the string
105112
// will be left unchanged (but this isn't signalled in any way)
106113
String & operator += (const String &rhs) {concat(rhs); return (*this);}
107114
String & operator += (const char *cstr) {concat(cstr); return (*this);}
108115
String & operator += (char c) {concat(c); return (*this);}
109-
String & operator += (unsigned char num) {concat(num); return (*this);}
116+
String & operator += (unsigned char num) {concat(num); return (*this);}
110117
String & operator += (int num) {concat(num); return (*this);}
111118
String & operator += (unsigned int num) {concat(num); return (*this);}
112119
String & operator += (long num) {concat(num); return (*this);}
113120
String & operator += (unsigned long num) {concat(num); return (*this);}
121+
String & operator += (float num) {concat(num); return (*this);}
122+
String & operator += (double num) {concat(num); return (*this);}
123+
String & operator += (const __FlashStringHelper *str){concat(str); return (*this);}
114124

115125
friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
116126
friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
@@ -120,6 +130,9 @@ class String
120130
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
121131
friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
122132
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
133+
friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
134+
friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
135+
friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs);
123136

124137
// comparison (only works w/ Strings and "strings")
125138
operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
@@ -158,18 +171,21 @@ class String
158171
int lastIndexOf( char ch, unsigned int fromIndex ) const;
159172
int lastIndexOf( const String &str ) const;
160173
int lastIndexOf( const String &str, unsigned int fromIndex ) const;
161-
String substring( unsigned int beginIndex ) const;
174+
String substring( unsigned int beginIndex ) const { return substring(beginIndex, len); };
162175
String substring( unsigned int beginIndex, unsigned int endIndex ) const;
163176

164177
// modification
165178
void replace(char find, char replace);
166179
void replace(const String& find, const String& replace);
180+
void remove(unsigned int index);
181+
void remove(unsigned int index, unsigned int count);
167182
void toLowerCase(void);
168183
void toUpperCase(void);
169184
void trim(void);
170185

171186
// parsing/conversion
172187
long toInt(void) const;
188+
float toFloat(void) const;
173189

174190
protected:
175191
char *buffer; // the actual char array
@@ -184,6 +200,7 @@ class String
184200

185201
// copy and move
186202
String & copy(const char *cstr, unsigned int length);
203+
String & copy(const __FlashStringHelper *pstr, unsigned int length);
187204
#ifdef __GXX_EXPERIMENTAL_CXX0X__
188205
void move(String &rhs);
189206
#endif
@@ -200,6 +217,8 @@ class StringSumHelper : public String
200217
StringSumHelper(unsigned int num) : String(num) {}
201218
StringSumHelper(long num) : String(num) {}
202219
StringSumHelper(unsigned long num) : String(num) {}
220+
StringSumHelper(float num) : String(num) {}
221+
StringSumHelper(double num) : String(num) {}
203222
};
204223

205224
#endif // __cplusplus

0 commit comments

Comments
 (0)