Skip to content

Commit

Permalink
Merge esp8266#7781
Browse files Browse the repository at this point in the history
  • Loading branch information
mcspr committed Feb 3, 2021
1 parent 20413f8 commit 69f0a12
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 163 deletions.
132 changes: 68 additions & 64 deletions cores/esp8266/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <Arduino.h>
#include "WString.h"
#include "stdlib_noniso.h"

#include <cstdio>

/*********************************************/
/* Constructors */
/*********************************************/
Expand All @@ -50,11 +51,6 @@ String::String(String &&rval) noexcept {
move(rval);
}

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

String::String(unsigned char value, unsigned char base) {
init();
char buf[1 + 8 * sizeof(unsigned char)];
Expand Down Expand Up @@ -340,84 +336,92 @@ unsigned char String::concat(const __FlashStringHelper *str) {
}

/*********************************************/
/* Concatenate */
/* Insert */
/*********************************************/

StringSumHelper &operator +(const StringSumHelper &lhs, const String &rhs) {
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
if (!a.concat(rhs.buffer(), rhs.len()))
a.invalidate();
return a;
}
String &String::insert(size_t position, const char *other, size_t other_length) {
if (position > length())
return *this;

StringSumHelper &operator +(const StringSumHelper &lhs, const char *cstr) {
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
if (!cstr || !a.concat(cstr, strlen(cstr)))
a.invalidate();
return a;
}
auto len = length();
auto total = len + other_length;
if (!reserve(total))
return *this;

StringSumHelper &operator +(const StringSumHelper &lhs, char c) {
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
if (!a.concat(c))
a.invalidate();
return a;
auto left = len - position;
setLen(total);

auto *start = wbuffer() + position;
memmove(start + other_length, start, left);
memmove_P(start, other, other_length);
wbuffer()[total] = '\0';

return *this;
}

StringSumHelper &operator +(const StringSumHelper &lhs, unsigned char num) {
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
if (!a.concat(num))
a.invalidate();
return a;
String &String::insert(size_t position, const __FlashStringHelper *other) {
auto *p = reinterpret_cast<const char*>(other);
return insert(position, p, strlen_P(p));
}

StringSumHelper &operator +(const StringSumHelper &lhs, int num) {
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
if (!a.concat(num))
a.invalidate();
return a;
String &String::insert(size_t position, char other) {
char tmp[2] { other, '\0' };
return insert(position, tmp, 1);
}

StringSumHelper &operator +(const StringSumHelper &lhs, unsigned int num) {
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
if (!a.concat(num))
a.invalidate();
return a;
String &String::insert(size_t position, const char *other) {
return insert(position, other, strlen(other));
}

StringSumHelper &operator +(const StringSumHelper &lhs, long num) {
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
if (!a.concat(num))
a.invalidate();
return a;
String &String::insert(size_t position, const String &other) {
return insert(position, other.c_str(), other.length());
}

StringSumHelper &operator +(const StringSumHelper &lhs, unsigned long num) {
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
if (!a.concat(num))
a.invalidate();
return a;
String operator +(const String &lhs, String &&rhs) {
String res;
auto total = lhs.length() + rhs.length();
if (rhs.capacity() > total) {
rhs.insert(0, lhs);
res = std::move(rhs);
} else {
res.reserve(total);
res += lhs;
res += rhs;
rhs.invalidate();
}

return res;
}

StringSumHelper &operator +(const StringSumHelper &lhs, float num) {
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
if (!a.concat(num))
a.invalidate();
return a;
String operator +(String &&lhs, String &&rhs) {
String res;
auto total = lhs.length() + rhs.length();
if ((total > lhs.capacity()) && (total < rhs.capacity())) {
rhs.insert(0, lhs);
res = std::move(rhs);
} else {
lhs += rhs;
rhs.invalidate();
res = std::move(lhs);
}

return res;
}

StringSumHelper &operator +(const StringSumHelper &lhs, double num) {
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
if (!a.concat(num))
a.invalidate();
return a;
String operator +(char lhs, const String &rhs) {
String res;
res.reserve(rhs.length() + 1);
res += lhs;
res += rhs;
return res;
}

StringSumHelper &operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs) {
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
if (!a.concat(rhs))
a.invalidate();
return a;
String operator +(const char *lhs, const String &rhs) {
String res;
res.reserve(strlen_P(lhs) + rhs.length());
res += lhs;
res += rhs;
return res;
}

/*********************************************/
Expand Down
Loading

0 comments on commit 69f0a12

Please sign in to comment.