From 3fb1c595d1c12e547517e1e0810de9bd079c55a4 Mon Sep 17 00:00:00 2001 From: Matt Jenkins Date: Tue, 1 Apr 2014 14:02:17 +0100 Subject: Fixed string constructor overloading bug --- cores/arduino/WString.cpp | 4 ++-- cores/arduino/WString.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp index e19f543..47afddf 100644 --- a/cores/arduino/WString.cpp +++ b/cores/arduino/WString.cpp @@ -100,14 +100,14 @@ String::String(unsigned long value, unsigned char base) *this = buf; } -String::String(float value, int decimalPlaces) +String::String(float value, unsigned char decimalPlaces) { init(); char buf[33]; *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); } -String::String(double value, int decimalPlaces) +String::String(double value, unsigned char decimalPlaces) { init(); char buf[33]; diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index 2d372c5..f4da569 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -68,8 +68,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); + explicit String(float, unsigned char decimalPlaces=6); + explicit String(double, unsigned char decimalPlaces=6); ~String(void); // memory management -- cgit v1.2.3-18-g5258 From 908c526c4c10d51ea604a15d6cd97d803e3749dd Mon Sep 17 00:00:00 2001 From: Matt Jenkins Date: Tue, 1 Apr 2014 14:46:13 +0100 Subject: Import WString from 1.5.6 --- cores/arduino/WString.cpp | 78 +++++++++++++++++++++++++++++++++++------------ cores/arduino/WString.h | 17 ++++++++--- 2 files changed, 71 insertions(+), 24 deletions(-) diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp index 47afddf..ed880ce 100644 --- a/cores/arduino/WString.cpp +++ b/cores/arduino/WString.cpp @@ -21,7 +21,6 @@ #include "WString.h" - /*********************************************/ /* Constructors */ /*********************************************/ @@ -38,6 +37,12 @@ String::String(const String &value) *this = value; } +String::String(const __FlashStringHelper *pstr) +{ + init(); + *this = pstr; +} + #ifdef __GXX_EXPERIMENTAL_CXX0X__ String::String(String &&rval) { @@ -63,7 +68,7 @@ String::String(char c) String::String(unsigned char value, unsigned char base) { init(); - char buf[9]; + char buf[1 + 8 * sizeof(unsigned char)]; utoa(value, buf, base); *this = buf; } @@ -71,7 +76,7 @@ String::String(unsigned char value, unsigned char base) String::String(int value, unsigned char base) { init(); - char buf[18]; + char buf[2 + 8 * sizeof(int)]; itoa(value, buf, base); *this = buf; } @@ -79,7 +84,7 @@ String::String(int value, unsigned char base) String::String(unsigned int value, unsigned char base) { init(); - char buf[17]; + char buf[1 + 8 * sizeof(unsigned int)]; utoa(value, buf, base); *this = buf; } @@ -87,7 +92,7 @@ String::String(unsigned int value, unsigned char base) String::String(long value, unsigned char base) { init(); - char buf[34]; + char buf[2 + 8 * sizeof(long)]; ltoa(value, buf, base); *this = buf; } @@ -95,7 +100,7 @@ String::String(long value, unsigned char base) String::String(unsigned long value, unsigned char base) { init(); - char buf[33]; + char buf[1 + 8 * sizeof(unsigned long)]; ultoa(value, buf, base); *this = buf; } @@ -113,6 +118,7 @@ String::String(double value, unsigned char decimalPlaces) char buf[33]; *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); } + String::~String() { free(buffer); @@ -127,7 +133,6 @@ inline void String::init(void) buffer = NULL; capacity = 0; len = 0; - flags = 0; } void String::invalidate(void) @@ -173,6 +178,17 @@ String & String::copy(const char *cstr, unsigned int length) return *this; } +String & String::copy(const __FlashStringHelper *pstr, unsigned int length) +{ + if (!reserve(length)) { + invalidate(); + return *this; + } + len = length; + strcpy_P(buffer, (PGM_P)pstr); + return *this; +} + #ifdef __GXX_EXPERIMENTAL_CXX0X__ void String::move(String &rhs) { @@ -227,6 +243,14 @@ String & String::operator = (const char *cstr) return *this; } +String & String::operator = (const __FlashStringHelper *pstr) +{ + if (pstr) copy(pstr, strlen_P((PGM_P)pstr)); + else invalidate(); + + return *this; +} + /*********************************************/ /* concat */ /*********************************************/ @@ -263,35 +287,35 @@ unsigned char String::concat(char c) unsigned char String::concat(unsigned char num) { - char buf[4]; + char buf[1 + 3 * sizeof(unsigned char)]; itoa(num, buf, 10); return concat(buf, strlen(buf)); } unsigned char String::concat(int num) { - char buf[7]; + char buf[2 + 3 * sizeof(int)]; itoa(num, buf, 10); return concat(buf, strlen(buf)); } unsigned char String::concat(unsigned int num) { - char buf[6]; + char buf[1 + 3 * sizeof(unsigned int)]; utoa(num, buf, 10); return concat(buf, strlen(buf)); } unsigned char String::concat(long num) { - char buf[12]; + char buf[2 + 3 * sizeof(long)]; ltoa(num, buf, 10); return concat(buf, strlen(buf)); } unsigned char String::concat(unsigned long num) { - char buf[11]; + char buf[1 + 3 * sizeof(unsigned long)]; ultoa(num, buf, 10); return concat(buf, strlen(buf)); } @@ -299,17 +323,29 @@ unsigned char String::concat(unsigned long num) unsigned char String::concat(float num) { char buf[20]; - char* string = dtostrf(num, 8, 6, buf); + char* string = dtostrf(num, 4, 2, buf); return concat(string, strlen(string)); } unsigned char String::concat(double num) { char buf[20]; - char* string = dtostrf(num, 8, 6, buf); + char* string = dtostrf(num, 4, 2, buf); return concat(string, strlen(string)); } +unsigned char String::concat(const __FlashStringHelper * str) +{ + if (!str) return 0; + int length = strlen_P((const char *) str); + if (length == 0) return 1; + unsigned int newlen = len + length; + if (!reserve(newlen)) return 0; + strcpy_P(buffer + len, (const char *) str); + len = newlen; + return 1; +} + /*********************************************/ /* Concatenate */ /*********************************************/ @@ -383,6 +419,14 @@ StringSumHelper & operator + (const StringSumHelper &lhs, double num) if (!a.concat(num)) a.invalidate(); return a; } + +StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(rhs)) a.invalidate(); + return a; +} + /*********************************************/ /* Comparison */ /*********************************************/ @@ -567,11 +611,6 @@ int String::lastIndexOf(const String &s2, unsigned int fromIndex) const return found; } -String String::substring( unsigned int left ) const -{ - return substring(left, len); -} - String String::substring(unsigned int left, unsigned int right) const { if (left > right) { @@ -698,7 +737,6 @@ long String::toInt(void) const return 0; } - float String::toFloat(void) const { if (buffer) return float(atof(buffer)); diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index f4da569..7402430 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -58,6 +58,7 @@ public: // be false). String(const char *cstr = ""); String(const String &str); + String(const __FlashStringHelper *str); #ifdef __GXX_EXPERIMENTAL_CXX0X__ String(String &&rval); String(StringSumHelper &&rval); @@ -68,8 +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, unsigned char decimalPlaces=6); - explicit String(double, unsigned char decimalPlaces=6); + explicit String(float, unsigned char decimalPlaces=2); + explicit String(double, unsigned char decimalPlaces=2); ~String(void); // memory management @@ -84,6 +85,7 @@ public: // marked as invalid ("if (s)" will be false). String & operator = (const String &rhs); String & operator = (const char *cstr); + String & operator = (const __FlashStringHelper *str); #ifdef __GXX_EXPERIMENTAL_CXX0X__ String & operator = (String &&rval); String & operator = (StringSumHelper &&rval); @@ -104,6 +106,7 @@ public: 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 // will be left unchanged (but this isn't signalled in any way) @@ -115,6 +118,9 @@ 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); friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr); @@ -126,6 +132,7 @@ public: 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") operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; } @@ -164,7 +171,7 @@ public: int lastIndexOf( char ch, unsigned int fromIndex ) const; int lastIndexOf( const String &str ) const; int lastIndexOf( const String &str, unsigned int fromIndex ) const; - String substring( unsigned int beginIndex ) const; + String substring( unsigned int beginIndex ) const { return substring(beginIndex, len); }; String substring( unsigned int beginIndex, unsigned int endIndex ) const; // modification @@ -184,7 +191,6 @@ protected: char *buffer; // the actual char array unsigned int capacity; // the array length minus one (for the '\0') unsigned int len; // the String length (not counting the '\0') - unsigned char flags; // unused, for future features protected: void init(void); void invalidate(void); @@ -193,6 +199,7 @@ protected: // copy and move String & copy(const char *cstr, unsigned int length); + String & copy(const __FlashStringHelper *pstr, unsigned int length); #ifdef __GXX_EXPERIMENTAL_CXX0X__ void move(String &rhs); #endif @@ -209,6 +216,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 -- cgit v1.2.3-18-g5258