aboutsummaryrefslogtreecommitdiff
path: root/cores/arduino/WString.h
diff options
context:
space:
mode:
authorDavid A. Mellis <d.mellis@arduino.cc>2011-03-26 18:52:54 -0400
committerDavid A. Mellis <d.mellis@arduino.cc>2011-03-26 18:52:54 -0400
commit99e642a26dc2a406cdfa2d43665f45749b63d5c8 (patch)
treef18963c97cbb2693df9330f4de66e180e3e618fb /cores/arduino/WString.h
parentcedea72273a1d0b70c83f46808342b23ddaddca7 (diff)
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).
Diffstat (limited to 'cores/arduino/WString.h')
-rw-r--r--cores/arduino/WString.h81
1 files changed, 14 insertions, 67 deletions
diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h
index 180f58e..bbbe7e8 100644
--- a/cores/arduino/WString.h
+++ b/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