Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions cores/esp32/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ String::String(StringSumHelper &&rval) {

String::String(char c) {
init();
char buf[2];
buf[0] = c;
buf[1] = 0;
char buf[] = { c, '\0' };
*this = buf;
}

Expand Down Expand Up @@ -290,10 +288,11 @@ String & String::operator =(const char *cstr) {
return *this;
}

String & String::operator = (const __FlashStringHelper *pstr)
{
if (pstr) copy(pstr, strlen_P((PGM_P)pstr));
else invalidate();
String & String::operator =(const __FlashStringHelper *pstr) {
if(pstr)
copy(pstr, strlen_P((PGM_P)pstr));
else
invalidate();

return *this;
}
Expand Down Expand Up @@ -347,22 +346,18 @@ unsigned char String::concat(const char *cstr) {
}

unsigned char String::concat(char c) {
char buf[2];
buf[0] = c;
buf[1] = 0;
char buf[] = { c, '\0' };
return concat(buf, 1);
}

unsigned char String::concat(unsigned char num) {
char buf[1 + 3 * sizeof(unsigned char)];
sprintf(buf, "%d", num);
return concat(buf, strlen(buf));
return concat(buf, sprintf(buf, "%d", num));
}

unsigned char String::concat(int num) {
char buf[2 + 3 * sizeof(int)];
sprintf(buf, "%d", num);
return concat(buf, strlen(buf));
return concat(buf, sprintf(buf, "%d", num));
}

unsigned char String::concat(unsigned int num) {
Expand All @@ -373,8 +368,7 @@ unsigned char String::concat(unsigned int num) {

unsigned char String::concat(long num) {
char buf[2 + 3 * sizeof(long)];
sprintf(buf, "%ld", num);
return concat(buf, strlen(buf));
return concat(buf, sprintf(buf, "%ld", num));
}

unsigned char String::concat(unsigned long num) {
Expand Down