Make String::concat() faster for some types. (#5307)

* Make String::concat() faster for some types.

This patch removes the unneeded call to `strlen()` when concatenating some types to a `String`. Additionally it fixes some whitespace for consistency.

* Update WString.cpp
This commit is contained in:
Clemens Kirchgatterer 2021-06-18 12:10:40 +02:00 committed by GitHub
parent c7bdb234bf
commit 21947ebe76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -59,9 +59,7 @@ String::String(StringSumHelper &&rval) {
String::String(char c) { String::String(char c) {
init(); init();
char buf[2]; char buf[] = { c, '\0' };
buf[0] = c;
buf[1] = 0;
*this = buf; *this = buf;
} }
@ -290,10 +288,11 @@ String & String::operator =(const char *cstr) {
return *this; return *this;
} }
String & String::operator = (const __FlashStringHelper *pstr) String & String::operator =(const __FlashStringHelper *pstr) {
{ if(pstr)
if (pstr) copy(pstr, strlen_P((PGM_P)pstr)); copy(pstr, strlen_P((PGM_P)pstr));
else invalidate(); else
invalidate();
return *this; return *this;
} }
@ -347,22 +346,18 @@ unsigned char String::concat(const char *cstr) {
} }
unsigned char String::concat(char c) { unsigned char String::concat(char c) {
char buf[2]; char buf[] = { c, '\0' };
buf[0] = c;
buf[1] = 0;
return concat(buf, 1); return concat(buf, 1);
} }
unsigned char String::concat(unsigned char num) { unsigned char String::concat(unsigned char num) {
char buf[1 + 3 * sizeof(unsigned char)]; char buf[1 + 3 * sizeof(unsigned char)];
sprintf(buf, "%d", num); return concat(buf, sprintf(buf, "%d", num));
return concat(buf, strlen(buf));
} }
unsigned char String::concat(int num) { unsigned char String::concat(int num) {
char buf[2 + 3 * sizeof(int)]; char buf[2 + 3 * sizeof(int)];
sprintf(buf, "%d", num); return concat(buf, sprintf(buf, "%d", num));
return concat(buf, strlen(buf));
} }
unsigned char String::concat(unsigned int num) { unsigned char String::concat(unsigned int num) {
@ -373,8 +368,7 @@ unsigned char String::concat(unsigned int num) {
unsigned char String::concat(long num) { unsigned char String::concat(long num) {
char buf[2 + 3 * sizeof(long)]; char buf[2 + 3 * sizeof(long)];
sprintf(buf, "%ld", num); return concat(buf, sprintf(buf, "%ld", num));
return concat(buf, strlen(buf));
} }
unsigned char String::concat(unsigned long num) { unsigned char String::concat(unsigned long num) {