Skip to content

Commit

Permalink
Merge pull request #11 from Jason2866/opt_wstring
Browse files Browse the repository at this point in the history
Opt wstring
  • Loading branch information
Jason2866 authored Oct 21, 2020
2 parents f18a251 + 4f94abc commit d15959a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 116 deletions.
112 changes: 34 additions & 78 deletions cores/esp8266/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
Copyright 2011, Paul Stoffregen, [email protected]
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
Expand Down Expand Up @@ -45,17 +44,15 @@ String::String(const __FlashStringHelper *pstr) {
*this = pstr; // see operator =
}

#ifdef __GXX_EXPERIMENTAL_CXX0X__
String::String(String &&rval) {
String::String(String &&rval) noexcept {
init();
move(rval);
}

String::String(StringSumHelper &&rval) {
String::String(StringSumHelper &&rval) noexcept {
init();
move(rval);
}
#endif

String::String(char c) {
init();
Expand Down Expand Up @@ -120,19 +117,9 @@ String::String(double value, unsigned char decimalPlaces) {
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
}

String::~String() {
invalidate();
}

// /*********************************************/
// /* Memory Management */
// /*********************************************/

inline void String::init(void) {
setSSO(true);
setLen(0);
wbuffer()[0] = 0;
}
/*********************************************/
/* Memory Management */
/*********************************************/

void String::invalidate(void) {
if(!isSSO() && wbuffer())
Expand Down Expand Up @@ -199,9 +186,9 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
return 0;
}

// /*********************************************/
// /* Copy and Move */
// /*********************************************/
/*********************************************/
/* Copy and Move */
/*********************************************/

String & String::copy(const char *cstr, unsigned int length) {
if (!reserve(length)) {
Expand All @@ -223,36 +210,11 @@ String & String::copy(const __FlashStringHelper *pstr, unsigned int length) {
return *this;
}

#ifdef __GXX_EXPERIMENTAL_CXX0X__
void String::move(String &rhs) {
if (buffer()) {
if (capacity() >= rhs.len()) {
memmove_P(wbuffer(), rhs.buffer(), rhs.length() + 1);
setLen(rhs.len());
rhs.invalidate();
return;
} else {
if (!isSSO()) {
free(wbuffer());
setBuffer(nullptr);
}
}
}
if (rhs.isSSO()) {
setSSO(true);
memmove_P(sso.buff, rhs.sso.buff, sizeof(sso.buff));
} else {
setSSO(false);
setBuffer(rhs.wbuffer());
}
setCapacity(rhs.capacity());
setLen(rhs.len());
rhs.setSSO(false);
rhs.setCapacity(0);
rhs.setLen(0);
rhs.setBuffer(nullptr);
void String::move(String &rhs) noexcept {
invalidate();
sso = rhs.sso;
rhs.init();
}
#endif

String & String::operator =(const String &rhs) {
if (this == &rhs)
Expand All @@ -266,19 +228,17 @@ String & String::operator =(const String &rhs) {
return *this;
}

#ifdef __GXX_EXPERIMENTAL_CXX0X__
String & String::operator =(String &&rval) {
String & String::operator =(String &&rval) noexcept {
if (this != &rval)
move(rval);
return *this;
}

String & String::operator =(StringSumHelper &&rval) {
String & String::operator =(StringSumHelper &&rval) noexcept {
if (this != &rval)
move(rval);
return *this;
}
#endif

String & String::operator =(const char *cstr) {
if (cstr)
Expand All @@ -297,9 +257,9 @@ String & String::operator = (const __FlashStringHelper *pstr)
return *this;
}

// /*********************************************/
// /* concat */
// /*********************************************/
/*********************************************/
/* concat */
/*********************************************/

unsigned char String::concat(const String &s) {
// Special case if we're concatting ourself (s += s;) since we may end up
Expand Down Expand Up @@ -483,9 +443,9 @@ StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHel
return a;
}

// /*********************************************/
// /* Comparison */
// /*********************************************/
/*********************************************/
/* Comparison */
/*********************************************/

int String::compareTo(const String &s) const {
if(!buffer() || !s.buffer()) {
Expand Down Expand Up @@ -587,13 +547,9 @@ unsigned char String::endsWith(const String &s2) const {
return strcmp(&buffer()[len() - s2.len()], s2.buffer()) == 0;
}

// /*********************************************/
// /* Character Access */
// /*********************************************/

char String::charAt(unsigned int loc) const {
return operator[](loc);
}
/*********************************************/
/* Character Access */
/*********************************************/

void String::setCharAt(unsigned int loc, char c) {
if (loc < len())
Expand Down Expand Up @@ -629,9 +585,9 @@ void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int ind
buf[n] = 0;
}

// /*********************************************/
// /* Search */
// /*********************************************/
/*********************************************/
/* Search */
/*********************************************/

int String::indexOf(char c) const {
return indexOf(c, 0);
Expand Down Expand Up @@ -713,9 +669,9 @@ String String::substring(unsigned int left, unsigned int right) const {
return out;
}

// /*********************************************/
// /* Modification */
// /*********************************************/
/*********************************************/
/* Modification */
/*********************************************/

void String::replace(char find, char replace) {
if (!buffer())
Expand Down Expand Up @@ -828,9 +784,9 @@ void String::trim(void) {
wbuffer()[newlen] = 0;
}

// /*********************************************/
// /* Parsing / Conversion */
// /*********************************************/
/*********************************************/
/* Parsing / Conversion */
/*********************************************/

long String::toInt(void) const {
if (buffer())
Expand Down
55 changes: 30 additions & 25 deletions cores/esp8266/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
...mostly rewritten by Paul Stoffregen...
Copyright (c) 2009-10 Hernando Barragan. All right reserved.
Copyright 2011, Paul Stoffregen, [email protected]
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
Expand Down Expand Up @@ -53,13 +53,14 @@ class String {
// if the initial value is null or invalid, or if memory allocation
// fails, the string will be marked as invalid (i.e. "if (s)" will
// be false).
String(const char *cstr = nullptr);
String() {
init();
}
String(const char *cstr);
String(const String &str);
String(const __FlashStringHelper *str);
#ifdef __GXX_EXPERIMENTAL_CXX0X__
String(String &&rval);
String(StringSumHelper &&rval);
#endif
String(String &&rval) noexcept;
String(StringSumHelper &&rval) noexcept;
explicit String(char c);
explicit String(unsigned char, unsigned char base = 10);
explicit String(int, unsigned char base = 10);
Expand All @@ -68,7 +69,9 @@ class String {
explicit String(unsigned long, unsigned char base = 10);
explicit String(float, unsigned char decimalPlaces = 2);
explicit String(double, unsigned char decimalPlaces = 2);
~String(void);
~String() {
invalidate();
}

// memory management
// return true on success, false on failure (in which case, the string
Expand All @@ -95,10 +98,8 @@ class String {
String & operator =(const String &rhs);
String & operator =(const char *cstr);
String & operator = (const __FlashStringHelper *str);
#ifdef __GXX_EXPERIMENTAL_CXX0X__
String & operator =(String &&rval);
String & operator =(StringSumHelper &&rval);
#endif
String & operator =(String &&rval) noexcept;
String & operator =(StringSumHelper &&rval) noexcept;

// concatenate (works w/ built-in types)

Expand Down Expand Up @@ -219,7 +220,9 @@ class String {
}

// character access
char charAt(unsigned int index) const;
char charAt(unsigned int index) const {
return operator[](index);
}
void setCharAt(unsigned int index, char c);
char operator [](unsigned int index) const;
char& operator [](unsigned int index);
Expand Down Expand Up @@ -275,11 +278,11 @@ class String {
// parsing/conversion
long toInt(void) const;
float toFloat(void) const;
double toDouble(void) const;
double toDouble(void) const;

protected:
// Contains the string info when we're not in SSO mode
struct _ptr {
struct _ptr {
char * buff;
uint16_t cap;
uint16_t len;
Expand All @@ -288,37 +291,39 @@ class String {
enum { SSOSIZE = sizeof(struct _ptr) + 4 - 1 }; // Characters to allocate space for SSO, must be 12 or more
struct _sso {
char buff[SSOSIZE];
unsigned char len : 7; // Ensure only one byte is allocated by GCC for the bitfields
unsigned char isSSO : 1;
unsigned char len : 7; // Ensure only one byte is allocated by GCC for the bitfields
unsigned char isHeap : 1;
} __attribute__((packed)); // Ensure that GCC doesn't expand the flag byte to a 32-bit word for alignment issues
enum { CAPACITY_MAX = 65535 }; // If typeof(cap) changed from uint16_t, be sure to update this enum to the max value storable in the type
union {
struct _ptr ptr;
struct _sso sso;
};
// Accessor functions
inline bool isSSO() const { return sso.isSSO; }
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.isSSO = set; }
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; }
inline 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<char *>(sso.buff) : ptr.buff; } // Writable version of buffer

protected:
void init(void);
void init(void) {
sso.isHeap = 0;
sso.len = 0;
sso.buff[0] = 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);
#ifdef __GXX_EXPERIMENTAL_CXX0X__
void move(String &rhs);
#endif
void move(String &rhs) noexcept;
};

class StringSumHelper: public String {
Expand Down
16 changes: 4 additions & 12 deletions cores/esp8266/core_version.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
#ifndef ARDUINO_ESP8266_GIT_VER
#define ARDUINO_ESP8266_GIT_VER 0x00000000
#endif

#ifndef ARDUINO_ESP8266_GIT_DESC
#define ARDUINO_ESP8266_GIT_DESC unspecified
#endif

// ARDUINO_ESP8266_RELEASE is defined for released versions as a string containing the version name, i.e. "2_3_0_RC1"
// ARDUINO_ESP8266_RELEASE is used in the core internally. Please use ESP.getCoreVersion() function instead.

// ARDUINO_ESP8266_RELEASE_<version number> are defined for releases, for use in #ifdef... constructs
#define ARDUINO_ESP8266_GIT_VER 0x2843a5ac
#define ARDUINO_ESP8266_GIT_DESC 2.7.3-3-g2843a5ac
#define ARDUINO_ESP8266_RELEASE_2_7_4_5
#define ARDUINO_ESP8266_RELEASE "2_7_4_5"
2 changes: 1 addition & 1 deletion cores/esp8266/libb64/cencode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ int base64_encode_blockend(char* code_out, base64_encodestate* state_in){

int base64_encode_chars(const char* plaintext_in, int length_in, char* code_out){
base64_encodestate _state;
base64_init_encodestate(&_state);
base64_init_encodestate_nonewlines(&_state);
int len = base64_encode_block(plaintext_in, length_in, code_out, &_state);
return len + base64_encode_blockend((code_out + len), &_state);
}
Expand Down

0 comments on commit d15959a

Please sign in to comment.