2121 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2222 */
2323
24- #include < Arduino.h>
2524#include " WString.h"
2625#include " stdlib_noniso.h"
2726
27+ #include < cstdio>
28+
2829/* ********************************************/
2930/* Constructors */
3031/* ********************************************/
@@ -50,11 +51,6 @@ String::String(String &&rval) noexcept {
5051 move (rval);
5152}
5253
53- String::String (StringSumHelper &&rval) noexcept {
54- init ();
55- move (rval);
56- }
57-
5854String::String (unsigned char value, unsigned char base) {
5955 init ();
6056 char buf[1 + 8 * sizeof (unsigned char )];
@@ -340,84 +336,92 @@ unsigned char String::concat(const __FlashStringHelper *str) {
340336}
341337
342338/* ********************************************/
343- /* Concatenate */
339+ /* Insert */
344340/* ********************************************/
345341
346- StringSumHelper &operator +(const StringSumHelper &lhs, const String &rhs) {
347- StringSumHelper &a = const_cast <StringSumHelper &>(lhs);
348- if (!a.concat (rhs.buffer (), rhs.len ()))
349- a.invalidate ();
350- return a;
351- }
342+ String &String::insert (size_t position, const char *other, size_t other_length) {
343+ if (position > length ())
344+ return *this ;
352345
353- StringSumHelper &operator +(const StringSumHelper &lhs, const char *cstr) {
354- StringSumHelper &a = const_cast <StringSumHelper &>(lhs);
355- if (!cstr || !a.concat (cstr, strlen (cstr)))
356- a.invalidate ();
357- return a;
358- }
346+ auto len = length ();
347+ auto total = len + other_length;
348+ if (!reserve (total))
349+ return *this ;
359350
360- StringSumHelper &operator +(const StringSumHelper &lhs, char c) {
361- StringSumHelper &a = const_cast <StringSumHelper &>(lhs);
362- if (!a.concat (c))
363- a.invalidate ();
364- return a;
351+ auto left = len - position;
352+ setLen (total);
353+
354+ auto *start = wbuffer () + position;
355+ memmove (start + other_length, start, left);
356+ memmove_P (start, other, other_length);
357+ wbuffer ()[total] = ' \0 ' ;
358+
359+ return *this ;
365360}
366361
367- StringSumHelper &operator +(const StringSumHelper &lhs, unsigned char num) {
368- StringSumHelper &a = const_cast <StringSumHelper &>(lhs);
369- if (!a.concat (num))
370- a.invalidate ();
371- return a;
362+ String &String::insert (size_t position, const __FlashStringHelper *other) {
363+ auto *p = reinterpret_cast <const char *>(other);
364+ return insert (position, p, strlen_P (p));
372365}
373366
374- StringSumHelper &operator +(const StringSumHelper &lhs, int num) {
375- StringSumHelper &a = const_cast <StringSumHelper &>(lhs);
376- if (!a.concat (num))
377- a.invalidate ();
378- return a;
367+ String &String::insert (size_t position, char other) {
368+ char tmp[2 ] { other, ' \0 ' };
369+ return insert (position, tmp, 1 );
379370}
380371
381- StringSumHelper &operator +(const StringSumHelper &lhs, unsigned int num) {
382- StringSumHelper &a = const_cast <StringSumHelper &>(lhs);
383- if (!a.concat (num))
384- a.invalidate ();
385- return a;
372+ String &String::insert (size_t position, const char *other) {
373+ return insert (position, other, strlen (other));
386374}
387375
388- StringSumHelper &operator +(const StringSumHelper &lhs, long num) {
389- StringSumHelper &a = const_cast <StringSumHelper &>(lhs);
390- if (!a.concat (num))
391- a.invalidate ();
392- return a;
376+ String &String::insert (size_t position, const String &other) {
377+ return insert (position, other.c_str (), other.length ());
393378}
394379
395- StringSumHelper &operator +(const StringSumHelper &lhs, unsigned long num) {
396- StringSumHelper &a = const_cast <StringSumHelper &>(lhs);
397- if (!a.concat (num))
398- a.invalidate ();
399- return a;
380+ String operator +(const String &lhs, String &&rhs) {
381+ String res;
382+ auto total = lhs.length () + rhs.length ();
383+ if (rhs.capacity () > total) {
384+ rhs.insert (0 , lhs);
385+ res = std::move (rhs);
386+ } else {
387+ res.reserve (total);
388+ res += lhs;
389+ res += rhs;
390+ rhs.invalidate ();
391+ }
392+
393+ return res;
400394}
401395
402- StringSumHelper &operator +(const StringSumHelper &lhs, float num) {
403- StringSumHelper &a = const_cast <StringSumHelper &>(lhs);
404- if (!a.concat (num))
405- a.invalidate ();
406- return a;
396+ String operator +(String &&lhs, String &&rhs) {
397+ String res;
398+ auto total = lhs.length () + rhs.length ();
399+ if ((total > lhs.capacity ()) && (total < rhs.capacity ())) {
400+ rhs.insert (0 , lhs);
401+ res = std::move (rhs);
402+ } else {
403+ lhs += rhs;
404+ rhs.invalidate ();
405+ res = std::move (lhs);
406+ }
407+
408+ return res;
407409}
408410
409- StringSumHelper &operator +(const StringSumHelper &lhs, double num) {
410- StringSumHelper &a = const_cast <StringSumHelper &>(lhs);
411- if (!a.concat (num))
412- a.invalidate ();
413- return a;
411+ String operator +(char lhs, const String &rhs) {
412+ String res;
413+ res.reserve (rhs.length () + 1 );
414+ res += lhs;
415+ res += rhs;
416+ return res;
414417}
415418
416- StringSumHelper &operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs) {
417- StringSumHelper &a = const_cast <StringSumHelper &>(lhs);
418- if (!a.concat (rhs))
419- a.invalidate ();
420- return a;
419+ String operator +(const char *lhs, const String &rhs) {
420+ String res;
421+ res.reserve (strlen_P (lhs) + rhs.length ());
422+ res += lhs;
423+ res += rhs;
424+ return res;
421425}
422426
423427/* ********************************************/
0 commit comments