21
21
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
22
*/
23
23
24
- #include < Arduino.h>
25
24
#include " WString.h"
26
25
#include " stdlib_noniso.h"
27
26
27
+ #include < cstdio>
28
+
28
29
/* ********************************************/
29
30
/* Constructors */
30
31
/* ********************************************/
@@ -50,11 +51,6 @@ String::String(String &&rval) noexcept {
50
51
move (rval);
51
52
}
52
53
53
- String::String (StringSumHelper &&rval) noexcept {
54
- init ();
55
- move (rval);
56
- }
57
-
58
54
String::String (unsigned char value, unsigned char base) {
59
55
init ();
60
56
char buf[1 + 8 * sizeof (unsigned char )];
@@ -340,84 +336,92 @@ unsigned char String::concat(const __FlashStringHelper *str) {
340
336
}
341
337
342
338
/* ********************************************/
343
- /* Concatenate */
339
+ /* Insert */
344
340
/* ********************************************/
345
341
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 ;
352
345
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 ;
359
350
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 ;
365
360
}
366
361
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));
372
365
}
373
366
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 );
379
370
}
380
371
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));
386
374
}
387
375
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 ());
393
378
}
394
379
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;
400
394
}
401
395
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;
407
409
}
408
410
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;
414
417
}
415
418
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;
421
425
}
422
426
423
427
/* ********************************************/
0 commit comments