Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WString: return bool instead of unsigned char #7939

Merged
merged 2 commits into from
May 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 55 additions & 55 deletions cores/esp8266/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,18 @@ void String::invalidate(void) {
init();
}

unsigned char String::reserve(unsigned int size) {
bool String::reserve(unsigned int size) {
if (buffer() && capacity() >= size)
return 1;
return true;
if (changeBuffer(size)) {
if (len() == 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?
if (maxStrLen < sizeof(sso.buff) - 1) {
if (isSSO() || !buffer()) {
Expand All @@ -175,7 +175,7 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
memcpy(wbuffer(), temp, maxStrLen);
free((void *)temp);
}
return 1;
return true;
}
// Fallthrough to normal allocator
size_t newSize = (maxStrLen + 16) & (~0xf);
Expand All @@ -189,7 +189,7 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
#endif
// Make sure we can fit newsize in the buffer
if (newSize > CAPACITY_MAX) {
return 0;
return false;
}
uint16_t oldLen = len();
char *newbuffer = (char *)realloc(isSSO() ? nullptr : wbuffer(), newSize);
Expand All @@ -206,9 +206,9 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
setCapacity(newSize - 1);
setLen(oldLen); // Needed in case of SSO where len() never existed
setBuffer(newbuffer);
return 1;
return true;
}
return 0;
return false;
}

/*********************************************/
Expand Down Expand Up @@ -277,111 +277,111 @@ String &String::operator =(const __FlashStringHelper *pstr) {
/* 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
// realloc'ing the buffer and moving s.buffer in the method called
if (&s == this) {
unsigned int newlen = 2 * len();
if (!s.buffer())
return 0;
return false;
if (s.len() == 0)
return 1;
return true;
if (!reserve(newlen))
return 0;
return false;
memmove_P(wbuffer() + len(), buffer(), len());
setLen(newlen);
wbuffer()[newlen] = 0;
return 1;
return true;
} else {
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;
if (!cstr)
return 0;
return false;
if (length == 0)
return 1;
return true;
if (!reserve(newlen))
return 0;
return false;
memmove_P(wbuffer() + len(), cstr, length + 1);
setLen(newlen);
wbuffer()[newlen] = 0;
return 1;
return true;
}

unsigned char String::concat(const char *cstr) {
bool String::concat(const char *cstr) {
if (!cstr)
return 0;
return false;
return concat(cstr, strlen(cstr));
}

unsigned char String::concat(char c) {
bool String::concat(char c) {
return concat(&c, 1);
}

unsigned char String::concat(unsigned char num) {
bool String::concat(unsigned char num) {
char buf[1 + 3 * sizeof(unsigned char)];
return concat(buf, sprintf(buf, "%d", num));
}

unsigned char String::concat(int num) {
bool String::concat(int num) {
char buf[2 + 3 * sizeof(int)];
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)];
utoa(num, buf, 10);
return concat(buf, strlen(buf));
}

unsigned char String::concat(long num) {
bool String::concat(long num) {
char buf[2 + 3 * sizeof(long)];
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)];
ultoa(num, buf, 10);
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)];
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)];
return concat(buf, sprintf(buf, "%llu", num));
}

unsigned char String::concat(float num) {
bool String::concat(float num) {
char buf[20];
char *string = dtostrf(num, 4, 2, buf);
return concat(string, strlen(string));
}

unsigned char String::concat(double num) {
bool String::concat(double num) {
char buf[20];
char *string = dtostrf(num, 4, 2, buf);
return concat(string, strlen(string));
}

unsigned char String::concat(const __FlashStringHelper *str) {
bool String::concat(const __FlashStringHelper *str) {
if (!str)
return 0;
return false;
int length = strlen_P((PGM_P)str);
if (length == 0)
return 1;
return true;
unsigned int newlen = len() + length;
if (!reserve(newlen))
return 0;
return false;
memcpy_P(wbuffer() + len(), (PGM_P)str, length + 1);
setLen(newlen);
return 1;
return true;
}

/*********************************************/
Expand Down Expand Up @@ -488,48 +488,48 @@ int String::compareTo(const String &s) const {
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);
}

unsigned char String::equals(const char *cstr) const {
bool String::equals(const char *cstr) const {
if (len() == 0)
return (cstr == NULL || *cstr == 0);
if (cstr == NULL)
return buffer()[0] == 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;
}

unsigned char String::operator>(const String &rhs) const {
bool String::operator>(const String &rhs) const {
return compareTo(rhs) > 0;
}

unsigned char String::operator<=(const String &rhs) const {
bool String::operator<=(const String &rhs) const {
return compareTo(rhs) <= 0;
}

unsigned char String::operator>=(const String &rhs) const {
bool String::operator>=(const String &rhs) const {
return compareTo(rhs) >= 0;
}

unsigned char String::equalsIgnoreCase(const String &s2) const {
bool String::equalsIgnoreCase(const String &s2) const {
if (this == &s2)
return 1;
return true;
if (len() != s2.len())
return 0;
return false;
if (len() == 0)
return 1;
return true;
const char *p1 = buffer();
const char *p2 = s2.buffer();
while (*p1) {
if (tolower(*p1++) != tolower(*p2++))
return 0;
return false;
}
return 1;
return true;
}

unsigned char String::equalsConstantTime(const String &s2) const {
Expand Down Expand Up @@ -559,21 +559,21 @@ unsigned char String::equalsConstantTime(const String &s2) const {
return (equalcond & diffcond); //bitwise AND
}

unsigned char String::startsWith(const String &s2) const {
bool String::startsWith(const String &s2) const {
if (len() < s2.len())
return 0;
return false;
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())
return 0;
return false;
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())
return 0;
return false;
return strcmp(&buffer()[len() - s2.len()], s2.buffer()) == 0;
}

Expand All @@ -597,7 +597,7 @@ char &String::operator[](unsigned int index) {

char String::operator[](unsigned int index) const {
if (index >= len() || !buffer())
return 0;
return '\0';
return buffer()[index];
}

Expand Down
Loading