aboutsummaryrefslogtreecommitdiff
path: root/cores/arduino/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/arduino/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/arduino/WString.cpp')
-rw-r--r--cores/arduino/WString.cpp64
1 files changed, 63 insertions, 1 deletions
diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp
index 5951a64..ace128d 100644
--- a/cores/arduino/WString.cpp
+++ b/cores/arduino/WString.cpp
@@ -106,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);
@@ -308,6 +322,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));
+}
+
unsigned char String::concat(const __FlashStringHelper * str)
{
if (!str) return 0;
@@ -380,6 +408,20 @@ 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);
@@ -643,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;
@@ -681,4 +739,8 @@ long String::toInt(void) const
return 0;
}
-
+float String::toFloat(void) const
+{
+ if (buffer) return float(atof(buffer));
+ return 0;
+}