Skip to content

Commit 550b6ad

Browse files
committed
Merged various bugfix / improvements to String class.
Merge branch 'master' into ide-1.5.x
2 parents db286ac + c8a79d0 commit 550b6ad

File tree

4 files changed

+203
-8
lines changed

4 files changed

+203
-8
lines changed

cores/arduino/WString.cpp

+63-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ String::String(unsigned long value, unsigned char base)
106106
*this = buf;
107107
}
108108

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+
109123
String::~String()
110124
{
111125
free(buffer);
@@ -308,6 +322,20 @@ unsigned char String::concat(unsigned long num)
308322
return concat(buf, strlen(buf));
309323
}
310324

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+
311339
unsigned char String::concat(const __FlashStringHelper * str)
312340
{
313341
if (!str) return 0;
@@ -380,6 +408,20 @@ StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
380408
return a;
381409
}
382410

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+
383425
StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
384426
{
385427
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
@@ -643,6 +685,22 @@ void String::replace(const String& find, const String& replace)
643685
}
644686
}
645687

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+
646704
void String::toLowerCase(void)
647705
{
648706
if (!buffer) return;
@@ -681,4 +739,8 @@ long String::toInt(void) const
681739
return 0;
682740
}
683741

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

cores/arduino/WString.h

+13
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class String
6969
explicit String(unsigned int, unsigned char base=10);
7070
explicit String(long, unsigned char base=10);
7171
explicit String(unsigned long, unsigned char base=10);
72+
explicit String(float, int decimalPlaces=6);
73+
explicit String(double, int decimalPlaces=6);
7274
~String(void);
7375

7476
// memory management
@@ -102,6 +104,8 @@ class String
102104
unsigned char concat(unsigned int num);
103105
unsigned char concat(long num);
104106
unsigned char concat(unsigned long num);
107+
unsigned char concat(float num);
108+
unsigned char concat(double num);
105109
unsigned char concat(const __FlashStringHelper * str);
106110

107111
// if there's not enough memory for the concatenated value, the string
@@ -114,6 +118,8 @@ class String
114118
String & operator += (unsigned int num) {concat(num); return (*this);}
115119
String & operator += (long num) {concat(num); return (*this);}
116120
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);}
117123
String & operator += (const __FlashStringHelper *str){concat(str); return (*this);}
118124

119125
friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
@@ -124,6 +130,8 @@ class String
124130
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
125131
friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
126132
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);
127135
friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs);
128136

129137
// comparison (only works w/ Strings and "strings")
@@ -169,12 +177,15 @@ class String
169177
// modification
170178
void replace(char find, char replace);
171179
void replace(const String& find, const String& replace);
180+
void remove(unsigned int index);
181+
void remove(unsigned int index, unsigned int count);
172182
void toLowerCase(void);
173183
void toUpperCase(void);
174184
void trim(void);
175185

176186
// parsing/conversion
177187
long toInt(void) const;
188+
float toFloat(void) const;
178189

179190
protected:
180191
char *buffer; // the actual char array
@@ -206,6 +217,8 @@ class StringSumHelper : public String
206217
StringSumHelper(unsigned int num) : String(num) {}
207218
StringSumHelper(long num) : String(num) {}
208219
StringSumHelper(unsigned long num) : String(num) {}
220+
StringSumHelper(float num) : String(num) {}
221+
StringSumHelper(double num) : String(num) {}
209222
};
210223

211224
#endif // __cplusplus

cores/robot/WString.cpp

+107-6
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
/*********************************************/
@@ -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+
}

0 commit comments

Comments
 (0)