From 58d04ab3a3a0b13524a862168bed1c3d0708766e Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sat, 19 Mar 2011 11:14:17 -0400 Subject: Return an invalid string (not a partial one) when operator+() fails. --- cores/arduino/WString.cpp | 53 ++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 26 deletions(-) (limited to 'cores/arduino/WString.cpp') diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp index 516bd20..61ce375 100644 --- a/cores/arduino/WString.cpp +++ b/cores/arduino/WString.cpp @@ -112,6 +112,13 @@ inline void String::init(void) flags = 0; } +void String::invalidate(void) +{ + if (buffer) free(buffer); + buffer = NULL; + capacity = len = 0; +} + unsigned char String::reserve(unsigned int size) { if (buffer && capacity >= size) return 1; @@ -140,11 +147,7 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) String & String::copy(const char *cstr, unsigned int length) { if (!reserve(length)) { - if (buffer) { - free(buffer); - buffer = NULL; - } - len = capacity = 0; + invalidate(); return *this; } len = length; @@ -177,7 +180,11 @@ void String::move(String &rhs) String & String::operator = (const String &rhs) { if (this == &rhs) return *this; - return copy(rhs.buffer, rhs.len); + + if (rhs.buffer) copy(rhs.buffer, rhs.len); + else invalidate(); + + return *this; } #ifdef __GXX_EXPERIMENTAL_CXX0X__ @@ -196,16 +203,9 @@ String & String::operator = (StringSumHelper &&rval) String & String::operator = (const char *cstr) { - if (cstr) { - copy(cstr, strlen(cstr)); - } else { - if (buffer) { - free(buffer); - capacity = 0; - buffer = NULL; - } - len = 0; - } + if (cstr) copy(cstr, strlen(cstr)); + else invalidate(); + return *this; } @@ -229,7 +229,8 @@ unsigned char String::concat(const String &s) unsigned char String::concat(const char *cstr, unsigned int length) { unsigned int newlen = len + length; - if (!cstr || length == 0) return 1; // nothing to append = success + if (!cstr) return 0; + if (length == 0) return 1; if (!reserve(newlen)) return 0; strcpy(buffer + len, cstr); len = newlen; @@ -238,7 +239,7 @@ unsigned char String::concat(const char *cstr, unsigned int length) unsigned char String::concat(const char *cstr) { - if (!cstr) return 1; // nothing to append = success + if (!cstr) return 0; return concat(cstr, strlen(cstr)); } @@ -285,56 +286,56 @@ unsigned char String::concat(unsigned long num) StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs) { StringSumHelper &a = const_cast(lhs); - a.concat(rhs.buffer, rhs.len); + if (!a.concat(rhs.buffer, rhs.len)) a.invalidate(); return a; } StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr) { StringSumHelper &a = const_cast(lhs); - if (cstr) a.concat(cstr, strlen(cstr)); + if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate(); return a; } StringSumHelper & operator + (const StringSumHelper &lhs, char c) { StringSumHelper &a = const_cast(lhs); - a.concat(c); + if (!a.concat(c)) a.invalidate(); return a; } StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char c) { StringSumHelper &a = const_cast(lhs); - a.concat(c); + if (!a.concat(c)) a.invalidate(); return a; } StringSumHelper & operator + (const StringSumHelper &lhs, int num) { StringSumHelper &a = const_cast(lhs); - a.concat(num); + if (!a.concat(num)) a.invalidate(); return a; } StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num) { StringSumHelper &a = const_cast(lhs); - a.concat(num); + if (!a.concat(num)) a.invalidate(); return a; } StringSumHelper & operator + (const StringSumHelper &lhs, long num) { StringSumHelper &a = const_cast(lhs); - a.concat(num); + if (!a.concat(num)) a.invalidate(); return a; } StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num) { StringSumHelper &a = const_cast(lhs); - a.concat(num); + if (!a.concat(num)) a.invalidate(); return a; } -- cgit v1.2.3-18-g5258