aboutsummaryrefslogtreecommitdiff
path: root/cores/robot/WString.cpp
diff options
context:
space:
mode:
authorCristian Maglie <c.maglie@bug.st>2013-06-06 19:53:31 +0200
committerCristian Maglie <c.maglie@bug.st>2013-06-06 19:54:58 +0200
commit550b6adcfc4e2dba5678155a883eecdf0840c8e9 (patch)
treeac7613d448a3c10138bed97909dd4dbf5177d0f2 /cores/robot/WString.cpp
parentdb286ac0c1305f9f6338acc37da7c24ebdc9243b (diff)
parentc8a79d0d0c999997d2ee9cd5b773b5ff6561e130 (diff)
Merged various bugfix / improvements to String class.
Merge branch 'master' into ide-1.5.x
Diffstat (limited to 'cores/robot/WString.cpp')
-rw-r--r--cores/robot/WString.cpp113
1 files changed, 107 insertions, 6 deletions
diff --git a/cores/robot/WString.cpp b/cores/robot/WString.cpp
index c6839fc..ace128d 100644
--- a/cores/robot/WString.cpp
+++ b/cores/robot/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)
{
@@ -100,6 +106,20 @@ 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);
@@ -160,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)
{
@@ -214,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 */
/*********************************************/
@@ -283,6 +322,32 @@ 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));
+}
+
+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 +408,27 @@ StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
return a;
}
+StringSumHelper & operator + (const StringSumHelper &lhs, float num)
+{
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!a.concat(num)) a.invalidate();
+ return a;
+}
+
+StringSumHelper & operator + (const StringSumHelper &lhs, double num)
+{
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ 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 */
/*********************************************/
@@ -527,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) {
@@ -604,6 +685,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;
@@ -642,4 +739,8 @@ long String::toInt(void) const
return 0;
}
-
+float String::toFloat(void) const
+{
+ if (buffer) return float(atof(buffer));
+ return 0;
+}