From 6bef2ada0627be776e463661ccbcdeba5a373f3a Mon Sep 17 00:00:00 2001 From: Ryan Esteves Date: Wed, 5 Jun 2013 14:08:59 -0400 Subject: [PATCH 1/5] Added remove methods to WString --- cores/arduino/WString.cpp | 16 ++++++++++++++++ cores/arduino/WString.h | 2 ++ 2 files changed, 18 insertions(+) diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp index c6839fc..aab2a54 100644 --- a/cores/arduino/WString.cpp +++ b/cores/arduino/WString.cpp @@ -604,6 +604,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; diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index 642b016..b587a3d 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -164,6 +164,8 @@ 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); From c8a79d0d0c999997d2ee9cd5b773b5ff6561e130 Mon Sep 17 00:00:00 2001 From: Tevin Zhang Date: Wed, 29 May 2013 10:42:07 +0800 Subject: [PATCH 2/5] add String.toFloat --- cores/arduino/WString.cpp | 45 +++++++++++++++++++++++++++++++++++++++ cores/arduino/WString.h | 7 ++++++ 2 files changed, 52 insertions(+) diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp index aab2a54..e19f543 100644 --- a/cores/arduino/WString.cpp +++ b/cores/arduino/WString.cpp @@ -100,6 +100,19 @@ 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); @@ -283,6 +296,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)); +} + /*********************************************/ /* Concatenate */ /*********************************************/ @@ -343,6 +370,19 @@ StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num) return a; } +StringSumHelper & operator + (const StringSumHelper &lhs, float num) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, double num) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} /*********************************************/ /* Comparison */ /*********************************************/ @@ -659,3 +699,8 @@ long String::toInt(void) const } +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 b587a3d..2d372c5 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -68,6 +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); ~String(void); // memory management @@ -100,6 +102,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); // if there's not enough memory for the concatenated value, the string // will be left unchanged (but this isn't signalled in any way) @@ -120,6 +124,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); // comparison (only works w/ Strings and "strings") operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; } @@ -172,6 +178,7 @@ public: // parsing/conversion long toInt(void) const; + float toFloat(void) const; protected: char *buffer; // the actual char array From 2719777a48418210563bf58199331eefe24969af Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 6 Jun 2013 15:37:04 +0200 Subject: [PATCH 3/5] String class: removed deep copy on substring method. Small code cleanup. --- cores/arduino/WString.cpp | 5 ----- cores/arduino/WString.h | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp index c6839fc..77bdd8a 100644 --- a/cores/arduino/WString.cpp +++ b/cores/arduino/WString.cpp @@ -527,11 +527,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) { diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index 642b016..8938bf9 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -158,7 +158,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 From db286ac0c1305f9f6338acc37da7c24ebdc9243b Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 6 Jun 2013 15:41:23 +0200 Subject: [PATCH 4/5] Added support for Flash string on String class. --- cores/arduino/WString.cpp | 44 +++++++++++++++++++++++++++++++++++++++ cores/arduino/WString.h | 8 ++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp index 77bdd8a..5951a64 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) { @@ -160,6 +166,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) { @@ -214,6 +231,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 */ /*********************************************/ @@ -283,6 +308,18 @@ unsigned char String::concat(unsigned long num) return concat(buf, strlen(buf)); } +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 */ /*********************************************/ @@ -343,6 +380,13 @@ StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num) return a; } +StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(rhs)) a.invalidate(); + return a; +} + /*********************************************/ /* Comparison */ /*********************************************/ diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index 8938bf9..c07e1a9 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); @@ -82,6 +83,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); @@ -100,17 +102,19 @@ public: unsigned char concat(unsigned int num); unsigned char concat(long num); unsigned char concat(unsigned long 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) String & operator += (const String &rhs) {concat(rhs); return (*this);} String & operator += (const char *cstr) {concat(cstr); return (*this);} String & operator += (char c) {concat(c); return (*this);} - String & operator += (unsigned char num) {concat(num); return (*this);} + String & operator += (unsigned char num) {concat(num); return (*this);} String & operator += (int num) {concat(num); return (*this);} 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 += (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); @@ -120,6 +124,7 @@ 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, const __FlashStringHelper *rhs); // comparison (only works w/ Strings and "strings") operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; } @@ -184,6 +189,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 From f25e5e94f7cd529fe57e1cbe94a9aafce94aaddf Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 6 Jun 2013 20:04:43 +0200 Subject: [PATCH 5/5] Fixed buffer overflow on String class (Paul Stoffregen) --- cores/arduino/WString.cpp | 4 ++-- cores/robot/WString.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp index ace128d..8c85cc5 100644 --- a/cores/arduino/WString.cpp +++ b/cores/arduino/WString.cpp @@ -296,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)); } diff --git a/cores/robot/WString.cpp b/cores/robot/WString.cpp index ace128d..8c85cc5 100644 --- a/cores/robot/WString.cpp +++ b/cores/robot/WString.cpp @@ -296,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)); }