Skip to content

Commit e4e2bb8

Browse files
committed
Optimize String class in CORE-1.0.0 (dravuni) to NOT inline some methods.
BACKPORT: esp8266/Arduino@fe04afb
1 parent b2c87c4 commit e4e2bb8

File tree

2 files changed

+124
-80
lines changed

2 files changed

+124
-80
lines changed

cores/esp32/WString.cpp

Lines changed: 79 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -290,19 +290,25 @@ String & String::operator =(const char *cstr) {
290290
return *this;
291291
}
292292

293-
String & String::operator = (const __FlashStringHelper *pstr)
293+
String & String::operator =(const __FlashStringHelper *pstr)
294294
{
295295
if (pstr) copy(pstr, strlen_P((PGM_P)pstr));
296296
else invalidate();
297297

298298
return *this;
299299
}
300300

301+
String & String::operator =(char c) {
302+
char buffer[2] { c, '\0' };
303+
*this = buffer;
304+
return *this;
305+
}
306+
301307
// /*********************************************/
302308
// /* concat */
303309
// /*********************************************/
304310

305-
unsigned char String::concat(const String &s) {
311+
bool String::concat(const String &s) {
306312
// Special case if we're concatting ourself (s += s;) since we may end up
307313
// realloc'ing the buffer and moving s.buffer in the method called
308314
if (&s == this) {
@@ -322,7 +328,7 @@ unsigned char String::concat(const String &s) {
322328
}
323329
}
324330

325-
unsigned char String::concat(const char *cstr, unsigned int length) {
331+
bool String::concat(const char *cstr, unsigned int length) {
326332
unsigned int newlen = len() + length;
327333
if(!cstr)
328334
return 0;
@@ -340,62 +346,62 @@ unsigned char String::concat(const char *cstr, unsigned int length) {
340346
return 1;
341347
}
342348

343-
unsigned char String::concat(const char *cstr) {
349+
bool String::concat(const char *cstr) {
344350
if(!cstr)
345351
return 0;
346352
return concat(cstr, strlen(cstr));
347353
}
348354

349-
unsigned char String::concat(char c) {
355+
bool String::concat(char c) {
350356
char buf[2];
351357
buf[0] = c;
352358
buf[1] = 0;
353359
return concat(buf, 1);
354360
}
355361

356-
unsigned char String::concat(unsigned char num) {
362+
bool String::concat(unsigned char num) {
357363
char buf[1 + 3 * sizeof(unsigned char)];
358364
sprintf(buf, "%d", num);
359365
return concat(buf, strlen(buf));
360366
}
361367

362-
unsigned char String::concat(int num) {
368+
bool String::concat(int num) {
363369
char buf[2 + 3 * sizeof(int)];
364370
sprintf(buf, "%d", num);
365371
return concat(buf, strlen(buf));
366372
}
367373

368-
unsigned char String::concat(unsigned int num) {
374+
bool String::concat(unsigned int num) {
369375
char buf[1 + 3 * sizeof(unsigned int)];
370376
utoa(num, buf, 10);
371377
return concat(buf, strlen(buf));
372378
}
373379

374-
unsigned char String::concat(long num) {
380+
bool String::concat(long num) {
375381
char buf[2 + 3 * sizeof(long)];
376382
sprintf(buf, "%ld", num);
377383
return concat(buf, strlen(buf));
378384
}
379385

380-
unsigned char String::concat(unsigned long num) {
386+
bool String::concat(unsigned long num) {
381387
char buf[1 + 3 * sizeof(unsigned long)];
382388
ultoa(num, buf, 10);
383389
return concat(buf, strlen(buf));
384390
}
385391

386-
unsigned char String::concat(float num) {
392+
bool String::concat(float num) {
387393
char buf[20];
388394
char* string = dtostrf(num, 4, 2, buf);
389395
return concat(string, strlen(string));
390396
}
391397

392-
unsigned char String::concat(double num) {
398+
bool String::concat(double num) {
393399
char buf[20];
394400
char* string = dtostrf(num, 4, 2, buf);
395401
return concat(string, strlen(string));
396402
}
397403

398-
unsigned char String::concat(const __FlashStringHelper * str) {
404+
bool String::concat(const __FlashStringHelper * str) {
399405
if (!str) return 0;
400406
int length = strlen_P((PGM_P)str);
401407
if (length == 0) return 1;
@@ -503,35 +509,39 @@ int String::compareTo(const String &s) const {
503509
return strcmp(buffer(), s.buffer());
504510
}
505511

506-
unsigned char String::equals(const String &s2) const {
512+
bool String::equals(const String &s2) const {
507513
return (len() == s2.len() && compareTo(s2) == 0);
508514
}
509515

510-
unsigned char String::equals(const char *cstr) const {
516+
bool String::equals(const char *cstr) const {
511517
if(len() == 0)
512518
return (cstr == NULL || *cstr == 0);
513519
if(cstr == NULL)
514520
return buffer()[0] == 0;
515521
return strcmp(buffer(), cstr) == 0;
516522
}
517523

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 {
519529
return compareTo(rhs) < 0;
520530
}
521531

522-
unsigned char String::operator>(const String &rhs) const {
532+
bool String::operator>(const String &rhs) const {
523533
return compareTo(rhs) > 0;
524534
}
525535

526-
unsigned char String::operator<=(const String &rhs) const {
536+
bool String::operator<=(const String &rhs) const {
527537
return compareTo(rhs) <= 0;
528538
}
529539

530-
unsigned char String::operator>=(const String &rhs) const {
540+
bool String::operator>=(const String &rhs) const {
531541
return compareTo(rhs) >= 0;
532542
}
533543

534-
unsigned char String::equalsIgnoreCase(const String &s2) const {
544+
bool String::equalsIgnoreCase(const String &s2) const {
535545
if(this == &s2)
536546
return 1;
537547
if(len() != s2.len())
@@ -547,7 +557,11 @@ unsigned char String::equalsIgnoreCase(const String &s2) const {
547557
return 1;
548558
}
549559

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 {
551565
// To avoid possible time-based attacks present function
552566
// compares given strings in a constant time.
553567
if(len() != s2.len())
@@ -574,24 +588,40 @@ unsigned char String::equalsConstantTime(const String &s2) const {
574588
return (equalcond & diffcond); //bitwise AND
575589
}
576590

577-
unsigned char String::startsWith(const String &s2) const {
591+
bool String::startsWith(const String &s2) const {
578592
if(len() < s2.len())
579593
return 0;
580594
return startsWith(s2, 0);
581595
}
582596

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 {
584606
if(offset > (unsigned)(len() - s2.len()) || !buffer() || !s2.buffer())
585607
return 0;
586608
return strncmp(&buffer()[offset], s2.buffer(), s2.len()) == 0;
587609
}
588610

589-
unsigned char String::endsWith(const String &s2) const {
611+
bool String::endsWith(const String &s2) const {
590612
if(len() < s2.len() || !buffer() || !s2.buffer())
591613
return 0;
592614
return strcmp(&buffer()[len() - s2.len()], s2.buffer()) == 0;
593615
}
594616

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+
595625
// /*********************************************/
596626
// /* Character Access */
597627
// /*********************************************/
@@ -700,6 +730,14 @@ int String::lastIndexOf(const String &s2, unsigned int fromIndex) const {
700730
return found;
701731
}
702732

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+
703741
String String::substring(unsigned int left, unsigned int right) const {
704742
if(left > right) {
705743
unsigned int temp = right;
@@ -768,7 +806,7 @@ void String::replace(const String& find, const String& replace) {
768806
while(index >= 0 && (index = lastIndexOf(find, index)) >= 0) {
769807
readFrom = wbuffer() + index + find.len();
770808
memmove(readFrom + diff, readFrom, len() - (readFrom - buffer()));
771-
int newLen = len() + diff;
809+
int newLen = len() + diff;
772810
memmove(wbuffer() + index, replace.buffer(), replace.len());
773811
setLen(newLen);
774812
wbuffer()[newLen] = 0;
@@ -777,6 +815,22 @@ void String::replace(const String& find, const String& replace) {
777815
}
778816
}
779817

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+
780834
void String::remove(unsigned int index) {
781835
// Pass the biggest integer as the count. The remove method
782836
// below will take care of truncating it at the end of the

0 commit comments

Comments
 (0)