Skip to content

Commit 0894b51

Browse files
authored
WString: direct operator overloads instead of StringSumHelper (#7781)
* wip * huh, turns out String = 'c' did some weird stuff * style * allow "blah" + String, 'c' + String and F("...") + String also, simplify const char* vs. __FlashStringHelper, and just always use _P functions * shuffle things into .cpp * trying to fix arduinojson based on the implementation, we only need to specify that this symbol is a class * fix accidental realloc, add test for operator+ basic chaining should work just like with master comparing std::move() buffers won't work though, because we never allow anything but `const StringSumHelper&` references * fixup! fix accidental realloc, add test for operator+ * don't need another branch * template +=(String / char* / numbers) and +(String, numbers / char*) * nul after moving (isnt mem always zeroed tho?) * check if lhs cant keep before switching to rhs * fix String used to store struct data `cannot bind bit-field '...' to 'signed char&' `cannot bind bit-field '...' to 'unssigned char&' noticed in both tasmota and espeasy, where this generates a webpage content from some status struct containing bitfields * style once more * typo * recover 4440021
1 parent 1b922ed commit 0894b51

File tree

4 files changed

+199
-199
lines changed

4 files changed

+199
-199
lines changed

cores/esp8266/StreamString.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ class StreamString: public String, public S2Stream
236236
StreamString(const String& string): String(string), S2Stream(this) { }
237237
StreamString(const __FlashStringHelper *str): String(str), S2Stream(this) { }
238238
StreamString(String&& string): String(string), S2Stream(this) { }
239-
StreamString(StringSumHelper&& sum): String(sum), S2Stream(this) { }
240239

241240
explicit StreamString(char c): String(c), S2Stream(this) { }
242241
explicit StreamString(unsigned char c, unsigned char base = 10): String(c, base), S2Stream(this) { }
@@ -281,13 +280,6 @@ class StreamString: public String, public S2Stream
281280
resetpp();
282281
return *this;
283282
}
284-
285-
StreamString& operator= (StringSumHelper&& rval)
286-
{
287-
String::operator=(rval);
288-
resetpp();
289-
return *this;
290-
}
291283
};
292284

293285
#endif // __STREAMSTRING_H

cores/esp8266/WString.cpp

Lines changed: 65 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2222
*/
2323

24-
#include <Arduino.h>
24+
#include "Arduino.h"
2525
#include "WString.h"
2626
#include "stdlib_noniso.h"
2727

@@ -56,11 +56,6 @@ String::String(String &&rval) noexcept {
5656
move(rval);
5757
}
5858

59-
String::String(StringSumHelper &&rval) noexcept {
60-
init();
61-
move(rval);
62-
}
63-
6459
String::String(unsigned char value, unsigned char base) {
6560
init();
6661
char buf[1 + 8 * sizeof(unsigned char)];
@@ -390,98 +385,92 @@ unsigned char String::concat(const __FlashStringHelper *str) {
390385
}
391386

392387
/*********************************************/
393-
/* Concatenate */
388+
/* Insert */
394389
/*********************************************/
395390

396-
StringSumHelper &operator +(const StringSumHelper &lhs, const String &rhs) {
397-
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
398-
if (!a.concat(rhs.buffer(), rhs.len()))
399-
a.invalidate();
400-
return a;
401-
}
391+
String &String::insert(size_t position, const char *other, size_t other_length) {
392+
if (position > length())
393+
return *this;
402394

403-
StringSumHelper &operator +(const StringSumHelper &lhs, const char *cstr) {
404-
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
405-
if (!cstr || !a.concat(cstr, strlen(cstr)))
406-
a.invalidate();
407-
return a;
408-
}
395+
auto len = length();
396+
auto total = len + other_length;
397+
if (!reserve(total))
398+
return *this;
409399

410-
StringSumHelper &operator +(const StringSumHelper &lhs, char c) {
411-
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
412-
if (!a.concat(c))
413-
a.invalidate();
414-
return a;
415-
}
400+
auto left = len - position;
401+
setLen(total);
416402

417-
StringSumHelper &operator +(const StringSumHelper &lhs, unsigned char num) {
418-
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
419-
if (!a.concat(num))
420-
a.invalidate();
421-
return a;
422-
}
403+
auto *start = wbuffer() + position;
404+
memmove(start + other_length, start, left);
405+
memmove_P(start, other, other_length);
406+
wbuffer()[total] = '\0';
423407

424-
StringSumHelper &operator +(const StringSumHelper &lhs, int num) {
425-
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
426-
if (!a.concat(num))
427-
a.invalidate();
428-
return a;
408+
return *this;
429409
}
430410

431-
StringSumHelper &operator +(const StringSumHelper &lhs, unsigned int num) {
432-
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
433-
if (!a.concat(num))
434-
a.invalidate();
435-
return a;
411+
String &String::insert(size_t position, const __FlashStringHelper *other) {
412+
auto *p = reinterpret_cast<const char*>(other);
413+
return insert(position, p, strlen_P(p));
436414
}
437415

438-
StringSumHelper &operator +(const StringSumHelper &lhs, long num) {
439-
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
440-
if (!a.concat(num))
441-
a.invalidate();
442-
return a;
416+
String &String::insert(size_t position, char other) {
417+
char tmp[2] { other, '\0' };
418+
return insert(position, tmp, 1);
443419
}
444420

445-
StringSumHelper &operator +(const StringSumHelper &lhs, unsigned long num) {
446-
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
447-
if (!a.concat(num))
448-
a.invalidate();
449-
return a;
421+
String &String::insert(size_t position, const char *other) {
422+
return insert(position, other, strlen(other));
450423
}
451424

452-
StringSumHelper &operator +(const StringSumHelper &lhs, long long num) {
453-
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
454-
if (!a.concat(num))
455-
a.invalidate();
456-
return a;
425+
String &String::insert(size_t position, const String &other) {
426+
return insert(position, other.c_str(), other.length());
457427
}
458428

459-
StringSumHelper &operator +(const StringSumHelper &lhs, unsigned long long num) {
460-
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
461-
if (!a.concat(num))
462-
a.invalidate();
463-
return a;
429+
String operator +(const String &lhs, String &&rhs) {
430+
String res;
431+
auto total = lhs.length() + rhs.length();
432+
if (rhs.capacity() > total) {
433+
rhs.insert(0, lhs);
434+
res = std::move(rhs);
435+
} else {
436+
res.reserve(total);
437+
res += lhs;
438+
res += rhs;
439+
rhs.invalidate();
440+
}
441+
442+
return res;
464443
}
465444

466-
StringSumHelper &operator +(const StringSumHelper &lhs, float num) {
467-
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
468-
if (!a.concat(num))
469-
a.invalidate();
470-
return a;
445+
String operator +(String &&lhs, String &&rhs) {
446+
String res;
447+
auto total = lhs.length() + rhs.length();
448+
if ((total > lhs.capacity()) && (total < rhs.capacity())) {
449+
rhs.insert(0, lhs);
450+
res = std::move(rhs);
451+
} else {
452+
lhs += rhs;
453+
rhs.invalidate();
454+
res = std::move(lhs);
455+
}
456+
457+
return res;
471458
}
472459

473-
StringSumHelper &operator +(const StringSumHelper &lhs, double num) {
474-
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
475-
if (!a.concat(num))
476-
a.invalidate();
477-
return a;
460+
String operator +(char lhs, const String &rhs) {
461+
String res;
462+
res.reserve(rhs.length() + 1);
463+
res += lhs;
464+
res += rhs;
465+
return res;
478466
}
479467

480-
StringSumHelper &operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs) {
481-
StringSumHelper &a = const_cast<StringSumHelper &>(lhs);
482-
if (!a.concat(rhs))
483-
a.invalidate();
484-
return a;
468+
String operator +(const char *lhs, const String &rhs) {
469+
String res;
470+
res.reserve(strlen_P(lhs) + rhs.length());
471+
res += lhs;
472+
res += rhs;
473+
return res;
485474
}
486475

487476
/*********************************************/

0 commit comments

Comments
 (0)