|
21 | 21 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
22 | 22 | */
|
23 | 23 |
|
24 |
| -#include <Arduino.h> |
| 24 | +#include "Arduino.h" |
25 | 25 | #include "WString.h"
|
26 | 26 | #include "stdlib_noniso.h"
|
27 | 27 |
|
@@ -56,11 +56,6 @@ String::String(String &&rval) noexcept {
|
56 | 56 | move(rval);
|
57 | 57 | }
|
58 | 58 |
|
59 |
| -String::String(StringSumHelper &&rval) noexcept { |
60 |
| - init(); |
61 |
| - move(rval); |
62 |
| -} |
63 |
| - |
64 | 59 | String::String(unsigned char value, unsigned char base) {
|
65 | 60 | init();
|
66 | 61 | char buf[1 + 8 * sizeof(unsigned char)];
|
@@ -390,98 +385,92 @@ unsigned char String::concat(const __FlashStringHelper *str) {
|
390 | 385 | }
|
391 | 386 |
|
392 | 387 | /*********************************************/
|
393 |
| -/* Concatenate */ |
| 388 | +/* Insert */ |
394 | 389 | /*********************************************/
|
395 | 390 |
|
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; |
402 | 394 |
|
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; |
409 | 399 |
|
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); |
416 | 402 |
|
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'; |
423 | 407 |
|
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; |
429 | 409 | }
|
430 | 410 |
|
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)); |
436 | 414 | }
|
437 | 415 |
|
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); |
443 | 419 | }
|
444 | 420 |
|
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)); |
450 | 423 | }
|
451 | 424 |
|
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()); |
457 | 427 | }
|
458 | 428 |
|
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; |
464 | 443 | }
|
465 | 444 |
|
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; |
471 | 458 | }
|
472 | 459 |
|
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; |
478 | 466 | }
|
479 | 467 |
|
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; |
485 | 474 | }
|
486 | 475 |
|
487 | 476 | /*********************************************/
|
|
0 commit comments