mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-21 10:26:06 +03:00
WString: return bool instead of unsigned char (#7939)
Clean up the intent, resulting assembly stays the same.
This commit is contained in:
parent
3a3d1c693d
commit
0aaadd9fa1
@ -147,18 +147,18 @@ void String::invalidate(void) {
|
|||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::reserve(unsigned int size) {
|
bool String::reserve(unsigned int size) {
|
||||||
if (buffer() && capacity() >= size)
|
if (buffer() && capacity() >= size)
|
||||||
return 1;
|
return true;
|
||||||
if (changeBuffer(size)) {
|
if (changeBuffer(size)) {
|
||||||
if (len() == 0)
|
if (len() == 0)
|
||||||
wbuffer()[0] = 0;
|
wbuffer()[0] = 0;
|
||||||
return 1;
|
return true;
|
||||||
}
|
}
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::changeBuffer(unsigned int maxStrLen) {
|
bool String::changeBuffer(unsigned int maxStrLen) {
|
||||||
// Can we use SSO here to avoid allocation?
|
// Can we use SSO here to avoid allocation?
|
||||||
if (maxStrLen < sizeof(sso.buff) - 1) {
|
if (maxStrLen < sizeof(sso.buff) - 1) {
|
||||||
if (isSSO() || !buffer()) {
|
if (isSSO() || !buffer()) {
|
||||||
@ -175,7 +175,7 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
|
|||||||
memcpy(wbuffer(), temp, maxStrLen);
|
memcpy(wbuffer(), temp, maxStrLen);
|
||||||
free((void *)temp);
|
free((void *)temp);
|
||||||
}
|
}
|
||||||
return 1;
|
return true;
|
||||||
}
|
}
|
||||||
// Fallthrough to normal allocator
|
// Fallthrough to normal allocator
|
||||||
size_t newSize = (maxStrLen + 16) & (~0xf);
|
size_t newSize = (maxStrLen + 16) & (~0xf);
|
||||||
@ -189,7 +189,7 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
|
|||||||
#endif
|
#endif
|
||||||
// Make sure we can fit newsize in the buffer
|
// Make sure we can fit newsize in the buffer
|
||||||
if (newSize > CAPACITY_MAX) {
|
if (newSize > CAPACITY_MAX) {
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
uint16_t oldLen = len();
|
uint16_t oldLen = len();
|
||||||
char *newbuffer = (char *)realloc(isSSO() ? nullptr : wbuffer(), newSize);
|
char *newbuffer = (char *)realloc(isSSO() ? nullptr : wbuffer(), newSize);
|
||||||
@ -206,9 +206,9 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
|
|||||||
setCapacity(newSize - 1);
|
setCapacity(newSize - 1);
|
||||||
setLen(oldLen); // Needed in case of SSO where len() never existed
|
setLen(oldLen); // Needed in case of SSO where len() never existed
|
||||||
setBuffer(newbuffer);
|
setBuffer(newbuffer);
|
||||||
return 1;
|
return true;
|
||||||
}
|
}
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*********************************************/
|
/*********************************************/
|
||||||
@ -277,111 +277,111 @@ String &String::operator =(const __FlashStringHelper *pstr) {
|
|||||||
/* concat */
|
/* concat */
|
||||||
/*********************************************/
|
/*********************************************/
|
||||||
|
|
||||||
unsigned char String::concat(const String &s) {
|
bool String::concat(const String &s) {
|
||||||
// Special case if we're concatting ourself (s += s;) since we may end up
|
// Special case if we're concatting ourself (s += s;) since we may end up
|
||||||
// realloc'ing the buffer and moving s.buffer in the method called
|
// realloc'ing the buffer and moving s.buffer in the method called
|
||||||
if (&s == this) {
|
if (&s == this) {
|
||||||
unsigned int newlen = 2 * len();
|
unsigned int newlen = 2 * len();
|
||||||
if (!s.buffer())
|
if (!s.buffer())
|
||||||
return 0;
|
return false;
|
||||||
if (s.len() == 0)
|
if (s.len() == 0)
|
||||||
return 1;
|
return true;
|
||||||
if (!reserve(newlen))
|
if (!reserve(newlen))
|
||||||
return 0;
|
return false;
|
||||||
memmove_P(wbuffer() + len(), buffer(), len());
|
memmove_P(wbuffer() + len(), buffer(), len());
|
||||||
setLen(newlen);
|
setLen(newlen);
|
||||||
wbuffer()[newlen] = 0;
|
wbuffer()[newlen] = 0;
|
||||||
return 1;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return concat(s.buffer(), s.len());
|
return concat(s.buffer(), s.len());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::concat(const char *cstr, unsigned int length) {
|
bool String::concat(const char *cstr, unsigned int length) {
|
||||||
unsigned int newlen = len() + length;
|
unsigned int newlen = len() + length;
|
||||||
if (!cstr)
|
if (!cstr)
|
||||||
return 0;
|
return false;
|
||||||
if (length == 0)
|
if (length == 0)
|
||||||
return 1;
|
return true;
|
||||||
if (!reserve(newlen))
|
if (!reserve(newlen))
|
||||||
return 0;
|
return false;
|
||||||
memmove_P(wbuffer() + len(), cstr, length + 1);
|
memmove_P(wbuffer() + len(), cstr, length + 1);
|
||||||
setLen(newlen);
|
setLen(newlen);
|
||||||
wbuffer()[newlen] = 0;
|
wbuffer()[newlen] = 0;
|
||||||
return 1;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::concat(const char *cstr) {
|
bool String::concat(const char *cstr) {
|
||||||
if (!cstr)
|
if (!cstr)
|
||||||
return 0;
|
return false;
|
||||||
return concat(cstr, strlen(cstr));
|
return concat(cstr, strlen(cstr));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::concat(char c) {
|
bool String::concat(char c) {
|
||||||
return concat(&c, 1);
|
return concat(&c, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::concat(unsigned char num) {
|
bool String::concat(unsigned char num) {
|
||||||
char buf[1 + 3 * sizeof(unsigned char)];
|
char buf[1 + 3 * sizeof(unsigned char)];
|
||||||
return concat(buf, sprintf(buf, "%d", num));
|
return concat(buf, sprintf(buf, "%d", num));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::concat(int num) {
|
bool String::concat(int num) {
|
||||||
char buf[2 + 3 * sizeof(int)];
|
char buf[2 + 3 * sizeof(int)];
|
||||||
return concat(buf, sprintf(buf, "%d", num));
|
return concat(buf, sprintf(buf, "%d", num));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::concat(unsigned int num) {
|
bool String::concat(unsigned int num) {
|
||||||
char buf[1 + 3 * sizeof(unsigned int)];
|
char buf[1 + 3 * sizeof(unsigned int)];
|
||||||
utoa(num, buf, 10);
|
utoa(num, buf, 10);
|
||||||
return concat(buf, strlen(buf));
|
return concat(buf, strlen(buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::concat(long num) {
|
bool String::concat(long num) {
|
||||||
char buf[2 + 3 * sizeof(long)];
|
char buf[2 + 3 * sizeof(long)];
|
||||||
return concat(buf, sprintf(buf, "%ld", num));
|
return concat(buf, sprintf(buf, "%ld", num));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::concat(unsigned long num) {
|
bool String::concat(unsigned long num) {
|
||||||
char buf[1 + 3 * sizeof(unsigned long)];
|
char buf[1 + 3 * sizeof(unsigned long)];
|
||||||
ultoa(num, buf, 10);
|
ultoa(num, buf, 10);
|
||||||
return concat(buf, strlen(buf));
|
return concat(buf, strlen(buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::concat(long long num) {
|
bool String::concat(long long num) {
|
||||||
char buf[2 + 3 * sizeof(long long)];
|
char buf[2 + 3 * sizeof(long long)];
|
||||||
return concat(buf, sprintf(buf, "%lld", num));
|
return concat(buf, sprintf(buf, "%lld", num));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::concat(unsigned long long num) {
|
bool String::concat(unsigned long long num) {
|
||||||
char buf[1 + 3 * sizeof(unsigned long long)];
|
char buf[1 + 3 * sizeof(unsigned long long)];
|
||||||
return concat(buf, sprintf(buf, "%llu", num));
|
return concat(buf, sprintf(buf, "%llu", num));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::concat(float num) {
|
bool String::concat(float num) {
|
||||||
char buf[20];
|
char buf[20];
|
||||||
char *string = dtostrf(num, 4, 2, buf);
|
char *string = dtostrf(num, 4, 2, buf);
|
||||||
return concat(string, strlen(string));
|
return concat(string, strlen(string));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::concat(double num) {
|
bool String::concat(double num) {
|
||||||
char buf[20];
|
char buf[20];
|
||||||
char *string = dtostrf(num, 4, 2, buf);
|
char *string = dtostrf(num, 4, 2, buf);
|
||||||
return concat(string, strlen(string));
|
return concat(string, strlen(string));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::concat(const __FlashStringHelper *str) {
|
bool String::concat(const __FlashStringHelper *str) {
|
||||||
if (!str)
|
if (!str)
|
||||||
return 0;
|
return false;
|
||||||
int length = strlen_P((PGM_P)str);
|
int length = strlen_P((PGM_P)str);
|
||||||
if (length == 0)
|
if (length == 0)
|
||||||
return 1;
|
return true;
|
||||||
unsigned int newlen = len() + length;
|
unsigned int newlen = len() + length;
|
||||||
if (!reserve(newlen))
|
if (!reserve(newlen))
|
||||||
return 0;
|
return false;
|
||||||
memcpy_P(wbuffer() + len(), (PGM_P)str, length + 1);
|
memcpy_P(wbuffer() + len(), (PGM_P)str, length + 1);
|
||||||
setLen(newlen);
|
setLen(newlen);
|
||||||
return 1;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*********************************************/
|
/*********************************************/
|
||||||
@ -488,11 +488,11 @@ int String::compareTo(const String &s) const {
|
|||||||
return strcmp(buffer(), s.buffer());
|
return strcmp(buffer(), s.buffer());
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::equals(const String &s2) const {
|
bool String::equals(const String &s2) const {
|
||||||
return (len() == s2.len() && compareTo(s2) == 0);
|
return (len() == s2.len() && compareTo(s2) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::equals(const char *cstr) const {
|
bool String::equals(const char *cstr) const {
|
||||||
if (len() == 0)
|
if (len() == 0)
|
||||||
return (cstr == NULL || *cstr == 0);
|
return (cstr == NULL || *cstr == 0);
|
||||||
if (cstr == NULL)
|
if (cstr == NULL)
|
||||||
@ -500,36 +500,36 @@ unsigned char String::equals(const char *cstr) const {
|
|||||||
return strcmp(buffer(), cstr) == 0;
|
return strcmp(buffer(), cstr) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::operator<(const String &rhs) const {
|
bool String::operator<(const String &rhs) const {
|
||||||
return compareTo(rhs) < 0;
|
return compareTo(rhs) < 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::operator>(const String &rhs) const {
|
bool String::operator>(const String &rhs) const {
|
||||||
return compareTo(rhs) > 0;
|
return compareTo(rhs) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::operator<=(const String &rhs) const {
|
bool String::operator<=(const String &rhs) const {
|
||||||
return compareTo(rhs) <= 0;
|
return compareTo(rhs) <= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::operator>=(const String &rhs) const {
|
bool String::operator>=(const String &rhs) const {
|
||||||
return compareTo(rhs) >= 0;
|
return compareTo(rhs) >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::equalsIgnoreCase(const String &s2) const {
|
bool String::equalsIgnoreCase(const String &s2) const {
|
||||||
if (this == &s2)
|
if (this == &s2)
|
||||||
return 1;
|
return true;
|
||||||
if (len() != s2.len())
|
if (len() != s2.len())
|
||||||
return 0;
|
return false;
|
||||||
if (len() == 0)
|
if (len() == 0)
|
||||||
return 1;
|
return true;
|
||||||
const char *p1 = buffer();
|
const char *p1 = buffer();
|
||||||
const char *p2 = s2.buffer();
|
const char *p2 = s2.buffer();
|
||||||
while (*p1) {
|
while (*p1) {
|
||||||
if (tolower(*p1++) != tolower(*p2++))
|
if (tolower(*p1++) != tolower(*p2++))
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
return 1;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::equalsConstantTime(const String &s2) const {
|
unsigned char String::equalsConstantTime(const String &s2) const {
|
||||||
@ -559,21 +559,21 @@ unsigned char String::equalsConstantTime(const String &s2) const {
|
|||||||
return (equalcond & diffcond); //bitwise AND
|
return (equalcond & diffcond); //bitwise AND
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::startsWith(const String &s2) const {
|
bool String::startsWith(const String &s2) const {
|
||||||
if (len() < s2.len())
|
if (len() < s2.len())
|
||||||
return 0;
|
return false;
|
||||||
return startsWith(s2, 0);
|
return startsWith(s2, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::startsWith(const String &s2, unsigned int offset) const {
|
bool String::startsWith(const String &s2, unsigned int offset) const {
|
||||||
if (offset > (unsigned)(len() - s2.len()) || !buffer() || !s2.buffer())
|
if (offset > (unsigned)(len() - s2.len()) || !buffer() || !s2.buffer())
|
||||||
return 0;
|
return false;
|
||||||
return strncmp(&buffer()[offset], s2.buffer(), s2.len()) == 0;
|
return strncmp(&buffer()[offset], s2.buffer(), s2.len()) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::endsWith(const String &s2) const {
|
bool String::endsWith(const String &s2) const {
|
||||||
if (len() < s2.len() || !buffer() || !s2.buffer())
|
if (len() < s2.len() || !buffer() || !s2.buffer())
|
||||||
return 0;
|
return false;
|
||||||
return strcmp(&buffer()[len() - s2.len()], s2.buffer()) == 0;
|
return strcmp(&buffer()[len() - s2.len()], s2.buffer()) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -597,7 +597,7 @@ char &String::operator[](unsigned int index) {
|
|||||||
|
|
||||||
char String::operator[](unsigned int index) const {
|
char String::operator[](unsigned int index) const {
|
||||||
if (index >= len() || !buffer())
|
if (index >= len() || !buffer())
|
||||||
return 0;
|
return '\0';
|
||||||
return buffer()[index];
|
return buffer()[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,13 +45,6 @@ class StringSumHelper;
|
|||||||
|
|
||||||
// The string class
|
// The string class
|
||||||
class String {
|
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:
|
public:
|
||||||
// constructors
|
// constructors
|
||||||
// creates a copy of the initial value.
|
// creates a copy of the initial value.
|
||||||
@ -90,7 +83,7 @@ class String {
|
|||||||
// return true on success, false on failure (in which case, the string
|
// return true on success, false on failure (in which case, the string
|
||||||
// is left unchanged). reserve(0), if successful, will validate an
|
// is left unchanged). reserve(0), if successful, will validate an
|
||||||
// invalid string (i.e., "if (s)" will be true afterwards)
|
// invalid string (i.e., "if (s)" will be true afterwards)
|
||||||
unsigned char reserve(unsigned int size);
|
bool reserve(unsigned int size);
|
||||||
unsigned int length(void) const {
|
unsigned int length(void) const {
|
||||||
return buffer() ? len() : 0;
|
return buffer() ? len() : 0;
|
||||||
}
|
}
|
||||||
@ -119,20 +112,20 @@ class String {
|
|||||||
// returns true on success, false on failure (in which case, the string
|
// returns true on success, false on failure (in which case, the string
|
||||||
// is left unchanged). if the argument is null or invalid, the
|
// is left unchanged). if the argument is null or invalid, the
|
||||||
// concatenation is considered unsuccessful.
|
// concatenation is considered unsuccessful.
|
||||||
unsigned char concat(const String &str);
|
bool concat(const String &str);
|
||||||
unsigned char concat(const char *cstr);
|
bool concat(const char *cstr);
|
||||||
unsigned char concat(char c);
|
bool concat(char c);
|
||||||
unsigned char concat(unsigned char c);
|
bool concat(unsigned char c);
|
||||||
unsigned char concat(int num);
|
bool concat(int num);
|
||||||
unsigned char concat(unsigned int num);
|
bool concat(unsigned int num);
|
||||||
unsigned char concat(long num);
|
bool concat(long num);
|
||||||
unsigned char concat(unsigned long num);
|
bool concat(unsigned long num);
|
||||||
unsigned char concat(long long num);
|
bool concat(long long num);
|
||||||
unsigned char concat(unsigned long long num);
|
bool concat(unsigned long long num);
|
||||||
unsigned char concat(float num);
|
bool concat(float num);
|
||||||
unsigned char concat(double num);
|
bool concat(double num);
|
||||||
unsigned char concat(const __FlashStringHelper *str);
|
bool concat(const __FlashStringHelper *str);
|
||||||
unsigned char concat(const char *cstr, unsigned int length);
|
bool concat(const char *cstr, unsigned int length);
|
||||||
|
|
||||||
// if there's not enough memory for the concatenated value, the string
|
// if there's not enough memory for the concatenated value, the string
|
||||||
// will be left unchanged (but this isn't signalled in any way)
|
// will be left unchanged (but this isn't signalled in any way)
|
||||||
@ -142,44 +135,44 @@ class String {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// comparison (only works w/ Strings and "strings")
|
explicit operator bool() const {
|
||||||
operator StringIfHelperType() const {
|
return buffer() != nullptr;
|
||||||
return buffer() ? &String::StringIfHelper : 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int compareTo(const String &s) const;
|
int compareTo(const String &s) const;
|
||||||
unsigned char equals(const String &s) const;
|
bool equals(const String &s) const;
|
||||||
unsigned char equals(const char *cstr) const;
|
bool equals(const char *cstr) const;
|
||||||
unsigned char operator ==(const String &rhs) const {
|
bool operator ==(const String &rhs) const {
|
||||||
return equals(rhs);
|
return equals(rhs);
|
||||||
}
|
}
|
||||||
unsigned char operator ==(const char *cstr) const {
|
bool operator ==(const char *cstr) const {
|
||||||
return equals(cstr);
|
return equals(cstr);
|
||||||
}
|
}
|
||||||
unsigned char operator !=(const String &rhs) const {
|
bool operator !=(const String &rhs) const {
|
||||||
return !equals(rhs);
|
return !equals(rhs);
|
||||||
}
|
}
|
||||||
unsigned char operator !=(const char *cstr) const {
|
bool operator !=(const char *cstr) const {
|
||||||
return !equals(cstr);
|
return !equals(cstr);
|
||||||
}
|
}
|
||||||
unsigned char operator <(const String &rhs) const;
|
bool operator <(const String &rhs) const;
|
||||||
unsigned char operator >(const String &rhs) const;
|
bool operator >(const String &rhs) const;
|
||||||
unsigned char operator <=(const String &rhs) const;
|
bool operator <=(const String &rhs) const;
|
||||||
unsigned char operator >=(const String &rhs) const;
|
bool operator >=(const String &rhs) const;
|
||||||
unsigned char equalsIgnoreCase(const String &s) const;
|
bool equalsIgnoreCase(const String &s) const;
|
||||||
unsigned char equalsConstantTime(const String &s) const;
|
unsigned char equalsConstantTime(const String &s) const;
|
||||||
unsigned char startsWith(const String &prefix) const;
|
bool startsWith(const String &prefix) const;
|
||||||
unsigned char startsWith(const char *prefix) const {
|
bool startsWith(const char *prefix) const {
|
||||||
return this->startsWith(String(prefix));
|
return this->startsWith(String(prefix));
|
||||||
}
|
}
|
||||||
unsigned char startsWith(const __FlashStringHelper *prefix) const {
|
bool startsWith(const __FlashStringHelper *prefix) const {
|
||||||
return this->startsWith(String(prefix));
|
return this->startsWith(String(prefix));
|
||||||
}
|
}
|
||||||
unsigned char startsWith(const String &prefix, unsigned int offset) const;
|
bool startsWith(const String &prefix, unsigned int offset) const;
|
||||||
unsigned char endsWith(const String &suffix) const;
|
bool endsWith(const String &suffix) const;
|
||||||
unsigned char endsWith(const char *suffix) const {
|
bool endsWith(const char *suffix) const {
|
||||||
return this->endsWith(String(suffix));
|
return this->endsWith(String(suffix));
|
||||||
}
|
}
|
||||||
unsigned char endsWith(const __FlashStringHelper *suffix) const {
|
bool endsWith(const __FlashStringHelper *suffix) const {
|
||||||
return this->endsWith(String(suffix));
|
return this->endsWith(String(suffix));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -310,7 +303,7 @@ class String {
|
|||||||
// `always_inline` attribute is necessary in order to keep inlining.
|
// `always_inline` attribute is necessary in order to keep inlining.
|
||||||
}
|
}
|
||||||
void invalidate(void);
|
void invalidate(void);
|
||||||
unsigned char changeBuffer(unsigned int maxStrLen);
|
bool changeBuffer(unsigned int maxStrLen);
|
||||||
|
|
||||||
// copy or insert at a specific position
|
// copy or insert at a specific position
|
||||||
String ©(const char *cstr, unsigned int length);
|
String ©(const char *cstr, unsigned int length);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user