Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WString: direct operator overloads instead of StringSumHelper #7781

Merged
merged 23 commits into from
Mar 21, 2021
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9c47a1c
wip
mcspr Dec 19, 2020
d1d584e
huh, turns out String = 'c' did some weird stuff
mcspr Dec 19, 2020
c9a55f7
style
mcspr Dec 22, 2020
7a9cf16
Merge remote-tracking branch 'origin/master' into string/no-sum-helper
mcspr Dec 22, 2020
14e1e7c
allow "blah" + String, 'c' + String and F("...") + String
mcspr Dec 22, 2020
559a5db
shuffle things into .cpp
mcspr Dec 22, 2020
6dbcfa9
trying to fix arduinojson
mcspr Dec 22, 2020
4683923
fix accidental realloc, add test for operator+
mcspr Dec 23, 2020
6a3fc86
fixup! fix accidental realloc, add test for operator+
mcspr Dec 23, 2020
52c8b37
don't need another branch
mcspr Dec 23, 2020
5c35ae3
template +=(String / char* / numbers) and +(String, numbers / char*)
mcspr Dec 23, 2020
c908321
Merge remote-tracking branch 'origin/master' into string/no-sum-helper
mcspr Dec 23, 2020
425b366
Merge remote-tracking branch 'origin/master' into string/no-sum-helper
mcspr Jan 5, 2021
e553e26
nul after moving (isnt mem always zeroed tho?)
mcspr Jan 6, 2021
73a28d0
check if lhs cant keep before switching to rhs
mcspr Jan 6, 2021
fa00fba
fix String used to store struct data
mcspr Jan 6, 2021
8af5a00
style once more
mcspr Jan 6, 2021
c57ae69
typo
mcspr Jan 6, 2021
ed60441
Merge branch 'master' into string/no-sum-helper
earlephilhower Jan 8, 2021
4b70ead
Merge branch 'master' into string/no-sum-helper
earlephilhower Jan 9, 2021
b4ec910
Merge remote-tracking branch 'origin/master' into string/no-sum-helper
mcspr Jan 27, 2021
95ebd47
Merge remote-tracking branch 'origin/master' into string/no-sum-helper
mcspr Mar 21, 2021
3299833
recover 444002180bca8e36b3014eaf5ecf5e690837b440
mcspr Mar 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 67 additions & 64 deletions cores/esp8266/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

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

Expand Down Expand Up @@ -50,11 +50,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 +335,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;

auto left = len - position;
setLen(total);

StringSumHelper &operator +(const StringSumHelper &lhs, char c) {
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
if (!a.concat(c))
a.invalidate();
return a;
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