From 6bef2ada0627be776e463661ccbcdeba5a373f3a Mon Sep 17 00:00:00 2001 From: Ryan Esteves Date: Wed, 5 Jun 2013 14:08:59 -0400 Subject: Added remove methods to WString --- cores/arduino/WString.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'cores/arduino/WString.cpp') 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; -- cgit v1.2.3-18-g5258 From c8a79d0d0c999997d2ee9cd5b773b5ff6561e130 Mon Sep 17 00:00:00 2001 From: Tevin Zhang Date: Wed, 29 May 2013 10:42:07 +0800 Subject: add String.toFloat --- cores/arduino/WString.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'cores/arduino/WString.cpp') 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; +} -- cgit v1.2.3-18-g5258