From 0778f8a3f3afa6c714a0126830600ca9450cbd28 Mon Sep 17 00:00:00 2001 From: Ryan Esteves Date: Wed, 5 Jun 2013 14:08:59 -0400 Subject: [PATCH 1/8] Added remove methods to WString --- hardware/arduino/cores/arduino/WString.cpp | 16 ++++++++++++++++ hardware/arduino/cores/arduino/WString.h | 2 ++ 2 files changed, 18 insertions(+) diff --git a/hardware/arduino/cores/arduino/WString.cpp b/hardware/arduino/cores/arduino/WString.cpp index c6839fc0d..aab2a5476 100644 --- a/hardware/arduino/cores/arduino/WString.cpp +++ b/hardware/arduino/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/hardware/arduino/cores/arduino/WString.h b/hardware/arduino/cores/arduino/WString.h index 642b016c5..b587a3d62 100644 --- a/hardware/arduino/cores/arduino/WString.h +++ b/hardware/arduino/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 82a2c1d3d968fcf852b63ccac5100d241f6ab17c Mon Sep 17 00:00:00 2001 From: Tevin Zhang Date: Wed, 29 May 2013 10:42:07 +0800 Subject: [PATCH 2/8] add String.toFloat --- hardware/arduino/cores/arduino/WString.cpp | 45 ++++++++++++++++++++++ hardware/arduino/cores/arduino/WString.h | 7 ++++ 2 files changed, 52 insertions(+) diff --git a/hardware/arduino/cores/arduino/WString.cpp b/hardware/arduino/cores/arduino/WString.cpp index aab2a5476..e19f54343 100644 --- a/hardware/arduino/cores/arduino/WString.cpp +++ b/hardware/arduino/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/hardware/arduino/cores/arduino/WString.h b/hardware/arduino/cores/arduino/WString.h index b587a3d62..2d372c5af 100644 --- a/hardware/arduino/cores/arduino/WString.h +++ b/hardware/arduino/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 16b7b67c47d559cdfbabec831a073bbef008c7a7 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 28 May 2013 11:12:39 +0200 Subject: [PATCH 3/8] Fixed problem with % processing on .po files. Fixed quote ' processing on I18N lib. --- app/src/processing/app/I18n.java | 22 ++++++++++++++++++---- app/src/processing/app/Sketch.java | 4 ++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/src/processing/app/I18n.java b/app/src/processing/app/I18n.java index 7ca442784..6c7c9079d 100644 --- a/app/src/processing/app/I18n.java +++ b/app/src/processing/app/I18n.java @@ -46,15 +46,29 @@ public class I18n { } public static String _(String s) { + String res; try { - return i18n.getString(s); - } - catch (MissingResourceException e) { - return s; + res = i18n.getString(s); + } catch (MissingResourceException e) { + res = s; } + + // The single % is the arguments selector in .PO files. + // We must put double %% inside the translations to avoid + // getting .PO processing in the way. + res = res.replace("%%", "%"); + + return res; } public static String format(String fmt, Object ... args) { + // Single quote is used to escape curly bracket arguments. + + // - Prevents strings fixed at translation time to be fixed again + fmt = fmt.replace("''", "'"); + // - Replace ' with the escaped version '' + fmt = fmt.replace("'", "''"); + return MessageFormat.format(fmt, args); } diff --git a/app/src/processing/app/Sketch.java b/app/src/processing/app/Sketch.java index 11f7034f6..ee1c82f78 100644 --- a/app/src/processing/app/Sketch.java +++ b/app/src/processing/app/Sketch.java @@ -1634,12 +1634,12 @@ public class Sketch { long textSize = sizes[0]; long dataSize = sizes[1]; System.out.println(I18n - .format(_("Binary sketch size: {0} bytes (of a {1} byte maximum) - {2}% used"), + .format(_("Binary sketch size: {0} bytes (of a {1} byte maximum) - {2}%% used"), textSize, maxTextSize, textSize * 100 / maxTextSize)); if (dataSize >= 0) { if (maxDataSize > 0) { System.out.println(I18n.format( - _("Minimum Memory usage: {0} bytes (of a {1} byte maximum) - {2}% used"), + _("Minimum Memory usage: {0} bytes (of a {1} byte maximum) - {2}%% used"), dataSize, maxDataSize, dataSize * 100 / maxDataSize)); } else { System.out.println(I18n.format(_("Minimum Memory usage: {0} bytes"), dataSize)); From b341a7c7516929fc77fcb777d17a62600f07554e Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 6 Jun 2013 15:37:04 +0200 Subject: [PATCH 4/8] String class: removed deep copy on substring method. Small code cleanup. --- .../arduino/avr/cores/arduino/WString.cpp | 5 ---- hardware/arduino/avr/cores/arduino/WString.h | 2 +- .../arduino/sam/cores/arduino/WString.cpp | 30 +++++++++---------- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/WString.cpp b/hardware/arduino/avr/cores/arduino/WString.cpp index c6839fc0d..77bdd8a13 100644 --- a/hardware/arduino/avr/cores/arduino/WString.cpp +++ b/hardware/arduino/avr/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/hardware/arduino/avr/cores/arduino/WString.h b/hardware/arduino/avr/cores/arduino/WString.h index 642b016c5..8938bf971 100644 --- a/hardware/arduino/avr/cores/arduino/WString.h +++ b/hardware/arduino/avr/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 diff --git a/hardware/arduino/sam/cores/arduino/WString.cpp b/hardware/arduino/sam/cores/arduino/WString.cpp index 4b6ea859c..bd1c2dca6 100644 --- a/hardware/arduino/sam/cores/arduino/WString.cpp +++ b/hardware/arduino/sam/cores/arduino/WString.cpp @@ -150,13 +150,13 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) /* Copy and Move */ /*********************************************/ -String & String::copy(const char *cstr, unsigned int _length) +String & String::copy(const char *cstr, unsigned int length) { - if (!reserve(_length)) { + if (!reserve(length)) { invalidate(); return *this; } - len = _length; + len = length; strcpy(buffer, cstr); return *this; } @@ -224,11 +224,11 @@ unsigned char String::concat(const String &s) return concat(s.buffer, s.len); } -unsigned char String::concat(const char *cstr, unsigned int _length) +unsigned char String::concat(const char *cstr, unsigned int length) { - unsigned int newlen = len + _length; + unsigned int newlen = len + length; if (!cstr) return 0; - if (_length == 0) return 1; + if (length == 0) return 1; if (!reserve(newlen)) return 0; strcpy(buffer + len, cstr); len = newlen; @@ -549,24 +549,24 @@ String String::substring(unsigned int left, unsigned int right) const /* Modification */ /*********************************************/ -void String::replace(char find, char _replace) +void String::replace(char find, char replace) { if (!buffer) return; for (char *p = buffer; *p; p++) { - if (*p == find) *p = _replace; + if (*p == find) *p = replace; } } -void String::replace(const String& find, const String& _replace) +void String::replace(const String& find, const String& replace) { if (len == 0 || find.len == 0) return; - int diff = _replace.len - find.len; + int diff = replace.len - find.len; char *readFrom = buffer; char *foundAt; if (diff == 0) { while ((foundAt = strstr(readFrom, find.buffer)) != NULL) { - memcpy(foundAt, _replace.buffer, _replace.len); - readFrom = foundAt + _replace.len; + memcpy(foundAt, replace.buffer, replace.len); + readFrom = foundAt + replace.len; } } else if (diff < 0) { char *writeTo = buffer; @@ -574,8 +574,8 @@ void String::replace(const String& find, const String& _replace) unsigned int n = foundAt - readFrom; memcpy(writeTo, readFrom, n); writeTo += n; - memcpy(writeTo, _replace.buffer, _replace.len); - writeTo += _replace.len; + memcpy(writeTo, replace.buffer, replace.len); + writeTo += replace.len; readFrom = foundAt + find.len; len += diff; } @@ -594,7 +594,7 @@ void String::replace(const String& find, const String& _replace) memmove(readFrom + diff, readFrom, len - (readFrom - buffer)); len += diff; buffer[len] = 0; - memcpy(buffer + index, _replace.buffer, _replace.len); + memcpy(buffer + index, replace.buffer, replace.len); index--; } } From 1130fede3a2b0af2182131b48f5892b308f7aab6 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 6 Jun 2013 15:41:23 +0200 Subject: [PATCH 5/8] Added support for Flash string on String class. --- build/shared/revisions.txt | 1 + .../arduino/avr/cores/arduino/WString.cpp | 44 +++++++++++++++++++ hardware/arduino/avr/cores/arduino/WString.h | 8 +++- .../arduino/sam/cores/arduino/WString.cpp | 44 +++++++++++++++++++ hardware/arduino/sam/cores/arduino/WString.h | 7 +++ .../arduino/sam/cores/arduino/avr/pgmspace.h | 1 + 6 files changed, 104 insertions(+), 1 deletion(-) diff --git a/build/shared/revisions.txt b/build/shared/revisions.txt index a1a82ca24..e6013e31d 100644 --- a/build/shared/revisions.txt +++ b/build/shared/revisions.txt @@ -14,6 +14,7 @@ ARDUINO 1.5.3 BETA * sam: Added compatibility for avr/pgmspace.h (Paul Stoffregen) * sam: Added serialEvent*() support * sam: Fixed micros() to work with inside interrupts. (stimmer) +* avr: Added support for Flash strings on String class (Jantje) [libraries] * sam: Added CAN library (still in early stage of development) (Palliser) diff --git a/hardware/arduino/avr/cores/arduino/WString.cpp b/hardware/arduino/avr/cores/arduino/WString.cpp index 77bdd8a13..5951a6493 100644 --- a/hardware/arduino/avr/cores/arduino/WString.cpp +++ b/hardware/arduino/avr/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/hardware/arduino/avr/cores/arduino/WString.h b/hardware/arduino/avr/cores/arduino/WString.h index 8938bf971..c07e1a915 100644 --- a/hardware/arduino/avr/cores/arduino/WString.h +++ b/hardware/arduino/avr/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 diff --git a/hardware/arduino/sam/cores/arduino/WString.cpp b/hardware/arduino/sam/cores/arduino/WString.cpp index bd1c2dca6..e024763f6 100644 --- a/hardware/arduino/sam/cores/arduino/WString.cpp +++ b/hardware/arduino/sam/cores/arduino/WString.cpp @@ -39,6 +39,12 @@ String::String(const String &value) *this = value; } +String::String(const __FlashStringHelper *pstr) +{ + init(); + *this = pstr; +} + #ifdef __GXX_EXPERIMENTAL_CXX0X__ String::String(String &&rval) { @@ -161,6 +167,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) { @@ -215,6 +232,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 */ /*********************************************/ @@ -284,6 +309,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 */ /*********************************************/ @@ -344,6 +381,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/hardware/arduino/sam/cores/arduino/WString.h b/hardware/arduino/sam/cores/arduino/WString.h index 2b0647869..e6503308f 100644 --- a/hardware/arduino/sam/cores/arduino/WString.h +++ b/hardware/arduino/sam/cores/arduino/WString.h @@ -26,6 +26,7 @@ #include #include #include +#include // When compiling programs with this class, the following gcc parameters // dramatically increase performance and memory (RAM) efficiency, typically @@ -57,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); @@ -81,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); @@ -99,6 +102,7 @@ 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) @@ -110,6 +114,7 @@ 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 += (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); @@ -119,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; } @@ -183,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 diff --git a/hardware/arduino/sam/cores/arduino/avr/pgmspace.h b/hardware/arduino/sam/cores/arduino/avr/pgmspace.h index aef5b9598..9b344c9b8 100644 --- a/hardware/arduino/sam/cores/arduino/avr/pgmspace.h +++ b/hardware/arduino/sam/cores/arduino/avr/pgmspace.h @@ -24,6 +24,7 @@ typedef uint32_t prog_uint32_t; #define strcat_P(dest, src) strcat((dest), (src)) #define strcmp_P(a, b) strcmp((a), (b)) #define strstr_P(a, b) strstr((a), (b)) +#define strlen_P(a) strlen((a)) #define sprintf_P(s, f, ...) sprintf((s), (f), __VA_ARGS__) #define pgm_read_byte(addr) (*(const unsigned char *)(addr)) From ba7fb5518f827ce74beab0fa3115617b728ca5ef Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 6 Jun 2013 20:04:43 +0200 Subject: [PATCH 6/8] Fixed buffer overflow on String class (Paul Stoffregen) --- hardware/arduino/avr/cores/arduino/WString.cpp | 4 ++-- hardware/arduino/avr/cores/robot/WString.cpp | 4 ++-- hardware/arduino/sam/cores/arduino/WString.cpp | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/WString.cpp b/hardware/arduino/avr/cores/arduino/WString.cpp index ace128dad..8c85cc592 100644 --- a/hardware/arduino/avr/cores/arduino/WString.cpp +++ b/hardware/arduino/avr/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/hardware/arduino/avr/cores/robot/WString.cpp b/hardware/arduino/avr/cores/robot/WString.cpp index ace128dad..8c85cc592 100644 --- a/hardware/arduino/avr/cores/robot/WString.cpp +++ b/hardware/arduino/avr/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)); } diff --git a/hardware/arduino/sam/cores/arduino/WString.cpp b/hardware/arduino/sam/cores/arduino/WString.cpp index fa47491a9..de1a2f1b2 100644 --- a/hardware/arduino/sam/cores/arduino/WString.cpp +++ b/hardware/arduino/sam/cores/arduino/WString.cpp @@ -297,14 +297,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)); } From 81d41fc761ca6d53502be6dc832b27bf1d2a0cde Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 6 Jun 2013 20:07:02 +0200 Subject: [PATCH 7/8] Updated revision log --- build/shared/revisions.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build/shared/revisions.txt b/build/shared/revisions.txt index e6013e31d..2fc67e11e 100644 --- a/build/shared/revisions.txt +++ b/build/shared/revisions.txt @@ -15,6 +15,8 @@ ARDUINO 1.5.3 BETA * sam: Added serialEvent*() support * sam: Fixed micros() to work with inside interrupts. (stimmer) * avr: Added support for Flash strings on String class (Jantje) +* Added support for floating point numbers in String class (Tevin Zhang, SebiTimeWaster) +* sam: Fixed String buffer overflows (Paul Stoffregen) [libraries] * sam: Added CAN library (still in early stage of development) (Palliser) From a2e7413d229812ff123cb8864747558b270498f1 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 6 Jun 2013 23:11:43 +0200 Subject: [PATCH 8/8] More efficient dtostrf() emulation on ARM --- hardware/arduino/sam/cores/arduino/avr/dtostrf.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/hardware/arduino/sam/cores/arduino/avr/dtostrf.c b/hardware/arduino/sam/cores/arduino/avr/dtostrf.c index 1b5884580..51541739c 100644 --- a/hardware/arduino/sam/cores/arduino/avr/dtostrf.c +++ b/hardware/arduino/sam/cores/arduino/avr/dtostrf.c @@ -20,11 +20,8 @@ char *dtostrf (double val, signed char width, unsigned char prec, char *sout) { char fmt[20]; - if (width<0) { - sprintf(fmt, "%%-%d.%df", width, prec); - } else { - sprintf(fmt, "%%%d.%df", width, prec); - } + sprintf(fmt, "%%%d.%df", width, prec); sprintf(sout, fmt, val); return sout; } +