aboutsummaryrefslogtreecommitdiff
path: root/cores
diff options
context:
space:
mode:
authorMatt Jenkins <matt@majenko.co.uk>2014-04-01 14:46:13 +0100
committerMatt Jenkins <matt@majenko.co.uk>2014-04-01 14:46:13 +0100
commit908c526c4c10d51ea604a15d6cd97d803e3749dd (patch)
tree97171ee132cc6675db895c22156e85aaf59f1bf3 /cores
parent3fb1c595d1c12e547517e1e0810de9bd079c55a4 (diff)
Import WString from 1.5.6
Diffstat (limited to 'cores')
-rw-r--r--cores/arduino/WString.cpp78
-rw-r--r--cores/arduino/WString.h17
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<StringSumHelper&>(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