diff options
Diffstat (limited to 'cores/arduino/WString.cpp')
-rw-r--r-- | cores/arduino/WString.cpp | 60 |
1 files changed, 50 insertions, 10 deletions
diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp index e19f543..db79da4 100644 --- a/cores/arduino/WString.cpp +++ b/cores/arduino/WString.cpp @@ -38,6 +38,12 @@ String::String(const String &value) *this = value; } +String::String(const __FlashStringHelper *pstr) +{ + init(); + *this = pstr; +} + #ifdef __GXX_EXPERIMENTAL_CXX0X__ String::String(String &&rval) { @@ -113,6 +119,7 @@ String::String(double value, int decimalPlaces) char buf[33]; *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); } + String::~String() { free(buffer); @@ -173,6 +180,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, (const prog_char *)pstr); + return *this; +} + #ifdef __GXX_EXPERIMENTAL_CXX0X__ void String::move(String &rhs) { @@ -227,6 +245,14 @@ String & String::operator = (const char *cstr) return *this; } +String & String::operator = (const __FlashStringHelper *pstr) +{ + if (pstr) copy(pstr, strlen_P((const prog_char *)pstr)); + else invalidate(); + + return *this; +} + /*********************************************/ /* concat */ /*********************************************/ @@ -270,14 +296,14 @@ unsigned char String::concat(unsigned char num) unsigned char String::concat(int num) { - char buf[7]; + char buf[12]; itoa(num, buf, 10); return concat(buf, strlen(buf)); } unsigned char String::concat(unsigned int num) { - char buf[6]; + char buf[11]; utoa(num, buf, 10); return concat(buf, strlen(buf)); } @@ -299,17 +325,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 +421,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<StringSumHelper&>(lhs); + if (!a.concat(rhs)) a.invalidate(); + return a; +} + /*********************************************/ /* Comparison */ /*********************************************/ @@ -567,11 +613,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 +739,6 @@ long String::toInt(void) const return 0; } - float String::toFloat(void) const { if (buffer) return float(atof(buffer)); |