diff --git a/cores/esp8266/StreamString.cpp b/cores/esp8266/StreamString.cpp index 7ebf11d0e2..bb99232236 100644 --- a/cores/esp8266/StreamString.cpp +++ b/cores/esp8266/StreamString.cpp @@ -1,66 +1,66 @@ -/** - StreamString.cpp - - Copyright (c) 2015 Markus Sattler. All rights reserved. - This file is part of the esp8266 core for Arduino environment. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - - */ - -#include -#include "StreamString.h" - -size_t StreamString::write(const uint8_t *data, size_t size) { - if(size && data) { - if(reserve(length() + size + 1)) { - memcpy((void *) (buffer + len), (const void *) data, size); - len += size; - *(buffer + len) = 0x00; // add null for string end - return size; - } - } - return 0; -} - -size_t StreamString::write(uint8_t data) { - return concat((char) data); -} - -int StreamString::available() { - return length(); -} - -int StreamString::read() { - if(length()) { - char c = charAt(0); - remove(0, 1); - return c; - - } - return -1; -} - -int StreamString::peek() { - if(length()) { - char c = charAt(0); - return c; - } - return -1; -} - -void StreamString::flush() { -} - +/** + StreamString.cpp + + Copyright (c) 2015 Markus Sattler. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + */ + +#include +#include "StreamString.h" + +size_t StreamString::write(const uint8_t *data, size_t size) { + if(size && data) { + if(reserve(length() + size + 1)) { + memcpy((void *) (wbuffer() + len()), (const void *) data, size); + setLen(len() + size); + *(wbuffer() + len()) = 0x00; // add null for string end + return size; + } + } + return 0; +} + +size_t StreamString::write(uint8_t data) { + return concat((char) data); +} + +int StreamString::available() { + return length(); +} + +int StreamString::read() { + if(length()) { + char c = charAt(0); + remove(0, 1); + return c; + + } + return -1; +} + +int StreamString::peek() { + if(length()) { + char c = charAt(0); + return c; + } + return -1; +} + +void StreamString::flush() { +} + diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index 51c91238e2..4d4fa3bf21 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -129,39 +129,68 @@ String::~String() { // /*********************************************/ inline void String::init(void) { - buffer = NULL; - capacity = 0; - len = 0; + setSSO(false); + setCapacity(0); + setLen(0); + setBuffer(nullptr); } void String::invalidate(void) { - if(buffer) - free(buffer); + if(!sso() && wbuffer()) + free(wbuffer()); init(); } unsigned char String::reserve(unsigned int size) { - if(buffer && capacity >= size) + if(buffer() && capacity() >= size) return 1; if(changeBuffer(size)) { - if(len == 0) - buffer[0] = 0; + if(len() == 0) + wbuffer()[0] = 0; return 1; } return 0; } unsigned char String::changeBuffer(unsigned int maxStrLen) { + // Can we use SSO here to avoid allocation? + if (maxStrLen < sizeof(sso_buf)) { + if (sso() || !buffer()) { + // Already using SSO, nothing to do + setSSO(true); + return 1; + } else { // if bufptr && !sso() + // Using bufptr, need to shrink into sso_buff + char temp[sizeof(sso_buf)]; + memcpy(temp, buffer(), maxStrLen); + free(wbuffer()); + setSSO(true); + memcpy(wbuffer(), temp, maxStrLen); + return 1; + } + } + // Fallthrough to normal allocator size_t newSize = (maxStrLen + 16) & (~0xf); - char *newbuffer = (char *) realloc(buffer, newSize); + // Make sure we can fit newsize in the buffer + if (newSize > CAPACITY_MAX) { + return false; + } + uint16_t oldLen = len(); + char *newbuffer = (char *) realloc(sso() ? nullptr : wbuffer(), newSize); if(newbuffer) { - size_t oldSize = capacity + 1; // include NULL. + size_t oldSize = capacity() + 1; // include NULL. + if (sso()) { + // Copy the SSO buffer into allocated space + memcpy(newbuffer, sso_buf, sizeof(sso_buf)); + } if (newSize > oldSize) { memset(newbuffer + oldSize, 0, newSize - oldSize); } - capacity = newSize - 1; - buffer = newbuffer; + setSSO(false); + setCapacity(newSize - 1); + setLen(oldLen); // Needed in case of SSO where len() never existed + setBuffer(newbuffer); return 1; } return 0; @@ -176,8 +205,8 @@ String & String::copy(const char *cstr, unsigned int length) { invalidate(); return *this; } - len = length; - strcpy(buffer, cstr); + setLen(length); + strcpy(wbuffer(), cstr); return *this; } @@ -186,29 +215,39 @@ String & String::copy(const __FlashStringHelper *pstr, unsigned int length) { invalidate(); return *this; } - len = length; - strcpy_P(buffer, (PGM_P)pstr); + setLen(length); + strcpy_P(wbuffer(), (PGM_P)pstr); return *this; } #ifdef __GXX_EXPERIMENTAL_CXX0X__ void String::move(String &rhs) { - if(buffer) { - if(capacity >= rhs.len) { - strcpy(buffer, rhs.buffer); - len = rhs.len; - rhs.len = 0; + if(buffer()) { + if(capacity() >= rhs.len()) { + strcpy(wbuffer(), rhs.buffer()); + setLen(rhs.len()); + rhs.invalidate(); return; } else { - free(buffer); + if (!sso()) { + free(wbuffer()); + setBuffer(nullptr); + } } } - buffer = rhs.buffer; - capacity = rhs.capacity; - len = rhs.len; - rhs.buffer = NULL; - rhs.capacity = 0; - rhs.len = 0; + if (rhs.sso()) { + setSSO(true); + memcpy(sso_buf, rhs.sso_buf, sizeof(sso_buf)); + } else { + setSSO(false); + setBuffer(rhs.wbuffer()); + } + setCapacity(rhs.capacity()); + setLen(rhs.len()); + rhs.setSSO(false); + rhs.setCapacity(0); + rhs.setLen(0); + rhs.setBuffer(nullptr); } #endif @@ -216,8 +255,8 @@ String & String::operator =(const String &rhs) { if(this == &rhs) return *this; - if(rhs.buffer) - copy(rhs.buffer, rhs.len); + if(rhs.buffer()) + copy(rhs.buffer(), rhs.len()); else invalidate(); @@ -263,32 +302,32 @@ unsigned char 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) + unsigned int newlen = 2 * len(); + if (!s.buffer()) return 0; - if (s.len == 0) + if (s.len() == 0) return 1; if (!reserve(newlen)) return 0; - memcpy(buffer + len, buffer, len); - len = newlen; - buffer[len] = 0; + memcpy(wbuffer() + len(), buffer(), len()); + setLen(newlen); + wbuffer()[len()] = 0; return 1; } else { - return concat(s.buffer, s.len); + return concat(s.buffer(), s.len()); } } unsigned char String::concat(const char *cstr, unsigned int length) { - unsigned int newlen = len + length; + unsigned int newlen = len() + length; if(!cstr) return 0; if(length == 0) return 1; if(!reserve(newlen)) return 0; - strcpy(buffer + len, cstr); - len = newlen; + strcpy(wbuffer() + len(), cstr); + setLen(newlen); return 1; } @@ -351,10 +390,10 @@ unsigned char String::concat(const __FlashStringHelper * str) { if (!str) return 0; int length = strlen_P((PGM_P)str); if (length == 0) return 1; - unsigned int newlen = len + length; + unsigned int newlen = len() + length; if (!reserve(newlen)) return 0; - strcpy_P(buffer + len, (PGM_P)str); - len = newlen; + strcpy_P(wbuffer() + len(), (PGM_P)str); + setLen(newlen); return 1; } @@ -364,7 +403,7 @@ unsigned char String::concat(const __FlashStringHelper * str) { StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs) { StringSumHelper &a = const_cast(lhs); - if(!a.concat(rhs.buffer, rhs.len)) + if(!a.concat(rhs.buffer(), rhs.len())) a.invalidate(); return a; } @@ -435,7 +474,8 @@ StringSumHelper & operator +(const StringSumHelper &lhs, double num) { StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs) { StringSumHelper &a = const_cast(lhs); - if (!a.concat(rhs)) a.invalidate(); + if (!a.concat(rhs)) + a.invalidate(); return a; } @@ -444,26 +484,26 @@ StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHel // /*********************************************/ int String::compareTo(const String &s) const { - if(!buffer || !s.buffer) { - if(s.buffer && s.len > 0) - return 0 - *(unsigned char *) s.buffer; - if(buffer && len > 0) - return *(unsigned char *) buffer; + if(!buffer() || !s.buffer()) { + if(s.buffer() && s.len() > 0) + return 0 - *(unsigned char *) s.buffer(); + if(buffer() && len() > 0) + return *(unsigned char *) buffer(); return 0; } - return strcmp(buffer, s.buffer); + return strcmp(buffer(), s.buffer()); } unsigned char 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 { - if(len == 0) + if(len() == 0) return (cstr == NULL || *cstr == 0); if(cstr == NULL) - return buffer[0] == 0; - return strcmp(buffer, cstr) == 0; + return buffer()[0] == 0; + return strcmp(buffer(), cstr) == 0; } unsigned char String::operator<(const String &rhs) const { @@ -485,12 +525,12 @@ unsigned char String::operator>=(const String &rhs) const { unsigned char String::equalsIgnoreCase(const String &s2) const { if(this == &s2) return 1; - if(len != s2.len) + if(len() != s2.len()) return 0; - if(len == 0) + if(len() == 0) return 1; - const char *p1 = buffer; - const char *p2 = s2.buffer; + const char *p1 = buffer(); + const char *p2 = s2.buffer(); while(*p1) { if(tolower(*p1++) != tolower(*p2++)) return 0; @@ -501,14 +541,14 @@ unsigned char String::equalsIgnoreCase(const String &s2) const { unsigned char String::equalsConstantTime(const String &s2) const { // To avoid possible time-based attacks present function // compares given strings in a constant time. - if(len != s2.len) + if(len() != s2.len()) return 0; //at this point lengths are the same - if(len == 0) + if(len() == 0) return 1; //at this point lenghts are the same and non-zero - const char *p1 = buffer; - const char *p2 = s2.buffer; + const char *p1 = buffer(); + const char *p2 = s2.buffer(); unsigned int equalchars = 0; unsigned int diffchars = 0; while(*p1) { @@ -520,27 +560,27 @@ unsigned char String::equalsConstantTime(const String &s2) const { ++p2; } //the following should force a constant time eval of the condition without a compiler "logical shortcut" - unsigned char equalcond = (equalchars == len); + unsigned char equalcond = (equalchars == len()); unsigned char diffcond = (diffchars == 0); return (equalcond & diffcond); //bitwise AND } unsigned char String::startsWith(const String &s2) const { - if(len < s2.len) + if(len() < s2.len()) return 0; return startsWith(s2, 0); } unsigned char String::startsWith(const String &s2, unsigned int offset) const { - if(offset > len - s2.len || !buffer || !s2.buffer) + if(offset > (unsigned)(len() - s2.len()) || !buffer() || !s2.buffer()) return 0; - 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 { - if(len < s2.len || !buffer || !s2.buffer) + if(len() < s2.len() || !buffer() || !s2.buffer()) return 0; - return strcmp(&buffer[len - s2.len], s2.buffer) == 0; + return strcmp(&buffer()[len() - s2.len()], s2.buffer()) == 0; } // /*********************************************/ @@ -552,36 +592,36 @@ char String::charAt(unsigned int loc) const { } void String::setCharAt(unsigned int loc, char c) { - if(loc < len) - buffer[loc] = c; + if(loc < len()) + wbuffer()[loc] = c; } char & String::operator[](unsigned int index) { static char dummy_writable_char; - if(index >= len || !buffer) { + if(index >= len() || !buffer()) { dummy_writable_char = 0; return dummy_writable_char; } - return buffer[index]; + return wbuffer()[index]; } char String::operator[](unsigned int index) const { - if(index >= len || !buffer) + if(index >= len() || !buffer()) return 0; - return buffer[index]; + return buffer()[index]; } void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const { if(!bufsize || !buf) return; - if(index >= len) { + if(index >= len()) { buf[0] = 0; return; } unsigned int n = bufsize - 1; - if(n > len - index) - n = len - index; - strncpy((char *) buf, buffer + index, n); + if(n > len() - index) + n = len() - index; + strncpy((char *) buf, buffer() + index, n); buf[n] = 0; } @@ -594,12 +634,12 @@ int String::indexOf(char c) const { } int String::indexOf(char ch, unsigned int fromIndex) const { - if(fromIndex >= len) + if(fromIndex >= len()) return -1; - const char* temp = strchr(buffer + fromIndex, ch); + const char* temp = strchr(buffer() + fromIndex, ch); if(temp == NULL) return -1; - return temp - buffer; + return temp - buffer(); } int String::indexOf(const String &s2) const { @@ -607,46 +647,46 @@ int String::indexOf(const String &s2) const { } int String::indexOf(const String &s2, unsigned int fromIndex) const { - if(fromIndex >= len) + if(fromIndex >= len()) return -1; - const char *found = strstr(buffer + fromIndex, s2.buffer); + const char *found = strstr(buffer() + fromIndex, s2.buffer()); if(found == NULL) return -1; - return found - buffer; + return found - buffer(); } int String::lastIndexOf(char theChar) const { - return lastIndexOf(theChar, len - 1); + return lastIndexOf(theChar, len() - 1); } int String::lastIndexOf(char ch, unsigned int fromIndex) const { - if(fromIndex >= len) + if(fromIndex >= len()) return -1; - char tempchar = buffer[fromIndex + 1]; - buffer[fromIndex + 1] = '\0'; - char* temp = strrchr(buffer, ch); - buffer[fromIndex + 1] = tempchar; + char tempchar = buffer()[fromIndex + 1]; + wbuffer()[fromIndex + 1] = '\0'; + char* temp = strrchr(wbuffer(), ch); + wbuffer()[fromIndex + 1] = tempchar; if(temp == NULL) return -1; - return temp - buffer; + return temp - buffer(); } int String::lastIndexOf(const String &s2) const { - return lastIndexOf(s2, len - s2.len); + return lastIndexOf(s2, len() - s2.len()); } int String::lastIndexOf(const String &s2, unsigned int fromIndex) const { - if(s2.len == 0 || len == 0 || s2.len > len) + if(s2.len() == 0 || len() == 0 || s2.len() > len()) return -1; - if(fromIndex >= len) - fromIndex = len - 1; + if(fromIndex >= len()) + fromIndex = len() - 1; int found = -1; - for(char *p = buffer; p <= buffer + fromIndex; p++) { - p = strstr(p, s2.buffer); + for(char *p = wbuffer(); p <= wbuffer() + fromIndex; p++) { + p = strstr(p, s2.buffer()); if(!p) break; - if((unsigned int) (p - buffer) <= fromIndex) - found = p - buffer; + if((unsigned int) (p - wbuffer()) <= fromIndex) + found = p - buffer(); } return found; } @@ -658,14 +698,14 @@ String String::substring(unsigned int left, unsigned int right) const { left = temp; } String out; - if(left >= len) + if(left >= len()) return out; - if(right > len) - right = len; - char temp = buffer[right]; // save the replaced character - buffer[right] = '\0'; - out = buffer + left; // pointer arithmetic - buffer[right] = temp; //restore character + if(right > len()) + right = len(); + char temp = buffer()[right]; // save the replaced character + wbuffer()[right] = '\0'; + out = wbuffer() + left; // pointer arithmetic + wbuffer()[right] = temp; //restore character return out; } @@ -674,54 +714,54 @@ String String::substring(unsigned int left, unsigned int right) const { // /*********************************************/ void String::replace(char find, char replace) { - if(!buffer) + if(!buffer()) return; - for(char *p = buffer; *p; p++) { + for(char *p = wbuffer(); *p; p++) { if(*p == find) *p = replace; } } void String::replace(const String& find, const String& replace) { - if(len == 0 || find.len == 0) + if(len() == 0 || find.len() == 0) return; - int diff = replace.len - find.len; - char *readFrom = buffer; + int diff = replace.len() - find.len(); + char *readFrom = wbuffer(); char *foundAt; if(diff == 0) { - while((foundAt = strstr(readFrom, find.buffer)) != NULL) { - memcpy(foundAt, replace.buffer, replace.len); - readFrom = foundAt + replace.len; + while((foundAt = strstr(readFrom, find.buffer())) != NULL) { + memcpy(foundAt, replace.buffer(), replace.len()); + readFrom = foundAt + replace.len(); } } else if(diff < 0) { - char *writeTo = buffer; - while((foundAt = strstr(readFrom, find.buffer)) != NULL) { + char *writeTo = wbuffer(); + while((foundAt = strstr(readFrom, find.buffer())) != NULL) { unsigned int n = foundAt - readFrom; memcpy(writeTo, readFrom, n); writeTo += n; - memcpy(writeTo, replace.buffer, replace.len); - writeTo += replace.len; - readFrom = foundAt + find.len; - len += diff; + memcpy(writeTo, replace.buffer(), replace.len()); + writeTo += replace.len(); + readFrom = foundAt + find.len(); + setLen(len() + diff); } strcpy(writeTo, readFrom); } else { - unsigned int size = len; // compute size needed for result - while((foundAt = strstr(readFrom, find.buffer)) != NULL) { - readFrom = foundAt + find.len; + unsigned int size = len(); // compute size needed for result + while((foundAt = strstr(readFrom, find.buffer())) != NULL) { + readFrom = foundAt + find.len(); size += diff; } - if(size == len) + if(size == len()) return; - if(size > capacity && !changeBuffer(size)) + if(size > capacity() && !changeBuffer(size)) return; // XXX: tell user! - int index = len - 1; + int index = len() - 1; while(index >= 0 && (index = lastIndexOf(find, index)) >= 0) { - readFrom = buffer + index + find.len; - memmove(readFrom + diff, readFrom, len - (readFrom - buffer)); - len += diff; - buffer[len] = 0; - memcpy(buffer + index, replace.buffer, replace.len); + readFrom = wbuffer() + index + find.len(); + memmove(readFrom + diff, readFrom, len() - (readFrom - buffer())); + setLen(len() + diff); + wbuffer()[len()] = 0; + memcpy(wbuffer() + index, replace.buffer(), replace.len()); index--; } } @@ -735,50 +775,52 @@ void String::remove(unsigned int index) { } void String::remove(unsigned int index, unsigned int count) { - if(index >= len) { + if(index >= len()) { return; } if(count <= 0) { return; } - if(count > len - index) { - count = len - index; + if(count > len() - index) { + count = len() - index; } - char *writeTo = buffer + index; - len = len - count; - memmove(writeTo, buffer + index + count, len - index); - buffer[len] = 0; + char *writeTo = wbuffer() + index; + unsigned int newlen = len() - count; + setLen(newlen); + memmove(writeTo, wbuffer() + index + count, newlen - index); + wbuffer()[newlen] = 0; } void String::toLowerCase(void) { - if(!buffer) + if(!buffer()) return; - for(char *p = buffer; *p; p++) { + for(char *p = wbuffer(); *p; p++) { *p = tolower(*p); } } void String::toUpperCase(void) { - if(!buffer) + if(!buffer()) return; - for(char *p = buffer; *p; p++) { + for(char *p = wbuffer(); *p; p++) { *p = toupper(*p); } } void String::trim(void) { - if(!buffer || len == 0) + if(!buffer() || len() == 0) return; - char *begin = buffer; + char *begin = wbuffer(); while(isspace(*begin)) begin++; - char *end = buffer + len - 1; + char *end = wbuffer() + len() - 1; while(isspace(*end) && end >= begin) end--; - len = end + 1 - begin; - if(begin > buffer) - memmove(buffer, begin, len); - buffer[len] = 0; + unsigned int newlen = end + 1 - begin; + setLen(newlen); + if(begin > buffer()) + memmove(wbuffer(), begin, newlen); + wbuffer()[newlen] = 0; } // /*********************************************/ @@ -786,14 +828,14 @@ void String::trim(void) { // /*********************************************/ long String::toInt(void) const { - if(buffer) - return atol(buffer); + if(buffer()) + return atol(buffer()); return 0; } float String::toFloat(void) const { - if(buffer) - return atof(buffer); + if(buffer()) + return atof(buffer()); return 0; } diff --git a/cores/esp8266/WString.h b/cores/esp8266/WString.h index bbf8ee40ad..aecf9b4a62 100644 --- a/cores/esp8266/WString.h +++ b/cores/esp8266/WString.h @@ -76,8 +76,8 @@ class String { // invalid string (i.e., "if (s)" will be true afterwards) unsigned char reserve(unsigned int size); inline unsigned int length(void) const { - if(buffer) { - return len; + if(buffer()) { + return len(); } else { return 0; } @@ -172,7 +172,7 @@ class String { // comparison (only works w/ Strings and "strings") operator StringIfHelperType() const { - return buffer ? &String::StringIfHelper : 0; + return buffer() ? &String::StringIfHelper : 0; } int compareTo(const String &s) const; unsigned char equals(const String &s) const; @@ -208,9 +208,9 @@ class String { void toCharArray(char *buf, unsigned int bufsize, unsigned int index = 0) const { getBytes((unsigned char *) buf, bufsize, index); } - const char* c_str() const { return buffer; } - char* begin() { return buffer; } - char* end() { return buffer + length(); } + const char* c_str() const { return buffer(); } + char* begin() { return wbuffer(); } + char* end() { return wbuffer() + length(); } const char* begin() const { return c_str(); } const char* end() const { return c_str() + length(); } @@ -224,7 +224,7 @@ class String { int lastIndexOf(const String &str) const; int lastIndexOf(const String &str, unsigned int fromIndex) const; String substring(unsigned int beginIndex) const { - return substring(beginIndex, len); + return substring(beginIndex, len()); } ; String substring(unsigned int beginIndex, unsigned int endIndex) const; @@ -243,9 +243,34 @@ class String { float toFloat(void) const; protected: - char *buffer; // the actual char array - unsigned int capacity; // the array length minus one (for the '\0') - unsigned int len; // the String length (not counting the '\0') + // Contains the string info when we're not in SSO mode + struct _ptr { + char * buff; + uint16_t cap; + uint16_t len; + }; + + // SSO is handled by checking the last byte of sso_buff. + // When not in SSO mode, that byte is set to 0xff, while when in SSO mode it is always 0x00 (so it can serve as the string terminator as well as a flag) + // This allows strings up up to 12 (11 + \0 termination) without any extra space. + enum { SSOSIZE = sizeof(struct _ptr) + 4 }; // Characters to allocate space for SSO, must be 12 or more + enum { CAPACITY_MAX = 65535 }; // If size of capacity changed, be sure to update this enum + union { + struct _ptr ptr; + char sso_buf[SSOSIZE]; + }; + // Accessor functions + inline bool sso() const { return sso_buf[SSOSIZE - 1] == 0; } + inline unsigned int len() const { return sso() ? strlen(sso_buf) : ptr.len; } + inline unsigned int capacity() const { return sso() ? SSOSIZE - 1 : ptr.cap; } + inline void setSSO(bool sso) { sso_buf[SSOSIZE - 1] = sso ? 0x00 : 0xff; } + inline void setLen(int len) { if (!sso()) ptr.len = len; } + inline void setCapacity(int cap) { if (!sso()) ptr.cap = cap; } + inline void setBuffer(char *buff) { if (!sso()) ptr.buff = buff; } + // Buffer accessor functions + inline const char *buffer() const { return (const char *)(sso() ? sso_buf : ptr.buff); } + inline char *wbuffer() const { return sso() ? const_cast(sso_buf) : ptr.buff; } // Writable version of buffer + protected: void init(void); void invalidate(void); diff --git a/tests/host/core/test_string.cpp b/tests/host/core/test_string.cpp index ff13de8c79..c2ef68025f 100644 --- a/tests/host/core/test_string.cpp +++ b/tests/host/core/test_string.cpp @@ -268,3 +268,96 @@ TEST_CASE("String sizes near 8b", "[core][String]") REQUIRE(!strcmp(s16.c_str(),"123456789012345_")); REQUIRE(!strcmp(s17.c_str(),"1234567890123456_")); } + +TEST_CASE("String SSO works", "[core][String]") +{ + // This test assumes that SSO_SIZE==8, if that changes the test must as well + String s; + s += "0"; + REQUIRE(s == "0"); + REQUIRE(s.length() == 1); + const char *savesso = s.c_str(); + s += 1; + REQUIRE(s.c_str() == savesso); + REQUIRE(s == "01"); + REQUIRE(s.length() == 2); + s += "2"; + REQUIRE(s.c_str() == savesso); + REQUIRE(s == "012"); + REQUIRE(s.length() == 3); + s += 3; + REQUIRE(s.c_str() == savesso); + REQUIRE(s == "0123"); + REQUIRE(s.length() == 4); + s += "4"; + REQUIRE(s.c_str() == savesso); + REQUIRE(s == "01234"); + REQUIRE(s.length() == 5); + s += "5"; + REQUIRE(s.c_str() == savesso); + REQUIRE(s == "012345"); + REQUIRE(s.length() == 6); + s += "6"; + REQUIRE(s.c_str() == savesso); + REQUIRE(s == "0123456"); + REQUIRE(s.length() == 7); + s += "7"; + REQUIRE(s.c_str() == savesso); + REQUIRE(s == "01234567"); + REQUIRE(s.length() == 8); + s += "8"; + REQUIRE(s.c_str() == savesso); + REQUIRE(s == "012345678"); + REQUIRE(s.length() == 9); + s += "9"; + REQUIRE(s.c_str() == savesso); + REQUIRE(s == "0123456789"); + REQUIRE(s.length() == 10); + s += "a"; + REQUIRE(s.c_str() == savesso); + REQUIRE(s == "0123456789a"); + REQUIRE(s.length() == 11); + if (sizeof(savesso) == 4) { + s += "b"; + REQUIRE(s.c_str() != savesso); + REQUIRE(s == "0123456789ab"); + REQUIRE(s.length() == 12); + s += "c"; + REQUIRE(s.c_str() != savesso); + REQUIRE(s == "0123456789abc"); + REQUIRE(s.length() == 13); + } else { + s += "bcde"; + REQUIRE(s.c_str() == savesso); + REQUIRE(s == "0123456789abcde"); + REQUIRE(s.length() == 15); + s += "fghi"; + REQUIRE(s.c_str() == savesso); + REQUIRE(s == "0123456789abcdefghi"); + REQUIRE(s.length() == 19); + s += "j"; + REQUIRE(s.c_str() != savesso); + REQUIRE(s == "0123456789abcdefghij"); + REQUIRE(s.length() == 20); + s += "k"; + REQUIRE(s.c_str() != savesso); + REQUIRE(s == "0123456789abcdefghijk"); + REQUIRE(s.length() == 21); + s += "l"; + REQUIRE(s.c_str() != savesso); + REQUIRE(s == "0123456789abcdefghijkl"); + REQUIRE(s.length() == 22); + s += "m"; + REQUIRE(s.c_str() != savesso); + REQUIRE(s == "0123456789abcdefghijklm"); + REQUIRE(s.length() == 23); + s += "nopq"; + REQUIRE(s.c_str() != savesso); + REQUIRE(s == "0123456789abcdefghijklmnopq"); + REQUIRE(s.length() == 27); + s += "rstu"; + REQUIRE(s.c_str() != savesso); + REQUIRE(s == "0123456789abcdefghijklmnopqrstu"); + REQUIRE(s.length() == 31); + } +}