From 99e642a26dc2a406cdfa2d43665f45749b63d5c8 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sat, 26 Mar 2011 18:52:54 -0400 Subject: String: removing implicit numeric conversions and new approach to "if (s)". This makes explicit the String constructors that take numeric types and chars and removes the versions of concat() and operator=() and operator+() that accept numberic types. It also replaces the operator bool() with a operator that converts to a function pointer. This allows for uses like "if (s)" but not "s + 123". See: http://www.artima.com/cppsource/safebool.html. This allowed removing the disambiguating operator+() functions and relying solely on StringSumHelper and anonymous temporaries once again. Also, now treating unsigned char's like int when constructing Strings from them, i.e. String(byte(65)) is now "65" not "A". This is consistent with the new behavior of Serial.print(byte). --- cores/arduino/WString.cpp | 106 +++++----------------------------------------- 1 file changed, 10 insertions(+), 96 deletions(-) (limited to 'cores/arduino/WString.cpp') diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp index 61ce375..d057849 100644 --- a/cores/arduino/WString.cpp +++ b/cores/arduino/WString.cpp @@ -54,16 +54,21 @@ String::String(StringSumHelper &&rval) String::String(char c) { init(); - *this = c; + char buf[2]; + buf[0] = c; + buf[1] = 0; + *this = buf; } -String::String(unsigned char c) +String::String(unsigned char value, unsigned char base) { init(); - *this = (char)c; + char buf[9]; + utoa(value, buf, base); + *this = buf; } -String::String(const int value, unsigned char base) +String::String(int value, unsigned char base) { init(); char buf[18]; @@ -75,7 +80,7 @@ String::String(unsigned int value, unsigned char base) { init(); char buf[17]; - utoa(value, buf, base); + utoa(value, buf, base); *this = buf; } @@ -209,14 +214,6 @@ String & String::operator = (const char *cstr) return *this; } -String & String::operator = (char c) -{ - char buf[2]; - buf[0] = c; - buf[1] = 0; - return copy(buf, 1); -} - /*********************************************/ /* concat */ /*********************************************/ @@ -243,42 +240,6 @@ unsigned char String::concat(const char *cstr) return concat(cstr, strlen(cstr)); } -unsigned char String::concat(char c) -{ - char buf[2]; - buf[0] = c; - buf[1] = 0; - return concat(buf, 1); -} - -unsigned char String::concat(int num) -{ - char buf[7]; - itoa(num, buf, 10); - return concat(buf, strlen(buf)); -} - -unsigned char String::concat(unsigned int num) -{ - char buf[6]; - utoa(num, buf, 10); - return concat(buf, strlen(buf)); -} - -unsigned char String::concat(long num) -{ - char buf[12]; - ltoa(num, buf, 10); - return concat(buf, strlen(buf)); -} - -unsigned char String::concat(unsigned long num) -{ - char buf[11]; - ultoa(num, buf, 10); - return concat(buf, strlen(buf)); -} - /*********************************************/ /* Concatenate */ /*********************************************/ @@ -297,57 +258,10 @@ StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr) return a; } -StringSumHelper & operator + (const StringSumHelper &lhs, char c) -{ - StringSumHelper &a = const_cast(lhs); - if (!a.concat(c)) a.invalidate(); - return a; -} - -StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char c) -{ - StringSumHelper &a = const_cast(lhs); - if (!a.concat(c)) a.invalidate(); - return a; -} - -StringSumHelper & operator + (const StringSumHelper &lhs, int num) -{ - StringSumHelper &a = const_cast(lhs); - if (!a.concat(num)) a.invalidate(); - return a; -} - -StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num) -{ - StringSumHelper &a = const_cast(lhs); - if (!a.concat(num)) a.invalidate(); - return a; -} - -StringSumHelper & operator + (const StringSumHelper &lhs, long num) -{ - StringSumHelper &a = const_cast(lhs); - if (!a.concat(num)) a.invalidate(); - return a; -} - -StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num) -{ - StringSumHelper &a = const_cast(lhs); - if (!a.concat(num)) a.invalidate(); - return a; -} - /*********************************************/ /* Comparison */ /*********************************************/ -String::operator bool() const -{ - return !!buffer; -} - int String::compareTo(const String &s) const { if (!buffer || !s.buffer) { -- cgit v1.2.3-18-g5258