@@ -290,19 +290,25 @@ String & String::operator =(const char *cstr) {
290
290
return *this ;
291
291
}
292
292
293
- String & String::operator = (const __FlashStringHelper *pstr)
293
+ String & String::operator =(const __FlashStringHelper *pstr)
294
294
{
295
295
if (pstr) copy (pstr, strlen_P ((PGM_P)pstr));
296
296
else invalidate ();
297
297
298
298
return *this ;
299
299
}
300
300
301
+ String & String::operator =(char c) {
302
+ char buffer[2 ] { c, ' \0 ' };
303
+ *this = buffer;
304
+ return *this ;
305
+ }
306
+
301
307
// /*********************************************/
302
308
// /* concat */
303
309
// /*********************************************/
304
310
305
- unsigned char String::concat (const String &s) {
311
+ bool String::concat (const String &s) {
306
312
// Special case if we're concatting ourself (s += s;) since we may end up
307
313
// realloc'ing the buffer and moving s.buffer in the method called
308
314
if (&s == this ) {
@@ -322,7 +328,7 @@ unsigned char String::concat(const String &s) {
322
328
}
323
329
}
324
330
325
- unsigned char String::concat (const char *cstr, unsigned int length) {
331
+ bool String::concat (const char *cstr, unsigned int length) {
326
332
unsigned int newlen = len () + length;
327
333
if (!cstr)
328
334
return 0 ;
@@ -340,62 +346,62 @@ unsigned char String::concat(const char *cstr, unsigned int length) {
340
346
return 1 ;
341
347
}
342
348
343
- unsigned char String::concat (const char *cstr) {
349
+ bool String::concat (const char *cstr) {
344
350
if (!cstr)
345
351
return 0 ;
346
352
return concat (cstr, strlen (cstr));
347
353
}
348
354
349
- unsigned char String::concat (char c) {
355
+ bool String::concat (char c) {
350
356
char buf[2 ];
351
357
buf[0 ] = c;
352
358
buf[1 ] = 0 ;
353
359
return concat (buf, 1 );
354
360
}
355
361
356
- unsigned char String::concat (unsigned char num) {
362
+ bool String::concat (unsigned char num) {
357
363
char buf[1 + 3 * sizeof (unsigned char )];
358
364
sprintf (buf, " %d" , num);
359
365
return concat (buf, strlen (buf));
360
366
}
361
367
362
- unsigned char String::concat (int num) {
368
+ bool String::concat (int num) {
363
369
char buf[2 + 3 * sizeof (int )];
364
370
sprintf (buf, " %d" , num);
365
371
return concat (buf, strlen (buf));
366
372
}
367
373
368
- unsigned char String::concat (unsigned int num) {
374
+ bool String::concat (unsigned int num) {
369
375
char buf[1 + 3 * sizeof (unsigned int )];
370
376
utoa (num, buf, 10 );
371
377
return concat (buf, strlen (buf));
372
378
}
373
379
374
- unsigned char String::concat (long num) {
380
+ bool String::concat (long num) {
375
381
char buf[2 + 3 * sizeof (long )];
376
382
sprintf (buf, " %ld" , num);
377
383
return concat (buf, strlen (buf));
378
384
}
379
385
380
- unsigned char String::concat (unsigned long num) {
386
+ bool String::concat (unsigned long num) {
381
387
char buf[1 + 3 * sizeof (unsigned long )];
382
388
ultoa (num, buf, 10 );
383
389
return concat (buf, strlen (buf));
384
390
}
385
391
386
- unsigned char String::concat (float num) {
392
+ bool String::concat (float num) {
387
393
char buf[20 ];
388
394
char * string = dtostrf (num, 4 , 2 , buf);
389
395
return concat (string, strlen (string));
390
396
}
391
397
392
- unsigned char String::concat (double num) {
398
+ bool String::concat (double num) {
393
399
char buf[20 ];
394
400
char * string = dtostrf (num, 4 , 2 , buf);
395
401
return concat (string, strlen (string));
396
402
}
397
403
398
- unsigned char String::concat (const __FlashStringHelper * str) {
404
+ bool String::concat (const __FlashStringHelper * str) {
399
405
if (!str) return 0 ;
400
406
int length = strlen_P ((PGM_P)str);
401
407
if (length == 0 ) return 1 ;
@@ -503,35 +509,39 @@ int String::compareTo(const String &s) const {
503
509
return strcmp (buffer (), s.buffer ());
504
510
}
505
511
506
- unsigned char String::equals (const String &s2) const {
512
+ bool String::equals (const String &s2) const {
507
513
return (len () == s2.len () && compareTo (s2) == 0 );
508
514
}
509
515
510
- unsigned char String::equals (const char *cstr) const {
516
+ bool String::equals (const char *cstr) const {
511
517
if (len () == 0 )
512
518
return (cstr == NULL || *cstr == 0 );
513
519
if (cstr == NULL )
514
520
return buffer ()[0 ] == 0 ;
515
521
return strcmp (buffer (), cstr) == 0 ;
516
522
}
517
523
518
- unsigned char String::operator <(const String &rhs) const {
524
+ bool String::equals (const __FlashStringHelper *s) const {
525
+ return equals (String (s));
526
+ }
527
+
528
+ bool String::operator <(const String &rhs) const {
519
529
return compareTo (rhs) < 0 ;
520
530
}
521
531
522
- unsigned char String::operator >(const String &rhs) const {
532
+ bool String::operator >(const String &rhs) const {
523
533
return compareTo (rhs) > 0 ;
524
534
}
525
535
526
- unsigned char String::operator <=(const String &rhs) const {
536
+ bool String::operator <=(const String &rhs) const {
527
537
return compareTo (rhs) <= 0 ;
528
538
}
529
539
530
- unsigned char String::operator >=(const String &rhs) const {
540
+ bool String::operator >=(const String &rhs) const {
531
541
return compareTo (rhs) >= 0 ;
532
542
}
533
543
534
- unsigned char String::equalsIgnoreCase (const String &s2) const {
544
+ bool String::equalsIgnoreCase (const String &s2) const {
535
545
if (this == &s2)
536
546
return 1 ;
537
547
if (len () != s2.len ())
@@ -547,7 +557,11 @@ unsigned char String::equalsIgnoreCase(const String &s2) const {
547
557
return 1 ;
548
558
}
549
559
550
- unsigned char String::equalsConstantTime (const String &s2) const {
560
+ bool String::equalsIgnoreCase (const __FlashStringHelper *s) const {
561
+ return equalsIgnoreCase (String (s));
562
+ }
563
+
564
+ bool String::equalsConstantTime (const String &s2) const {
551
565
// To avoid possible time-based attacks present function
552
566
// compares given strings in a constant time.
553
567
if (len () != s2.len ())
@@ -574,24 +588,40 @@ unsigned char String::equalsConstantTime(const String &s2) const {
574
588
return (equalcond & diffcond); // bitwise AND
575
589
}
576
590
577
- unsigned char String::startsWith (const String &s2) const {
591
+ bool String::startsWith (const String &s2) const {
578
592
if (len () < s2.len ())
579
593
return 0 ;
580
594
return startsWith (s2, 0 );
581
595
}
582
596
583
- unsigned char String::startsWith (const String &s2, unsigned int offset) const {
597
+ bool String::startsWith (const char *prefix) const {
598
+ return this ->startsWith (String (prefix));
599
+ }
600
+
601
+ bool String::startsWith (const __FlashStringHelper *prefix) const {
602
+ return this ->startsWith (String (prefix));
603
+ }
604
+
605
+ bool String::startsWith (const String &s2, unsigned int offset) const {
584
606
if (offset > (unsigned )(len () - s2.len ()) || !buffer () || !s2.buffer ())
585
607
return 0 ;
586
608
return strncmp (&buffer ()[offset], s2.buffer (), s2.len ()) == 0 ;
587
609
}
588
610
589
- unsigned char String::endsWith (const String &s2) const {
611
+ bool String::endsWith (const String &s2) const {
590
612
if (len () < s2.len () || !buffer () || !s2.buffer ())
591
613
return 0 ;
592
614
return strcmp (&buffer ()[len () - s2.len ()], s2.buffer ()) == 0 ;
593
615
}
594
616
617
+ bool String::endsWith (const char * suffix) const {
618
+ return this ->endsWith (String (suffix));
619
+ }
620
+
621
+ bool String::endsWith (const __FlashStringHelper * suffix) const {
622
+ return this ->endsWith (String (suffix));
623
+ }
624
+
595
625
// /*********************************************/
596
626
// /* Character Access */
597
627
// /*********************************************/
@@ -700,6 +730,14 @@ int String::lastIndexOf(const String &s2, unsigned int fromIndex) const {
700
730
return found;
701
731
}
702
732
733
+ int String::lastIndexOf (const __FlashStringHelper *str) const {
734
+ return lastIndexOf (String (str));
735
+ }
736
+
737
+ int String::lastIndexOf (const __FlashStringHelper *str, unsigned int fromIndex) const {
738
+ return lastIndexOf (String (str), fromIndex);
739
+ }
740
+
703
741
String String::substring (unsigned int left, unsigned int right) const {
704
742
if (left > right) {
705
743
unsigned int temp = right;
@@ -768,7 +806,7 @@ void String::replace(const String& find, const String& replace) {
768
806
while (index >= 0 && (index = lastIndexOf (find, index)) >= 0 ) {
769
807
readFrom = wbuffer () + index + find.len ();
770
808
memmove (readFrom + diff, readFrom, len () - (readFrom - buffer ()));
771
- int newLen = len () + diff;
809
+ int newLen = len () + diff;
772
810
memmove (wbuffer () + index, replace.buffer (), replace.len ());
773
811
setLen (newLen);
774
812
wbuffer ()[newLen] = 0 ;
@@ -777,6 +815,22 @@ void String::replace(const String& find, const String& replace) {
777
815
}
778
816
}
779
817
818
+ void String::replace (const char *find, const String &replace) {
819
+ this ->replace (String (find), replace);
820
+ }
821
+ void String::replace (const __FlashStringHelper *find, const String &replace) {
822
+ this ->replace (String (find), replace);
823
+ }
824
+ void String::replace (const char *find, const char *replace) {
825
+ this ->replace (String (find), String (replace));
826
+ }
827
+ void String::replace (const __FlashStringHelper *find, const char *replace) {
828
+ this ->replace (String (find), String (replace));
829
+ }
830
+ void String::replace (const __FlashStringHelper *find, const __FlashStringHelper *replace) {
831
+ this ->replace (String (find), String (replace));
832
+ }
833
+
780
834
void String::remove (unsigned int index) {
781
835
// Pass the biggest integer as the count. The remove method
782
836
// below will take care of truncating it at the end of the
0 commit comments