Skip to content

Commit

Permalink
wstring: correctly do move invalidation & copy
Browse files Browse the repository at this point in the history
based on the #7553 without isSSO -> isHeap rename and inline optimizations
additionally, remove useless pre-c++11 preprocessor checks

Co-authored-by: Takayuki 'January June' Suwa <[email protected]>
  • Loading branch information
mcspr and jjsuwa committed Sep 27, 2020
1 parent d3b3a43 commit 7ce820a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 39 deletions.
35 changes: 3 additions & 32 deletions cores/esp8266/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ String::String(const __FlashStringHelper *pstr) {
*this = pstr; // see operator =
}

#ifdef __GXX_EXPERIMENTAL_CXX0X__
String::String(String &&rval) noexcept {
init();
move(rval);
Expand All @@ -55,7 +54,6 @@ String::String(StringSumHelper &&rval) noexcept {
init();
move(rval);
}
#endif

String::String(char c) {
init();
Expand Down Expand Up @@ -223,36 +221,11 @@ String & String::copy(const __FlashStringHelper *pstr, unsigned int length) {
return *this;
}

#ifdef __GXX_EXPERIMENTAL_CXX0X__
void String::move(String &rhs) noexcept {
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);
invalidate();
sso = rhs.sso;
rhs.init();
}
#endif

String & String::operator =(const String &rhs) {
if (this == &rhs)
Expand All @@ -266,7 +239,6 @@ String & String::operator =(const String &rhs) {
return *this;
}

#ifdef __GXX_EXPERIMENTAL_CXX0X__
String & String::operator =(String &&rval) noexcept {
if (this != &rval)
move(rval);
Expand All @@ -278,7 +250,6 @@ String & String::operator =(StringSumHelper &&rval) noexcept {
move(rval);
return *this;
}
#endif

String & String::operator =(const char *cstr) {
if (cstr)
Expand Down
11 changes: 4 additions & 7 deletions cores/esp8266/WString.h
Original file line number Diff line number Diff line change
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) noexcept;
String(StringSumHelper &&rval) noexcept;
#endif
explicit String(char c);
explicit String(unsigned char, unsigned char base = 10);
explicit String(int, unsigned char base = 10);
Expand Down Expand Up @@ -95,10 +96,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) noexcept;
String & operator =(StringSumHelper &&rval) noexcept;
#endif

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

Expand Down Expand Up @@ -316,9 +315,7 @@ class String {
// 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) noexcept;
#endif
};

class StringSumHelper: public String {
Expand Down

0 comments on commit 7ce820a

Please sign in to comment.