diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index 82468d4018..ce62822af9 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -5,16 +5,17 @@ Copyright 2011, Paul Stoffregen, paul@pjrc.com Modified by Ivan Grokhotkov, 2014 - esp8266 support Modified by Michael C. Miller, 2015 - esp8266 progmem support - + 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 @@ -90,7 +91,7 @@ String::String(unsigned int value, unsigned char base) { String::String(long value, unsigned char base) { init(); char buf[2 + 8 * sizeof(long)]; - if (base==10) { + if (base == 10) { sprintf(buf, "%ld", value); } else { ltoa(value, buf, base); @@ -122,16 +123,16 @@ String::String(double value, unsigned char decimalPlaces) { /*********************************************/ void String::invalidate(void) { - if(!isSSO() && wbuffer()) + if (!isSSO() && 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) + if (changeBuffer(size)) { + if (len() == 0) wbuffer()[0] = 0; return 1; } @@ -146,18 +147,16 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) { uint16_t oldLen = len(); setSSO(true); setLen(oldLen); - return 1; } else { // if bufptr && !isSSO() // Using bufptr, need to shrink into sso.buff - char temp[sizeof(sso.buff)]; - memcpy(temp, buffer(), maxStrLen); - free(wbuffer()); + const char *temp = buffer(); uint16_t oldLen = len(); setSSO(true); setLen(oldLen); memcpy(wbuffer(), temp, maxStrLen); - return 1; + free((void *)temp); } + return 1; } // Fallthrough to normal allocator size_t newSize = (maxStrLen + 16) & (~0xf); @@ -166,15 +165,14 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) { return false; } uint16_t oldLen = len(); - char *newbuffer = (char *) realloc(isSSO() ? nullptr : wbuffer(), newSize); + char *newbuffer = (char *)realloc(isSSO() ? nullptr : wbuffer(), newSize); if (newbuffer) { size_t oldSize = capacity() + 1; // include NULL. if (isSSO()) { // Copy the SSO buffer into allocated space memmove_P(newbuffer, sso.buff, sizeof(sso.buff)); } - if (newSize > oldSize) - { + if (newSize > oldSize) { memset(newbuffer + oldSize, 0, newSize - oldSize); } setSSO(false); @@ -190,7 +188,7 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) { /* Copy and Move */ /*********************************************/ -String & String::copy(const char *cstr, unsigned int length) { +String &String::copy(const char *cstr, unsigned int length) { if (!reserve(length)) { invalidate(); return *this; @@ -200,7 +198,7 @@ String & String::copy(const char *cstr, unsigned int length) { return *this; } -String & String::copy(const __FlashStringHelper *pstr, unsigned int length) { +String &String::copy(const __FlashStringHelper *pstr, unsigned int length) { if (!reserve(length)) { invalidate(); return *this; @@ -216,44 +214,35 @@ void String::move(String &rhs) noexcept { rhs.init(); } -String & String::operator =(const String &rhs) { +String &String::operator =(const String &rhs) { if (this == &rhs) return *this; - if (rhs.buffer()) copy(rhs.buffer(), rhs.len()); else invalidate(); - - return *this; -} - -String & String::operator =(String &&rval) noexcept { - if (this != &rval) - move(rval); return *this; } -String & String::operator =(StringSumHelper &&rval) noexcept { +String &String::operator =(String &&rval) noexcept { if (this != &rval) move(rval); return *this; } -String & String::operator =(const char *cstr) { +String &String::operator =(const char *cstr) { if (cstr) copy(cstr, strlen(cstr)); else invalidate(); - return *this; } -String & String::operator = (const __FlashStringHelper *pstr) -{ - if (pstr) copy(pstr, strlen_P((PGM_P)pstr)); - else invalidate(); - +String &String::operator =(const __FlashStringHelper *pstr) { + if (pstr) + copy(pstr, strlen_P((PGM_P)pstr)); + else + invalidate(); return *this; } @@ -340,22 +329,25 @@ unsigned char String::concat(unsigned long num) { unsigned char String::concat(float num) { char buf[20]; - char* string = dtostrf(num, 4, 2, buf); + char *string = dtostrf(num, 4, 2, buf); return concat(string, strlen(string)); } unsigned char String::concat(double num) { char buf[20]; - char* string = dtostrf(num, 4, 2, buf); + char *string = dtostrf(num, 4, 2, buf); return concat(string, strlen(string)); } -unsigned char String::concat(const __FlashStringHelper * str) { - if (!str) return 0; +unsigned char String::concat(const __FlashStringHelper *str) { + if (!str) + return 0; int length = strlen_P((PGM_P)str); - if (length == 0) return 1; + if (length == 0) + return 1; unsigned int newlen = len() + length; - if (!reserve(newlen)) return 0; + if (!reserve(newlen)) + return 0; memcpy_P(wbuffer() + len(), (PGM_P)str, length + 1); setLen(newlen); return 1; @@ -365,79 +357,79 @@ unsigned char String::concat(const __FlashStringHelper * str) { /* Concatenate */ /*********************************************/ -StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs) { - StringSumHelper &a = const_cast(lhs); +StringSumHelper &operator +(const StringSumHelper &lhs, const String &rhs) { + StringSumHelper &a = const_cast(lhs); if (!a.concat(rhs.buffer(), rhs.len())) a.invalidate(); return a; } -StringSumHelper & operator +(const StringSumHelper &lhs, const char *cstr) { - StringSumHelper &a = const_cast(lhs); +StringSumHelper &operator +(const StringSumHelper &lhs, const char *cstr) { + StringSumHelper &a = const_cast(lhs); if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate(); return a; } -StringSumHelper & operator +(const StringSumHelper &lhs, char c) { - StringSumHelper &a = const_cast(lhs); +StringSumHelper &operator +(const StringSumHelper &lhs, char c) { + StringSumHelper &a = const_cast(lhs); if (!a.concat(c)) a.invalidate(); return a; } -StringSumHelper & operator +(const StringSumHelper &lhs, unsigned char num) { - StringSumHelper &a = const_cast(lhs); +StringSumHelper &operator +(const StringSumHelper &lhs, unsigned char num) { + StringSumHelper &a = const_cast(lhs); if (!a.concat(num)) a.invalidate(); return a; } -StringSumHelper & operator +(const StringSumHelper &lhs, int num) { - StringSumHelper &a = const_cast(lhs); +StringSumHelper &operator +(const StringSumHelper &lhs, int num) { + StringSumHelper &a = const_cast(lhs); if (!a.concat(num)) a.invalidate(); return a; } -StringSumHelper & operator +(const StringSumHelper &lhs, unsigned int num) { - StringSumHelper &a = const_cast(lhs); +StringSumHelper &operator +(const StringSumHelper &lhs, unsigned int num) { + StringSumHelper &a = const_cast(lhs); if (!a.concat(num)) a.invalidate(); return a; } -StringSumHelper & operator +(const StringSumHelper &lhs, long num) { - StringSumHelper &a = const_cast(lhs); +StringSumHelper &operator +(const StringSumHelper &lhs, long num) { + StringSumHelper &a = const_cast(lhs); if (!a.concat(num)) a.invalidate(); return a; } -StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long num) { - StringSumHelper &a = const_cast(lhs); +StringSumHelper &operator +(const StringSumHelper &lhs, unsigned long num) { + StringSumHelper &a = const_cast(lhs); if (!a.concat(num)) a.invalidate(); return a; } -StringSumHelper & operator +(const StringSumHelper &lhs, float num) { - StringSumHelper &a = const_cast(lhs); +StringSumHelper &operator +(const StringSumHelper &lhs, float num) { + StringSumHelper &a = const_cast(lhs); if (!a.concat(num)) a.invalidate(); return a; } -StringSumHelper & operator +(const StringSumHelper &lhs, double num) { - StringSumHelper &a = const_cast(lhs); +StringSumHelper &operator +(const StringSumHelper &lhs, double num) { + StringSumHelper &a = const_cast(lhs); if (!a.concat(num)) a.invalidate(); return a; } -StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs) +StringSumHelper &operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs) { - StringSumHelper &a = const_cast(lhs); + StringSumHelper &a = const_cast(lhs); if (!a.concat(rhs)) a.invalidate(); return a; @@ -448,11 +440,11 @@ 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()); @@ -510,7 +502,7 @@ unsigned char String::equalsConstantTime(const String &s2) const { //at this point lengths are the same if (len() == 0) return 1; - //at this point lenghts are the same and non-zero + //at this point lengths are the same and non-zero const char *p1 = buffer(); const char *p2 = s2.buffer(); unsigned int equalchars = 0; @@ -530,19 +522,19 @@ unsigned char String::equalsConstantTime(const String &s2) const { } 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 > (unsigned)(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; } 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; } @@ -556,7 +548,7 @@ void String::setCharAt(unsigned int loc, char c) { wbuffer()[loc] = c; } -char & String::operator[](unsigned int index) { +char &String::operator[](unsigned int index) { static char dummy_writable_char; if (index >= len() || !buffer()) { dummy_writable_char = 0; @@ -581,7 +573,7 @@ void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int ind unsigned int n = bufsize - 1; if (n > len() - index) n = len() - index; - strncpy((char *) buf, buffer() + index, n); + strncpy((char *)buf, buffer() + index, n); buf[n] = 0; } @@ -596,7 +588,7 @@ int String::indexOf(char c) const { int String::indexOf(char ch, unsigned int fromIndex) const { 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(); @@ -622,13 +614,14 @@ int String::lastIndexOf(char theChar) const { int String::lastIndexOf(char ch, unsigned int fromIndex) const { if (fromIndex >= len()) return -1; - char tempchar = buffer()[fromIndex + 1]; - wbuffer()[fromIndex + 1] = '\0'; - char* temp = strrchr(wbuffer(), ch); - wbuffer()[fromIndex + 1] = tempchar; + char *wbuffer = this->wbuffer(); + char tempchar = wbuffer[fromIndex + 1]; // save the replaced character + wbuffer[fromIndex + 1] = '\0'; + char *temp = strrchr(wbuffer, ch); + wbuffer[fromIndex + 1] = tempchar; // restore character if (temp == NULL) return -1; - return temp - buffer(); + return temp - wbuffer; } int String::lastIndexOf(const String &s2) const { @@ -641,11 +634,11 @@ int String::lastIndexOf(const String &s2, unsigned int fromIndex) const { if (fromIndex >= len()) fromIndex = len() - 1; int found = -1; - for (char *p = wbuffer(); p <= wbuffer() + fromIndex; p++) { + for (const char *p = buffer(); p <= buffer() + fromIndex; p++) { p = strstr(p, s2.buffer()); if (!p) break; - if ((unsigned int) (p - wbuffer()) <= fromIndex) + if ((unsigned int)(p - buffer()) <= fromIndex) found = p - buffer(); } return found; @@ -662,10 +655,11 @@ String String::substring(unsigned int left, unsigned int right) const { return out; 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 + char *wbuffer = this->wbuffer(); + char tempchar = wbuffer[right]; // save the replaced character + wbuffer[right] = '\0'; + out = wbuffer + left; // pointer arithmetic + wbuffer[right] = tempchar; // restore character return out; } @@ -704,7 +698,7 @@ void String::replace(const String& find, const String& replace) { readFrom = foundAt + find.len(); setLen(len() + diff); } - memmove_P(writeTo, readFrom, strlen(readFrom)+1); + memmove_P(writeTo, readFrom, strlen(readFrom) + 1); } else { unsigned int size = len(); // compute size needed for result while ((foundAt = strstr(readFrom, find.buffer())) != NULL) { @@ -732,7 +726,7 @@ void String::remove(unsigned int index) { // Pass the biggest integer as the count. The remove method // below will take care of truncating it at the end of the // string. - remove(index, (unsigned int) -1); + remove(index, (unsigned int)-1); } void String::remove(unsigned int index, unsigned int count) { @@ -797,11 +791,10 @@ long String::toInt(void) const { float String::toFloat(void) const { if (buffer()) return atof(buffer()); - return 0; + return 0.0F; } -double String::toDouble(void) const -{ +double String::toDouble(void) const { if (buffer()) return atof(buffer()); return 0.0; diff --git a/cores/esp8266/WString.h b/cores/esp8266/WString.h index 340e8a2dd6..bc60881577 100644 --- a/cores/esp8266/WString.h +++ b/cores/esp8266/WString.h @@ -3,17 +3,17 @@ ...mostly rewritten by Paul Stoffregen... Copyright (c) 2009-10 Hernando Barragan. All right reserved. Copyright 2011, Paul Stoffregen, paul@pjrc.com - + 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 @@ -78,28 +78,26 @@ class String { // is left unchanged). reserve(0), if successful, will validate an // 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(); - } else { - return 0; - } - } - inline void clear(void) { + unsigned int length(void) const { + return buffer() ? len() : 0; + } + void clear(void) { setLen(0); } - inline bool isEmpty(void) const { + bool isEmpty(void) const { return length() == 0; } // creates a copy of the assigned value. if the value is null or // invalid, or if the memory allocation fails, the string will be // marked as invalid ("if (s)" will be false). - String & operator =(const String &rhs); - String & operator =(const char *cstr); - String & operator = (const __FlashStringHelper *str); - String & operator =(String &&rval) noexcept; - String & operator =(StringSumHelper &&rval) noexcept; + String &operator =(const String &rhs); + String &operator =(const char *cstr); + String &operator =(const __FlashStringHelper *str); + String &operator =(String &&rval) noexcept; + String &operator =(StringSumHelper &&rval) noexcept { + return operator =((String &&)rval); + } // concatenate (works w/ built-in types) @@ -116,67 +114,67 @@ class String { unsigned char concat(unsigned long num); unsigned char concat(float num); unsigned char concat(double num); - unsigned char concat(const __FlashStringHelper * str); + unsigned char concat(const __FlashStringHelper *str); unsigned char concat(const char *cstr, unsigned int length); // 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) { + String &operator +=(const String &rhs) { concat(rhs); - return (*this); + return *this; } - String & operator +=(const char *cstr) { + String &operator +=(const char *cstr) { concat(cstr); - return (*this); + return *this; } - String & operator +=(char c) { + String &operator +=(char c) { concat(c); - return (*this); + return *this; } - String & operator +=(unsigned char num) { + String &operator +=(unsigned char num) { concat(num); - return (*this); + return *this; } - String & operator +=(int num) { + String &operator +=(int num) { concat(num); - return (*this); + return *this; } - String & operator +=(unsigned int num) { + String &operator +=(unsigned int num) { concat(num); - return (*this); + return *this; } - String & operator +=(long num) { + String &operator +=(long num) { concat(num); - return (*this); + return *this; } - String & operator +=(unsigned long num) { + String &operator +=(unsigned long num) { concat(num); - return (*this); + return *this; } - String & operator +=(float num) { + String &operator +=(float num) { concat(num); - return (*this); + return *this; } - String & operator +=(double num) { + String &operator +=(double num) { concat(num); - return (*this); + return *this; } - String & operator += (const __FlashStringHelper *str){ + String &operator +=(const __FlashStringHelper *str) { concat(str); - return (*this); + return *this; } - 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 num); - 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); - friend StringSumHelper & operator +(const StringSumHelper &lhs, float num); - friend StringSumHelper & operator +(const StringSumHelper &lhs, double num); - friend StringSumHelper & operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs); + 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 num); + 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); + friend StringSumHelper &operator +(const StringSumHelper &lhs, float num); + friend StringSumHelper &operator +(const StringSumHelper &lhs, double num); + friend StringSumHelper &operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs); // comparison (only works w/ Strings and "strings") operator StringIfHelperType() const { @@ -204,24 +202,24 @@ class String { unsigned char equalsIgnoreCase(const String &s) const; unsigned char equalsConstantTime(const String &s) const; unsigned char startsWith(const String &prefix) const; - unsigned char startsWith(const char * prefix) const { + unsigned char startsWith(const char *prefix) const { return this->startsWith(String(prefix)); } - unsigned char startsWith(const __FlashStringHelper * prefix) const { + unsigned char startsWith(const __FlashStringHelper *prefix) const { return this->startsWith(String(prefix)); } unsigned char startsWith(const String &prefix, unsigned int offset) const; unsigned char endsWith(const String &suffix) const; - unsigned char endsWith(const char * suffix) const { + unsigned char endsWith(const char *suffix) const { return this->endsWith(String(suffix)); } - unsigned char endsWith(const __FlashStringHelper * suffix) const { + unsigned char endsWith(const __FlashStringHelper *suffix) const { return this->endsWith(String(suffix)); } // character access char charAt(unsigned int index) const { - return operator[](index); + return operator [](index); } void setCharAt(unsigned int index, char c); char operator [](unsigned int index) const; @@ -230,11 +228,11 @@ 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 wbuffer(); } - char* end() { return wbuffer() + length(); } - const char* begin() const { return c_str(); } - const char* end() const { return c_str() + 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(); } // search int indexOf(char ch) const; @@ -248,25 +246,24 @@ class String { String substring(unsigned int beginIndex) const { return substring(beginIndex, len()); } - ; String substring(unsigned int beginIndex, unsigned int endIndex) const; // modification void replace(char find, char replace); - void replace(const String& find, const String& replace); - void replace(const char * find, const String& replace) { + void replace(const String &find, const String &replace); + void replace(const char *find, const String &replace) { this->replace(String(find), replace); } - void replace(const __FlashStringHelper * find, const String& replace) { + void replace(const __FlashStringHelper *find, const String &replace) { this->replace(String(find), replace); } - void replace(const char * find, const char * replace) { + void replace(const char *find, const char *replace) { this->replace(String(find), String(replace)); } - void replace(const __FlashStringHelper * find, const char * replace) { + void replace(const __FlashStringHelper *find, const char *replace) { this->replace(String(find), String(replace)); } - void replace(const __FlashStringHelper * find, const __FlashStringHelper * replace) { + void replace(const __FlashStringHelper *find, const __FlashStringHelper *replace) { this->replace(String(find), String(replace)); } void remove(unsigned int index); @@ -300,29 +297,29 @@ class String { struct _sso sso; }; // Accessor functions - inline bool isSSO() const { return !sso.isHeap; } - inline unsigned int len() const { return isSSO() ? sso.len : ptr.len; } - inline unsigned int capacity() const { return isSSO() ? (unsigned int)SSOSIZE - 1 : ptr.cap; } // Size of max string not including terminal NUL - inline void setSSO(bool set) { sso.isHeap = !set; } - inline void setLen(int len) { if (isSSO()) sso.len = len; else ptr.len = len; } - inline void setCapacity(int cap) { if (!isSSO()) ptr.cap = cap; } - inline void setBuffer(char *buff) { if (!isSSO()) ptr.buff = buff; } + bool isSSO() const { return !sso.isHeap; } + unsigned int len() const { return isSSO() ? sso.len : ptr.len; } + unsigned int capacity() const { return isSSO() ? (unsigned int)SSOSIZE - 1 : ptr.cap; } // Size of max string not including terminal NUL + void setSSO(bool set) { sso.isHeap = !set; } + void setLen(int len) { if (isSSO()) sso.len = len; else ptr.len = len; } + void setCapacity(int cap) { if (!isSSO()) ptr.cap = cap; } + void setBuffer(char *buff) { if (!isSSO()) ptr.buff = buff; } // Buffer accessor functions - inline const char *buffer() const { return (const char *)(isSSO() ? sso.buff : ptr.buff); } - inline char *wbuffer() const { return isSSO() ? const_cast(sso.buff) : ptr.buff; } // Writable version of buffer + const char *buffer() const { return wbuffer(); } + char *wbuffer() const { return isSSO() ? const_cast(sso.buff) : ptr.buff; } // Writable version of buffer protected: void init(void) { - sso.isHeap = 0; - sso.len = 0; sso.buff[0] = 0; + sso.len = 0; + sso.isHeap = 0; } void invalidate(void); unsigned char changeBuffer(unsigned int maxStrLen); // copy and move - String & copy(const char *cstr, unsigned int length); - String & copy(const __FlashStringHelper *pstr, unsigned int length); + String ©(const char *cstr, unsigned int length); + String ©(const __FlashStringHelper *pstr, unsigned int length); void move(String &rhs) noexcept; }; @@ -358,6 +355,9 @@ class StringSumHelper: public String { StringSumHelper(double num) : String(num) { } + StringSumHelper(const __FlashStringHelper *s) : + String(s) { + } }; extern const String emptyString;