diff options
author | Cristian Maglie <c.maglie@bug.st> | 2013-06-06 19:53:31 +0200 |
---|---|---|
committer | Cristian Maglie <c.maglie@bug.st> | 2013-06-06 19:54:58 +0200 |
commit | 550b6adcfc4e2dba5678155a883eecdf0840c8e9 (patch) | |
tree | ac7613d448a3c10138bed97909dd4dbf5177d0f2 /cores/arduino | |
parent | db286ac0c1305f9f6338acc37da7c24ebdc9243b (diff) | |
parent | c8a79d0d0c999997d2ee9cd5b773b5ff6561e130 (diff) |
Merged various bugfix / improvements to String class.
Merge branch 'master' into ide-1.5.x
Diffstat (limited to 'cores/arduino')
-rw-r--r-- | cores/arduino/WString.cpp | 64 | ||||
-rw-r--r-- | cores/arduino/WString.h | 13 |
2 files changed, 76 insertions, 1 deletions
diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp index 5951a64..ace128d 100644 --- a/cores/arduino/WString.cpp +++ b/cores/arduino/WString.cpp @@ -106,6 +106,20 @@ String::String(unsigned long value, unsigned char base) *this = buf; } +String::String(float value, int decimalPlaces) +{ + init(); + char buf[33]; + *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); +} + +String::String(double value, int decimalPlaces) +{ + init(); + char buf[33]; + *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); +} + String::~String() { free(buffer); @@ -308,6 +322,20 @@ unsigned char String::concat(unsigned long num) return concat(buf, strlen(buf)); } +unsigned char String::concat(float num) +{ + char buf[20]; + char* string = dtostrf(num, 8, 6, buf); + return concat(string, strlen(string)); +} + +unsigned char String::concat(double num) +{ + char buf[20]; + char* string = dtostrf(num, 8, 6, buf); + return concat(string, strlen(string)); +} + unsigned char String::concat(const __FlashStringHelper * str) { if (!str) return 0; @@ -380,6 +408,20 @@ StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num) return a; } +StringSumHelper & operator + (const StringSumHelper &lhs, float num) +{ + StringSumHelper &a = const_cast<StringSumHelper&>(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, double num) +{ + StringSumHelper &a = const_cast<StringSumHelper&>(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs) { StringSumHelper &a = const_cast<StringSumHelper&>(lhs); @@ -643,6 +685,22 @@ void String::replace(const String& find, const String& replace) } } +void String::remove(unsigned int index){ + if (index >= len) { return; } + int count = len - index; + remove(index, count); +} + +void String::remove(unsigned int index, unsigned int count){ + if (index >= len) { return; } + if (count <= 0) { return; } + if (index + count > len) { count = len - index; } + char *writeTo = buffer + index; + len = len - count; + strncpy(writeTo, buffer + index + count,len - index); + buffer[len] = 0; +} + void String::toLowerCase(void) { if (!buffer) return; @@ -681,4 +739,8 @@ long String::toInt(void) const return 0; } - +float String::toFloat(void) const +{ + if (buffer) return float(atof(buffer)); + return 0; +} diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index c07e1a9..44a8419 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -69,6 +69,8 @@ public: explicit String(unsigned int, unsigned char base=10); explicit String(long, unsigned char base=10); explicit String(unsigned long, unsigned char base=10); + explicit String(float, int decimalPlaces=6); + explicit String(double, int decimalPlaces=6); ~String(void); // memory management @@ -102,6 +104,8 @@ public: unsigned char concat(unsigned int num); unsigned char concat(long num); unsigned char concat(unsigned long num); + unsigned char concat(float num); + unsigned char concat(double num); unsigned char concat(const __FlashStringHelper * str); // if there's not enough memory for the concatenated value, the string @@ -114,6 +118,8 @@ public: String & operator += (unsigned int num) {concat(num); return (*this);} String & operator += (long num) {concat(num); return (*this);} String & operator += (unsigned long num) {concat(num); return (*this);} + String & operator += (float num) {concat(num); return (*this);} + String & operator += (double num) {concat(num); return (*this);} String & operator += (const __FlashStringHelper *str){concat(str); return (*this);} friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs); @@ -124,6 +130,8 @@ public: friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num); friend StringSumHelper & operator + (const StringSumHelper &lhs, long num); friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, float num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, double num); friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs); // comparison (only works w/ Strings and "strings") @@ -169,12 +177,15 @@ public: // modification void replace(char find, char replace); void replace(const String& find, const String& replace); + void remove(unsigned int index); + void remove(unsigned int index, unsigned int count); void toLowerCase(void); void toUpperCase(void); void trim(void); // parsing/conversion long toInt(void) const; + float toFloat(void) const; protected: char *buffer; // the actual char array @@ -206,6 +217,8 @@ public: StringSumHelper(unsigned int num) : String(num) {} StringSumHelper(long num) : String(num) {} StringSumHelper(unsigned long num) : String(num) {} + StringSumHelper(float num) : String(num) {} + StringSumHelper(double num) : String(num) {} }; #endif // __cplusplus |