diff --git a/hardware/arduino/cores/arduino/WString.cpp b/hardware/arduino/cores/arduino/WString.cpp index 61ce3755b..d057849aa 100644 --- a/hardware/arduino/cores/arduino/WString.cpp +++ b/hardware/arduino/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) { diff --git a/hardware/arduino/cores/arduino/WString.h b/hardware/arduino/cores/arduino/WString.h index 180f58e5a..bbbe7e80a 100644 --- a/hardware/arduino/cores/arduino/WString.h +++ b/hardware/arduino/cores/arduino/WString.h @@ -41,6 +41,12 @@ class StringSumHelper; // The string class class String { + // use a function pointer to allow for "if (s)" without the + // complications of an operator bool(). for more information, see: + // http://www.artima.com/cppsource/safebool.html + typedef void (String::*StringIfHelperType)() const; + void StringIfHelper() const {} + public: // constructors // creates a copy of the initial value. @@ -53,12 +59,12 @@ public: String(String &&rval); String(StringSumHelper &&rval); #endif - String(char c); - String(unsigned char c); - String(int, unsigned char base=10); - String(unsigned int, unsigned char base=10); - String(long, unsigned char base=10); - String(unsigned long, unsigned char base=10); + explicit String(char c); + explicit String(unsigned char, unsigned char base=10); + explicit String(int, unsigned char base=10); + explicit String(unsigned int, unsigned char base=10); + explicit String(long, unsigned char base=10); + explicit String(unsigned long, unsigned char base=10); ~String(void); // memory management @@ -77,7 +83,6 @@ public: String & operator = (String &&rval); String & operator = (StringSumHelper &&rval); #endif - String & operator = (char c); // concat // returns true on success, false on failure (in which case, the string @@ -85,36 +90,19 @@ public: // concatenation is considered unsucessful. unsigned char concat(const String &str); unsigned char concat(const char *cstr); - unsigned char concat(char c); - unsigned char concat(unsigned char c) {return concat((char)c);} - unsigned char concat(int num); - unsigned char concat(unsigned int num); - unsigned char concat(long num); - unsigned char concat(unsigned long num); // if there's not enough memory for the concatenated value, the string // will be left unchanged (but this isn't signalled in any way) String & operator += (const String &rhs) {concat(rhs); return (*this);} String & operator += (const char *cstr) {concat(cstr); return (*this);} - String & operator += (char c) {concat(c); return (*this);} - String & operator += (unsigned char c) {concat((char)c); return (*this);} - String & operator += (int num) {concat(num); return (*this);} - 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);} // concatenate friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs); friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr); - friend StringSumHelper & operator + (const StringSumHelper &lhs, char c); - friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char c); - friend StringSumHelper & operator + (const StringSumHelper &lhs, int num); - friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num); - friend StringSumHelper & operator + (const StringSumHelper &lhs, long num); - friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num); // comparison - operator bool() const; + operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; } + int compareTo(const String &s) const; unsigned char equals(const String &s) const; unsigned char equals(const char *cstr) const; @@ -185,48 +173,7 @@ class StringSumHelper : public String public: StringSumHelper(const String &s) : String(s) {} StringSumHelper(const char *p) : String(p) {} - StringSumHelper(char c) : String(c) {} - StringSumHelper(unsigned char c) : String(c) {} - StringSumHelper(int num) : String(num, 10) {} - StringSumHelper(unsigned int num) : String(num, 10) {} - StringSumHelper(long num) : String(num, 10) {} - StringSumHelper(unsigned long num) : String(num, 10) {} }; -inline StringSumHelper operator + (const String &lhs, const String &rhs) - { return StringSumHelper(lhs) + rhs; } - -inline StringSumHelper operator + (const String &lhs, const char *cstr) - { return StringSumHelper(lhs) + cstr; } -inline StringSumHelper operator + (const String &lhs, char c) - { return StringSumHelper(lhs) + c; } -inline StringSumHelper operator + (const String &lhs, unsigned char c) - { return StringSumHelper(lhs) + c; } -inline StringSumHelper operator + (const String &lhs, int num) - { return StringSumHelper(lhs) + num; } -inline StringSumHelper operator + (const String &lhs, unsigned int num) - { return StringSumHelper(lhs) + num; } -inline StringSumHelper operator + (const String &lhs, long num) - { return StringSumHelper(lhs) + num; } -inline StringSumHelper operator + (const String &lhs, unsigned long num) - { return StringSumHelper(lhs) + num; } - -inline StringSumHelper operator + (const char *cstr, const String &rhs) - { return StringSumHelper(cstr) + rhs; } -inline StringSumHelper operator + (char c, const String &rhs) - { return StringSumHelper(c) + rhs; } -inline StringSumHelper operator + (unsigned char c, const String &rhs) - { return StringSumHelper(c) + rhs; } -inline StringSumHelper operator + (int num, const String &rhs) - { return StringSumHelper(num) + rhs; } -inline StringSumHelper operator + (unsigned int num, const String &rhs) - { return StringSumHelper(num) + rhs; } -inline StringSumHelper operator + (long num, const String &rhs) - { return StringSumHelper(num) + rhs; } -inline StringSumHelper operator + (unsigned long num, const String &rhs) - { return StringSumHelper(num) + rhs; } - - - #endif // __cplusplus #endif // String_class_h